blob: b61b52f0a9fbdc8bdd34ab41765f0c74f1532011 [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)},
Matt Carlsonba1f3c72011-04-05 14:22:50 +0000267 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700268 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
269 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
270 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
271 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
272 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
273 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
274 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
275 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276};
277
278MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
279
Andreas Mohr50da8592006-08-14 23:54:30 -0700280static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 const char string[ETH_GSTRING_LEN];
282} ethtool_stats_keys[TG3_NUM_STATS] = {
283 { "rx_octets" },
284 { "rx_fragments" },
285 { "rx_ucast_packets" },
286 { "rx_mcast_packets" },
287 { "rx_bcast_packets" },
288 { "rx_fcs_errors" },
289 { "rx_align_errors" },
290 { "rx_xon_pause_rcvd" },
291 { "rx_xoff_pause_rcvd" },
292 { "rx_mac_ctrl_rcvd" },
293 { "rx_xoff_entered" },
294 { "rx_frame_too_long_errors" },
295 { "rx_jabbers" },
296 { "rx_undersize_packets" },
297 { "rx_in_length_errors" },
298 { "rx_out_length_errors" },
299 { "rx_64_or_less_octet_packets" },
300 { "rx_65_to_127_octet_packets" },
301 { "rx_128_to_255_octet_packets" },
302 { "rx_256_to_511_octet_packets" },
303 { "rx_512_to_1023_octet_packets" },
304 { "rx_1024_to_1522_octet_packets" },
305 { "rx_1523_to_2047_octet_packets" },
306 { "rx_2048_to_4095_octet_packets" },
307 { "rx_4096_to_8191_octet_packets" },
308 { "rx_8192_to_9022_octet_packets" },
309
310 { "tx_octets" },
311 { "tx_collisions" },
312
313 { "tx_xon_sent" },
314 { "tx_xoff_sent" },
315 { "tx_flow_control" },
316 { "tx_mac_errors" },
317 { "tx_single_collisions" },
318 { "tx_mult_collisions" },
319 { "tx_deferred" },
320 { "tx_excessive_collisions" },
321 { "tx_late_collisions" },
322 { "tx_collide_2times" },
323 { "tx_collide_3times" },
324 { "tx_collide_4times" },
325 { "tx_collide_5times" },
326 { "tx_collide_6times" },
327 { "tx_collide_7times" },
328 { "tx_collide_8times" },
329 { "tx_collide_9times" },
330 { "tx_collide_10times" },
331 { "tx_collide_11times" },
332 { "tx_collide_12times" },
333 { "tx_collide_13times" },
334 { "tx_collide_14times" },
335 { "tx_collide_15times" },
336 { "tx_ucast_packets" },
337 { "tx_mcast_packets" },
338 { "tx_bcast_packets" },
339 { "tx_carrier_sense_errors" },
340 { "tx_discards" },
341 { "tx_errors" },
342
343 { "dma_writeq_full" },
344 { "dma_write_prioq_full" },
345 { "rxbds_empty" },
346 { "rx_discards" },
347 { "rx_errors" },
348 { "rx_threshold_hit" },
349
350 { "dma_readq_full" },
351 { "dma_read_prioq_full" },
352 { "tx_comp_queue_full" },
353
354 { "ring_set_send_prod_index" },
355 { "ring_status_update" },
356 { "nic_irqs" },
357 { "nic_avoided_irqs" },
358 { "nic_tx_threshold_hit" }
359};
360
Andreas Mohr50da8592006-08-14 23:54:30 -0700361static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700362 const char string[ETH_GSTRING_LEN];
363} ethtool_test_keys[TG3_NUM_TEST] = {
364 { "nvram test (online) " },
365 { "link test (online) " },
366 { "register test (offline)" },
367 { "memory test (offline)" },
368 { "loopback test (offline)" },
369 { "interrupt test (offline)" },
370};
371
Michael Chanb401e9e2005-12-19 16:27:04 -0800372static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
373{
374 writel(val, tp->regs + off);
375}
376
377static u32 tg3_read32(struct tg3 *tp, u32 off)
378{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000379 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800380}
381
Matt Carlson0d3031d2007-10-10 18:02:43 -0700382static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
383{
384 writel(val, tp->aperegs + off);
385}
386
387static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
388{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000389 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700390}
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
393{
Michael Chan68929142005-08-09 20:17:14 -0700394 unsigned long flags;
395
396 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700397 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
398 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700399 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700400}
401
402static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
403{
404 writel(val, tp->regs + off);
405 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
Michael Chan68929142005-08-09 20:17:14 -0700408static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
409{
410 unsigned long flags;
411 u32 val;
412
413 spin_lock_irqsave(&tp->indirect_lock, flags);
414 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
415 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
416 spin_unlock_irqrestore(&tp->indirect_lock, flags);
417 return val;
418}
419
420static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
421{
422 unsigned long flags;
423
424 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
425 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
426 TG3_64BIT_REG_LOW, val);
427 return;
428 }
Matt Carlson66711e62009-11-13 13:03:49 +0000429 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700430 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
431 TG3_64BIT_REG_LOW, val);
432 return;
433 }
434
435 spin_lock_irqsave(&tp->indirect_lock, flags);
436 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
437 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
438 spin_unlock_irqrestore(&tp->indirect_lock, flags);
439
440 /* In indirect mode when disabling interrupts, we also need
441 * to clear the interrupt bit in the GRC local ctrl register.
442 */
443 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
444 (val == 0x1)) {
445 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
446 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
447 }
448}
449
450static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
451{
452 unsigned long flags;
453 u32 val;
454
455 spin_lock_irqsave(&tp->indirect_lock, flags);
456 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
457 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
458 spin_unlock_irqrestore(&tp->indirect_lock, flags);
459 return val;
460}
461
Michael Chanb401e9e2005-12-19 16:27:04 -0800462/* usec_wait specifies the wait time in usec when writing to certain registers
463 * where it is unsafe to read back the register without some delay.
464 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
465 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
466 */
467static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Michael Chanb401e9e2005-12-19 16:27:04 -0800469 if ((tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) ||
470 (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
471 /* Non-posted methods */
472 tp->write32(tp, off, val);
473 else {
474 /* Posted method */
475 tg3_write32(tp, off, val);
476 if (usec_wait)
477 udelay(usec_wait);
478 tp->read32(tp, off);
479 }
480 /* Wait again after the read for the posted method to guarantee that
481 * the wait time is met.
482 */
483 if (usec_wait)
484 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
Michael Chan09ee9292005-08-09 20:17:00 -0700487static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
488{
489 tp->write32_mbox(tp, off, val);
Michael Chan68929142005-08-09 20:17:14 -0700490 if (!(tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) &&
491 !(tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
492 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700493}
494
Michael Chan20094932005-08-09 20:16:32 -0700495static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
497 void __iomem *mbox = tp->regs + off;
498 writel(val, mbox);
499 if (tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG)
500 writel(val, mbox);
501 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
502 readl(mbox);
503}
504
Michael Chanb5d37722006-09-27 16:06:21 -0700505static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
506{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000507 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700508}
509
510static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
511{
512 writel(val, tp->regs + off + GRCMBOX_BASE);
513}
514
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000515#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700516#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000517#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
518#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
519#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700520
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000521#define tw32(reg, val) tp->write32(tp, reg, val)
522#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
523#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
524#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
527{
Michael Chan68929142005-08-09 20:17:14 -0700528 unsigned long flags;
529
Michael Chanb5d37722006-09-27 16:06:21 -0700530 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
531 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
532 return;
533
Michael Chan68929142005-08-09 20:17:14 -0700534 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chanbbadf502006-04-06 21:46:34 -0700535 if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
536 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
537 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Michael Chanbbadf502006-04-06 21:46:34 -0700539 /* Always leave this as zero. */
540 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
541 } else {
542 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
543 tw32_f(TG3PCI_MEM_WIN_DATA, val);
544
545 /* Always leave this as zero. */
546 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
547 }
Michael Chan68929142005-08-09 20:17:14 -0700548 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
551static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
552{
Michael Chan68929142005-08-09 20:17:14 -0700553 unsigned long flags;
554
Michael Chanb5d37722006-09-27 16:06:21 -0700555 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
556 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
557 *val = 0;
558 return;
559 }
560
Michael Chan68929142005-08-09 20:17:14 -0700561 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chanbbadf502006-04-06 21:46:34 -0700562 if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
563 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
564 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Michael Chanbbadf502006-04-06 21:46:34 -0700566 /* Always leave this as zero. */
567 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
568 } else {
569 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
570 *val = tr32(TG3PCI_MEM_WIN_DATA);
571
572 /* Always leave this as zero. */
573 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
574 }
Michael Chan68929142005-08-09 20:17:14 -0700575 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
Matt Carlson0d3031d2007-10-10 18:02:43 -0700578static void tg3_ape_lock_init(struct tg3 *tp)
579{
580 int i;
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000581 u32 regbase;
582
583 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
584 regbase = TG3_APE_LOCK_GRANT;
585 else
586 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700587
588 /* Make sure the driver hasn't any stale locks. */
589 for (i = 0; i < 8; i++)
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000590 tg3_ape_write32(tp, regbase + 4 * i, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700591}
592
593static int tg3_ape_lock(struct tg3 *tp, int locknum)
594{
595 int i, off;
596 int ret = 0;
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000597 u32 status, req, gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700598
599 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
600 return 0;
601
602 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000603 case TG3_APE_LOCK_GRC:
604 case TG3_APE_LOCK_MEM:
605 break;
606 default:
607 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700608 }
609
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000610 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
611 req = TG3_APE_LOCK_REQ;
612 gnt = TG3_APE_LOCK_GRANT;
613 } else {
614 req = TG3_APE_PER_LOCK_REQ;
615 gnt = TG3_APE_PER_LOCK_GRANT;
616 }
617
Matt Carlson0d3031d2007-10-10 18:02:43 -0700618 off = 4 * locknum;
619
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000620 tg3_ape_write32(tp, req + off, APE_LOCK_REQ_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700621
622 /* Wait for up to 1 millisecond to acquire lock. */
623 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000624 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700625 if (status == APE_LOCK_GRANT_DRIVER)
626 break;
627 udelay(10);
628 }
629
630 if (status != APE_LOCK_GRANT_DRIVER) {
631 /* Revoke the lock request. */
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000632 tg3_ape_write32(tp, gnt + off,
Matt Carlson0d3031d2007-10-10 18:02:43 -0700633 APE_LOCK_GRANT_DRIVER);
634
635 ret = -EBUSY;
636 }
637
638 return ret;
639}
640
641static void tg3_ape_unlock(struct tg3 *tp, int locknum)
642{
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000643 u32 gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700644
645 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
646 return;
647
648 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000649 case TG3_APE_LOCK_GRC:
650 case TG3_APE_LOCK_MEM:
651 break;
652 default:
653 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700654 }
655
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000656 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
657 gnt = TG3_APE_LOCK_GRANT;
658 else
659 gnt = TG3_APE_PER_LOCK_GRANT;
660
661 tg3_ape_write32(tp, gnt + 4 * locknum, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700662}
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664static void tg3_disable_ints(struct tg3 *tp)
665{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000666 int i;
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 tw32(TG3PCI_MISC_HOST_CTRL,
669 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000670 for (i = 0; i < tp->irq_max; i++)
671 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674static void tg3_enable_ints(struct tg3 *tp)
675{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000676 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000677
Michael Chanbbe832c2005-06-24 20:20:04 -0700678 tp->irq_sync = 0;
679 wmb();
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 tw32(TG3PCI_MISC_HOST_CTRL,
682 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000683
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000684 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000685 for (i = 0; i < tp->irq_cnt; i++) {
686 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000687
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000688 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
689 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
690 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
691
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000692 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000693 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000694
695 /* Force an initial interrupt */
696 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
697 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
698 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
699 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000700 tw32(HOSTCC_MODE, tp->coal_now);
701
702 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
Matt Carlson17375d22009-08-28 14:02:18 +0000705static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700706{
Matt Carlson17375d22009-08-28 14:02:18 +0000707 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000708 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700709 unsigned int work_exists = 0;
710
711 /* check for phy events */
712 if (!(tp->tg3_flags &
713 (TG3_FLAG_USE_LINKCHG_REG |
714 TG3_FLAG_POLL_SERDES))) {
715 if (sblk->status & SD_STATUS_LINK_CHG)
716 work_exists = 1;
717 }
718 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000719 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000720 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700721 work_exists = 1;
722
723 return work_exists;
724}
725
Matt Carlson17375d22009-08-28 14:02:18 +0000726/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700727 * similar to tg3_enable_ints, but it accurately determines whether there
728 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400729 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 */
Matt Carlson17375d22009-08-28 14:02:18 +0000731static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Matt Carlson17375d22009-08-28 14:02:18 +0000733 struct tg3 *tp = tnapi->tp;
734
Matt Carlson898a56f2009-08-28 14:02:40 +0000735 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 mmiowb();
737
David S. Millerfac9b832005-05-18 22:46:34 -0700738 /* When doing tagged status, this work check is unnecessary.
739 * The last_tag we write above tells the chip which piece of
740 * work we've completed.
741 */
742 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
Matt Carlson17375d22009-08-28 14:02:18 +0000743 tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700744 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000745 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748static void tg3_switch_clocks(struct tg3 *tp)
749{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000750 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 u32 orig_clock_ctrl;
752
Matt Carlson795d01c2007-10-07 23:28:17 -0700753 if ((tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
754 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700755 return;
756
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000757 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 orig_clock_ctrl = clock_ctrl;
760 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
761 CLOCK_CTRL_CLKRUN_OENABLE |
762 0x1f);
763 tp->pci_clock_ctrl = clock_ctrl;
764
765 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
766 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800767 tw32_wait_f(TG3PCI_CLOCK_CTRL,
768 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
770 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800771 tw32_wait_f(TG3PCI_CLOCK_CTRL,
772 clock_ctrl |
773 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
774 40);
775 tw32_wait_f(TG3PCI_CLOCK_CTRL,
776 clock_ctrl | (CLOCK_CTRL_ALTCLK),
777 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800779 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
781
782#define PHY_BUSY_LOOPS 5000
783
784static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
785{
786 u32 frame_val;
787 unsigned int loops;
788 int ret;
789
790 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
791 tw32_f(MAC_MI_MODE,
792 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
793 udelay(80);
794 }
795
796 *val = 0x0;
797
Matt Carlson882e9792009-09-01 13:21:36 +0000798 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 MI_COM_PHY_ADDR_MASK);
800 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
801 MI_COM_REG_ADDR_MASK);
802 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 tw32_f(MAC_MI_COM, frame_val);
805
806 loops = PHY_BUSY_LOOPS;
807 while (loops != 0) {
808 udelay(10);
809 frame_val = tr32(MAC_MI_COM);
810
811 if ((frame_val & MI_COM_BUSY) == 0) {
812 udelay(5);
813 frame_val = tr32(MAC_MI_COM);
814 break;
815 }
816 loops -= 1;
817 }
818
819 ret = -EBUSY;
820 if (loops != 0) {
821 *val = frame_val & MI_COM_DATA_MASK;
822 ret = 0;
823 }
824
825 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
826 tw32_f(MAC_MI_MODE, tp->mi_mode);
827 udelay(80);
828 }
829
830 return ret;
831}
832
833static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
834{
835 u32 frame_val;
836 unsigned int loops;
837 int ret;
838
Matt Carlsonf07e9af2010-08-02 11:26:07 +0000839 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Michael Chanb5d37722006-09-27 16:06:21 -0700840 (reg == MII_TG3_CTRL || reg == MII_TG3_AUX_CTRL))
841 return 0;
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
844 tw32_f(MAC_MI_MODE,
845 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
846 udelay(80);
847 }
848
Matt Carlson882e9792009-09-01 13:21:36 +0000849 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 MI_COM_PHY_ADDR_MASK);
851 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
852 MI_COM_REG_ADDR_MASK);
853 frame_val |= (val & MI_COM_DATA_MASK);
854 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 tw32_f(MAC_MI_COM, frame_val);
857
858 loops = PHY_BUSY_LOOPS;
859 while (loops != 0) {
860 udelay(10);
861 frame_val = tr32(MAC_MI_COM);
862 if ((frame_val & MI_COM_BUSY) == 0) {
863 udelay(5);
864 frame_val = tr32(MAC_MI_COM);
865 break;
866 }
867 loops -= 1;
868 }
869
870 ret = -EBUSY;
871 if (loops != 0)
872 ret = 0;
873
874 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
875 tw32_f(MAC_MI_MODE, tp->mi_mode);
876 udelay(80);
877 }
878
879 return ret;
880}
881
Matt Carlson95e28692008-05-25 23:44:14 -0700882static int tg3_bmcr_reset(struct tg3 *tp)
883{
884 u32 phy_control;
885 int limit, err;
886
887 /* OK, reset it, and poll the BMCR_RESET bit until it
888 * clears or we time out.
889 */
890 phy_control = BMCR_RESET;
891 err = tg3_writephy(tp, MII_BMCR, phy_control);
892 if (err != 0)
893 return -EBUSY;
894
895 limit = 5000;
896 while (limit--) {
897 err = tg3_readphy(tp, MII_BMCR, &phy_control);
898 if (err != 0)
899 return -EBUSY;
900
901 if ((phy_control & BMCR_RESET) == 0) {
902 udelay(40);
903 break;
904 }
905 udelay(10);
906 }
Roel Kluind4675b52009-02-12 16:33:27 -0800907 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -0700908 return -EBUSY;
909
910 return 0;
911}
912
Matt Carlson158d7ab2008-05-29 01:37:54 -0700913static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
914{
Francois Romieu3d165432009-01-19 16:56:50 -0800915 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700916 u32 val;
917
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000918 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700919
920 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000921 val = -EIO;
922
923 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700924
925 return val;
926}
927
928static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
929{
Francois Romieu3d165432009-01-19 16:56:50 -0800930 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000931 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700932
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000933 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700934
935 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000936 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700937
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000938 spin_unlock_bh(&tp->lock);
939
940 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700941}
942
943static int tg3_mdio_reset(struct mii_bus *bp)
944{
945 return 0;
946}
947
Matt Carlson9c61d6b2008-11-03 16:54:56 -0800948static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -0700949{
950 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800951 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -0700952
Matt Carlson3f0e3ad2009-11-02 14:24:36 +0000953 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800954 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +0000955 case PHY_ID_BCM50610:
956 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800957 val = MAC_PHYCFG2_50610_LED_MODES;
958 break;
Matt Carlson6a443a02010-02-17 15:17:04 +0000959 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800960 val = MAC_PHYCFG2_AC131_LED_MODES;
961 break;
Matt Carlson6a443a02010-02-17 15:17:04 +0000962 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800963 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
964 break;
Matt Carlson6a443a02010-02-17 15:17:04 +0000965 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800966 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
967 break;
968 default:
Matt Carlsona9daf362008-05-25 23:49:44 -0700969 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800970 }
971
972 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
973 tw32(MAC_PHYCFG2, val);
974
975 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +0000976 val &= ~(MAC_PHYCFG1_RGMII_INT |
977 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
978 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800979 tw32(MAC_PHYCFG1, val);
980
981 return;
982 }
983
Matt Carlson14417062010-02-17 15:16:59 +0000984 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800985 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
986 MAC_PHYCFG2_FMODE_MASK_MASK |
987 MAC_PHYCFG2_GMODE_MASK_MASK |
988 MAC_PHYCFG2_ACT_MASK_MASK |
989 MAC_PHYCFG2_QUAL_MASK_MASK |
990 MAC_PHYCFG2_INBAND_ENABLE;
991
992 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -0700993
Matt Carlsonbb85fbb2009-08-25 10:09:07 +0000994 val = tr32(MAC_PHYCFG1);
995 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
996 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Matt Carlson14417062010-02-17 15:16:59 +0000997 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)) {
Matt Carlsona9daf362008-05-25 23:49:44 -0700998 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
999 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
1000 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1001 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1002 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001003 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1004 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1005 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001006
Matt Carlsona9daf362008-05-25 23:49:44 -07001007 val = tr32(MAC_EXT_RGMII_MODE);
1008 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1009 MAC_RGMII_MODE_RX_QUALITY |
1010 MAC_RGMII_MODE_RX_ACTIVITY |
1011 MAC_RGMII_MODE_RX_ENG_DET |
1012 MAC_RGMII_MODE_TX_ENABLE |
1013 MAC_RGMII_MODE_TX_LOWPWR |
1014 MAC_RGMII_MODE_TX_RESET);
Matt Carlson14417062010-02-17 15:16:59 +00001015 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)) {
Matt Carlsona9daf362008-05-25 23:49:44 -07001016 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1017 val |= MAC_RGMII_MODE_RX_INT_B |
1018 MAC_RGMII_MODE_RX_QUALITY |
1019 MAC_RGMII_MODE_RX_ACTIVITY |
1020 MAC_RGMII_MODE_RX_ENG_DET;
1021 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1022 val |= MAC_RGMII_MODE_TX_ENABLE |
1023 MAC_RGMII_MODE_TX_LOWPWR |
1024 MAC_RGMII_MODE_TX_RESET;
1025 }
1026 tw32(MAC_EXT_RGMII_MODE, val);
1027}
1028
Matt Carlson158d7ab2008-05-29 01:37:54 -07001029static void tg3_mdio_start(struct tg3 *tp)
1030{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001031 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1032 tw32_f(MAC_MI_MODE, tp->mi_mode);
1033 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001034
Matt Carlson9ea48182010-02-17 15:17:01 +00001035 if ((tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) &&
1036 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1037 tg3_mdio_config_5785(tp);
1038}
1039
1040static int tg3_mdio_init(struct tg3 *tp)
1041{
1042 int i;
1043 u32 reg;
1044 struct phy_device *phydev;
1045
Matt Carlson0a58d662011-04-05 14:22:45 +00001046 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
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 Carlson0a58d662011-04-05 14:22:45 +00001624 ((tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001625 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001626 return;
1627
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001628 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001629 tg3_phy_fet_toggle_apd(tp, enable);
1630 return;
1631 }
1632
Matt Carlson6833c042008-11-21 17:18:59 -08001633 reg = MII_TG3_MISC_SHDW_WREN |
1634 MII_TG3_MISC_SHDW_SCR5_SEL |
1635 MII_TG3_MISC_SHDW_SCR5_LPED |
1636 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1637 MII_TG3_MISC_SHDW_SCR5_SDTL |
1638 MII_TG3_MISC_SHDW_SCR5_C125OE;
1639 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1640 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1641
1642 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1643
1644
1645 reg = MII_TG3_MISC_SHDW_WREN |
1646 MII_TG3_MISC_SHDW_APD_SEL |
1647 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1648 if (enable)
1649 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1650
1651 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1652}
1653
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001654static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
1655{
1656 u32 phy;
1657
1658 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001659 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001660 return;
1661
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001662 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001663 u32 ephy;
1664
Matt Carlson535ef6e2009-08-25 10:09:36 +00001665 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
1666 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
1667
1668 tg3_writephy(tp, MII_TG3_FET_TEST,
1669 ephy | MII_TG3_FET_SHADOW_EN);
1670 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001671 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00001672 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001673 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00001674 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1675 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001676 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00001677 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001678 }
1679 } else {
1680 phy = MII_TG3_AUXCTL_MISC_RDSEL_MISC |
1681 MII_TG3_AUXCTL_SHDWSEL_MISC;
1682 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, phy) &&
1683 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &phy)) {
1684 if (enable)
1685 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1686 else
1687 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1688 phy |= MII_TG3_AUXCTL_MISC_WREN;
1689 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1690 }
1691 }
1692}
1693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694static void tg3_phy_set_wirespeed(struct tg3 *tp)
1695{
1696 u32 val;
1697
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001698 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 return;
1700
1701 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x7007) &&
1702 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &val))
1703 tg3_writephy(tp, MII_TG3_AUX_CTRL,
1704 (val | (1 << 15) | (1 << 4)));
1705}
1706
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001707static void tg3_phy_apply_otp(struct tg3 *tp)
1708{
1709 u32 otp, phy;
1710
1711 if (!tp->phy_otp)
1712 return;
1713
1714 otp = tp->phy_otp;
1715
1716 /* Enable SM_DSP clock and tx 6dB coding. */
1717 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1718 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
1719 MII_TG3_AUXCTL_ACTL_TX_6DB;
1720 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1721
1722 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
1723 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
1724 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
1725
1726 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
1727 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
1728 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
1729
1730 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
1731 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
1732 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
1733
1734 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
1735 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
1736
1737 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
1738 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
1739
1740 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
1741 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
1742 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
1743
1744 /* Turn off SM_DSP clock. */
1745 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1746 MII_TG3_AUXCTL_ACTL_TX_6DB;
1747 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1748}
1749
Matt Carlson52b02d02010-10-14 10:37:41 +00001750static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
1751{
1752 u32 val;
1753
1754 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
1755 return;
1756
1757 tp->setlpicnt = 0;
1758
1759 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
1760 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00001761 tp->link_config.active_duplex == DUPLEX_FULL &&
1762 (tp->link_config.active_speed == SPEED_100 ||
1763 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00001764 u32 eeectl;
1765
1766 if (tp->link_config.active_speed == SPEED_1000)
1767 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
1768 else
1769 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
1770
1771 tw32(TG3_CPMU_EEE_CTRL, eeectl);
1772
Matt Carlson3110f5f52010-12-06 08:28:50 +00001773 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
1774 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00001775
Matt Carlson21a00ab2011-01-25 15:58:55 +00001776 switch (val) {
1777 case TG3_CL45_D7_EEERES_STAT_LP_1000T:
1778 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
1779 case ASIC_REV_5717:
1780 case ASIC_REV_5719:
1781 case ASIC_REV_57765:
1782 /* Enable SM_DSP clock and tx 6dB coding. */
1783 val = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1784 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
1785 MII_TG3_AUXCTL_ACTL_TX_6DB;
1786 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
1787
1788 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
1789
1790 /* Turn off SM_DSP clock. */
1791 val = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1792 MII_TG3_AUXCTL_ACTL_TX_6DB;
1793 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
1794 }
1795 /* Fallthrough */
1796 case TG3_CL45_D7_EEERES_STAT_LP_100TX:
Matt Carlson52b02d02010-10-14 10:37:41 +00001797 tp->setlpicnt = 2;
Matt Carlson21a00ab2011-01-25 15:58:55 +00001798 }
Matt Carlson52b02d02010-10-14 10:37:41 +00001799 }
1800
1801 if (!tp->setlpicnt) {
1802 val = tr32(TG3_CPMU_EEE_MODE);
1803 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
1804 }
1805}
1806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807static int tg3_wait_macro_done(struct tg3 *tp)
1808{
1809 int limit = 100;
1810
1811 while (limit--) {
1812 u32 tmp32;
1813
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001814 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 if ((tmp32 & 0x1000) == 0)
1816 break;
1817 }
1818 }
Roel Kluind4675b52009-02-12 16:33:27 -08001819 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 return -EBUSY;
1821
1822 return 0;
1823}
1824
1825static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
1826{
1827 static const u32 test_pat[4][6] = {
1828 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
1829 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
1830 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
1831 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
1832 };
1833 int chan;
1834
1835 for (chan = 0; chan < 4; chan++) {
1836 int i;
1837
1838 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1839 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001840 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842 for (i = 0; i < 6; i++)
1843 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
1844 test_pat[chan][i]);
1845
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001846 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 if (tg3_wait_macro_done(tp)) {
1848 *resetp = 1;
1849 return -EBUSY;
1850 }
1851
1852 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1853 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001854 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 if (tg3_wait_macro_done(tp)) {
1856 *resetp = 1;
1857 return -EBUSY;
1858 }
1859
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001860 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 if (tg3_wait_macro_done(tp)) {
1862 *resetp = 1;
1863 return -EBUSY;
1864 }
1865
1866 for (i = 0; i < 6; i += 2) {
1867 u32 low, high;
1868
1869 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
1870 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
1871 tg3_wait_macro_done(tp)) {
1872 *resetp = 1;
1873 return -EBUSY;
1874 }
1875 low &= 0x7fff;
1876 high &= 0x000f;
1877 if (low != test_pat[chan][i] ||
1878 high != test_pat[chan][i+1]) {
1879 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
1880 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
1881 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
1882
1883 return -EBUSY;
1884 }
1885 }
1886 }
1887
1888 return 0;
1889}
1890
1891static int tg3_phy_reset_chanpat(struct tg3 *tp)
1892{
1893 int chan;
1894
1895 for (chan = 0; chan < 4; chan++) {
1896 int i;
1897
1898 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1899 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001900 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 for (i = 0; i < 6; i++)
1902 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001903 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 if (tg3_wait_macro_done(tp))
1905 return -EBUSY;
1906 }
1907
1908 return 0;
1909}
1910
1911static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
1912{
1913 u32 reg32, phy9_orig;
1914 int retries, do_phy_reset, err;
1915
1916 retries = 10;
1917 do_phy_reset = 1;
1918 do {
1919 if (do_phy_reset) {
1920 err = tg3_bmcr_reset(tp);
1921 if (err)
1922 return err;
1923 do_phy_reset = 0;
1924 }
1925
1926 /* Disable transmitter and interrupt. */
1927 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
1928 continue;
1929
1930 reg32 |= 0x3000;
1931 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1932
1933 /* Set full-duplex, 1000 mbps. */
1934 tg3_writephy(tp, MII_BMCR,
1935 BMCR_FULLDPLX | TG3_BMCR_SPEED1000);
1936
1937 /* Set to master mode. */
1938 if (tg3_readphy(tp, MII_TG3_CTRL, &phy9_orig))
1939 continue;
1940
1941 tg3_writephy(tp, MII_TG3_CTRL,
1942 (MII_TG3_CTRL_AS_MASTER |
1943 MII_TG3_CTRL_ENABLE_AS_MASTER));
1944
1945 /* Enable SM_DSP_CLOCK and 6dB. */
1946 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1947
1948 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00001949 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
1951 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
1952 if (!err)
1953 break;
1954 } while (--retries);
1955
1956 err = tg3_phy_reset_chanpat(tp);
1957 if (err)
1958 return err;
1959
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00001960 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
1962 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001963 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
1965 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
1966 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
1967 /* Set Extended packet length bit for jumbo frames */
1968 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4400);
Matt Carlson859a5882010-04-05 10:19:28 +00001969 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1971 }
1972
1973 tg3_writephy(tp, MII_TG3_CTRL, phy9_orig);
1974
1975 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
1976 reg32 &= ~0x3000;
1977 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1978 } else if (!err)
1979 err = -EBUSY;
1980
1981 return err;
1982}
1983
1984/* This will reset the tigon3 PHY if there is no valid
1985 * link unless the FORCE argument is non-zero.
1986 */
1987static int tg3_phy_reset(struct tg3 *tp)
1988{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00001989 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 int err;
1991
Michael Chan60189dd2006-12-17 17:08:07 -08001992 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08001993 val = tr32(GRC_MISC_CFG);
1994 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
1995 udelay(40);
1996 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00001997 err = tg3_readphy(tp, MII_BMSR, &val);
1998 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 if (err != 0)
2000 return -EBUSY;
2001
Michael Chanc8e1e822006-04-29 18:55:17 -07002002 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
2003 netif_carrier_off(tp->dev);
2004 tg3_link_report(tp);
2005 }
2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2008 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2009 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2010 err = tg3_phy_reset_5703_4_5(tp);
2011 if (err)
2012 return err;
2013 goto out;
2014 }
2015
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002016 cpmuctrl = 0;
2017 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2018 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2019 cpmuctrl = tr32(TG3_CPMU_CTRL);
2020 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2021 tw32(TG3_CPMU_CTRL,
2022 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2023 }
2024
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 err = tg3_bmcr_reset(tp);
2026 if (err)
2027 return err;
2028
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002029 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002030 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2031 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002032
2033 tw32(TG3_CPMU_CTRL, cpmuctrl);
2034 }
2035
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002036 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2037 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002038 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2039 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2040 CPMU_LSPD_1000MB_MACCLK_12_5) {
2041 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2042 udelay(40);
2043 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2044 }
2045 }
2046
Matt Carlson0a58d662011-04-05 14:22:45 +00002047 if ((tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002048 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002049 return 0;
2050
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002051 tg3_phy_apply_otp(tp);
2052
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002053 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002054 tg3_phy_toggle_apd(tp, true);
2055 else
2056 tg3_phy_toggle_apd(tp, false);
2057
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058out:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002059 if (tp->phy_flags & TG3_PHYFLG_ADC_BUG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002061 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2062 tg3_phydsp_write(tp, 0x000a, 0x0323);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
2064 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002065 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002066 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2067 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002069 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002071 tg3_phydsp_write(tp, 0x000a, 0x310b);
2072 tg3_phydsp_write(tp, 0x201f, 0x9506);
2073 tg3_phydsp_write(tp, 0x401f, 0x14e2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002075 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Michael Chanc424cb22006-04-29 18:56:34 -07002076 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
2077 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002078 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
Michael Chanc1d2a192007-01-08 19:57:20 -08002079 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2080 tg3_writephy(tp, MII_TG3_TEST1,
2081 MII_TG3_TEST1_TRIM_EN | 0x4);
2082 } else
2083 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
Michael Chanc424cb22006-04-29 18:56:34 -07002084 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
2085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 /* Set Extended packet length bit (bit 14) on all chips that */
2087 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002088 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 /* Cannot do read-modify-write on 5401 */
2090 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
Matt Carlson8f666b02009-08-28 13:58:24 +00002091 } else if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 /* Set bit 14 with read-modify-write to preserve other bits */
2093 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0007) &&
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002094 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &val))
2095 tg3_writephy(tp, MII_TG3_AUX_CTRL, val | 0x4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 }
2097
2098 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2099 * jumbo frames transmission.
2100 */
Matt Carlson8f666b02009-08-28 13:58:24 +00002101 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002102 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002103 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002104 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 }
2106
Michael Chan715116a2006-09-27 16:09:25 -07002107 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002108 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002109 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002110 }
2111
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002112 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 tg3_phy_set_wirespeed(tp);
2114 return 0;
2115}
2116
2117static void tg3_frob_aux_power(struct tg3 *tp)
2118{
Matt Carlson683644b2011-03-09 16:58:23 +00002119 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Matt Carlson334355a2010-01-20 16:58:10 +00002121 /* The GPIOs do something completely different on 57765. */
2122 if ((tp->tg3_flags2 & TG3_FLG2_IS_NIC) == 0 ||
Matt Carlsona50d0792010-06-05 17:24:37 +00002123 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson334355a2010-01-20 16:58:10 +00002124 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 return;
2126
Matt Carlson683644b2011-03-09 16:58:23 +00002127 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2128 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +00002129 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2130 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) &&
Matt Carlson683644b2011-03-09 16:58:23 +00002131 tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002132 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002134 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002135
Michael Chanbc1c7562006-03-20 17:48:03 -08002136 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002137 if (dev_peer) {
2138 struct tg3 *tp_peer = netdev_priv(dev_peer);
2139
2140 if (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE)
2141 return;
2142
2143 if ((tp_peer->tg3_flags & TG3_FLAG_WOL_ENABLE) ||
2144 (tp_peer->tg3_flags & TG3_FLAG_ENABLE_ASF))
2145 need_vaux = true;
2146 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
Matt Carlson683644b2011-03-09 16:58:23 +00002149 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) ||
2150 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
2151 need_vaux = true;
2152
2153 if (need_vaux) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2155 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002156 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2157 (GRC_LCLCTRL_GPIO_OE0 |
2158 GRC_LCLCTRL_GPIO_OE1 |
2159 GRC_LCLCTRL_GPIO_OE2 |
2160 GRC_LCLCTRL_GPIO_OUTPUT0 |
2161 GRC_LCLCTRL_GPIO_OUTPUT1),
2162 100);
Matt Carlson8d519ab2009-04-20 06:58:01 +00002163 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2164 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -07002165 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2166 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2167 GRC_LCLCTRL_GPIO_OE1 |
2168 GRC_LCLCTRL_GPIO_OE2 |
2169 GRC_LCLCTRL_GPIO_OUTPUT0 |
2170 GRC_LCLCTRL_GPIO_OUTPUT1 |
2171 tp->grc_local_ctrl;
2172 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2173
2174 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2175 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2176
2177 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2178 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 } else {
2180 u32 no_gpio2;
Michael Chandc56b7d2005-12-19 16:26:28 -08002181 u32 grc_local_ctrl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
Michael Chandc56b7d2005-12-19 16:26:28 -08002183 /* Workaround to prevent overdrawing Amps. */
2184 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2185 ASIC_REV_5714) {
2186 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chanb401e9e2005-12-19 16:27:04 -08002187 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2188 grc_local_ctrl, 100);
Michael Chandc56b7d2005-12-19 16:26:28 -08002189 }
2190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 /* On 5753 and variants, GPIO2 cannot be used. */
2192 no_gpio2 = tp->nic_sram_data_cfg &
2193 NIC_SRAM_DATA_CFG_NO_GPIO2;
2194
Michael Chandc56b7d2005-12-19 16:26:28 -08002195 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 GRC_LCLCTRL_GPIO_OE1 |
2197 GRC_LCLCTRL_GPIO_OE2 |
2198 GRC_LCLCTRL_GPIO_OUTPUT1 |
2199 GRC_LCLCTRL_GPIO_OUTPUT2;
2200 if (no_gpio2) {
2201 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2202 GRC_LCLCTRL_GPIO_OUTPUT2);
2203 }
Michael Chanb401e9e2005-12-19 16:27:04 -08002204 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2205 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
2207 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2208
Michael Chanb401e9e2005-12-19 16:27:04 -08002209 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2210 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
2212 if (!no_gpio2) {
2213 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chanb401e9e2005-12-19 16:27:04 -08002214 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2215 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 }
2217 }
2218 } else {
2219 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
2220 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002221 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2222 (GRC_LCLCTRL_GPIO_OE1 |
2223 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
Michael Chanb401e9e2005-12-19 16:27:04 -08002225 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2226 GRC_LCLCTRL_GPIO_OE1, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
Michael Chanb401e9e2005-12-19 16:27:04 -08002228 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2229 (GRC_LCLCTRL_GPIO_OE1 |
2230 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 }
2232 }
2233}
2234
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002235static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2236{
2237 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2238 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002239 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002240 if (speed != SPEED_10)
2241 return 1;
2242 } else if (speed == SPEED_10)
2243 return 1;
2244
2245 return 0;
2246}
2247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248static int tg3_setup_phy(struct tg3 *, int);
2249
2250#define RESET_KIND_SHUTDOWN 0
2251#define RESET_KIND_INIT 1
2252#define RESET_KIND_SUSPEND 2
2253
2254static void tg3_write_sig_post_reset(struct tg3 *, int);
2255static int tg3_halt_cpu(struct tg3 *, u32);
2256
Matt Carlson0a459aa2008-11-03 16:54:15 -08002257static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002258{
Matt Carlsonce057f02007-11-12 21:08:03 -08002259 u32 val;
2260
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002261 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002262 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2263 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2264 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2265
2266 sg_dig_ctrl |=
2267 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2268 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2269 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2270 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002271 return;
Michael Chan51297242007-02-13 12:17:57 -08002272 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002273
Michael Chan60189dd2006-12-17 17:08:07 -08002274 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002275 tg3_bmcr_reset(tp);
2276 val = tr32(GRC_MISC_CFG);
2277 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2278 udelay(40);
2279 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002280 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002281 u32 phytest;
2282 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2283 u32 phy;
2284
2285 tg3_writephy(tp, MII_ADVERTISE, 0);
2286 tg3_writephy(tp, MII_BMCR,
2287 BMCR_ANENABLE | BMCR_ANRESTART);
2288
2289 tg3_writephy(tp, MII_TG3_FET_TEST,
2290 phytest | MII_TG3_FET_SHADOW_EN);
2291 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2292 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2293 tg3_writephy(tp,
2294 MII_TG3_FET_SHDW_AUXMODE4,
2295 phy);
2296 }
2297 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2298 }
2299 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002300 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002301 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2302 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002303
2304 tg3_writephy(tp, MII_TG3_AUX_CTRL,
2305 MII_TG3_AUXCTL_SHDWSEL_PWRCTL |
2306 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2307 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2308 MII_TG3_AUXCTL_PCTL_VREG_11V);
Michael Chan715116a2006-09-27 16:09:25 -07002309 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002310
Michael Chan15c3b692006-03-22 01:06:52 -08002311 /* The PHY should not be powered down on some chips because
2312 * of bugs.
2313 */
2314 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2315 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2316 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002317 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Michael Chan15c3b692006-03-22 01:06:52 -08002318 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002319
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002320 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2321 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002322 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2323 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2324 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2325 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2326 }
2327
Michael Chan15c3b692006-03-22 01:06:52 -08002328 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2329}
2330
Matt Carlson3f007892008-11-03 16:51:36 -08002331/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002332static int tg3_nvram_lock(struct tg3 *tp)
2333{
2334 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2335 int i;
2336
2337 if (tp->nvram_lock_cnt == 0) {
2338 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2339 for (i = 0; i < 8000; i++) {
2340 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2341 break;
2342 udelay(20);
2343 }
2344 if (i == 8000) {
2345 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2346 return -ENODEV;
2347 }
2348 }
2349 tp->nvram_lock_cnt++;
2350 }
2351 return 0;
2352}
2353
2354/* tp->lock is held. */
2355static void tg3_nvram_unlock(struct tg3 *tp)
2356{
2357 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2358 if (tp->nvram_lock_cnt > 0)
2359 tp->nvram_lock_cnt--;
2360 if (tp->nvram_lock_cnt == 0)
2361 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2362 }
2363}
2364
2365/* tp->lock is held. */
2366static void tg3_enable_nvram_access(struct tg3 *tp)
2367{
2368 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +00002369 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002370 u32 nvaccess = tr32(NVRAM_ACCESS);
2371
2372 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2373 }
2374}
2375
2376/* tp->lock is held. */
2377static void tg3_disable_nvram_access(struct tg3 *tp)
2378{
2379 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +00002380 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002381 u32 nvaccess = tr32(NVRAM_ACCESS);
2382
2383 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2384 }
2385}
2386
2387static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2388 u32 offset, u32 *val)
2389{
2390 u32 tmp;
2391 int i;
2392
2393 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2394 return -EINVAL;
2395
2396 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2397 EEPROM_ADDR_DEVID_MASK |
2398 EEPROM_ADDR_READ);
2399 tw32(GRC_EEPROM_ADDR,
2400 tmp |
2401 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2402 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2403 EEPROM_ADDR_ADDR_MASK) |
2404 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2405
2406 for (i = 0; i < 1000; i++) {
2407 tmp = tr32(GRC_EEPROM_ADDR);
2408
2409 if (tmp & EEPROM_ADDR_COMPLETE)
2410 break;
2411 msleep(1);
2412 }
2413 if (!(tmp & EEPROM_ADDR_COMPLETE))
2414 return -EBUSY;
2415
Matt Carlson62cedd12009-04-20 14:52:29 -07002416 tmp = tr32(GRC_EEPROM_DATA);
2417
2418 /*
2419 * The data will always be opposite the native endian
2420 * format. Perform a blind byteswap to compensate.
2421 */
2422 *val = swab32(tmp);
2423
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002424 return 0;
2425}
2426
2427#define NVRAM_CMD_TIMEOUT 10000
2428
2429static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2430{
2431 int i;
2432
2433 tw32(NVRAM_CMD, nvram_cmd);
2434 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2435 udelay(10);
2436 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2437 udelay(10);
2438 break;
2439 }
2440 }
2441
2442 if (i == NVRAM_CMD_TIMEOUT)
2443 return -EBUSY;
2444
2445 return 0;
2446}
2447
2448static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2449{
2450 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2451 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2452 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2453 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2454 (tp->nvram_jedecnum == JEDEC_ATMEL))
2455
2456 addr = ((addr / tp->nvram_pagesize) <<
2457 ATMEL_AT45DB0X1B_PAGE_POS) +
2458 (addr % tp->nvram_pagesize);
2459
2460 return addr;
2461}
2462
2463static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2464{
2465 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2466 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2467 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2468 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2469 (tp->nvram_jedecnum == JEDEC_ATMEL))
2470
2471 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2472 tp->nvram_pagesize) +
2473 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2474
2475 return addr;
2476}
2477
Matt Carlsone4f34112009-02-25 14:25:00 +00002478/* NOTE: Data read in from NVRAM is byteswapped according to
2479 * the byteswapping settings for all other register accesses.
2480 * tg3 devices are BE devices, so on a BE machine, the data
2481 * returned will be exactly as it is seen in NVRAM. On a LE
2482 * machine, the 32-bit value will be byteswapped.
2483 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002484static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2485{
2486 int ret;
2487
2488 if (!(tp->tg3_flags & TG3_FLAG_NVRAM))
2489 return tg3_nvram_read_using_eeprom(tp, offset, val);
2490
2491 offset = tg3_nvram_phys_addr(tp, offset);
2492
2493 if (offset > NVRAM_ADDR_MSK)
2494 return -EINVAL;
2495
2496 ret = tg3_nvram_lock(tp);
2497 if (ret)
2498 return ret;
2499
2500 tg3_enable_nvram_access(tp);
2501
2502 tw32(NVRAM_ADDR, offset);
2503 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2504 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2505
2506 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002507 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002508
2509 tg3_disable_nvram_access(tp);
2510
2511 tg3_nvram_unlock(tp);
2512
2513 return ret;
2514}
2515
Matt Carlsona9dc5292009-02-25 14:25:30 +00002516/* Ensures NVRAM data is in bytestream format. */
2517static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002518{
2519 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002520 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002521 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002522 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002523 return res;
2524}
2525
2526/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08002527static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2528{
2529 u32 addr_high, addr_low;
2530 int i;
2531
2532 addr_high = ((tp->dev->dev_addr[0] << 8) |
2533 tp->dev->dev_addr[1]);
2534 addr_low = ((tp->dev->dev_addr[2] << 24) |
2535 (tp->dev->dev_addr[3] << 16) |
2536 (tp->dev->dev_addr[4] << 8) |
2537 (tp->dev->dev_addr[5] << 0));
2538 for (i = 0; i < 4; i++) {
2539 if (i == 1 && skip_mac_1)
2540 continue;
2541 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2542 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2543 }
2544
2545 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2546 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2547 for (i = 0; i < 12; i++) {
2548 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2549 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2550 }
2551 }
2552
2553 addr_high = (tp->dev->dev_addr[0] +
2554 tp->dev->dev_addr[1] +
2555 tp->dev->dev_addr[2] +
2556 tp->dev->dev_addr[3] +
2557 tp->dev->dev_addr[4] +
2558 tp->dev->dev_addr[5]) &
2559 TX_BACKOFF_SEED_MASK;
2560 tw32(MAC_TX_BACKOFF_SEED, addr_high);
2561}
2562
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002563static void tg3_enable_register_access(struct tg3 *tp)
2564{
2565 /*
2566 * Make sure register accesses (indirect or otherwise) will function
2567 * correctly.
2568 */
2569 pci_write_config_dword(tp->pdev,
2570 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
2571}
2572
2573static int tg3_power_up(struct tg3 *tp)
2574{
2575 tg3_enable_register_access(tp);
2576
2577 pci_set_power_state(tp->pdev, PCI_D0);
2578
2579 /* Switch out of Vaux if it is a NIC */
2580 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
2581 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl, 100);
2582
2583 return 0;
2584}
2585
2586static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587{
2588 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002589 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002591 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002592
2593 /* Restore the CLKREQ setting. */
2594 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
2595 u16 lnkctl;
2596
2597 pci_read_config_word(tp->pdev,
2598 tp->pcie_cap + PCI_EXP_LNKCTL,
2599 &lnkctl);
2600 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2601 pci_write_config_word(tp->pdev,
2602 tp->pcie_cap + PCI_EXP_LNKCTL,
2603 lnkctl);
2604 }
2605
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2607 tw32(TG3PCI_MISC_HOST_CTRL,
2608 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2609
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002610 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002611 (tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
2612
Matt Carlsondd477002008-05-25 23:45:58 -07002613 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002614 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002615 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00002616 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002617 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002618 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002619
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002620 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002621
Matt Carlson80096062010-08-02 11:26:06 +00002622 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002623
2624 tp->link_config.orig_speed = phydev->speed;
2625 tp->link_config.orig_duplex = phydev->duplex;
2626 tp->link_config.orig_autoneg = phydev->autoneg;
2627 tp->link_config.orig_advertising = phydev->advertising;
2628
2629 advertising = ADVERTISED_TP |
2630 ADVERTISED_Pause |
2631 ADVERTISED_Autoneg |
2632 ADVERTISED_10baseT_Half;
2633
2634 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002635 device_should_wake) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002636 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2637 advertising |=
2638 ADVERTISED_100baseT_Half |
2639 ADVERTISED_100baseT_Full |
2640 ADVERTISED_10baseT_Full;
2641 else
2642 advertising |= ADVERTISED_10baseT_Full;
2643 }
2644
2645 phydev->advertising = advertising;
2646
2647 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002648
2649 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00002650 if (phyid != PHY_ID_BCMAC131) {
2651 phyid &= PHY_BCM_OUI_MASK;
2652 if (phyid == PHY_BCM_OUI_1 ||
2653 phyid == PHY_BCM_OUI_2 ||
2654 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08002655 do_low_power = true;
2656 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002657 }
Matt Carlsondd477002008-05-25 23:45:58 -07002658 } else {
Matt Carlson20232762008-12-21 20:18:56 -08002659 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002660
Matt Carlson80096062010-08-02 11:26:06 +00002661 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
2662 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07002663 tp->link_config.orig_speed = tp->link_config.speed;
2664 tp->link_config.orig_duplex = tp->link_config.duplex;
2665 tp->link_config.orig_autoneg = tp->link_config.autoneg;
2666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002668 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Matt Carlsondd477002008-05-25 23:45:58 -07002669 tp->link_config.speed = SPEED_10;
2670 tp->link_config.duplex = DUPLEX_HALF;
2671 tp->link_config.autoneg = AUTONEG_ENABLE;
2672 tg3_setup_phy(tp, 0);
2673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 }
2675
Michael Chanb5d37722006-09-27 16:06:21 -07002676 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2677 u32 val;
2678
2679 val = tr32(GRC_VCPU_EXT_CTRL);
2680 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
2681 } else if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08002682 int i;
2683 u32 val;
2684
2685 for (i = 0; i < 200; i++) {
2686 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2687 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2688 break;
2689 msleep(1);
2690 }
2691 }
Gary Zambranoa85feb82007-05-05 11:52:19 -07002692 if (tp->tg3_flags & TG3_FLAG_WOL_CAP)
2693 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2694 WOL_DRV_STATE_SHUTDOWN |
2695 WOL_DRV_WOL |
2696 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08002697
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002698 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 u32 mac_mode;
2700
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002701 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002702 if (do_low_power) {
Matt Carlsondd477002008-05-25 23:45:58 -07002703 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x5a);
2704 udelay(40);
2705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002707 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07002708 mac_mode = MAC_MODE_PORT_MODE_GMII;
2709 else
2710 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002712 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2713 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2714 ASIC_REV_5700) {
2715 u32 speed = (tp->tg3_flags &
2716 TG3_FLAG_WOL_SPEED_100MB) ?
2717 SPEED_100 : SPEED_10;
2718 if (tg3_5700_link_polarity(tp, speed))
2719 mac_mode |= MAC_MODE_LINK_POLARITY;
2720 else
2721 mac_mode &= ~MAC_MODE_LINK_POLARITY;
2722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 } else {
2724 mac_mode = MAC_MODE_PORT_MODE_TBI;
2725 }
2726
John W. Linvillecbf46852005-04-21 17:01:29 -07002727 if (!(tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 tw32(MAC_LED_CTRL, tp->led_ctrl);
2729
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002730 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
2731 if (((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
2732 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) &&
2733 ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
2734 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)))
2735 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
Matt Carlsond2394e6b2010-11-24 08:31:47 +00002737 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
2738 mac_mode |= MAC_MODE_APE_TX_EN |
2739 MAC_MODE_APE_RX_EN |
2740 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07002741
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 tw32_f(MAC_MODE, mac_mode);
2743 udelay(100);
2744
2745 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2746 udelay(10);
2747 }
2748
2749 if (!(tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB) &&
2750 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2751 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2752 u32 base_val;
2753
2754 base_val = tp->pci_clock_ctrl;
2755 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2756 CLOCK_CTRL_TXCLK_DISABLE);
2757
Michael Chanb401e9e2005-12-19 16:27:04 -08002758 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
2759 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Michael Chand7b0a852007-02-13 12:17:38 -08002760 } else if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
Matt Carlson795d01c2007-10-07 23:28:17 -07002761 (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
Michael Chand7b0a852007-02-13 12:17:38 -08002762 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)) {
Michael Chan4cf78e42005-07-25 12:29:19 -07002763 /* do nothing */
Michael Chan85e94ce2005-04-21 17:05:28 -07002764 } else if (!((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF))) {
2766 u32 newbits1, newbits2;
2767
2768 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2769 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2770 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
2771 CLOCK_CTRL_TXCLK_DISABLE |
2772 CLOCK_CTRL_ALTCLK);
2773 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2774 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
2775 newbits1 = CLOCK_CTRL_625_CORE;
2776 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
2777 } else {
2778 newbits1 = CLOCK_CTRL_ALTCLK;
2779 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2780 }
2781
Michael Chanb401e9e2005-12-19 16:27:04 -08002782 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
2783 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
Michael Chanb401e9e2005-12-19 16:27:04 -08002785 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
2786 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787
2788 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
2789 u32 newbits3;
2790
2791 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2792 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2793 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
2794 CLOCK_CTRL_TXCLK_DISABLE |
2795 CLOCK_CTRL_44MHZ_CORE);
2796 } else {
2797 newbits3 = CLOCK_CTRL_44MHZ_CORE;
2798 }
2799
Michael Chanb401e9e2005-12-19 16:27:04 -08002800 tw32_wait_f(TG3PCI_CLOCK_CTRL,
2801 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 }
2803 }
2804
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002805 if (!(device_should_wake) &&
Matt Carlson22435842008-11-21 17:21:13 -08002806 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08002807 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08002808
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 tg3_frob_aux_power(tp);
2810
2811 /* Workaround for unstable PLL clock */
2812 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
2813 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
2814 u32 val = tr32(0x7d00);
2815
2816 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
2817 tw32(0x7d00, val);
Michael Chan6921d202005-12-13 21:15:53 -08002818 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08002819 int err;
2820
2821 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08002823 if (!err)
2824 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08002825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 }
2827
Michael Chanbbadf502006-04-06 21:46:34 -07002828 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
2829
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 return 0;
2831}
2832
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002833static void tg3_power_down(struct tg3 *tp)
2834{
2835 tg3_power_down_prepare(tp);
2836
2837 pci_wake_from_d3(tp->pdev, tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
2838 pci_set_power_state(tp->pdev, PCI_D3hot);
2839}
2840
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
2842{
2843 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
2844 case MII_TG3_AUX_STAT_10HALF:
2845 *speed = SPEED_10;
2846 *duplex = DUPLEX_HALF;
2847 break;
2848
2849 case MII_TG3_AUX_STAT_10FULL:
2850 *speed = SPEED_10;
2851 *duplex = DUPLEX_FULL;
2852 break;
2853
2854 case MII_TG3_AUX_STAT_100HALF:
2855 *speed = SPEED_100;
2856 *duplex = DUPLEX_HALF;
2857 break;
2858
2859 case MII_TG3_AUX_STAT_100FULL:
2860 *speed = SPEED_100;
2861 *duplex = DUPLEX_FULL;
2862 break;
2863
2864 case MII_TG3_AUX_STAT_1000HALF:
2865 *speed = SPEED_1000;
2866 *duplex = DUPLEX_HALF;
2867 break;
2868
2869 case MII_TG3_AUX_STAT_1000FULL:
2870 *speed = SPEED_1000;
2871 *duplex = DUPLEX_FULL;
2872 break;
2873
2874 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002875 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07002876 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
2877 SPEED_10;
2878 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
2879 DUPLEX_HALF;
2880 break;
2881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 *speed = SPEED_INVALID;
2883 *duplex = DUPLEX_INVALID;
2884 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886}
2887
2888static void tg3_phy_copper_begin(struct tg3 *tp)
2889{
2890 u32 new_adv;
2891 int i;
2892
Matt Carlson80096062010-08-02 11:26:06 +00002893 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 /* Entering low power mode. Disable gigabit and
2895 * 100baseT advertisements.
2896 */
2897 tg3_writephy(tp, MII_TG3_CTRL, 0);
2898
2899 new_adv = (ADVERTISE_10HALF | ADVERTISE_10FULL |
2900 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
2901 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2902 new_adv |= (ADVERTISE_100HALF | ADVERTISE_100FULL);
2903
2904 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2905 } else if (tp->link_config.speed == SPEED_INVALID) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002906 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 tp->link_config.advertising &=
2908 ~(ADVERTISED_1000baseT_Half |
2909 ADVERTISED_1000baseT_Full);
2910
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002911 new_adv = ADVERTISE_CSMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 if (tp->link_config.advertising & ADVERTISED_10baseT_Half)
2913 new_adv |= ADVERTISE_10HALF;
2914 if (tp->link_config.advertising & ADVERTISED_10baseT_Full)
2915 new_adv |= ADVERTISE_10FULL;
2916 if (tp->link_config.advertising & ADVERTISED_100baseT_Half)
2917 new_adv |= ADVERTISE_100HALF;
2918 if (tp->link_config.advertising & ADVERTISED_100baseT_Full)
2919 new_adv |= ADVERTISE_100FULL;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002920
2921 new_adv |= tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2922
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2924
2925 if (tp->link_config.advertising &
2926 (ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full)) {
2927 new_adv = 0;
2928 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
2929 new_adv |= MII_TG3_CTRL_ADV_1000_HALF;
2930 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
2931 new_adv |= MII_TG3_CTRL_ADV_1000_FULL;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002932 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2934 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0))
2935 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2936 MII_TG3_CTRL_ENABLE_AS_MASTER);
2937 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
2938 } else {
2939 tg3_writephy(tp, MII_TG3_CTRL, 0);
2940 }
2941 } else {
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002942 new_adv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2943 new_adv |= ADVERTISE_CSMA;
2944
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 /* Asking for a specific link mode. */
2946 if (tp->link_config.speed == SPEED_1000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2948
2949 if (tp->link_config.duplex == DUPLEX_FULL)
2950 new_adv = MII_TG3_CTRL_ADV_1000_FULL;
2951 else
2952 new_adv = MII_TG3_CTRL_ADV_1000_HALF;
2953 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2954 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
2955 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2956 MII_TG3_CTRL_ENABLE_AS_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 if (tp->link_config.speed == SPEED_100) {
2959 if (tp->link_config.duplex == DUPLEX_FULL)
2960 new_adv |= ADVERTISE_100FULL;
2961 else
2962 new_adv |= ADVERTISE_100HALF;
2963 } else {
2964 if (tp->link_config.duplex == DUPLEX_FULL)
2965 new_adv |= ADVERTISE_10FULL;
2966 else
2967 new_adv |= ADVERTISE_10HALF;
2968 }
2969 tg3_writephy(tp, MII_ADVERTISE, new_adv);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002970
2971 new_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002973
2974 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 }
2976
Matt Carlson52b02d02010-10-14 10:37:41 +00002977 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
Matt Carlsona6b68da2010-12-06 08:28:52 +00002978 u32 val;
Matt Carlson52b02d02010-10-14 10:37:41 +00002979
2980 tw32(TG3_CPMU_EEE_MODE,
2981 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2982
2983 /* Enable SM_DSP clock and tx 6dB coding. */
2984 val = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
2985 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
2986 MII_TG3_AUXCTL_ACTL_TX_6DB;
2987 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
2988
Matt Carlson21a00ab2011-01-25 15:58:55 +00002989 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
2990 case ASIC_REV_5717:
2991 case ASIC_REV_57765:
2992 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
2993 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
2994 MII_TG3_DSP_CH34TP2_HIBW01);
2995 /* Fall through */
2996 case ASIC_REV_5719:
2997 val = MII_TG3_DSP_TAP26_ALNOKO |
2998 MII_TG3_DSP_TAP26_RMRXSTO |
2999 MII_TG3_DSP_TAP26_OPCSINPT;
3000 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
3001 }
Matt Carlson52b02d02010-10-14 10:37:41 +00003002
Matt Carlsona6b68da2010-12-06 08:28:52 +00003003 val = 0;
Matt Carlson52b02d02010-10-14 10:37:41 +00003004 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3005 /* Advertise 100-BaseTX EEE ability */
3006 if (tp->link_config.advertising &
Matt Carlson3110f5f52010-12-06 08:28:50 +00003007 ADVERTISED_100baseT_Full)
3008 val |= MDIO_AN_EEE_ADV_100TX;
Matt Carlson52b02d02010-10-14 10:37:41 +00003009 /* Advertise 1000-BaseT EEE ability */
3010 if (tp->link_config.advertising &
Matt Carlson3110f5f52010-12-06 08:28:50 +00003011 ADVERTISED_1000baseT_Full)
3012 val |= MDIO_AN_EEE_ADV_1000T;
Matt Carlson52b02d02010-10-14 10:37:41 +00003013 }
Matt Carlson3110f5f52010-12-06 08:28:50 +00003014 tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlson52b02d02010-10-14 10:37:41 +00003015
3016 /* Turn off SM_DSP clock. */
3017 val = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
3018 MII_TG3_AUXCTL_ACTL_TX_6DB;
3019 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
3020 }
3021
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
3023 tp->link_config.speed != SPEED_INVALID) {
3024 u32 bmcr, orig_bmcr;
3025
3026 tp->link_config.active_speed = tp->link_config.speed;
3027 tp->link_config.active_duplex = tp->link_config.duplex;
3028
3029 bmcr = 0;
3030 switch (tp->link_config.speed) {
3031 default:
3032 case SPEED_10:
3033 break;
3034
3035 case SPEED_100:
3036 bmcr |= BMCR_SPEED100;
3037 break;
3038
3039 case SPEED_1000:
3040 bmcr |= TG3_BMCR_SPEED1000;
3041 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
3044 if (tp->link_config.duplex == DUPLEX_FULL)
3045 bmcr |= BMCR_FULLDPLX;
3046
3047 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3048 (bmcr != orig_bmcr)) {
3049 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3050 for (i = 0; i < 1500; i++) {
3051 u32 tmp;
3052
3053 udelay(10);
3054 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3055 tg3_readphy(tp, MII_BMSR, &tmp))
3056 continue;
3057 if (!(tmp & BMSR_LSTATUS)) {
3058 udelay(40);
3059 break;
3060 }
3061 }
3062 tg3_writephy(tp, MII_BMCR, bmcr);
3063 udelay(40);
3064 }
3065 } else {
3066 tg3_writephy(tp, MII_BMCR,
3067 BMCR_ANENABLE | BMCR_ANRESTART);
3068 }
3069}
3070
3071static int tg3_init_5401phy_dsp(struct tg3 *tp)
3072{
3073 int err;
3074
3075 /* Turn off tap power management. */
3076 /* Set Extended packet length bit */
3077 err = tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
3078
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003079 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3080 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3081 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3082 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3083 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
3085 udelay(40);
3086
3087 return err;
3088}
3089
Michael Chan3600d912006-12-07 00:21:48 -08003090static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091{
Michael Chan3600d912006-12-07 00:21:48 -08003092 u32 adv_reg, all_mask = 0;
3093
3094 if (mask & ADVERTISED_10baseT_Half)
3095 all_mask |= ADVERTISE_10HALF;
3096 if (mask & ADVERTISED_10baseT_Full)
3097 all_mask |= ADVERTISE_10FULL;
3098 if (mask & ADVERTISED_100baseT_Half)
3099 all_mask |= ADVERTISE_100HALF;
3100 if (mask & ADVERTISED_100baseT_Full)
3101 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102
3103 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
3104 return 0;
3105
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 if ((adv_reg & all_mask) != all_mask)
3107 return 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003108 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 u32 tg3_ctrl;
3110
Michael Chan3600d912006-12-07 00:21:48 -08003111 all_mask = 0;
3112 if (mask & ADVERTISED_1000baseT_Half)
3113 all_mask |= ADVERTISE_1000HALF;
3114 if (mask & ADVERTISED_1000baseT_Full)
3115 all_mask |= ADVERTISE_1000FULL;
3116
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 if (tg3_readphy(tp, MII_TG3_CTRL, &tg3_ctrl))
3118 return 0;
3119
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 if ((tg3_ctrl & all_mask) != all_mask)
3121 return 0;
3122 }
3123 return 1;
3124}
3125
Matt Carlsonef167e22007-12-20 20:10:01 -08003126static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
3127{
3128 u32 curadv, reqadv;
3129
3130 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3131 return 1;
3132
3133 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3134 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3135
3136 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3137 if (curadv != reqadv)
3138 return 0;
3139
3140 if (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)
3141 tg3_readphy(tp, MII_LPA, rmtadv);
3142 } else {
3143 /* Reprogram the advertisement register, even if it
3144 * does not affect the current link. If the link
3145 * gets renegotiated in the future, we can save an
3146 * additional renegotiation cycle by advertising
3147 * it correctly in the first place.
3148 */
3149 if (curadv != reqadv) {
3150 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3151 ADVERTISE_PAUSE_ASYM);
3152 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3153 }
3154 }
3155
3156 return 1;
3157}
3158
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3160{
3161 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003162 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08003163 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 u16 current_speed;
3165 u8 current_duplex;
3166 int i, err;
3167
3168 tw32(MAC_EVENT, 0);
3169
3170 tw32_f(MAC_STATUS,
3171 (MAC_STATUS_SYNC_CHANGED |
3172 MAC_STATUS_CFG_CHANGED |
3173 MAC_STATUS_MI_COMPLETION |
3174 MAC_STATUS_LNKSTATE_CHANGED));
3175 udelay(40);
3176
Matt Carlson8ef21422008-05-02 16:47:53 -07003177 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3178 tw32_f(MAC_MI_MODE,
3179 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3180 udelay(80);
3181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
3183 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x02);
3184
3185 /* Some third-party PHYs need to be reset on link going
3186 * down.
3187 */
3188 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3189 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3190 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3191 netif_carrier_ok(tp->dev)) {
3192 tg3_readphy(tp, MII_BMSR, &bmsr);
3193 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3194 !(bmsr & BMSR_LSTATUS))
3195 force_reset = 1;
3196 }
3197 if (force_reset)
3198 tg3_phy_reset(tp);
3199
Matt Carlson79eb6902010-02-17 15:17:03 +00003200 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 tg3_readphy(tp, MII_BMSR, &bmsr);
3202 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
3203 !(tp->tg3_flags & TG3_FLAG_INIT_COMPLETE))
3204 bmsr = 0;
3205
3206 if (!(bmsr & BMSR_LSTATUS)) {
3207 err = tg3_init_5401phy_dsp(tp);
3208 if (err)
3209 return err;
3210
3211 tg3_readphy(tp, MII_BMSR, &bmsr);
3212 for (i = 0; i < 1000; i++) {
3213 udelay(10);
3214 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3215 (bmsr & BMSR_LSTATUS)) {
3216 udelay(40);
3217 break;
3218 }
3219 }
3220
Matt Carlson79eb6902010-02-17 15:17:03 +00003221 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3222 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 !(bmsr & BMSR_LSTATUS) &&
3224 tp->link_config.active_speed == SPEED_1000) {
3225 err = tg3_phy_reset(tp);
3226 if (!err)
3227 err = tg3_init_5401phy_dsp(tp);
3228 if (err)
3229 return err;
3230 }
3231 }
3232 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3233 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3234 /* 5701 {A0,B0} CRC bug workaround */
3235 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00003236 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3237 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
3238 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 }
3240
3241 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003242 tg3_readphy(tp, MII_TG3_ISTAT, &val);
3243 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003245 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003247 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3249
3250 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3251 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3252 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3253 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3254 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3255 else
3256 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3257 }
3258
3259 current_link_up = 0;
3260 current_speed = SPEED_INVALID;
3261 current_duplex = DUPLEX_INVALID;
3262
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003263 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4007);
3265 tg3_readphy(tp, MII_TG3_AUX_CTRL, &val);
3266 if (!(val & (1 << 10))) {
3267 val |= (1 << 10);
3268 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
3269 goto relink;
3270 }
3271 }
3272
3273 bmsr = 0;
3274 for (i = 0; i < 100; i++) {
3275 tg3_readphy(tp, MII_BMSR, &bmsr);
3276 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3277 (bmsr & BMSR_LSTATUS))
3278 break;
3279 udelay(40);
3280 }
3281
3282 if (bmsr & BMSR_LSTATUS) {
3283 u32 aux_stat, bmcr;
3284
3285 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3286 for (i = 0; i < 2000; i++) {
3287 udelay(10);
3288 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3289 aux_stat)
3290 break;
3291 }
3292
3293 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3294 &current_speed,
3295 &current_duplex);
3296
3297 bmcr = 0;
3298 for (i = 0; i < 200; i++) {
3299 tg3_readphy(tp, MII_BMCR, &bmcr);
3300 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3301 continue;
3302 if (bmcr && bmcr != 0x7fff)
3303 break;
3304 udelay(10);
3305 }
3306
Matt Carlsonef167e22007-12-20 20:10:01 -08003307 lcl_adv = 0;
3308 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
Matt Carlsonef167e22007-12-20 20:10:01 -08003310 tp->link_config.active_speed = current_speed;
3311 tp->link_config.active_duplex = current_duplex;
3312
3313 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3314 if ((bmcr & BMCR_ANENABLE) &&
3315 tg3_copper_is_advertising_all(tp,
3316 tp->link_config.advertising)) {
3317 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3318 &rmt_adv))
3319 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 }
3321 } else {
3322 if (!(bmcr & BMCR_ANENABLE) &&
3323 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003324 tp->link_config.duplex == current_duplex &&
3325 tp->link_config.flowctrl ==
3326 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 }
3329 }
3330
Matt Carlsonef167e22007-12-20 20:10:01 -08003331 if (current_link_up == 1 &&
3332 tp->link_config.active_duplex == DUPLEX_FULL)
3333 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 }
3335
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336relink:
Matt Carlson80096062010-08-02 11:26:06 +00003337 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 tg3_phy_copper_begin(tp);
3339
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003340 tg3_readphy(tp, MII_BMSR, &bmsr);
3341 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3342 (bmsr & BMSR_LSTATUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 current_link_up = 1;
3344 }
3345
3346 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3347 if (current_link_up == 1) {
3348 if (tp->link_config.active_speed == SPEED_100 ||
3349 tp->link_config.active_speed == SPEED_10)
3350 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3351 else
3352 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003353 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003354 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3355 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3357
3358 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3359 if (tp->link_config.active_duplex == DUPLEX_HALF)
3360 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3361
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003363 if (current_link_up == 1 &&
3364 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003366 else
3367 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 }
3369
3370 /* ??? Without this setting Netgear GA302T PHY does not
3371 * ??? send/receive packets...
3372 */
Matt Carlson79eb6902010-02-17 15:17:03 +00003373 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3375 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3376 tw32_f(MAC_MI_MODE, tp->mi_mode);
3377 udelay(80);
3378 }
3379
3380 tw32_f(MAC_MODE, tp->mac_mode);
3381 udelay(40);
3382
Matt Carlson52b02d02010-10-14 10:37:41 +00003383 tg3_phy_eee_adjust(tp, current_link_up);
3384
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
3386 /* Polled via timer. */
3387 tw32_f(MAC_EVENT, 0);
3388 } else {
3389 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3390 }
3391 udelay(40);
3392
3393 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3394 current_link_up == 1 &&
3395 tp->link_config.active_speed == SPEED_1000 &&
3396 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) ||
3397 (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED))) {
3398 udelay(120);
3399 tw32_f(MAC_STATUS,
3400 (MAC_STATUS_SYNC_CHANGED |
3401 MAC_STATUS_CFG_CHANGED));
3402 udelay(40);
3403 tg3_write_mem(tp,
3404 NIC_SRAM_FIRMWARE_MBOX,
3405 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3406 }
3407
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003408 /* Prevent send BD corruption. */
3409 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
3410 u16 oldlnkctl, newlnkctl;
3411
3412 pci_read_config_word(tp->pdev,
3413 tp->pcie_cap + PCI_EXP_LNKCTL,
3414 &oldlnkctl);
3415 if (tp->link_config.active_speed == SPEED_100 ||
3416 tp->link_config.active_speed == SPEED_10)
3417 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3418 else
3419 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3420 if (newlnkctl != oldlnkctl)
3421 pci_write_config_word(tp->pdev,
3422 tp->pcie_cap + PCI_EXP_LNKCTL,
3423 newlnkctl);
3424 }
3425
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 if (current_link_up != netif_carrier_ok(tp->dev)) {
3427 if (current_link_up)
3428 netif_carrier_on(tp->dev);
3429 else
3430 netif_carrier_off(tp->dev);
3431 tg3_link_report(tp);
3432 }
3433
3434 return 0;
3435}
3436
3437struct tg3_fiber_aneginfo {
3438 int state;
3439#define ANEG_STATE_UNKNOWN 0
3440#define ANEG_STATE_AN_ENABLE 1
3441#define ANEG_STATE_RESTART_INIT 2
3442#define ANEG_STATE_RESTART 3
3443#define ANEG_STATE_DISABLE_LINK_OK 4
3444#define ANEG_STATE_ABILITY_DETECT_INIT 5
3445#define ANEG_STATE_ABILITY_DETECT 6
3446#define ANEG_STATE_ACK_DETECT_INIT 7
3447#define ANEG_STATE_ACK_DETECT 8
3448#define ANEG_STATE_COMPLETE_ACK_INIT 9
3449#define ANEG_STATE_COMPLETE_ACK 10
3450#define ANEG_STATE_IDLE_DETECT_INIT 11
3451#define ANEG_STATE_IDLE_DETECT 12
3452#define ANEG_STATE_LINK_OK 13
3453#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3454#define ANEG_STATE_NEXT_PAGE_WAIT 15
3455
3456 u32 flags;
3457#define MR_AN_ENABLE 0x00000001
3458#define MR_RESTART_AN 0x00000002
3459#define MR_AN_COMPLETE 0x00000004
3460#define MR_PAGE_RX 0x00000008
3461#define MR_NP_LOADED 0x00000010
3462#define MR_TOGGLE_TX 0x00000020
3463#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3464#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3465#define MR_LP_ADV_SYM_PAUSE 0x00000100
3466#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3467#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3468#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3469#define MR_LP_ADV_NEXT_PAGE 0x00001000
3470#define MR_TOGGLE_RX 0x00002000
3471#define MR_NP_RX 0x00004000
3472
3473#define MR_LINK_OK 0x80000000
3474
3475 unsigned long link_time, cur_time;
3476
3477 u32 ability_match_cfg;
3478 int ability_match_count;
3479
3480 char ability_match, idle_match, ack_match;
3481
3482 u32 txconfig, rxconfig;
3483#define ANEG_CFG_NP 0x00000080
3484#define ANEG_CFG_ACK 0x00000040
3485#define ANEG_CFG_RF2 0x00000020
3486#define ANEG_CFG_RF1 0x00000010
3487#define ANEG_CFG_PS2 0x00000001
3488#define ANEG_CFG_PS1 0x00008000
3489#define ANEG_CFG_HD 0x00004000
3490#define ANEG_CFG_FD 0x00002000
3491#define ANEG_CFG_INVAL 0x00001f06
3492
3493};
3494#define ANEG_OK 0
3495#define ANEG_DONE 1
3496#define ANEG_TIMER_ENAB 2
3497#define ANEG_FAILED -1
3498
3499#define ANEG_STATE_SETTLE_TIME 10000
3500
3501static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3502 struct tg3_fiber_aneginfo *ap)
3503{
Matt Carlson5be73b42007-12-20 20:09:29 -08003504 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 unsigned long delta;
3506 u32 rx_cfg_reg;
3507 int ret;
3508
3509 if (ap->state == ANEG_STATE_UNKNOWN) {
3510 ap->rxconfig = 0;
3511 ap->link_time = 0;
3512 ap->cur_time = 0;
3513 ap->ability_match_cfg = 0;
3514 ap->ability_match_count = 0;
3515 ap->ability_match = 0;
3516 ap->idle_match = 0;
3517 ap->ack_match = 0;
3518 }
3519 ap->cur_time++;
3520
3521 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3522 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3523
3524 if (rx_cfg_reg != ap->ability_match_cfg) {
3525 ap->ability_match_cfg = rx_cfg_reg;
3526 ap->ability_match = 0;
3527 ap->ability_match_count = 0;
3528 } else {
3529 if (++ap->ability_match_count > 1) {
3530 ap->ability_match = 1;
3531 ap->ability_match_cfg = rx_cfg_reg;
3532 }
3533 }
3534 if (rx_cfg_reg & ANEG_CFG_ACK)
3535 ap->ack_match = 1;
3536 else
3537 ap->ack_match = 0;
3538
3539 ap->idle_match = 0;
3540 } else {
3541 ap->idle_match = 1;
3542 ap->ability_match_cfg = 0;
3543 ap->ability_match_count = 0;
3544 ap->ability_match = 0;
3545 ap->ack_match = 0;
3546
3547 rx_cfg_reg = 0;
3548 }
3549
3550 ap->rxconfig = rx_cfg_reg;
3551 ret = ANEG_OK;
3552
Matt Carlson33f401a2010-04-05 10:19:27 +00003553 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 case ANEG_STATE_UNKNOWN:
3555 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3556 ap->state = ANEG_STATE_AN_ENABLE;
3557
3558 /* fallthru */
3559 case ANEG_STATE_AN_ENABLE:
3560 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3561 if (ap->flags & MR_AN_ENABLE) {
3562 ap->link_time = 0;
3563 ap->cur_time = 0;
3564 ap->ability_match_cfg = 0;
3565 ap->ability_match_count = 0;
3566 ap->ability_match = 0;
3567 ap->idle_match = 0;
3568 ap->ack_match = 0;
3569
3570 ap->state = ANEG_STATE_RESTART_INIT;
3571 } else {
3572 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3573 }
3574 break;
3575
3576 case ANEG_STATE_RESTART_INIT:
3577 ap->link_time = ap->cur_time;
3578 ap->flags &= ~(MR_NP_LOADED);
3579 ap->txconfig = 0;
3580 tw32(MAC_TX_AUTO_NEG, 0);
3581 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3582 tw32_f(MAC_MODE, tp->mac_mode);
3583 udelay(40);
3584
3585 ret = ANEG_TIMER_ENAB;
3586 ap->state = ANEG_STATE_RESTART;
3587
3588 /* fallthru */
3589 case ANEG_STATE_RESTART:
3590 delta = ap->cur_time - ap->link_time;
Matt Carlson859a5882010-04-05 10:19:28 +00003591 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a5882010-04-05 10:19:28 +00003593 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 break;
3596
3597 case ANEG_STATE_DISABLE_LINK_OK:
3598 ret = ANEG_DONE;
3599 break;
3600
3601 case ANEG_STATE_ABILITY_DETECT_INIT:
3602 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003603 ap->txconfig = ANEG_CFG_FD;
3604 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3605 if (flowctrl & ADVERTISE_1000XPAUSE)
3606 ap->txconfig |= ANEG_CFG_PS1;
3607 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3608 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3610 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3611 tw32_f(MAC_MODE, tp->mac_mode);
3612 udelay(40);
3613
3614 ap->state = ANEG_STATE_ABILITY_DETECT;
3615 break;
3616
3617 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a5882010-04-05 10:19:28 +00003618 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 break;
3621
3622 case ANEG_STATE_ACK_DETECT_INIT:
3623 ap->txconfig |= ANEG_CFG_ACK;
3624 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3625 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3626 tw32_f(MAC_MODE, tp->mac_mode);
3627 udelay(40);
3628
3629 ap->state = ANEG_STATE_ACK_DETECT;
3630
3631 /* fallthru */
3632 case ANEG_STATE_ACK_DETECT:
3633 if (ap->ack_match != 0) {
3634 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3635 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3636 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3637 } else {
3638 ap->state = ANEG_STATE_AN_ENABLE;
3639 }
3640 } else if (ap->ability_match != 0 &&
3641 ap->rxconfig == 0) {
3642 ap->state = ANEG_STATE_AN_ENABLE;
3643 }
3644 break;
3645
3646 case ANEG_STATE_COMPLETE_ACK_INIT:
3647 if (ap->rxconfig & ANEG_CFG_INVAL) {
3648 ret = ANEG_FAILED;
3649 break;
3650 }
3651 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3652 MR_LP_ADV_HALF_DUPLEX |
3653 MR_LP_ADV_SYM_PAUSE |
3654 MR_LP_ADV_ASYM_PAUSE |
3655 MR_LP_ADV_REMOTE_FAULT1 |
3656 MR_LP_ADV_REMOTE_FAULT2 |
3657 MR_LP_ADV_NEXT_PAGE |
3658 MR_TOGGLE_RX |
3659 MR_NP_RX);
3660 if (ap->rxconfig & ANEG_CFG_FD)
3661 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3662 if (ap->rxconfig & ANEG_CFG_HD)
3663 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3664 if (ap->rxconfig & ANEG_CFG_PS1)
3665 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3666 if (ap->rxconfig & ANEG_CFG_PS2)
3667 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3668 if (ap->rxconfig & ANEG_CFG_RF1)
3669 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3670 if (ap->rxconfig & ANEG_CFG_RF2)
3671 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3672 if (ap->rxconfig & ANEG_CFG_NP)
3673 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3674
3675 ap->link_time = ap->cur_time;
3676
3677 ap->flags ^= (MR_TOGGLE_TX);
3678 if (ap->rxconfig & 0x0008)
3679 ap->flags |= MR_TOGGLE_RX;
3680 if (ap->rxconfig & ANEG_CFG_NP)
3681 ap->flags |= MR_NP_RX;
3682 ap->flags |= MR_PAGE_RX;
3683
3684 ap->state = ANEG_STATE_COMPLETE_ACK;
3685 ret = ANEG_TIMER_ENAB;
3686 break;
3687
3688 case ANEG_STATE_COMPLETE_ACK:
3689 if (ap->ability_match != 0 &&
3690 ap->rxconfig == 0) {
3691 ap->state = ANEG_STATE_AN_ENABLE;
3692 break;
3693 }
3694 delta = ap->cur_time - ap->link_time;
3695 if (delta > ANEG_STATE_SETTLE_TIME) {
3696 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3697 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3698 } else {
3699 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3700 !(ap->flags & MR_NP_RX)) {
3701 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3702 } else {
3703 ret = ANEG_FAILED;
3704 }
3705 }
3706 }
3707 break;
3708
3709 case ANEG_STATE_IDLE_DETECT_INIT:
3710 ap->link_time = ap->cur_time;
3711 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3712 tw32_f(MAC_MODE, tp->mac_mode);
3713 udelay(40);
3714
3715 ap->state = ANEG_STATE_IDLE_DETECT;
3716 ret = ANEG_TIMER_ENAB;
3717 break;
3718
3719 case ANEG_STATE_IDLE_DETECT:
3720 if (ap->ability_match != 0 &&
3721 ap->rxconfig == 0) {
3722 ap->state = ANEG_STATE_AN_ENABLE;
3723 break;
3724 }
3725 delta = ap->cur_time - ap->link_time;
3726 if (delta > ANEG_STATE_SETTLE_TIME) {
3727 /* XXX another gem from the Broadcom driver :( */
3728 ap->state = ANEG_STATE_LINK_OK;
3729 }
3730 break;
3731
3732 case ANEG_STATE_LINK_OK:
3733 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3734 ret = ANEG_DONE;
3735 break;
3736
3737 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3738 /* ??? unimplemented */
3739 break;
3740
3741 case ANEG_STATE_NEXT_PAGE_WAIT:
3742 /* ??? unimplemented */
3743 break;
3744
3745 default:
3746 ret = ANEG_FAILED;
3747 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749
3750 return ret;
3751}
3752
Matt Carlson5be73b42007-12-20 20:09:29 -08003753static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754{
3755 int res = 0;
3756 struct tg3_fiber_aneginfo aninfo;
3757 int status = ANEG_FAILED;
3758 unsigned int tick;
3759 u32 tmp;
3760
3761 tw32_f(MAC_TX_AUTO_NEG, 0);
3762
3763 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3764 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3765 udelay(40);
3766
3767 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3768 udelay(40);
3769
3770 memset(&aninfo, 0, sizeof(aninfo));
3771 aninfo.flags |= MR_AN_ENABLE;
3772 aninfo.state = ANEG_STATE_UNKNOWN;
3773 aninfo.cur_time = 0;
3774 tick = 0;
3775 while (++tick < 195000) {
3776 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3777 if (status == ANEG_DONE || status == ANEG_FAILED)
3778 break;
3779
3780 udelay(1);
3781 }
3782
3783 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3784 tw32_f(MAC_MODE, tp->mac_mode);
3785 udelay(40);
3786
Matt Carlson5be73b42007-12-20 20:09:29 -08003787 *txflags = aninfo.txconfig;
3788 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789
3790 if (status == ANEG_DONE &&
3791 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3792 MR_LP_ADV_FULL_DUPLEX)))
3793 res = 1;
3794
3795 return res;
3796}
3797
3798static void tg3_init_bcm8002(struct tg3 *tp)
3799{
3800 u32 mac_status = tr32(MAC_STATUS);
3801 int i;
3802
3803 /* Reset when initting first time or we have a link. */
3804 if ((tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) &&
3805 !(mac_status & MAC_STATUS_PCS_SYNCED))
3806 return;
3807
3808 /* Set PLL lock range. */
3809 tg3_writephy(tp, 0x16, 0x8007);
3810
3811 /* SW reset */
3812 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
3813
3814 /* Wait for reset to complete. */
3815 /* XXX schedule_timeout() ... */
3816 for (i = 0; i < 500; i++)
3817 udelay(10);
3818
3819 /* Config mode; select PMA/Ch 1 regs. */
3820 tg3_writephy(tp, 0x10, 0x8411);
3821
3822 /* Enable auto-lock and comdet, select txclk for tx. */
3823 tg3_writephy(tp, 0x11, 0x0a10);
3824
3825 tg3_writephy(tp, 0x18, 0x00a0);
3826 tg3_writephy(tp, 0x16, 0x41ff);
3827
3828 /* Assert and deassert POR. */
3829 tg3_writephy(tp, 0x13, 0x0400);
3830 udelay(40);
3831 tg3_writephy(tp, 0x13, 0x0000);
3832
3833 tg3_writephy(tp, 0x11, 0x0a50);
3834 udelay(40);
3835 tg3_writephy(tp, 0x11, 0x0a10);
3836
3837 /* Wait for signal to stabilize */
3838 /* XXX schedule_timeout() ... */
3839 for (i = 0; i < 15000; i++)
3840 udelay(10);
3841
3842 /* Deselect the channel register so we can read the PHYID
3843 * later.
3844 */
3845 tg3_writephy(tp, 0x10, 0x8011);
3846}
3847
3848static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
3849{
Matt Carlson82cd3d12007-12-20 20:09:00 -08003850 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 u32 sg_dig_ctrl, sg_dig_status;
3852 u32 serdes_cfg, expected_sg_dig_ctrl;
3853 int workaround, port_a;
3854 int current_link_up;
3855
3856 serdes_cfg = 0;
3857 expected_sg_dig_ctrl = 0;
3858 workaround = 0;
3859 port_a = 1;
3860 current_link_up = 0;
3861
3862 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
3863 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
3864 workaround = 1;
3865 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
3866 port_a = 0;
3867
3868 /* preserve bits 0-11,13,14 for signal pre-emphasis */
3869 /* preserve bits 20-23 for voltage regulator */
3870 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
3871 }
3872
3873 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3874
3875 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003876 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 if (workaround) {
3878 u32 val = serdes_cfg;
3879
3880 if (port_a)
3881 val |= 0xc010000;
3882 else
3883 val |= 0x4010000;
3884 tw32_f(MAC_SERDES_CFG, val);
3885 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003886
3887 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 }
3889 if (mac_status & MAC_STATUS_PCS_SYNCED) {
3890 tg3_setup_flow_control(tp, 0, 0);
3891 current_link_up = 1;
3892 }
3893 goto out;
3894 }
3895
3896 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003897 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898
Matt Carlson82cd3d12007-12-20 20:09:00 -08003899 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3900 if (flowctrl & ADVERTISE_1000XPAUSE)
3901 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
3902 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3903 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904
3905 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003906 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07003907 tp->serdes_counter &&
3908 ((mac_status & (MAC_STATUS_PCS_SYNCED |
3909 MAC_STATUS_RCVD_CFG)) ==
3910 MAC_STATUS_PCS_SYNCED)) {
3911 tp->serdes_counter--;
3912 current_link_up = 1;
3913 goto out;
3914 }
3915restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 if (workaround)
3917 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003918 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 udelay(5);
3920 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
3921
Michael Chan3d3ebe72006-09-27 15:59:15 -07003922 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003923 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
3925 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003926 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 mac_status = tr32(MAC_STATUS);
3928
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003929 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08003931 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932
Matt Carlson82cd3d12007-12-20 20:09:00 -08003933 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
3934 local_adv |= ADVERTISE_1000XPAUSE;
3935 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
3936 local_adv |= ADVERTISE_1000XPSE_ASYM;
3937
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003938 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003939 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003940 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003941 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942
3943 tg3_setup_flow_control(tp, local_adv, remote_adv);
3944 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003945 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003946 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003947 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003948 if (tp->serdes_counter)
3949 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 else {
3951 if (workaround) {
3952 u32 val = serdes_cfg;
3953
3954 if (port_a)
3955 val |= 0xc010000;
3956 else
3957 val |= 0x4010000;
3958
3959 tw32_f(MAC_SERDES_CFG, val);
3960 }
3961
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003962 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 udelay(40);
3964
3965 /* Link parallel detection - link is up */
3966 /* only if we have PCS_SYNC and not */
3967 /* receiving config code words */
3968 mac_status = tr32(MAC_STATUS);
3969 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
3970 !(mac_status & MAC_STATUS_RCVD_CFG)) {
3971 tg3_setup_flow_control(tp, 0, 0);
3972 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003973 tp->phy_flags |=
3974 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003975 tp->serdes_counter =
3976 SERDES_PARALLEL_DET_TIMEOUT;
3977 } else
3978 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 }
3980 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07003981 } else {
3982 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003983 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 }
3985
3986out:
3987 return current_link_up;
3988}
3989
3990static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
3991{
3992 int current_link_up = 0;
3993
Michael Chan5cf64b82007-05-05 12:11:21 -07003994 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996
3997 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08003998 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004000
Matt Carlson5be73b42007-12-20 20:09:29 -08004001 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4002 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003
Matt Carlson5be73b42007-12-20 20:09:29 -08004004 if (txflags & ANEG_CFG_PS1)
4005 local_adv |= ADVERTISE_1000XPAUSE;
4006 if (txflags & ANEG_CFG_PS2)
4007 local_adv |= ADVERTISE_1000XPSE_ASYM;
4008
4009 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4010 remote_adv |= LPA_1000XPAUSE;
4011 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4012 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013
4014 tg3_setup_flow_control(tp, local_adv, remote_adv);
4015
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 current_link_up = 1;
4017 }
4018 for (i = 0; i < 30; i++) {
4019 udelay(20);
4020 tw32_f(MAC_STATUS,
4021 (MAC_STATUS_SYNC_CHANGED |
4022 MAC_STATUS_CFG_CHANGED));
4023 udelay(40);
4024 if ((tr32(MAC_STATUS) &
4025 (MAC_STATUS_SYNC_CHANGED |
4026 MAC_STATUS_CFG_CHANGED)) == 0)
4027 break;
4028 }
4029
4030 mac_status = tr32(MAC_STATUS);
4031 if (current_link_up == 0 &&
4032 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4033 !(mac_status & MAC_STATUS_RCVD_CFG))
4034 current_link_up = 1;
4035 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004036 tg3_setup_flow_control(tp, 0, 0);
4037
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 /* Forcing 1000FD link up. */
4039 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040
4041 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4042 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004043
4044 tw32_f(MAC_MODE, tp->mac_mode);
4045 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 }
4047
4048out:
4049 return current_link_up;
4050}
4051
4052static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4053{
4054 u32 orig_pause_cfg;
4055 u16 orig_active_speed;
4056 u8 orig_active_duplex;
4057 u32 mac_status;
4058 int current_link_up;
4059 int i;
4060
Matt Carlson8d018622007-12-20 20:05:44 -08004061 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 orig_active_speed = tp->link_config.active_speed;
4063 orig_active_duplex = tp->link_config.active_duplex;
4064
4065 if (!(tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG) &&
4066 netif_carrier_ok(tp->dev) &&
4067 (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)) {
4068 mac_status = tr32(MAC_STATUS);
4069 mac_status &= (MAC_STATUS_PCS_SYNCED |
4070 MAC_STATUS_SIGNAL_DET |
4071 MAC_STATUS_CFG_CHANGED |
4072 MAC_STATUS_RCVD_CFG);
4073 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4074 MAC_STATUS_SIGNAL_DET)) {
4075 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4076 MAC_STATUS_CFG_CHANGED));
4077 return 0;
4078 }
4079 }
4080
4081 tw32_f(MAC_TX_AUTO_NEG, 0);
4082
4083 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4084 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
4085 tw32_f(MAC_MODE, tp->mac_mode);
4086 udelay(40);
4087
Matt Carlson79eb6902010-02-17 15:17:03 +00004088 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 tg3_init_bcm8002(tp);
4090
4091 /* Enable link change event even when serdes polling. */
4092 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4093 udelay(40);
4094
4095 current_link_up = 0;
4096 mac_status = tr32(MAC_STATUS);
4097
4098 if (tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG)
4099 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4100 else
4101 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4102
Matt Carlson898a56f2009-08-28 14:02:40 +00004103 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00004105 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106
4107 for (i = 0; i < 100; i++) {
4108 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4109 MAC_STATUS_CFG_CHANGED));
4110 udelay(5);
4111 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07004112 MAC_STATUS_CFG_CHANGED |
4113 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 break;
4115 }
4116
4117 mac_status = tr32(MAC_STATUS);
4118 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4119 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004120 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4121 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 tw32_f(MAC_MODE, (tp->mac_mode |
4123 MAC_MODE_SEND_CONFIGS));
4124 udelay(1);
4125 tw32_f(MAC_MODE, tp->mac_mode);
4126 }
4127 }
4128
4129 if (current_link_up == 1) {
4130 tp->link_config.active_speed = SPEED_1000;
4131 tp->link_config.active_duplex = DUPLEX_FULL;
4132 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4133 LED_CTRL_LNKLED_OVERRIDE |
4134 LED_CTRL_1000MBPS_ON));
4135 } else {
4136 tp->link_config.active_speed = SPEED_INVALID;
4137 tp->link_config.active_duplex = DUPLEX_INVALID;
4138 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4139 LED_CTRL_LNKLED_OVERRIDE |
4140 LED_CTRL_TRAFFIC_OVERRIDE));
4141 }
4142
4143 if (current_link_up != netif_carrier_ok(tp->dev)) {
4144 if (current_link_up)
4145 netif_carrier_on(tp->dev);
4146 else
4147 netif_carrier_off(tp->dev);
4148 tg3_link_report(tp);
4149 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004150 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 if (orig_pause_cfg != now_pause_cfg ||
4152 orig_active_speed != tp->link_config.active_speed ||
4153 orig_active_duplex != tp->link_config.active_duplex)
4154 tg3_link_report(tp);
4155 }
4156
4157 return 0;
4158}
4159
Michael Chan747e8f82005-07-25 12:33:22 -07004160static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4161{
4162 int current_link_up, err = 0;
4163 u32 bmsr, bmcr;
4164 u16 current_speed;
4165 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004166 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004167
4168 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4169 tw32_f(MAC_MODE, tp->mac_mode);
4170 udelay(40);
4171
4172 tw32(MAC_EVENT, 0);
4173
4174 tw32_f(MAC_STATUS,
4175 (MAC_STATUS_SYNC_CHANGED |
4176 MAC_STATUS_CFG_CHANGED |
4177 MAC_STATUS_MI_COMPLETION |
4178 MAC_STATUS_LNKSTATE_CHANGED));
4179 udelay(40);
4180
4181 if (force_reset)
4182 tg3_phy_reset(tp);
4183
4184 current_link_up = 0;
4185 current_speed = SPEED_INVALID;
4186 current_duplex = DUPLEX_INVALID;
4187
4188 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4189 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004190 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4191 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4192 bmsr |= BMSR_LSTATUS;
4193 else
4194 bmsr &= ~BMSR_LSTATUS;
4195 }
Michael Chan747e8f82005-07-25 12:33:22 -07004196
4197 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4198
4199 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004200 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004201 /* do nothing, just check for link up at the end */
4202 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4203 u32 adv, new_adv;
4204
4205 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4206 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4207 ADVERTISE_1000XPAUSE |
4208 ADVERTISE_1000XPSE_ASYM |
4209 ADVERTISE_SLCT);
4210
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004211 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004212
4213 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4214 new_adv |= ADVERTISE_1000XHALF;
4215 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4216 new_adv |= ADVERTISE_1000XFULL;
4217
4218 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4219 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4220 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4221 tg3_writephy(tp, MII_BMCR, bmcr);
4222
4223 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004224 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004225 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004226
4227 return err;
4228 }
4229 } else {
4230 u32 new_bmcr;
4231
4232 bmcr &= ~BMCR_SPEED1000;
4233 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4234
4235 if (tp->link_config.duplex == DUPLEX_FULL)
4236 new_bmcr |= BMCR_FULLDPLX;
4237
4238 if (new_bmcr != bmcr) {
4239 /* BMCR_SPEED1000 is a reserved bit that needs
4240 * to be set on write.
4241 */
4242 new_bmcr |= BMCR_SPEED1000;
4243
4244 /* Force a linkdown */
4245 if (netif_carrier_ok(tp->dev)) {
4246 u32 adv;
4247
4248 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4249 adv &= ~(ADVERTISE_1000XFULL |
4250 ADVERTISE_1000XHALF |
4251 ADVERTISE_SLCT);
4252 tg3_writephy(tp, MII_ADVERTISE, adv);
4253 tg3_writephy(tp, MII_BMCR, bmcr |
4254 BMCR_ANRESTART |
4255 BMCR_ANENABLE);
4256 udelay(10);
4257 netif_carrier_off(tp->dev);
4258 }
4259 tg3_writephy(tp, MII_BMCR, new_bmcr);
4260 bmcr = new_bmcr;
4261 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4262 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004263 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4264 ASIC_REV_5714) {
4265 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4266 bmsr |= BMSR_LSTATUS;
4267 else
4268 bmsr &= ~BMSR_LSTATUS;
4269 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004270 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004271 }
4272 }
4273
4274 if (bmsr & BMSR_LSTATUS) {
4275 current_speed = SPEED_1000;
4276 current_link_up = 1;
4277 if (bmcr & BMCR_FULLDPLX)
4278 current_duplex = DUPLEX_FULL;
4279 else
4280 current_duplex = DUPLEX_HALF;
4281
Matt Carlsonef167e22007-12-20 20:10:01 -08004282 local_adv = 0;
4283 remote_adv = 0;
4284
Michael Chan747e8f82005-07-25 12:33:22 -07004285 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004286 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004287
4288 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4289 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4290 common = local_adv & remote_adv;
4291 if (common & (ADVERTISE_1000XHALF |
4292 ADVERTISE_1000XFULL)) {
4293 if (common & ADVERTISE_1000XFULL)
4294 current_duplex = DUPLEX_FULL;
4295 else
4296 current_duplex = DUPLEX_HALF;
Matt Carlson57d8b882010-06-05 17:24:35 +00004297 } else if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
4298 /* Link is up via parallel detect */
Matt Carlson859a5882010-04-05 10:19:28 +00004299 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07004300 current_link_up = 0;
Matt Carlson859a5882010-04-05 10:19:28 +00004301 }
Michael Chan747e8f82005-07-25 12:33:22 -07004302 }
4303 }
4304
Matt Carlsonef167e22007-12-20 20:10:01 -08004305 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4306 tg3_setup_flow_control(tp, local_adv, remote_adv);
4307
Michael Chan747e8f82005-07-25 12:33:22 -07004308 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4309 if (tp->link_config.active_duplex == DUPLEX_HALF)
4310 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4311
4312 tw32_f(MAC_MODE, tp->mac_mode);
4313 udelay(40);
4314
4315 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4316
4317 tp->link_config.active_speed = current_speed;
4318 tp->link_config.active_duplex = current_duplex;
4319
4320 if (current_link_up != netif_carrier_ok(tp->dev)) {
4321 if (current_link_up)
4322 netif_carrier_on(tp->dev);
4323 else {
4324 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004325 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004326 }
4327 tg3_link_report(tp);
4328 }
4329 return err;
4330}
4331
4332static void tg3_serdes_parallel_detect(struct tg3 *tp)
4333{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004334 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004335 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004336 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004337 return;
4338 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004339
Michael Chan747e8f82005-07-25 12:33:22 -07004340 if (!netif_carrier_ok(tp->dev) &&
4341 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4342 u32 bmcr;
4343
4344 tg3_readphy(tp, MII_BMCR, &bmcr);
4345 if (bmcr & BMCR_ANENABLE) {
4346 u32 phy1, phy2;
4347
4348 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004349 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
4350 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07004351
4352 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004353 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4354 MII_TG3_DSP_EXP1_INT_STAT);
4355 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
4356 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004357
4358 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4359 /* We have signal detect and not receiving
4360 * config code words, link is up by parallel
4361 * detection.
4362 */
4363
4364 bmcr &= ~BMCR_ANENABLE;
4365 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4366 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004367 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004368 }
4369 }
Matt Carlson859a5882010-04-05 10:19:28 +00004370 } else if (netif_carrier_ok(tp->dev) &&
4371 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004372 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004373 u32 phy2;
4374
4375 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004376 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4377 MII_TG3_DSP_EXP1_INT_STAT);
4378 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004379 if (phy2 & 0x20) {
4380 u32 bmcr;
4381
4382 /* Config code words received, turn on autoneg. */
4383 tg3_readphy(tp, MII_BMCR, &bmcr);
4384 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4385
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004386 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004387
4388 }
4389 }
4390}
4391
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4393{
Matt Carlsonf2096f92011-04-05 14:22:48 +00004394 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 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 Carlsonf2096f92011-04-05 14:22:48 +00004405 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08004406
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
Matt Carlsonf2096f92011-04-05 14:22:48 +00004420 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4421 (6 << TX_LENGTHS_IPG_SHIFT);
4422 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
4423 val |= tr32(MAC_TX_LENGTHS) &
4424 (TX_LENGTHS_JMB_FRM_LEN_MSK |
4425 TX_LENGTHS_CNT_DWN_VAL_MSK);
4426
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 if (tp->link_config.active_speed == SPEED_1000 &&
4428 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00004429 tw32(MAC_TX_LENGTHS, val |
4430 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00004432 tw32(MAC_TX_LENGTHS, val |
4433 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434
4435 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
4436 if (netif_carrier_ok(tp->dev)) {
4437 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07004438 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 } else {
4440 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4441 }
4442 }
4443
Matt Carlson8ed5d972007-05-07 00:25:49 -07004444 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00004445 val = tr32(PCIE_PWR_MGMT_THRESH);
Matt Carlson8ed5d972007-05-07 00:25:49 -07004446 if (!netif_carrier_ok(tp->dev))
4447 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4448 tp->pwrmgmt_thresh;
4449 else
4450 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4451 tw32(PCIE_PWR_MGMT_THRESH, val);
4452 }
4453
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 return err;
4455}
4456
Matt Carlson66cfd1b2010-09-30 10:34:30 +00004457static inline int tg3_irq_sync(struct tg3 *tp)
4458{
4459 return tp->irq_sync;
4460}
4461
Matt Carlson97bd8e42011-04-13 11:05:04 +00004462static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
4463{
4464 int i;
4465
4466 dst = (u32 *)((u8 *)dst + off);
4467 for (i = 0; i < len; i += sizeof(u32))
4468 *dst++ = tr32(off + i);
4469}
4470
4471static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
4472{
4473 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
4474 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
4475 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
4476 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
4477 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
4478 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
4479 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
4480 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
4481 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
4482 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
4483 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
4484 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
4485 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
4486 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
4487 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
4488 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
4489 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
4490 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
4491 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
4492
4493 if (tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX)
4494 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
4495
4496 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
4497 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
4498 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
4499 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
4500 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
4501 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
4502 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
4503 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
4504
4505 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
4506 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
4507 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
4508 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
4509 }
4510
4511 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
4512 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
4513 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
4514 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
4515 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
4516
4517 if (tp->tg3_flags & TG3_FLAG_NVRAM)
4518 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
4519}
4520
4521static void tg3_dump_state(struct tg3 *tp)
4522{
4523 int i;
4524 u32 *regs;
4525
4526 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
4527 if (!regs) {
4528 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
4529 return;
4530 }
4531
4532 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
4533 /* Read up to but not including private PCI registers */
4534 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
4535 regs[i / sizeof(u32)] = tr32(i);
4536 } else
4537 tg3_dump_legacy_regs(tp, regs);
4538
4539 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
4540 if (!regs[i + 0] && !regs[i + 1] &&
4541 !regs[i + 2] && !regs[i + 3])
4542 continue;
4543
4544 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
4545 i * 4,
4546 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
4547 }
4548
4549 kfree(regs);
4550
4551 for (i = 0; i < tp->irq_cnt; i++) {
4552 struct tg3_napi *tnapi = &tp->napi[i];
4553
4554 /* SW status block */
4555 netdev_err(tp->dev,
4556 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
4557 i,
4558 tnapi->hw_status->status,
4559 tnapi->hw_status->status_tag,
4560 tnapi->hw_status->rx_jumbo_consumer,
4561 tnapi->hw_status->rx_consumer,
4562 tnapi->hw_status->rx_mini_consumer,
4563 tnapi->hw_status->idx[0].rx_producer,
4564 tnapi->hw_status->idx[0].tx_consumer);
4565
4566 netdev_err(tp->dev,
4567 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
4568 i,
4569 tnapi->last_tag, tnapi->last_irq_tag,
4570 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
4571 tnapi->rx_rcb_ptr,
4572 tnapi->prodring.rx_std_prod_idx,
4573 tnapi->prodring.rx_std_cons_idx,
4574 tnapi->prodring.rx_jmb_prod_idx,
4575 tnapi->prodring.rx_jmb_cons_idx);
4576 }
4577}
4578
Michael Chandf3e6542006-05-26 17:48:07 -07004579/* This is called whenever we suspect that the system chipset is re-
4580 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4581 * is bogus tx completions. We try to recover by setting the
4582 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4583 * in the workqueue.
4584 */
4585static void tg3_tx_recover(struct tg3 *tp)
4586{
4587 BUG_ON((tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) ||
4588 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4589
Matt Carlson5129c3a2010-04-05 10:19:23 +00004590 netdev_warn(tp->dev,
4591 "The system may be re-ordering memory-mapped I/O "
4592 "cycles to the network device, attempting to recover. "
4593 "Please report the problem to the driver maintainer "
4594 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07004595
4596 spin_lock(&tp->lock);
Michael Chandf3e6542006-05-26 17:48:07 -07004597 tp->tg3_flags |= TG3_FLAG_TX_RECOVERY_PENDING;
Michael Chandf3e6542006-05-26 17:48:07 -07004598 spin_unlock(&tp->lock);
4599}
4600
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004601static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07004602{
Matt Carlsonf65aac12010-08-02 11:26:03 +00004603 /* Tell compiler to fetch tx indices from memory. */
4604 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004605 return tnapi->tx_pending -
4606 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07004607}
4608
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609/* Tigon3 never reports partial packet sends. So we do not
4610 * need special logic to handle SKBs that have not had all
4611 * of their frags sent yet, like SunGEM does.
4612 */
Matt Carlson17375d22009-08-28 14:02:18 +00004613static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614{
Matt Carlson17375d22009-08-28 14:02:18 +00004615 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004616 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004617 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004618 struct netdev_queue *txq;
4619 int index = tnapi - tp->napi;
4620
Matt Carlson19cfaec2009-12-03 08:36:20 +00004621 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004622 index--;
4623
4624 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625
4626 while (sw_idx != hw_idx) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00004627 struct ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004629 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630
Michael Chandf3e6542006-05-26 17:48:07 -07004631 if (unlikely(skb == NULL)) {
4632 tg3_tx_recover(tp);
4633 return;
4634 }
4635
Alexander Duyckf4188d82009-12-02 16:48:38 +00004636 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004637 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004638 skb_headlen(skb),
4639 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640
4641 ri->skb = NULL;
4642
4643 sw_idx = NEXT_TX(sw_idx);
4644
4645 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004646 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004647 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4648 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00004649
4650 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004651 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004652 skb_shinfo(skb)->frags[i].size,
4653 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 sw_idx = NEXT_TX(sw_idx);
4655 }
4656
David S. Millerf47c11e2005-06-24 20:18:35 -07004657 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004658
4659 if (unlikely(tx_bug)) {
4660 tg3_tx_recover(tp);
4661 return;
4662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 }
4664
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004665 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666
Michael Chan1b2a7202006-08-07 21:46:02 -07004667 /* Need to make the tx_cons update visible to tg3_start_xmit()
4668 * before checking for netif_queue_stopped(). Without the
4669 * memory barrier, there is a small possibility that tg3_start_xmit()
4670 * will miss it and cause the queue to be stopped forever.
4671 */
4672 smp_mb();
4673
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004674 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004675 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004676 __netif_tx_lock(txq, smp_processor_id());
4677 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004678 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004679 netif_tx_wake_queue(txq);
4680 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07004681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682}
4683
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004684static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
4685{
4686 if (!ri->skb)
4687 return;
4688
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004689 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004690 map_sz, PCI_DMA_FROMDEVICE);
4691 dev_kfree_skb_any(ri->skb);
4692 ri->skb = NULL;
4693}
4694
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695/* Returns size of skb allocated or < 0 on error.
4696 *
4697 * We only need to fill in the address because the other members
4698 * of the RX descriptor are invariant, see tg3_init_rings.
4699 *
4700 * Note the purposeful assymetry of cpu vs. chip accesses. For
4701 * posting buffers we only dirty the first cache line of the RX
4702 * descriptor (containing the address). Whereas for the RX status
4703 * buffers the cpu only reads the last cacheline of the RX descriptor
4704 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4705 */
Matt Carlson86b21e52009-11-13 13:03:45 +00004706static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00004707 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708{
4709 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00004710 struct ring_info *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 struct sk_buff *skb;
4712 dma_addr_t mapping;
4713 int skb_size, dest_idx;
4714
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 switch (opaque_key) {
4716 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004717 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00004718 desc = &tpr->rx_std[dest_idx];
4719 map = &tpr->rx_std_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004720 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 break;
4722
4723 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004724 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004725 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004726 map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004727 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 break;
4729
4730 default:
4731 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733
4734 /* Do not overwrite any of the map or rp information
4735 * until we are sure we can commit to a new buffer.
4736 *
4737 * Callers depend upon this behavior and assume that
4738 * we leave everything unchanged if we fail.
4739 */
Matt Carlson287be122009-08-28 13:58:46 +00004740 skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 if (skb == NULL)
4742 return -ENOMEM;
4743
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 skb_reserve(skb, tp->rx_offset);
4745
Matt Carlson287be122009-08-28 13:58:46 +00004746 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00004748 if (pci_dma_mapping_error(tp->pdev, mapping)) {
4749 dev_kfree_skb(skb);
4750 return -EIO;
4751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752
4753 map->skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004754 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 desc->addr_hi = ((u64)mapping >> 32);
4757 desc->addr_lo = ((u64)mapping & 0xffffffff);
4758
4759 return skb_size;
4760}
4761
4762/* We only need to move over in the address because the other
4763 * members of the RX descriptor are invariant. See notes above
4764 * tg3_alloc_rx_skb for full details.
4765 */
Matt Carlsona3896162009-11-13 13:03:44 +00004766static void tg3_recycle_rx(struct tg3_napi *tnapi,
4767 struct tg3_rx_prodring_set *dpr,
4768 u32 opaque_key, int src_idx,
4769 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770{
Matt Carlson17375d22009-08-28 14:02:18 +00004771 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4773 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004774 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004775 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776
4777 switch (opaque_key) {
4778 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004779 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004780 dest_desc = &dpr->rx_std[dest_idx];
4781 dest_map = &dpr->rx_std_buffers[dest_idx];
4782 src_desc = &spr->rx_std[src_idx];
4783 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 break;
4785
4786 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004787 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004788 dest_desc = &dpr->rx_jmb[dest_idx].std;
4789 dest_map = &dpr->rx_jmb_buffers[dest_idx];
4790 src_desc = &spr->rx_jmb[src_idx].std;
4791 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 break;
4793
4794 default:
4795 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797
4798 dest_map->skb = src_map->skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004799 dma_unmap_addr_set(dest_map, mapping,
4800 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 dest_desc->addr_hi = src_desc->addr_hi;
4802 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00004803
4804 /* Ensure that the update to the skb happens after the physical
4805 * addresses have been transferred to the new BD location.
4806 */
4807 smp_wmb();
4808
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 src_map->skb = NULL;
4810}
4811
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812/* The RX ring scheme is composed of multiple rings which post fresh
4813 * buffers to the chip, and one special ring the chip uses to report
4814 * status back to the host.
4815 *
4816 * The special ring reports the status of received packets to the
4817 * host. The chip does not write into the original descriptor the
4818 * RX buffer was obtained from. The chip simply takes the original
4819 * descriptor as provided by the host, updates the status and length
4820 * field, then writes this into the next status ring entry.
4821 *
4822 * Each ring the host uses to post buffers to the chip is described
4823 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
4824 * it is first placed into the on-chip ram. When the packet's length
4825 * is known, it walks down the TG3_BDINFO entries to select the ring.
4826 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
4827 * which is within the range of the new packet's length is chosen.
4828 *
4829 * The "separate ring for rx status" scheme may sound queer, but it makes
4830 * sense from a cache coherency perspective. If only the host writes
4831 * to the buffer post rings, and only the chip writes to the rx status
4832 * rings, then cache lines never move beyond shared-modified state.
4833 * If both the host and chip were to write into the same ring, cache line
4834 * eviction could occur since both entities want it in an exclusive state.
4835 */
Matt Carlson17375d22009-08-28 14:02:18 +00004836static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837{
Matt Carlson17375d22009-08-28 14:02:18 +00004838 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07004839 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004840 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00004841 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07004842 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004844 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004846 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 /*
4848 * We need to order the read of hw_idx and the read of
4849 * the opaque cookie.
4850 */
4851 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 work_mask = 0;
4853 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004854 std_prod_idx = tpr->rx_std_prod_idx;
4855 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00004857 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00004858 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 unsigned int len;
4860 struct sk_buff *skb;
4861 dma_addr_t dma_addr;
4862 u32 opaque_key, desc_idx, *post_ptr;
4863
4864 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
4865 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
4866 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004867 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004868 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004869 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004870 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07004871 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004873 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004874 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004875 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004876 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00004877 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879
4880 work_mask |= opaque_key;
4881
4882 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
4883 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
4884 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00004885 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 desc_idx, *post_ptr);
4887 drop_it_no_recycle:
4888 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00004889 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 goto next_pkt;
4891 }
4892
Matt Carlsonad829262008-11-21 17:16:16 -08004893 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
4894 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895
Matt Carlsond2757fc2010-04-12 06:58:27 +00004896 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 int skb_size;
4898
Matt Carlson86b21e52009-11-13 13:03:45 +00004899 skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00004900 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 if (skb_size < 0)
4902 goto drop_it;
4903
Matt Carlson287be122009-08-28 13:58:46 +00004904 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 PCI_DMA_FROMDEVICE);
4906
Matt Carlson61e800c2010-02-17 15:16:54 +00004907 /* Ensure that the update to the skb happens
4908 * after the usage of the old DMA mapping.
4909 */
4910 smp_wmb();
4911
4912 ri->skb = NULL;
4913
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 skb_put(skb, len);
4915 } else {
4916 struct sk_buff *copy_skb;
4917
Matt Carlsona3896162009-11-13 13:03:44 +00004918 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 desc_idx, *post_ptr);
4920
Matt Carlsonbf933c82011-01-25 15:58:49 +00004921 copy_skb = netdev_alloc_skb(tp->dev, len +
Matt Carlson9dc7a112010-04-12 06:58:28 +00004922 TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 if (copy_skb == NULL)
4924 goto drop_it_no_recycle;
4925
Matt Carlsonbf933c82011-01-25 15:58:49 +00004926 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 skb_put(copy_skb, len);
4928 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03004929 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
4931
4932 /* We'll reuse the original ring buffer. */
4933 skb = copy_skb;
4934 }
4935
Michał Mirosławdc668912011-04-07 03:35:07 +00004936 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
4938 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
4939 >> RXD_TCPCSUM_SHIFT) == 0xffff))
4940 skb->ip_summed = CHECKSUM_UNNECESSARY;
4941 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07004942 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943
4944 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004945
4946 if (len > (tp->dev->mtu + ETH_HLEN) &&
4947 skb->protocol != htons(ETH_P_8021Q)) {
4948 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00004949 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004950 }
4951
Matt Carlson9dc7a112010-04-12 06:58:28 +00004952 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00004953 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
4954 __vlan_hwaccel_put_tag(skb,
4955 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00004956
Matt Carlsonbf933c82011-01-25 15:58:49 +00004957 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 received++;
4960 budget--;
4961
4962next_pkt:
4963 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07004964
4965 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00004966 tpr->rx_std_prod_idx = std_prod_idx &
4967 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00004968 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4969 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07004970 work_mask &= ~RXD_OPAQUE_RING_STD;
4971 rx_std_posted = 0;
4972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07004974 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00004975 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07004976
4977 /* Refresh hw_idx to see if there is new work */
4978 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004979 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07004980 rmb();
4981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 }
4983
4984 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00004985 tnapi->rx_rcb_ptr = sw_idx;
4986 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987
4988 /* Refill RX ring(s). */
Matt Carlsone4af1af2010-02-12 14:47:05 +00004989 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004990 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00004991 tpr->rx_std_prod_idx = std_prod_idx &
4992 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004993 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4994 tpr->rx_std_prod_idx);
4995 }
4996 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00004997 tpr->rx_jmb_prod_idx = jmb_prod_idx &
4998 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004999 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5000 tpr->rx_jmb_prod_idx);
5001 }
5002 mmiowb();
5003 } else if (work_mask) {
5004 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
5005 * updated before the producer indices can be updated.
5006 */
5007 smp_wmb();
5008
Matt Carlson2c49a442010-09-30 10:34:35 +00005009 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
5010 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005011
Matt Carlsone4af1af2010-02-12 14:47:05 +00005012 if (tnapi != &tp->napi[1])
5013 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015
5016 return received;
5017}
5018
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005019static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 /* handle link change and other phy events */
5022 if (!(tp->tg3_flags &
5023 (TG3_FLAG_USE_LINKCHG_REG |
5024 TG3_FLAG_POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005025 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
5026
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 if (sblk->status & SD_STATUS_LINK_CHG) {
5028 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005029 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07005030 spin_lock(&tp->lock);
Matt Carlsondd477002008-05-25 23:45:58 -07005031 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
5032 tw32_f(MAC_STATUS,
5033 (MAC_STATUS_SYNC_CHANGED |
5034 MAC_STATUS_CFG_CHANGED |
5035 MAC_STATUS_MI_COMPLETION |
5036 MAC_STATUS_LNKSTATE_CHANGED));
5037 udelay(40);
5038 } else
5039 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07005040 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 }
5042 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005043}
5044
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005045static int tg3_rx_prodring_xfer(struct tg3 *tp,
5046 struct tg3_rx_prodring_set *dpr,
5047 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005048{
5049 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005050 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005051
5052 while (1) {
5053 src_prod_idx = spr->rx_std_prod_idx;
5054
5055 /* Make sure updates to the rx_std_buffers[] entries and the
5056 * standard producer index are seen in the correct order.
5057 */
5058 smp_rmb();
5059
5060 if (spr->rx_std_cons_idx == src_prod_idx)
5061 break;
5062
5063 if (spr->rx_std_cons_idx < src_prod_idx)
5064 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
5065 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005066 cpycnt = tp->rx_std_ring_mask + 1 -
5067 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005068
Matt Carlson2c49a442010-09-30 10:34:35 +00005069 cpycnt = min(cpycnt,
5070 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005071
5072 si = spr->rx_std_cons_idx;
5073 di = dpr->rx_std_prod_idx;
5074
Matt Carlsone92967b2010-02-12 14:47:06 +00005075 for (i = di; i < di + cpycnt; i++) {
5076 if (dpr->rx_std_buffers[i].skb) {
5077 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005078 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005079 break;
5080 }
5081 }
5082
5083 if (!cpycnt)
5084 break;
5085
5086 /* Ensure that updates to the rx_std_buffers ring and the
5087 * shadowed hardware producer ring from tg3_recycle_skb() are
5088 * ordered correctly WRT the skb check above.
5089 */
5090 smp_rmb();
5091
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005092 memcpy(&dpr->rx_std_buffers[di],
5093 &spr->rx_std_buffers[si],
5094 cpycnt * sizeof(struct ring_info));
5095
5096 for (i = 0; i < cpycnt; i++, di++, si++) {
5097 struct tg3_rx_buffer_desc *sbd, *dbd;
5098 sbd = &spr->rx_std[si];
5099 dbd = &dpr->rx_std[di];
5100 dbd->addr_hi = sbd->addr_hi;
5101 dbd->addr_lo = sbd->addr_lo;
5102 }
5103
Matt Carlson2c49a442010-09-30 10:34:35 +00005104 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
5105 tp->rx_std_ring_mask;
5106 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
5107 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005108 }
5109
5110 while (1) {
5111 src_prod_idx = spr->rx_jmb_prod_idx;
5112
5113 /* Make sure updates to the rx_jmb_buffers[] entries and
5114 * the jumbo producer index are seen in the correct order.
5115 */
5116 smp_rmb();
5117
5118 if (spr->rx_jmb_cons_idx == src_prod_idx)
5119 break;
5120
5121 if (spr->rx_jmb_cons_idx < src_prod_idx)
5122 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
5123 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005124 cpycnt = tp->rx_jmb_ring_mask + 1 -
5125 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005126
5127 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00005128 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005129
5130 si = spr->rx_jmb_cons_idx;
5131 di = dpr->rx_jmb_prod_idx;
5132
Matt Carlsone92967b2010-02-12 14:47:06 +00005133 for (i = di; i < di + cpycnt; i++) {
5134 if (dpr->rx_jmb_buffers[i].skb) {
5135 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005136 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005137 break;
5138 }
5139 }
5140
5141 if (!cpycnt)
5142 break;
5143
5144 /* Ensure that updates to the rx_jmb_buffers ring and the
5145 * shadowed hardware producer ring from tg3_recycle_skb() are
5146 * ordered correctly WRT the skb check above.
5147 */
5148 smp_rmb();
5149
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005150 memcpy(&dpr->rx_jmb_buffers[di],
5151 &spr->rx_jmb_buffers[si],
5152 cpycnt * sizeof(struct ring_info));
5153
5154 for (i = 0; i < cpycnt; i++, di++, si++) {
5155 struct tg3_rx_buffer_desc *sbd, *dbd;
5156 sbd = &spr->rx_jmb[si].std;
5157 dbd = &dpr->rx_jmb[di].std;
5158 dbd->addr_hi = sbd->addr_hi;
5159 dbd->addr_lo = sbd->addr_lo;
5160 }
5161
Matt Carlson2c49a442010-09-30 10:34:35 +00005162 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
5163 tp->rx_jmb_ring_mask;
5164 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
5165 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005166 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005167
5168 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005169}
5170
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005171static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
5172{
5173 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174
5175 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005176 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00005177 tg3_tx(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005178 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
Michael Chan4fd7ab52007-10-12 01:39:50 -07005179 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 }
5181
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 /* run RX thread, within the bounds set by NAPI.
5183 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005184 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005186 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00005187 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005189 if ((tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005190 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005191 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00005192 u32 std_prod_idx = dpr->rx_std_prod_idx;
5193 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005194
Matt Carlsone4af1af2010-02-12 14:47:05 +00005195 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005196 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00005197 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005198
5199 wmb();
5200
Matt Carlsone4af1af2010-02-12 14:47:05 +00005201 if (std_prod_idx != dpr->rx_std_prod_idx)
5202 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5203 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005204
Matt Carlsone4af1af2010-02-12 14:47:05 +00005205 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5206 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5207 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005208
5209 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005210
5211 if (err)
5212 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005213 }
5214
David S. Miller6f535762007-10-11 18:08:29 -07005215 return work_done;
5216}
David S. Millerf7383c22005-05-18 22:50:53 -07005217
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005218static int tg3_poll_msix(struct napi_struct *napi, int budget)
5219{
5220 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5221 struct tg3 *tp = tnapi->tp;
5222 int work_done = 0;
5223 struct tg3_hw_status *sblk = tnapi->hw_status;
5224
5225 while (1) {
5226 work_done = tg3_poll_work(tnapi, work_done, budget);
5227
5228 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5229 goto tx_recovery;
5230
5231 if (unlikely(work_done >= budget))
5232 break;
5233
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005234 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005235 * to tell the hw how much work has been processed,
5236 * so we must read it before checking for more work.
5237 */
5238 tnapi->last_tag = sblk->status_tag;
5239 tnapi->last_irq_tag = tnapi->last_tag;
5240 rmb();
5241
5242 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005243 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5244 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005245 napi_complete(napi);
5246 /* Reenable interrupts. */
5247 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5248 mmiowb();
5249 break;
5250 }
5251 }
5252
5253 return work_done;
5254
5255tx_recovery:
5256 /* work_done is guaranteed to be less than budget. */
5257 napi_complete(napi);
5258 schedule_work(&tp->reset_task);
5259 return work_done;
5260}
5261
Matt Carlsone64de4e2011-04-13 11:05:05 +00005262static void tg3_process_error(struct tg3 *tp)
5263{
5264 u32 val;
5265 bool real_error = false;
5266
5267 if (tp->tg3_flags & TG3_FLAG_ERROR_PROCESSED)
5268 return;
5269
5270 /* Check Flow Attention register */
5271 val = tr32(HOSTCC_FLOW_ATTN);
5272 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
5273 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
5274 real_error = true;
5275 }
5276
5277 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
5278 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
5279 real_error = true;
5280 }
5281
5282 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
5283 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
5284 real_error = true;
5285 }
5286
5287 if (!real_error)
5288 return;
5289
5290 tg3_dump_state(tp);
5291
5292 tp->tg3_flags |= TG3_FLAG_ERROR_PROCESSED;
5293 schedule_work(&tp->reset_task);
5294}
5295
David S. Miller6f535762007-10-11 18:08:29 -07005296static int tg3_poll(struct napi_struct *napi, int budget)
5297{
Matt Carlson8ef04422009-08-28 14:01:37 +00005298 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5299 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07005300 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00005301 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07005302
5303 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00005304 if (sblk->status & SD_STATUS_ERROR)
5305 tg3_process_error(tp);
5306
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005307 tg3_poll_link(tp);
5308
Matt Carlson17375d22009-08-28 14:02:18 +00005309 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07005310
5311 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5312 goto tx_recovery;
5313
5314 if (unlikely(work_done >= budget))
5315 break;
5316
Michael Chan4fd7ab52007-10-12 01:39:50 -07005317 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
Matt Carlson17375d22009-08-28 14:02:18 +00005318 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07005319 * to tell the hw how much work has been processed,
5320 * so we must read it before checking for more work.
5321 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005322 tnapi->last_tag = sblk->status_tag;
5323 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07005324 rmb();
5325 } else
5326 sblk->status &= ~SD_STATUS_UPDATED;
5327
Matt Carlson17375d22009-08-28 14:02:18 +00005328 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005329 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00005330 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005331 break;
5332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 }
5334
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005335 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07005336
5337tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07005338 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08005339 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07005340 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07005341 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342}
5343
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005344static void tg3_napi_disable(struct tg3 *tp)
5345{
5346 int i;
5347
5348 for (i = tp->irq_cnt - 1; i >= 0; i--)
5349 napi_disable(&tp->napi[i].napi);
5350}
5351
5352static void tg3_napi_enable(struct tg3 *tp)
5353{
5354 int i;
5355
5356 for (i = 0; i < tp->irq_cnt; i++)
5357 napi_enable(&tp->napi[i].napi);
5358}
5359
5360static void tg3_napi_init(struct tg3 *tp)
5361{
5362 int i;
5363
5364 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
5365 for (i = 1; i < tp->irq_cnt; i++)
5366 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
5367}
5368
5369static void tg3_napi_fini(struct tg3 *tp)
5370{
5371 int i;
5372
5373 for (i = 0; i < tp->irq_cnt; i++)
5374 netif_napi_del(&tp->napi[i].napi);
5375}
5376
5377static inline void tg3_netif_stop(struct tg3 *tp)
5378{
5379 tp->dev->trans_start = jiffies; /* prevent tx timeout */
5380 tg3_napi_disable(tp);
5381 netif_tx_disable(tp->dev);
5382}
5383
5384static inline void tg3_netif_start(struct tg3 *tp)
5385{
5386 /* NOTE: unconditional netif_tx_wake_all_queues is only
5387 * appropriate so long as all callers are assured to
5388 * have free tx slots (such as after tg3_init_hw)
5389 */
5390 netif_tx_wake_all_queues(tp->dev);
5391
5392 tg3_napi_enable(tp);
5393 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
5394 tg3_enable_ints(tp);
5395}
5396
David S. Millerf47c11e2005-06-24 20:18:35 -07005397static void tg3_irq_quiesce(struct tg3 *tp)
5398{
Matt Carlson4f125f42009-09-01 12:55:02 +00005399 int i;
5400
David S. Millerf47c11e2005-06-24 20:18:35 -07005401 BUG_ON(tp->irq_sync);
5402
5403 tp->irq_sync = 1;
5404 smp_mb();
5405
Matt Carlson4f125f42009-09-01 12:55:02 +00005406 for (i = 0; i < tp->irq_cnt; i++)
5407 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07005408}
5409
David S. Millerf47c11e2005-06-24 20:18:35 -07005410/* Fully shutdown all tg3 driver activity elsewhere in the system.
5411 * If irq_sync is non-zero, then the IRQ handler must be synchronized
5412 * with as well. Most of the time, this is not necessary except when
5413 * shutting down the device.
5414 */
5415static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
5416{
Michael Chan46966542007-07-11 19:47:19 -07005417 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07005418 if (irq_sync)
5419 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005420}
5421
5422static inline void tg3_full_unlock(struct tg3 *tp)
5423{
David S. Millerf47c11e2005-06-24 20:18:35 -07005424 spin_unlock_bh(&tp->lock);
5425}
5426
Michael Chanfcfa0a32006-03-20 22:28:41 -08005427/* One-shot MSI handler - Chip automatically disables interrupt
5428 * after sending MSI so driver doesn't have to do it.
5429 */
David Howells7d12e782006-10-05 14:55:46 +01005430static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08005431{
Matt Carlson09943a12009-08-28 14:01:57 +00005432 struct tg3_napi *tnapi = dev_id;
5433 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08005434
Matt Carlson898a56f2009-08-28 14:02:40 +00005435 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005436 if (tnapi->rx_rcb)
5437 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005438
5439 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005440 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005441
5442 return IRQ_HANDLED;
5443}
5444
Michael Chan88b06bc2005-04-21 17:13:25 -07005445/* MSI ISR - No need to check for interrupt sharing and no need to
5446 * flush status block and interrupt mailbox. PCI ordering rules
5447 * guarantee that MSI will arrive after the status block.
5448 */
David Howells7d12e782006-10-05 14:55:46 +01005449static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc2005-04-21 17:13:25 -07005450{
Matt Carlson09943a12009-08-28 14:01:57 +00005451 struct tg3_napi *tnapi = dev_id;
5452 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc2005-04-21 17:13:25 -07005453
Matt Carlson898a56f2009-08-28 14:02:40 +00005454 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005455 if (tnapi->rx_rcb)
5456 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc2005-04-21 17:13:25 -07005457 /*
David S. Millerfac9b832005-05-18 22:46:34 -07005458 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc2005-04-21 17:13:25 -07005459 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07005460 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc2005-04-21 17:13:25 -07005461 * NIC to stop sending us irqs, engaging "in-intr-handler"
5462 * event coalescing.
5463 */
5464 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07005465 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005466 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07005467
Michael Chan88b06bc2005-04-21 17:13:25 -07005468 return IRQ_RETVAL(1);
5469}
5470
David Howells7d12e782006-10-05 14:55:46 +01005471static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472{
Matt Carlson09943a12009-08-28 14:01:57 +00005473 struct tg3_napi *tnapi = dev_id;
5474 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005475 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 unsigned int handled = 1;
5477
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 /* In INTx mode, it is possible for the interrupt to arrive at
5479 * the CPU before the status block posted prior to the interrupt.
5480 * Reading the PCI State register will confirm whether the
5481 * interrupt is ours and will flush the status block.
5482 */
Michael Chand18edcb2007-03-24 20:57:11 -07005483 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
5484 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5485 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5486 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005487 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07005488 }
Michael Chand18edcb2007-03-24 20:57:11 -07005489 }
5490
5491 /*
5492 * Writing any value to intr-mbox-0 clears PCI INTA# and
5493 * chip-internal interrupt pending events.
5494 * Writing non-zero to intr-mbox-0 additional tells the
5495 * NIC to stop sending us irqs, engaging "in-intr-handler"
5496 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005497 *
5498 * Flush the mailbox to de-assert the IRQ immediately to prevent
5499 * spurious interrupts. The flush impacts performance but
5500 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005501 */
Michael Chanc04cb342007-05-07 00:26:15 -07005502 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07005503 if (tg3_irq_sync(tp))
5504 goto out;
5505 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00005506 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00005507 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00005508 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07005509 } else {
5510 /* No work, shared interrupt perhaps? re-enable
5511 * interrupts, and flush that PCI write
5512 */
5513 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
5514 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07005515 }
David S. Millerf47c11e2005-06-24 20:18:35 -07005516out:
David S. Millerfac9b832005-05-18 22:46:34 -07005517 return IRQ_RETVAL(handled);
5518}
5519
David Howells7d12e782006-10-05 14:55:46 +01005520static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07005521{
Matt Carlson09943a12009-08-28 14:01:57 +00005522 struct tg3_napi *tnapi = dev_id;
5523 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005524 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07005525 unsigned int handled = 1;
5526
David S. Millerfac9b832005-05-18 22:46:34 -07005527 /* In INTx mode, it is possible for the interrupt to arrive at
5528 * the CPU before the status block posted prior to the interrupt.
5529 * Reading the PCI State register will confirm whether the
5530 * interrupt is ours and will flush the status block.
5531 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005532 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Michael Chand18edcb2007-03-24 20:57:11 -07005533 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5534 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5535 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005536 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 }
Michael Chand18edcb2007-03-24 20:57:11 -07005538 }
5539
5540 /*
5541 * writing any value to intr-mbox-0 clears PCI INTA# and
5542 * chip-internal interrupt pending events.
5543 * writing non-zero to intr-mbox-0 additional tells the
5544 * NIC to stop sending us irqs, engaging "in-intr-handler"
5545 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005546 *
5547 * Flush the mailbox to de-assert the IRQ immediately to prevent
5548 * spurious interrupts. The flush impacts performance but
5549 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005550 */
Michael Chanc04cb342007-05-07 00:26:15 -07005551 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00005552
5553 /*
5554 * In a shared interrupt configuration, sometimes other devices'
5555 * interrupts will scream. We record the current status tag here
5556 * so that the above check can report that the screaming interrupts
5557 * are unhandled. Eventually they will be silenced.
5558 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005559 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00005560
Michael Chand18edcb2007-03-24 20:57:11 -07005561 if (tg3_irq_sync(tp))
5562 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00005563
Matt Carlson72334482009-08-28 14:03:01 +00005564 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00005565
Matt Carlson09943a12009-08-28 14:01:57 +00005566 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00005567
David S. Millerf47c11e2005-06-24 20:18:35 -07005568out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 return IRQ_RETVAL(handled);
5570}
5571
Michael Chan79381092005-04-21 17:13:59 -07005572/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01005573static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07005574{
Matt Carlson09943a12009-08-28 14:01:57 +00005575 struct tg3_napi *tnapi = dev_id;
5576 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005577 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07005578
Michael Chanf9804dd2005-09-27 12:13:10 -07005579 if ((sblk->status & SD_STATUS_UPDATED) ||
5580 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07005581 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07005582 return IRQ_RETVAL(1);
5583 }
5584 return IRQ_RETVAL(0);
5585}
5586
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07005587static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07005588static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589
Michael Chanb9ec6c12006-07-25 16:37:27 -07005590/* Restart hardware after configuration changes, self-test, etc.
5591 * Invoked with tp->lock held.
5592 */
5593static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07005594 __releases(tp->lock)
5595 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005596{
5597 int err;
5598
5599 err = tg3_init_hw(tp, reset_phy);
5600 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00005601 netdev_err(tp->dev,
5602 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07005603 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
5604 tg3_full_unlock(tp);
5605 del_timer_sync(&tp->timer);
5606 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00005607 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005608 dev_close(tp->dev);
5609 tg3_full_lock(tp, 0);
5610 }
5611 return err;
5612}
5613
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614#ifdef CONFIG_NET_POLL_CONTROLLER
5615static void tg3_poll_controller(struct net_device *dev)
5616{
Matt Carlson4f125f42009-09-01 12:55:02 +00005617 int i;
Michael Chan88b06bc2005-04-21 17:13:25 -07005618 struct tg3 *tp = netdev_priv(dev);
5619
Matt Carlson4f125f42009-09-01 12:55:02 +00005620 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00005621 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622}
5623#endif
5624
David Howellsc4028952006-11-22 14:57:56 +00005625static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626{
David Howellsc4028952006-11-22 14:57:56 +00005627 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005628 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629 unsigned int restart_timer;
5630
Michael Chan7faa0062006-02-02 17:29:28 -08005631 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08005632
5633 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08005634 tg3_full_unlock(tp);
5635 return;
5636 }
5637
5638 tg3_full_unlock(tp);
5639
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005640 tg3_phy_stop(tp);
5641
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 tg3_netif_stop(tp);
5643
David S. Millerf47c11e2005-06-24 20:18:35 -07005644 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645
5646 restart_timer = tp->tg3_flags2 & TG3_FLG2_RESTART_TIMER;
5647 tp->tg3_flags2 &= ~TG3_FLG2_RESTART_TIMER;
5648
Michael Chandf3e6542006-05-26 17:48:07 -07005649 if (tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING) {
5650 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5651 tp->write32_rx_mbox = tg3_write_flush_reg32;
5652 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
5653 tp->tg3_flags &= ~TG3_FLAG_TX_RECOVERY_PENDING;
5654 }
5655
Michael Chan944d9802005-05-29 14:57:48 -07005656 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005657 err = tg3_init_hw(tp, 1);
5658 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005659 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660
5661 tg3_netif_start(tp);
5662
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 if (restart_timer)
5664 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08005665
Michael Chanb9ec6c12006-07-25 16:37:27 -07005666out:
Michael Chan7faa0062006-02-02 17:29:28 -08005667 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005668
5669 if (!err)
5670 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671}
5672
5673static void tg3_tx_timeout(struct net_device *dev)
5674{
5675 struct tg3 *tp = netdev_priv(dev);
5676
Michael Chanb0408752007-02-13 12:18:30 -08005677 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00005678 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00005679 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08005680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681
5682 schedule_work(&tp->reset_task);
5683}
5684
Michael Chanc58ec932005-09-17 00:46:27 -07005685/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5686static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5687{
5688 u32 base = (u32) mapping & 0xffffffff;
5689
Eric Dumazet807540b2010-09-23 05:40:09 +00005690 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07005691}
5692
Michael Chan72f2afb2006-03-06 19:28:35 -08005693/* Test for DMA addresses > 40-bit */
5694static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5695 int len)
5696{
5697#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Michael Chan6728a8e2006-03-27 23:16:49 -08005698 if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG)
Eric Dumazet807540b2010-09-23 05:40:09 +00005699 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08005700 return 0;
5701#else
5702 return 0;
5703#endif
5704}
5705
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005706static void tg3_set_txd(struct tg3_napi *, int, dma_addr_t, int, u32, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707
Michael Chan72f2afb2006-03-06 19:28:35 -08005708/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005709static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
5710 struct sk_buff *skb, u32 last_plus_one,
5711 u32 *start, u32 base_flags, u32 mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712{
Matt Carlson24f4efd2009-11-13 13:03:35 +00005713 struct tg3 *tp = tnapi->tp;
Matt Carlson41588ba2008-04-19 18:12:33 -07005714 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07005715 dma_addr_t new_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 u32 entry = *start;
Michael Chanc58ec932005-09-17 00:46:27 -07005717 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718
Matt Carlson41588ba2008-04-19 18:12:33 -07005719 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
5720 new_skb = skb_copy(skb, GFP_ATOMIC);
5721 else {
5722 int more_headroom = 4 - ((unsigned long)skb->data & 3);
5723
5724 new_skb = skb_copy_expand(skb,
5725 skb_headroom(skb) + more_headroom,
5726 skb_tailroom(skb), GFP_ATOMIC);
5727 }
5728
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07005730 ret = -1;
5731 } else {
5732 /* New SKB is guaranteed to be linear. */
5733 entry = *start;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005734 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
5735 PCI_DMA_TODEVICE);
5736 /* Make sure the mapping succeeded */
5737 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
5738 ret = -1;
5739 dev_kfree_skb(new_skb);
5740 new_skb = NULL;
David S. Miller90079ce2008-09-11 04:52:51 -07005741
Michael Chanc58ec932005-09-17 00:46:27 -07005742 /* Make sure new skb does not cross any 4G boundaries.
5743 * Drop the packet if it does.
5744 */
Alexander Duyckf4188d82009-12-02 16:48:38 +00005745 } else if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5746 tg3_4g_overflow_test(new_addr, new_skb->len)) {
5747 pci_unmap_single(tp->pdev, new_addr, new_skb->len,
5748 PCI_DMA_TODEVICE);
Michael Chanc58ec932005-09-17 00:46:27 -07005749 ret = -1;
5750 dev_kfree_skb(new_skb);
5751 new_skb = NULL;
5752 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005753 tg3_set_txd(tnapi, entry, new_addr, new_skb->len,
Michael Chanc58ec932005-09-17 00:46:27 -07005754 base_flags, 1 | (mss << 1));
5755 *start = NEXT_TX(entry);
5756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 }
5758
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 /* Now clean up the sw ring entries. */
5760 i = 0;
5761 while (entry != last_plus_one) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00005762 int len;
5763
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005764 if (i == 0)
Alexander Duyckf4188d82009-12-02 16:48:38 +00005765 len = skb_headlen(skb);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005766 else
Alexander Duyckf4188d82009-12-02 16:48:38 +00005767 len = skb_shinfo(skb)->frags[i-1].size;
5768
5769 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005770 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00005771 mapping),
5772 len, PCI_DMA_TODEVICE);
5773 if (i == 0) {
5774 tnapi->tx_buffers[entry].skb = new_skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005775 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00005776 new_addr);
5777 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005778 tnapi->tx_buffers[entry].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 entry = NEXT_TX(entry);
5781 i++;
5782 }
5783
5784 dev_kfree_skb(skb);
5785
Michael Chanc58ec932005-09-17 00:46:27 -07005786 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787}
5788
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005789static void tg3_set_txd(struct tg3_napi *tnapi, int entry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790 dma_addr_t mapping, int len, u32 flags,
5791 u32 mss_and_is_end)
5792{
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005793 struct tg3_tx_buffer_desc *txd = &tnapi->tx_ring[entry];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794 int is_end = (mss_and_is_end & 0x1);
5795 u32 mss = (mss_and_is_end >> 1);
5796 u32 vlan_tag = 0;
5797
5798 if (is_end)
5799 flags |= TXD_FLAG_END;
5800 if (flags & TXD_FLAG_VLAN) {
5801 vlan_tag = flags >> 16;
5802 flags &= 0xffff;
5803 }
5804 vlan_tag |= (mss << TXD_MSS_SHIFT);
5805
5806 txd->addr_hi = ((u64) mapping >> 32);
5807 txd->addr_lo = ((u64) mapping & 0xffffffff);
5808 txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
5809 txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
5810}
5811
Michael Chan5a6f3072006-03-20 22:28:05 -08005812/* hard_start_xmit for devices that don't have any bugs and
Matt Carlsone849cdc2009-11-13 13:03:38 +00005813 * support TG3_FLG2_HW_TSO_2 and TG3_FLG2_HW_TSO_3 only.
Michael Chan5a6f3072006-03-20 22:28:05 -08005814 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005815static netdev_tx_t tg3_start_xmit(struct sk_buff *skb,
5816 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817{
5818 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819 u32 len, entry, base_flags, mss;
David S. Miller90079ce2008-09-11 04:52:51 -07005820 dma_addr_t mapping;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005821 struct tg3_napi *tnapi;
5822 struct netdev_queue *txq;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005823 unsigned int i, last;
5824
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005825 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5826 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Matt Carlson19cfaec2009-12-03 08:36:20 +00005827 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005828 tnapi++;
Michael Chan5a6f3072006-03-20 22:28:05 -08005829
Michael Chan00b70502006-06-17 21:58:45 -07005830 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005831 * and TX reclaim runs via tp->napi.poll inside of a software
Michael Chan5a6f3072006-03-20 22:28:05 -08005832 * interrupt. Furthermore, IRQ processing runs lockless so we have
5833 * no IRQ context deadlocks to worry about either. Rejoice!
5834 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005835 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005836 if (!netif_tx_queue_stopped(txq)) {
5837 netif_tx_stop_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005838
5839 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00005840 netdev_err(dev,
5841 "BUG! Tx Ring full when queue awake!\n");
Michael Chan5a6f3072006-03-20 22:28:05 -08005842 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005843 return NETDEV_TX_BUSY;
5844 }
5845
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005846 entry = tnapi->tx_prod;
Michael Chan5a6f3072006-03-20 22:28:05 -08005847 base_flags = 0;
Matt Carlsonbe98da62010-07-11 09:31:46 +00005848 mss = skb_shinfo(skb)->gso_size;
5849 if (mss) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005850 int tcp_opt_len, ip_tcp_len;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005851 u32 hdrlen;
Michael Chan5a6f3072006-03-20 22:28:05 -08005852
5853 if (skb_header_cloned(skb) &&
5854 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5855 dev_kfree_skb(skb);
5856 goto out_unlock;
5857 }
5858
Matt Carlson02e96082010-09-15 08:59:59 +00005859 if (skb_is_gso_v6(skb)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005860 hdrlen = skb_headlen(skb) - ETH_HLEN;
Matt Carlson02e96082010-09-15 08:59:59 +00005861 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005862 struct iphdr *iph = ip_hdr(skb);
5863
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005864 tcp_opt_len = tcp_optlen(skb);
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03005865 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
Michael Chanb0026622006-07-03 19:42:14 -07005866
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005867 iph->check = 0;
5868 iph->tot_len = htons(mss + ip_tcp_len + tcp_opt_len);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005869 hdrlen = ip_tcp_len + tcp_opt_len;
Michael Chanb0026622006-07-03 19:42:14 -07005870 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005871
Matt Carlsone849cdc2009-11-13 13:03:38 +00005872 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005873 mss |= (hdrlen & 0xc) << 12;
5874 if (hdrlen & 0x10)
5875 base_flags |= 0x00000010;
5876 base_flags |= (hdrlen & 0x3e0) << 5;
5877 } else
5878 mss |= hdrlen << 9;
5879
Michael Chan5a6f3072006-03-20 22:28:05 -08005880 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5881 TXD_FLAG_CPU_POST_DMA);
5882
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005883 tcp_hdr(skb)->check = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08005884
Matt Carlson859a5882010-04-05 10:19:28 +00005885 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005886 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson859a5882010-04-05 10:19:28 +00005887 }
5888
Jesse Grosseab6d182010-10-20 13:56:03 +00005889 if (vlan_tx_tag_present(skb))
Michael Chan5a6f3072006-03-20 22:28:05 -08005890 base_flags |= (TXD_FLAG_VLAN |
5891 (vlan_tx_tag_get(skb) << 16));
Michael Chan5a6f3072006-03-20 22:28:05 -08005892
Alexander Duyckf4188d82009-12-02 16:48:38 +00005893 len = skb_headlen(skb);
5894
5895 /* Queue skb data, a.k.a. the main skb fragment. */
5896 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
5897 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07005898 dev_kfree_skb(skb);
5899 goto out_unlock;
5900 }
5901
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005902 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005903 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005904
Matt Carlsonb703df62009-12-03 08:36:21 +00005905 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
Matt Carlson8fc2f992010-12-06 08:28:49 +00005906 !mss && skb->len > VLAN_ETH_FRAME_LEN)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005907 base_flags |= TXD_FLAG_JMB_PKT;
5908
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005909 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Michael Chan5a6f3072006-03-20 22:28:05 -08005910 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5911
5912 entry = NEXT_TX(entry);
5913
5914 /* Now loop through additional data fragments, and queue them. */
5915 if (skb_shinfo(skb)->nr_frags > 0) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005916 last = skb_shinfo(skb)->nr_frags - 1;
5917 for (i = 0; i <= last; i++) {
5918 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5919
5920 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005921 mapping = pci_map_page(tp->pdev,
5922 frag->page,
5923 frag->page_offset,
5924 len, PCI_DMA_TODEVICE);
5925 if (pci_dma_mapping_error(tp->pdev, mapping))
5926 goto dma_error;
5927
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005928 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005929 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00005930 mapping);
Michael Chan5a6f3072006-03-20 22:28:05 -08005931
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005932 tg3_set_txd(tnapi, entry, mapping, len,
Michael Chan5a6f3072006-03-20 22:28:05 -08005933 base_flags, (i == last) | (mss << 1));
5934
5935 entry = NEXT_TX(entry);
5936 }
5937 }
5938
5939 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005940 tw32_tx_mbox(tnapi->prodmbox, entry);
Michael Chan5a6f3072006-03-20 22:28:05 -08005941
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005942 tnapi->tx_prod = entry;
5943 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005944 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00005945
5946 /* netif_tx_stop_queue() must be done before checking
5947 * checking tx index in tg3_tx_avail() below, because in
5948 * tg3_tx(), we update tx index before checking for
5949 * netif_tx_queue_stopped().
5950 */
5951 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005952 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005953 netif_tx_wake_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005954 }
5955
5956out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005957 mmiowb();
Michael Chan5a6f3072006-03-20 22:28:05 -08005958
5959 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005960
5961dma_error:
5962 last = i;
5963 entry = tnapi->tx_prod;
5964 tnapi->tx_buffers[entry].skb = NULL;
5965 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005966 dma_unmap_addr(&tnapi->tx_buffers[entry], mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005967 skb_headlen(skb),
5968 PCI_DMA_TODEVICE);
5969 for (i = 0; i <= last; i++) {
5970 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5971 entry = NEXT_TX(entry);
5972
5973 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005974 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00005975 mapping),
5976 frag->size, PCI_DMA_TODEVICE);
5977 }
5978
5979 dev_kfree_skb(skb);
5980 return NETDEV_TX_OK;
Michael Chan5a6f3072006-03-20 22:28:05 -08005981}
5982
Stephen Hemminger613573252009-08-31 19:50:58 +00005983static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *,
5984 struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07005985
5986/* Use GSO to workaround a rare TSO bug that may be triggered when the
5987 * TSO header is greater than 80 bytes.
5988 */
5989static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
5990{
5991 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005992 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07005993
5994 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005995 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07005996 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00005997
5998 /* netif_tx_stop_queue() must be done before checking
5999 * checking tx index in tg3_tx_avail() below, because in
6000 * tg3_tx(), we update tx index before checking for
6001 * netif_tx_queue_stopped().
6002 */
6003 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006004 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08006005 return NETDEV_TX_BUSY;
6006
6007 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006008 }
6009
6010 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07006011 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07006012 goto tg3_tso_bug_end;
6013
6014 do {
6015 nskb = segs;
6016 segs = segs->next;
6017 nskb->next = NULL;
6018 tg3_start_xmit_dma_bug(nskb, tp->dev);
6019 } while (segs);
6020
6021tg3_tso_bug_end:
6022 dev_kfree_skb(skb);
6023
6024 return NETDEV_TX_OK;
6025}
Michael Chan52c0fd82006-06-29 20:15:54 -07006026
Michael Chan5a6f3072006-03-20 22:28:05 -08006027/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
6028 * support TG3_FLG2_HW_TSO_1 or firmware TSO only.
6029 */
Stephen Hemminger613573252009-08-31 19:50:58 +00006030static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *skb,
6031 struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08006032{
6033 struct tg3 *tp = netdev_priv(dev);
Michael Chan5a6f3072006-03-20 22:28:05 -08006034 u32 len, entry, base_flags, mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035 int would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07006036 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006037 struct tg3_napi *tnapi;
6038 struct netdev_queue *txq;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006039 unsigned int i, last;
6040
Matt Carlson24f4efd2009-11-13 13:03:35 +00006041 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
6042 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Matt Carlson19cfaec2009-12-03 08:36:20 +00006043 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlson24f4efd2009-11-13 13:03:35 +00006044 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045
Michael Chan00b70502006-06-17 21:58:45 -07006046 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006047 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07006048 * interrupt. Furthermore, IRQ processing runs lockless so we have
6049 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006051 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006052 if (!netif_tx_queue_stopped(txq)) {
6053 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006054
6055 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00006056 netdev_err(dev,
6057 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059 return NETDEV_TX_BUSY;
6060 }
6061
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006062 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07006064 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006066
Matt Carlsonbe98da62010-07-11 09:31:46 +00006067 mss = skb_shinfo(skb)->gso_size;
6068 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006069 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00006070 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071
6072 if (skb_header_cloned(skb) &&
6073 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
6074 dev_kfree_skb(skb);
6075 goto out_unlock;
6076 }
6077
Matt Carlson34195c32010-07-11 09:31:42 +00006078 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07006079 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080
Matt Carlson02e96082010-09-15 08:59:59 +00006081 if (skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00006082 hdr_len = skb_headlen(skb) - ETH_HLEN;
6083 } else {
6084 u32 ip_tcp_len;
6085
6086 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
6087 hdr_len = ip_tcp_len + tcp_opt_len;
6088
6089 iph->check = 0;
6090 iph->tot_len = htons(mss + hdr_len);
6091 }
6092
Michael Chan52c0fd82006-06-29 20:15:54 -07006093 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Michael Chan7f62ad52007-02-20 23:25:40 -08006094 (tp->tg3_flags2 & TG3_FLG2_TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00006095 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07006096
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
6098 TXD_FLAG_CPU_POST_DMA);
6099
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006101 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006103 } else
6104 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
6105 iph->daddr, 0,
6106 IPPROTO_TCP,
6107 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108
Matt Carlson615774f2009-11-13 13:03:39 +00006109 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
6110 mss |= (hdr_len & 0xc) << 12;
6111 if (hdr_len & 0x10)
6112 base_flags |= 0x00000010;
6113 base_flags |= (hdr_len & 0x3e0) << 5;
6114 } else if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_2)
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006115 mss |= hdr_len << 9;
6116 else if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_1) ||
6117 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006118 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119 int tsflags;
6120
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006121 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122 mss |= (tsflags << 11);
6123 }
6124 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006125 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126 int tsflags;
6127
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006128 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129 base_flags |= tsflags << 12;
6130 }
6131 }
6132 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00006133
Jesse Grosseab6d182010-10-20 13:56:03 +00006134 if (vlan_tx_tag_present(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135 base_flags |= (TXD_FLAG_VLAN |
6136 (vlan_tx_tag_get(skb) << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137
Matt Carlsonb703df62009-12-03 08:36:21 +00006138 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
Matt Carlson8fc2f992010-12-06 08:28:49 +00006139 !mss && skb->len > VLAN_ETH_FRAME_LEN)
Matt Carlson615774f2009-11-13 13:03:39 +00006140 base_flags |= TXD_FLAG_JMB_PKT;
6141
Alexander Duyckf4188d82009-12-02 16:48:38 +00006142 len = skb_headlen(skb);
6143
6144 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
6145 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07006146 dev_kfree_skb(skb);
6147 goto out_unlock;
6148 }
6149
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006150 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006151 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152
6153 would_hit_hwbug = 0;
6154
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006155 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) && len <= 8)
6156 would_hit_hwbug = 1;
6157
Matt Carlson0e1406d2009-11-02 12:33:33 +00006158 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
6159 tg3_4g_overflow_test(mapping, len))
Matt Carlson41588ba2008-04-19 18:12:33 -07006160 would_hit_hwbug = 1;
Matt Carlson0e1406d2009-11-02 12:33:33 +00006161
6162 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
6163 tg3_40bit_overflow_test(tp, mapping, len))
6164 would_hit_hwbug = 1;
6165
6166 if (tp->tg3_flags3 & TG3_FLG3_5701_DMA_BUG)
Michael Chanc58ec932005-09-17 00:46:27 -07006167 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006169 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
6171
6172 entry = NEXT_TX(entry);
6173
6174 /* Now loop through additional data fragments, and queue them. */
6175 if (skb_shinfo(skb)->nr_frags > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176 last = skb_shinfo(skb)->nr_frags - 1;
6177 for (i = 0; i <= last; i++) {
6178 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6179
6180 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006181 mapping = pci_map_page(tp->pdev,
6182 frag->page,
6183 frag->page_offset,
6184 len, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006186 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006187 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006188 mapping);
6189 if (pci_dma_mapping_error(tp->pdev, mapping))
6190 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006192 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) &&
6193 len <= 8)
6194 would_hit_hwbug = 1;
6195
Matt Carlson0e1406d2009-11-02 12:33:33 +00006196 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
6197 tg3_4g_overflow_test(mapping, len))
Michael Chanc58ec932005-09-17 00:46:27 -07006198 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199
Matt Carlson0e1406d2009-11-02 12:33:33 +00006200 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
6201 tg3_40bit_overflow_test(tp, mapping, len))
Michael Chan72f2afb2006-03-06 19:28:35 -08006202 would_hit_hwbug = 1;
6203
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006205 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206 base_flags, (i == last)|(mss << 1));
6207 else
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006208 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209 base_flags, (i == last));
6210
6211 entry = NEXT_TX(entry);
6212 }
6213 }
6214
6215 if (would_hit_hwbug) {
6216 u32 last_plus_one = entry;
6217 u32 start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218
Michael Chanc58ec932005-09-17 00:46:27 -07006219 start = entry - 1 - skb_shinfo(skb)->nr_frags;
6220 start &= (TG3_TX_RING_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221
6222 /* If the workaround fails due to memory/mapping
6223 * failure, silently drop this packet.
6224 */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006225 if (tigon3_dma_hwbug_workaround(tnapi, skb, last_plus_one,
Michael Chanc58ec932005-09-17 00:46:27 -07006226 &start, base_flags, mss))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227 goto out_unlock;
6228
6229 entry = start;
6230 }
6231
6232 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006233 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006235 tnapi->tx_prod = entry;
6236 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006237 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006238
6239 /* netif_tx_stop_queue() must be done before checking
6240 * checking tx index in tg3_tx_avail() below, because in
6241 * tg3_tx(), we update tx index before checking for
6242 * netif_tx_queue_stopped().
6243 */
6244 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006245 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006246 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248
6249out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00006250 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251
6252 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006253
6254dma_error:
6255 last = i;
6256 entry = tnapi->tx_prod;
6257 tnapi->tx_buffers[entry].skb = NULL;
6258 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006259 dma_unmap_addr(&tnapi->tx_buffers[entry], mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006260 skb_headlen(skb),
6261 PCI_DMA_TODEVICE);
6262 for (i = 0; i <= last; i++) {
6263 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6264 entry = NEXT_TX(entry);
6265
6266 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006267 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00006268 mapping),
6269 frag->size, PCI_DMA_TODEVICE);
6270 }
6271
6272 dev_kfree_skb(skb);
6273 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274}
6275
Michał Mirosławdc668912011-04-07 03:35:07 +00006276static u32 tg3_fix_features(struct net_device *dev, u32 features)
6277{
6278 struct tg3 *tp = netdev_priv(dev);
6279
6280 if (dev->mtu > ETH_DATA_LEN && (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
6281 features &= ~NETIF_F_ALL_TSO;
6282
6283 return features;
6284}
6285
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
6287 int new_mtu)
6288{
6289 dev->mtu = new_mtu;
6290
Michael Chanef7f5ec2005-07-25 12:32:25 -07006291 if (new_mtu > ETH_DATA_LEN) {
Michael Chana4e2b342005-10-26 15:46:52 -07006292 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michał Mirosławdc668912011-04-07 03:35:07 +00006293 netdev_update_features(dev);
Michael Chanef7f5ec2005-07-25 12:32:25 -07006294 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
Matt Carlson859a5882010-04-05 10:19:28 +00006295 } else {
Michael Chanef7f5ec2005-07-25 12:32:25 -07006296 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Matt Carlson859a5882010-04-05 10:19:28 +00006297 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07006298 } else {
Michał Mirosławdc668912011-04-07 03:35:07 +00006299 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanef7f5ec2005-07-25 12:32:25 -07006300 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
Michał Mirosławdc668912011-04-07 03:35:07 +00006301 netdev_update_features(dev);
6302 }
Michael Chan0f893dc2005-07-25 12:30:38 -07006303 tp->tg3_flags &= ~TG3_FLAG_JUMBO_RING_ENABLE;
Michael Chanef7f5ec2005-07-25 12:32:25 -07006304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305}
6306
6307static int tg3_change_mtu(struct net_device *dev, int new_mtu)
6308{
6309 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07006310 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311
6312 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
6313 return -EINVAL;
6314
6315 if (!netif_running(dev)) {
6316 /* We'll just catch it later when the
6317 * device is up'd.
6318 */
6319 tg3_set_mtu(dev, tp, new_mtu);
6320 return 0;
6321 }
6322
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006323 tg3_phy_stop(tp);
6324
Linus Torvalds1da177e2005-04-16 15:20:36 -07006325 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006326
6327 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328
Michael Chan944d9802005-05-29 14:57:48 -07006329 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330
6331 tg3_set_mtu(dev, tp, new_mtu);
6332
Michael Chanb9ec6c12006-07-25 16:37:27 -07006333 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334
Michael Chanb9ec6c12006-07-25 16:37:27 -07006335 if (!err)
6336 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006337
David S. Millerf47c11e2005-06-24 20:18:35 -07006338 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006340 if (!err)
6341 tg3_phy_start(tp);
6342
Michael Chanb9ec6c12006-07-25 16:37:27 -07006343 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344}
6345
Matt Carlson21f581a2009-08-28 14:00:25 +00006346static void tg3_rx_prodring_free(struct tg3 *tp,
6347 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349 int i;
6350
Matt Carlson8fea32b2010-09-15 08:59:58 +00006351 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006352 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006353 i = (i + 1) & tp->rx_std_ring_mask)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006354 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6355 tp->rx_pkt_map_sz);
6356
6357 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
6358 for (i = tpr->rx_jmb_cons_idx;
6359 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006360 i = (i + 1) & tp->rx_jmb_ring_mask) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006361 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6362 TG3_RX_JMB_MAP_SZ);
6363 }
6364 }
6365
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006366 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368
Matt Carlson2c49a442010-09-30 10:34:35 +00006369 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006370 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6371 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006372
Matt Carlson48035722010-10-14 10:37:43 +00006373 if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
6374 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006375 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006376 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6377 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006378 }
6379}
6380
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006381/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382 *
6383 * The chip has been shut down and the driver detached from
6384 * the networking, so no interrupts or new tx packets will
6385 * end up in the driver. tp->{tx,}lock are held and thus
6386 * we may not sleep.
6387 */
Matt Carlson21f581a2009-08-28 14:00:25 +00006388static int tg3_rx_prodring_alloc(struct tg3 *tp,
6389 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390{
Matt Carlson287be122009-08-28 13:58:46 +00006391 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006393 tpr->rx_std_cons_idx = 0;
6394 tpr->rx_std_prod_idx = 0;
6395 tpr->rx_jmb_cons_idx = 0;
6396 tpr->rx_jmb_prod_idx = 0;
6397
Matt Carlson8fea32b2010-09-15 08:59:58 +00006398 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006399 memset(&tpr->rx_std_buffers[0], 0,
6400 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00006401 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006402 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00006403 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006404 goto done;
6405 }
6406
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00006408 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409
Matt Carlson287be122009-08-28 13:58:46 +00006410 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Michael Chana4e2b342005-10-26 15:46:52 -07006411 if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00006412 tp->dev->mtu > ETH_DATA_LEN)
6413 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
6414 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07006415
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416 /* Initialize invariants of the rings, we only set this
6417 * stuff once. This works because the card does not
6418 * write into the rx buffer posting rings.
6419 */
Matt Carlson2c49a442010-09-30 10:34:35 +00006420 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421 struct tg3_rx_buffer_desc *rxd;
6422
Matt Carlson21f581a2009-08-28 14:00:25 +00006423 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00006424 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006425 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
6426 rxd->opaque = (RXD_OPAQUE_RING_STD |
6427 (i << RXD_OPAQUE_INDEX_SHIFT));
6428 }
6429
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006430 /* Now allocate fresh SKBs for each rx ring. */
6431 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson86b21e52009-11-13 13:03:45 +00006432 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006433 netdev_warn(tp->dev,
6434 "Using a smaller RX standard ring. Only "
6435 "%d out of %d buffers were allocated "
6436 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006437 if (i == 0)
6438 goto initfail;
6439 tp->rx_pending = i;
6440 break;
6441 }
6442 }
6443
Matt Carlson48035722010-10-14 10:37:43 +00006444 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) ||
6445 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006446 goto done;
6447
Matt Carlson2c49a442010-09-30 10:34:35 +00006448 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006449
Matt Carlson0d86df82010-02-17 15:17:00 +00006450 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE))
6451 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452
Matt Carlson2c49a442010-09-30 10:34:35 +00006453 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00006454 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006455
Matt Carlson0d86df82010-02-17 15:17:00 +00006456 rxd = &tpr->rx_jmb[i].std;
6457 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
6458 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
6459 RXD_FLAG_JUMBO;
6460 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
6461 (i << RXD_OPAQUE_INDEX_SHIFT));
6462 }
6463
6464 for (i = 0; i < tp->rx_jumbo_pending; i++) {
6465 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006466 netdev_warn(tp->dev,
6467 "Using a smaller RX jumbo ring. Only %d "
6468 "out of %d buffers were allocated "
6469 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00006470 if (i == 0)
6471 goto initfail;
6472 tp->rx_jumbo_pending = i;
6473 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474 }
6475 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006476
6477done:
Michael Chan32d8c572006-07-25 16:38:29 -07006478 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006479
6480initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00006481 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006482 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006483}
6484
Matt Carlson21f581a2009-08-28 14:00:25 +00006485static void tg3_rx_prodring_fini(struct tg3 *tp,
6486 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006487{
Matt Carlson21f581a2009-08-28 14:00:25 +00006488 kfree(tpr->rx_std_buffers);
6489 tpr->rx_std_buffers = NULL;
6490 kfree(tpr->rx_jmb_buffers);
6491 tpr->rx_jmb_buffers = NULL;
6492 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006493 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
6494 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006495 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006496 }
Matt Carlson21f581a2009-08-28 14:00:25 +00006497 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006498 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
6499 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006500 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006501 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006502}
6503
Matt Carlson21f581a2009-08-28 14:00:25 +00006504static int tg3_rx_prodring_init(struct tg3 *tp,
6505 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006506{
Matt Carlson2c49a442010-09-30 10:34:35 +00006507 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
6508 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006509 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006510 return -ENOMEM;
6511
Matt Carlson4bae65c2010-11-24 08:31:52 +00006512 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
6513 TG3_RX_STD_RING_BYTES(tp),
6514 &tpr->rx_std_mapping,
6515 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006516 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006517 goto err_out;
6518
Matt Carlson48035722010-10-14 10:37:43 +00006519 if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
6520 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006521 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00006522 GFP_KERNEL);
6523 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006524 goto err_out;
6525
Matt Carlson4bae65c2010-11-24 08:31:52 +00006526 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
6527 TG3_RX_JMB_RING_BYTES(tp),
6528 &tpr->rx_jmb_mapping,
6529 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006530 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006531 goto err_out;
6532 }
6533
6534 return 0;
6535
6536err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00006537 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006538 return -ENOMEM;
6539}
6540
6541/* Free up pending packets in all rx/tx rings.
6542 *
6543 * The chip has been shut down and the driver detached from
6544 * the networking, so no interrupts or new tx packets will
6545 * end up in the driver. tp->{tx,}lock is not held and we are not
6546 * in an interrupt context and thus may sleep.
6547 */
6548static void tg3_free_rings(struct tg3 *tp)
6549{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006550 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006551
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006552 for (j = 0; j < tp->irq_cnt; j++) {
6553 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006554
Matt Carlson8fea32b2010-09-15 08:59:58 +00006555 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00006556
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006557 if (!tnapi->tx_buffers)
6558 continue;
6559
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006560 for (i = 0; i < TG3_TX_RING_SIZE; ) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006561 struct ring_info *txp;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006562 struct sk_buff *skb;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006563 unsigned int k;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006564
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006565 txp = &tnapi->tx_buffers[i];
6566 skb = txp->skb;
6567
6568 if (skb == NULL) {
6569 i++;
6570 continue;
6571 }
6572
Alexander Duyckf4188d82009-12-02 16:48:38 +00006573 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006574 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006575 skb_headlen(skb),
6576 PCI_DMA_TODEVICE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006577 txp->skb = NULL;
6578
Alexander Duyckf4188d82009-12-02 16:48:38 +00006579 i++;
6580
6581 for (k = 0; k < skb_shinfo(skb)->nr_frags; k++) {
6582 txp = &tnapi->tx_buffers[i & (TG3_TX_RING_SIZE - 1)];
6583 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006584 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006585 skb_shinfo(skb)->frags[k].size,
6586 PCI_DMA_TODEVICE);
6587 i++;
6588 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006589
6590 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006591 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006592 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006593}
6594
6595/* Initialize tx/rx rings for packet processing.
6596 *
6597 * The chip has been shut down and the driver detached from
6598 * the networking, so no interrupts or new tx packets will
6599 * end up in the driver. tp->{tx,}lock are held and thus
6600 * we may not sleep.
6601 */
6602static int tg3_init_rings(struct tg3 *tp)
6603{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006604 int i;
Matt Carlson72334482009-08-28 14:03:01 +00006605
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006606 /* Free up all the SKBs. */
6607 tg3_free_rings(tp);
6608
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006609 for (i = 0; i < tp->irq_cnt; i++) {
6610 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006611
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006612 tnapi->last_tag = 0;
6613 tnapi->last_irq_tag = 0;
6614 tnapi->hw_status->status = 0;
6615 tnapi->hw_status->status_tag = 0;
6616 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6617
6618 tnapi->tx_prod = 0;
6619 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006620 if (tnapi->tx_ring)
6621 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006622
6623 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006624 if (tnapi->rx_rcb)
6625 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006626
Matt Carlson8fea32b2010-09-15 08:59:58 +00006627 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00006628 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006629 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006630 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006631 }
Matt Carlson72334482009-08-28 14:03:01 +00006632
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006633 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006634}
6635
6636/*
6637 * Must not be invoked with interrupt sources disabled and
6638 * the hardware shutdown down.
6639 */
6640static void tg3_free_consistent(struct tg3 *tp)
6641{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006642 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006643
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006644 for (i = 0; i < tp->irq_cnt; i++) {
6645 struct tg3_napi *tnapi = &tp->napi[i];
6646
6647 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006648 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006649 tnapi->tx_ring, tnapi->tx_desc_mapping);
6650 tnapi->tx_ring = NULL;
6651 }
6652
6653 kfree(tnapi->tx_buffers);
6654 tnapi->tx_buffers = NULL;
6655
6656 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006657 dma_free_coherent(&tp->pdev->dev,
6658 TG3_RX_RCB_RING_BYTES(tp),
6659 tnapi->rx_rcb,
6660 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006661 tnapi->rx_rcb = NULL;
6662 }
6663
Matt Carlson8fea32b2010-09-15 08:59:58 +00006664 tg3_rx_prodring_fini(tp, &tnapi->prodring);
6665
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006666 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006667 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
6668 tnapi->hw_status,
6669 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006670 tnapi->hw_status = NULL;
6671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006673
Linus Torvalds1da177e2005-04-16 15:20:36 -07006674 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006675 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
6676 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006677 tp->hw_stats = NULL;
6678 }
6679}
6680
6681/*
6682 * Must not be invoked with interrupt sources disabled and
6683 * the hardware shutdown down. Can sleep.
6684 */
6685static int tg3_alloc_consistent(struct tg3 *tp)
6686{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006687 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006688
Matt Carlson4bae65c2010-11-24 08:31:52 +00006689 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
6690 sizeof(struct tg3_hw_stats),
6691 &tp->stats_mapping,
6692 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006693 if (!tp->hw_stats)
6694 goto err_out;
6695
Linus Torvalds1da177e2005-04-16 15:20:36 -07006696 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6697
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006698 for (i = 0; i < tp->irq_cnt; i++) {
6699 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006700 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006701
Matt Carlson4bae65c2010-11-24 08:31:52 +00006702 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
6703 TG3_HW_STATUS_SIZE,
6704 &tnapi->status_mapping,
6705 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006706 if (!tnapi->hw_status)
6707 goto err_out;
6708
6709 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006710 sblk = tnapi->hw_status;
6711
Matt Carlson8fea32b2010-09-15 08:59:58 +00006712 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
6713 goto err_out;
6714
Matt Carlson19cfaec2009-12-03 08:36:20 +00006715 /* If multivector TSS is enabled, vector 0 does not handle
6716 * tx interrupts. Don't allocate any resources for it.
6717 */
6718 if ((!i && !(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) ||
6719 (i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))) {
6720 tnapi->tx_buffers = kzalloc(sizeof(struct ring_info) *
6721 TG3_TX_RING_SIZE,
6722 GFP_KERNEL);
6723 if (!tnapi->tx_buffers)
6724 goto err_out;
6725
Matt Carlson4bae65c2010-11-24 08:31:52 +00006726 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
6727 TG3_TX_RING_BYTES,
6728 &tnapi->tx_desc_mapping,
6729 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00006730 if (!tnapi->tx_ring)
6731 goto err_out;
6732 }
6733
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006734 /*
6735 * When RSS is enabled, the status block format changes
6736 * slightly. The "rx_jumbo_consumer", "reserved",
6737 * and "rx_mini_consumer" members get mapped to the
6738 * other three rx return ring producer indexes.
6739 */
6740 switch (i) {
6741 default:
6742 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
6743 break;
6744 case 2:
6745 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
6746 break;
6747 case 3:
6748 tnapi->rx_rcb_prod_idx = &sblk->reserved;
6749 break;
6750 case 4:
6751 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
6752 break;
6753 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006754
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006755 /*
6756 * If multivector RSS is enabled, vector 0 does not handle
6757 * rx or tx interrupts. Don't allocate any resources for it.
6758 */
6759 if (!i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS))
6760 continue;
6761
Matt Carlson4bae65c2010-11-24 08:31:52 +00006762 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
6763 TG3_RX_RCB_RING_BYTES(tp),
6764 &tnapi->rx_rcb_mapping,
6765 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006766 if (!tnapi->rx_rcb)
6767 goto err_out;
6768
6769 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006770 }
6771
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772 return 0;
6773
6774err_out:
6775 tg3_free_consistent(tp);
6776 return -ENOMEM;
6777}
6778
6779#define MAX_WAIT_CNT 1000
6780
6781/* To stop a block, clear the enable bit and poll till it
6782 * clears. tp->lock is held.
6783 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006784static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006785{
6786 unsigned int i;
6787 u32 val;
6788
6789 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
6790 switch (ofs) {
6791 case RCVLSC_MODE:
6792 case DMAC_MODE:
6793 case MBFREE_MODE:
6794 case BUFMGR_MODE:
6795 case MEMARB_MODE:
6796 /* We can't enable/disable these bits of the
6797 * 5705/5750, just say success.
6798 */
6799 return 0;
6800
6801 default:
6802 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006804 }
6805
6806 val = tr32(ofs);
6807 val &= ~enable_bit;
6808 tw32_f(ofs, val);
6809
6810 for (i = 0; i < MAX_WAIT_CNT; i++) {
6811 udelay(100);
6812 val = tr32(ofs);
6813 if ((val & enable_bit) == 0)
6814 break;
6815 }
6816
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006817 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00006818 dev_err(&tp->pdev->dev,
6819 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
6820 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006821 return -ENODEV;
6822 }
6823
6824 return 0;
6825}
6826
6827/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006828static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006829{
6830 int i, err;
6831
6832 tg3_disable_ints(tp);
6833
6834 tp->rx_mode &= ~RX_MODE_ENABLE;
6835 tw32_f(MAC_RX_MODE, tp->rx_mode);
6836 udelay(10);
6837
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006838 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
6839 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
6840 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
6841 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
6842 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
6843 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006844
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006845 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
6846 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
6847 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
6848 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
6849 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
6850 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
6851 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006852
6853 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
6854 tw32_f(MAC_MODE, tp->mac_mode);
6855 udelay(40);
6856
6857 tp->tx_mode &= ~TX_MODE_ENABLE;
6858 tw32_f(MAC_TX_MODE, tp->tx_mode);
6859
6860 for (i = 0; i < MAX_WAIT_CNT; i++) {
6861 udelay(100);
6862 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
6863 break;
6864 }
6865 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00006866 dev_err(&tp->pdev->dev,
6867 "%s timed out, TX_MODE_ENABLE will not clear "
6868 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07006869 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006870 }
6871
Michael Chane6de8ad2005-05-05 14:42:41 -07006872 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006873 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
6874 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006875
6876 tw32(FTQ_RESET, 0xffffffff);
6877 tw32(FTQ_RESET, 0x00000000);
6878
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006879 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
6880 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006881
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006882 for (i = 0; i < tp->irq_cnt; i++) {
6883 struct tg3_napi *tnapi = &tp->napi[i];
6884 if (tnapi->hw_status)
6885 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006887 if (tp->hw_stats)
6888 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6889
Linus Torvalds1da177e2005-04-16 15:20:36 -07006890 return err;
6891}
6892
Matt Carlson0d3031d2007-10-10 18:02:43 -07006893static void tg3_ape_send_event(struct tg3 *tp, u32 event)
6894{
6895 int i;
6896 u32 apedata;
6897
Matt Carlsondc6d0742010-09-15 08:59:55 +00006898 /* NCSI does not support APE events */
6899 if (tp->tg3_flags3 & TG3_FLG3_APE_HAS_NCSI)
6900 return;
6901
Matt Carlson0d3031d2007-10-10 18:02:43 -07006902 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
6903 if (apedata != APE_SEG_SIG_MAGIC)
6904 return;
6905
6906 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07006907 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006908 return;
6909
6910 /* Wait for up to 1 millisecond for APE to service previous event. */
6911 for (i = 0; i < 10; i++) {
6912 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
6913 return;
6914
6915 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
6916
6917 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6918 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
6919 event | APE_EVENT_STATUS_EVENT_PENDING);
6920
6921 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
6922
6923 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6924 break;
6925
6926 udelay(100);
6927 }
6928
6929 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6930 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
6931}
6932
6933static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
6934{
6935 u32 event;
6936 u32 apedata;
6937
6938 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
6939 return;
6940
6941 switch (kind) {
Matt Carlson33f401a2010-04-05 10:19:27 +00006942 case RESET_KIND_INIT:
6943 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
6944 APE_HOST_SEG_SIG_MAGIC);
6945 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
6946 APE_HOST_SEG_LEN_MAGIC);
6947 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
6948 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
6949 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
Matt Carlson6867c842010-07-11 09:31:44 +00006950 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
Matt Carlson33f401a2010-04-05 10:19:27 +00006951 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
6952 APE_HOST_BEHAV_NO_PHYLOCK);
Matt Carlsondc6d0742010-09-15 08:59:55 +00006953 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
6954 TG3_APE_HOST_DRVR_STATE_START);
Matt Carlson0d3031d2007-10-10 18:02:43 -07006955
Matt Carlson33f401a2010-04-05 10:19:27 +00006956 event = APE_EVENT_STATUS_STATE_START;
6957 break;
6958 case RESET_KIND_SHUTDOWN:
6959 /* With the interface we are currently using,
6960 * APE does not track driver state. Wiping
6961 * out the HOST SEGMENT SIGNATURE forces
6962 * the APE to assume OS absent status.
6963 */
6964 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
Matt Carlsonb2aee152008-11-03 16:51:11 -08006965
Matt Carlsondc6d0742010-09-15 08:59:55 +00006966 if (device_may_wakeup(&tp->pdev->dev) &&
6967 (tp->tg3_flags & TG3_FLAG_WOL_ENABLE)) {
6968 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
6969 TG3_APE_HOST_WOL_SPEED_AUTO);
6970 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
6971 } else
6972 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
6973
6974 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
6975
Matt Carlson33f401a2010-04-05 10:19:27 +00006976 event = APE_EVENT_STATUS_STATE_UNLOAD;
6977 break;
6978 case RESET_KIND_SUSPEND:
6979 event = APE_EVENT_STATUS_STATE_SUSPEND;
6980 break;
6981 default:
6982 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006983 }
6984
6985 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
6986
6987 tg3_ape_send_event(tp, event);
6988}
6989
Michael Chane6af3012005-04-21 17:12:05 -07006990/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006991static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
6992{
David S. Millerf49639e2006-06-09 11:58:36 -07006993 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
6994 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006995
6996 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6997 switch (kind) {
6998 case RESET_KIND_INIT:
6999 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7000 DRV_STATE_START);
7001 break;
7002
7003 case RESET_KIND_SHUTDOWN:
7004 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7005 DRV_STATE_UNLOAD);
7006 break;
7007
7008 case RESET_KIND_SUSPEND:
7009 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7010 DRV_STATE_SUSPEND);
7011 break;
7012
7013 default:
7014 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07007017
7018 if (kind == RESET_KIND_INIT ||
7019 kind == RESET_KIND_SUSPEND)
7020 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007021}
7022
7023/* tp->lock is held. */
7024static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
7025{
7026 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
7027 switch (kind) {
7028 case RESET_KIND_INIT:
7029 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7030 DRV_STATE_START_DONE);
7031 break;
7032
7033 case RESET_KIND_SHUTDOWN:
7034 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7035 DRV_STATE_UNLOAD_DONE);
7036 break;
7037
7038 default:
7039 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007041 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07007042
7043 if (kind == RESET_KIND_SHUTDOWN)
7044 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007045}
7046
7047/* tp->lock is held. */
7048static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
7049{
7050 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
7051 switch (kind) {
7052 case RESET_KIND_INIT:
7053 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7054 DRV_STATE_START);
7055 break;
7056
7057 case RESET_KIND_SHUTDOWN:
7058 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7059 DRV_STATE_UNLOAD);
7060 break;
7061
7062 case RESET_KIND_SUSPEND:
7063 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7064 DRV_STATE_SUSPEND);
7065 break;
7066
7067 default:
7068 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007070 }
7071}
7072
Michael Chan7a6f4362006-09-27 16:03:31 -07007073static int tg3_poll_fw(struct tg3 *tp)
7074{
7075 int i;
7076 u32 val;
7077
Michael Chanb5d37722006-09-27 16:06:21 -07007078 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08007079 /* Wait up to 20ms for init done. */
7080 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07007081 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
7082 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08007083 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07007084 }
7085 return -ENODEV;
7086 }
7087
Michael Chan7a6f4362006-09-27 16:03:31 -07007088 /* Wait for firmware initialization to complete. */
7089 for (i = 0; i < 100000; i++) {
7090 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
7091 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
7092 break;
7093 udelay(10);
7094 }
7095
7096 /* Chip might not be fitted with firmware. Some Sun onboard
7097 * parts are configured like that. So don't signal the timeout
7098 * of the above loop as an error, but do report the lack of
7099 * running firmware once.
7100 */
7101 if (i >= 100000 &&
7102 !(tp->tg3_flags2 & TG3_FLG2_NO_FWARE_REPORTED)) {
7103 tp->tg3_flags2 |= TG3_FLG2_NO_FWARE_REPORTED;
7104
Joe Perches05dbe002010-02-17 19:44:19 +00007105 netdev_info(tp->dev, "No firmware running\n");
Michael Chan7a6f4362006-09-27 16:03:31 -07007106 }
7107
Matt Carlson6b10c162010-02-12 14:47:08 +00007108 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
7109 /* The 57765 A0 needs a little more
7110 * time to do some important work.
7111 */
7112 mdelay(10);
7113 }
7114
Michael Chan7a6f4362006-09-27 16:03:31 -07007115 return 0;
7116}
7117
Michael Chanee6a99b2007-07-18 21:49:10 -07007118/* Save PCI command register before chip reset */
7119static void tg3_save_pci_state(struct tg3 *tp)
7120{
Matt Carlson8a6eac92007-10-21 16:17:55 -07007121 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007122}
7123
7124/* Restore PCI state after chip reset */
7125static void tg3_restore_pci_state(struct tg3 *tp)
7126{
7127 u32 val;
7128
7129 /* Re-enable indirect register accesses. */
7130 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7131 tp->misc_host_ctrl);
7132
7133 /* Set MAX PCI retry to zero. */
7134 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7135 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
7136 (tp->tg3_flags & TG3_FLAG_PCIX_MODE))
7137 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007138 /* Allow reads and writes to the APE register and memory space. */
7139 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
7140 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +00007141 PCISTATE_ALLOW_APE_SHMEM_WR |
7142 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07007143 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7144
Matt Carlson8a6eac92007-10-21 16:17:55 -07007145 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007146
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007147 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
7148 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
Matt Carlsoncf790032010-11-24 08:31:48 +00007149 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007150 else {
7151 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7152 tp->pci_cacheline_sz);
7153 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7154 tp->pci_lat_timer);
7155 }
Michael Chan114342f2007-10-15 02:12:26 -07007156 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08007157
Michael Chanee6a99b2007-07-18 21:49:10 -07007158 /* Make sure PCI-X relaxed ordering bit is clear. */
Matt Carlson52f44902008-11-21 17:17:04 -08007159 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Matt Carlson9974a352007-10-07 23:27:28 -07007160 u16 pcix_cmd;
7161
7162 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7163 &pcix_cmd);
7164 pcix_cmd &= ~PCI_X_CMD_ERO;
7165 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7166 pcix_cmd);
7167 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007168
7169 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007170
7171 /* Chip reset on 5780 will reset MSI enable bit,
7172 * so need to restore it.
7173 */
7174 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
7175 u16 ctrl;
7176
7177 pci_read_config_word(tp->pdev,
7178 tp->msi_cap + PCI_MSI_FLAGS,
7179 &ctrl);
7180 pci_write_config_word(tp->pdev,
7181 tp->msi_cap + PCI_MSI_FLAGS,
7182 ctrl | PCI_MSI_FLAGS_ENABLE);
7183 val = tr32(MSGINT_MODE);
7184 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7185 }
7186 }
7187}
7188
Linus Torvalds1da177e2005-04-16 15:20:36 -07007189static void tg3_stop_fw(struct tg3 *);
7190
7191/* tp->lock is held. */
7192static int tg3_chip_reset(struct tg3 *tp)
7193{
7194 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007195 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007196 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197
David S. Millerf49639e2006-06-09 11:58:36 -07007198 tg3_nvram_lock(tp);
7199
Matt Carlson77b483f2008-08-15 14:07:24 -07007200 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7201
David S. Millerf49639e2006-06-09 11:58:36 -07007202 /* No matching tg3_nvram_unlock() after this because
7203 * chip reset below will undo the nvram lock.
7204 */
7205 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007206
Michael Chanee6a99b2007-07-18 21:49:10 -07007207 /* GRC_MISC_CFG core clock reset will clear the memory
7208 * enable bit in PCI register 4 and the MSI enable bit
7209 * on some chips, so we save relevant registers here.
7210 */
7211 tg3_save_pci_state(tp);
7212
Michael Chand9ab5ad2006-03-20 22:27:35 -08007213 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08007214 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08007215 tw32(GRC_FASTBOOT_PC, 0);
7216
Linus Torvalds1da177e2005-04-16 15:20:36 -07007217 /*
7218 * We must avoid the readl() that normally takes place.
7219 * It locks machines, causes machine checks, and other
7220 * fun things. So, temporarily disable the 5701
7221 * hardware workaround, while we do the reset.
7222 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007223 write_op = tp->write32;
7224 if (write_op == tg3_write_flush_reg32)
7225 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007226
Michael Chand18edcb2007-03-24 20:57:11 -07007227 /* Prevent the irq handler from reading or writing PCI registers
7228 * during chip reset when the memory enable bit in the PCI command
7229 * register may be cleared. The chip does not generate interrupt
7230 * at this time, but the irq handler may still be called due to irq
7231 * sharing or irqpoll.
7232 */
7233 tp->tg3_flags |= TG3_FLAG_CHIP_RESETTING;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007234 for (i = 0; i < tp->irq_cnt; i++) {
7235 struct tg3_napi *tnapi = &tp->napi[i];
7236 if (tnapi->hw_status) {
7237 tnapi->hw_status->status = 0;
7238 tnapi->hw_status->status_tag = 0;
7239 }
7240 tnapi->last_tag = 0;
7241 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007242 }
Michael Chand18edcb2007-03-24 20:57:11 -07007243 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007244
7245 for (i = 0; i < tp->irq_cnt; i++)
7246 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007247
Matt Carlson255ca312009-08-25 10:07:27 +00007248 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7249 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7250 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7251 }
7252
Linus Torvalds1da177e2005-04-16 15:20:36 -07007253 /* do the reset */
7254 val = GRC_MISC_CFG_CORECLK_RESET;
7255
7256 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
Matt Carlson88075d92010-08-02 11:25:58 +00007257 /* Force PCIe 1.0a mode */
7258 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlson1407deb2011-04-05 14:22:44 +00007259 !(tp->tg3_flags3 & TG3_FLG3_57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00007260 tr32(TG3_PCIE_PHY_TSTCTL) ==
7261 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7262 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7263
Linus Torvalds1da177e2005-04-16 15:20:36 -07007264 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7265 tw32(GRC_MISC_CFG, (1 << 29));
7266 val |= (1 << 29);
7267 }
7268 }
7269
Michael Chanb5d37722006-09-27 16:06:21 -07007270 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7271 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7272 tw32(GRC_VCPU_EXT_CTRL,
7273 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7274 }
7275
Matt Carlsonf37500d2010-08-02 11:25:59 +00007276 /* Manage gphy power for all CPMU absent PCIe devices. */
7277 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
7278 !(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007279 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007280
Linus Torvalds1da177e2005-04-16 15:20:36 -07007281 tw32(GRC_MISC_CFG, val);
7282
Michael Chan1ee582d2005-08-09 20:16:46 -07007283 /* restore 5701 hardware bug workaround write method */
7284 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007285
7286 /* Unfortunately, we have to delay before the PCI read back.
7287 * Some 575X chips even will not respond to a PCI cfg access
7288 * when the reset command is given to the chip.
7289 *
7290 * How do these hardware designers expect things to work
7291 * properly if the PCI write is posted for a long period
7292 * of time? It is always necessary to have some method by
7293 * which a register read back can occur to push the write
7294 * out which does the reset.
7295 *
7296 * For most tg3 variants the trick below was working.
7297 * Ho hum...
7298 */
7299 udelay(120);
7300
7301 /* Flush PCI posted writes. The normal MMIO registers
7302 * are inaccessible at this time so this is the only
7303 * way to make this reliably (actually, this is no longer
7304 * the case, see above). I tried to use indirect
7305 * register read/write but this upset some 5701 variants.
7306 */
7307 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7308
7309 udelay(120);
7310
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007311 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) && tp->pcie_cap) {
Matt Carlsone7126992009-08-25 10:08:16 +00007312 u16 val16;
7313
Linus Torvalds1da177e2005-04-16 15:20:36 -07007314 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7315 int i;
7316 u32 cfg_val;
7317
7318 /* Wait for link training to complete. */
7319 for (i = 0; i < 5000; i++)
7320 udelay(100);
7321
7322 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7323 pci_write_config_dword(tp->pdev, 0xc4,
7324 cfg_val | (1 << 15));
7325 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007326
Matt Carlsone7126992009-08-25 10:08:16 +00007327 /* Clear the "no snoop" and "relaxed ordering" bits. */
7328 pci_read_config_word(tp->pdev,
7329 tp->pcie_cap + PCI_EXP_DEVCTL,
7330 &val16);
7331 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7332 PCI_EXP_DEVCTL_NOSNOOP_EN);
7333 /*
7334 * Older PCIe devices only support the 128 byte
7335 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007336 */
Matt Carlson6de34cb2010-08-02 11:25:55 +00007337 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007338 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007339 pci_write_config_word(tp->pdev,
7340 tp->pcie_cap + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007341 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007342
Matt Carlsoncf790032010-11-24 08:31:48 +00007343 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007344
7345 /* Clear error status */
7346 pci_write_config_word(tp->pdev,
7347 tp->pcie_cap + PCI_EXP_DEVSTA,
7348 PCI_EXP_DEVSTA_CED |
7349 PCI_EXP_DEVSTA_NFED |
7350 PCI_EXP_DEVSTA_FED |
7351 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007352 }
7353
Michael Chanee6a99b2007-07-18 21:49:10 -07007354 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355
Matt Carlsone64de4e2011-04-13 11:05:05 +00007356 tp->tg3_flags &= ~(TG3_FLAG_CHIP_RESETTING |
7357 TG3_FLAG_ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07007358
Michael Chanee6a99b2007-07-18 21:49:10 -07007359 val = 0;
7360 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chan4cf78e42005-07-25 12:29:19 -07007361 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007362 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007363
7364 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7365 tg3_stop_fw(tp);
7366 tw32(0x5000, 0x400);
7367 }
7368
7369 tw32(GRC_MODE, tp->grc_mode);
7370
7371 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007372 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007373
7374 tw32(0xc4, val | (1 << 15));
7375 }
7376
7377 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7378 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7379 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7380 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7381 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7382 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7383 }
7384
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007385 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
7386 tp->mac_mode = MAC_MODE_APE_TX_EN |
7387 MAC_MODE_APE_RX_EN |
7388 MAC_MODE_TDE_ENABLE;
7389
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007390 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007391 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
7392 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007393 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007394 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7395 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007396 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007397 val = 0;
7398
7399 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007400 udelay(40);
7401
Matt Carlson77b483f2008-08-15 14:07:24 -07007402 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7403
Michael Chan7a6f4362006-09-27 16:03:31 -07007404 err = tg3_poll_fw(tp);
7405 if (err)
7406 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007407
Matt Carlson0a9140c2009-08-28 12:27:50 +00007408 tg3_mdio_start(tp);
7409
Linus Torvalds1da177e2005-04-16 15:20:36 -07007410 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007411 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7412 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlson1407deb2011-04-05 14:22:44 +00007413 !(tp->tg3_flags3 & TG3_FLG3_57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007414 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007415
7416 tw32(0x7c00, val | (1 << 25));
7417 }
7418
Matt Carlsond78b59f2011-04-05 14:22:46 +00007419 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
7420 val = tr32(TG3_CPMU_CLCK_ORIDE);
7421 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
7422 }
7423
Linus Torvalds1da177e2005-04-16 15:20:36 -07007424 /* Reprobe ASF enable state. */
7425 tp->tg3_flags &= ~TG3_FLAG_ENABLE_ASF;
7426 tp->tg3_flags2 &= ~TG3_FLG2_ASF_NEW_HANDSHAKE;
7427 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7428 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7429 u32 nic_cfg;
7430
7431 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7432 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
7433 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
Matt Carlson4ba526c2008-08-15 14:10:04 -07007434 tp->last_event_jiffies = jiffies;
John W. Linvillecbf46852005-04-21 17:01:29 -07007435 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007436 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
7437 }
7438 }
7439
7440 return 0;
7441}
7442
7443/* tp->lock is held. */
7444static void tg3_stop_fw(struct tg3 *tp)
7445{
Matt Carlson0d3031d2007-10-10 18:02:43 -07007446 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
7447 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07007448 /* Wait for RX cpu to ACK the previous event. */
7449 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007450
7451 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007452
7453 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007454
Matt Carlson7c5026a2008-05-02 16:49:29 -07007455 /* Wait for RX cpu to ACK this event. */
7456 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007457 }
7458}
7459
7460/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007461static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007462{
7463 int err;
7464
7465 tg3_stop_fw(tp);
7466
Michael Chan944d9802005-05-29 14:57:48 -07007467 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007468
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007469 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007470 err = tg3_chip_reset(tp);
7471
Matt Carlsondaba2a62009-04-20 06:58:52 +00007472 __tg3_set_mac_addr(tp, 0);
7473
Michael Chan944d9802005-05-29 14:57:48 -07007474 tg3_write_sig_legacy(tp, kind);
7475 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007476
7477 if (err)
7478 return err;
7479
7480 return 0;
7481}
7482
Linus Torvalds1da177e2005-04-16 15:20:36 -07007483#define RX_CPU_SCRATCH_BASE 0x30000
7484#define RX_CPU_SCRATCH_SIZE 0x04000
7485#define TX_CPU_SCRATCH_BASE 0x34000
7486#define TX_CPU_SCRATCH_SIZE 0x04000
7487
7488/* tp->lock is held. */
7489static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
7490{
7491 int i;
7492
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +02007493 BUG_ON(offset == TX_CPU_BASE &&
7494 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007495
Michael Chanb5d37722006-09-27 16:06:21 -07007496 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7497 u32 val = tr32(GRC_VCPU_EXT_CTRL);
7498
7499 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
7500 return 0;
7501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007502 if (offset == RX_CPU_BASE) {
7503 for (i = 0; i < 10000; i++) {
7504 tw32(offset + CPU_STATE, 0xffffffff);
7505 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7506 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7507 break;
7508 }
7509
7510 tw32(offset + CPU_STATE, 0xffffffff);
7511 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
7512 udelay(10);
7513 } else {
7514 for (i = 0; i < 10000; i++) {
7515 tw32(offset + CPU_STATE, 0xffffffff);
7516 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7517 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7518 break;
7519 }
7520 }
7521
7522 if (i >= 10000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007523 netdev_err(tp->dev, "%s timed out, %s CPU\n",
7524 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007525 return -ENODEV;
7526 }
Michael Chanec41c7d2006-01-17 02:40:55 -08007527
7528 /* Clear firmware's nvram arbitration. */
7529 if (tp->tg3_flags & TG3_FLAG_NVRAM)
7530 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007531 return 0;
7532}
7533
7534struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007535 unsigned int fw_base;
7536 unsigned int fw_len;
7537 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007538};
7539
7540/* tp->lock is held. */
7541static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
7542 int cpu_scratch_size, struct fw_info *info)
7543{
Michael Chanec41c7d2006-01-17 02:40:55 -08007544 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007545 void (*write_op)(struct tg3 *, u32, u32);
7546
7547 if (cpu_base == TX_CPU_BASE &&
7548 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007549 netdev_err(tp->dev,
7550 "%s: Trying to load TX cpu firmware which is 5705\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007551 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007552 return -EINVAL;
7553 }
7554
7555 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
7556 write_op = tg3_write_mem;
7557 else
7558 write_op = tg3_write_indirect_reg32;
7559
Michael Chan1b628152005-05-29 14:59:49 -07007560 /* It is possible that bootcode is still loading at this point.
7561 * Get the nvram lock first before halting the cpu.
7562 */
Michael Chanec41c7d2006-01-17 02:40:55 -08007563 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007564 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08007565 if (!lock_err)
7566 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007567 if (err)
7568 goto out;
7569
7570 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
7571 write_op(tp, cpu_scratch_base + i, 0);
7572 tw32(cpu_base + CPU_STATE, 0xffffffff);
7573 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007574 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007575 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007576 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07007577 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007578 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007579
7580 err = 0;
7581
7582out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007583 return err;
7584}
7585
7586/* tp->lock is held. */
7587static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
7588{
7589 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007590 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007591 int err, i;
7592
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007593 fw_data = (void *)tp->fw->data;
7594
7595 /* Firmware blob starts with version numbers, followed by
7596 start address and length. We are setting complete length.
7597 length = end_address_of_bss - start_address_of_text.
7598 Remainder is the blob to be loaded contiguously
7599 from start address. */
7600
7601 info.fw_base = be32_to_cpu(fw_data[1]);
7602 info.fw_len = tp->fw->size - 12;
7603 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007604
7605 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
7606 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
7607 &info);
7608 if (err)
7609 return err;
7610
7611 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
7612 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
7613 &info);
7614 if (err)
7615 return err;
7616
7617 /* Now startup only the RX cpu. */
7618 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007619 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007620
7621 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007622 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007623 break;
7624 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7625 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007626 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007627 udelay(1000);
7628 }
7629 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007630 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
7631 "should be %08x\n", __func__,
Joe Perches05dbe002010-02-17 19:44:19 +00007632 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007633 return -ENODEV;
7634 }
7635 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7636 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
7637
7638 return 0;
7639}
7640
Linus Torvalds1da177e2005-04-16 15:20:36 -07007641/* 5705 needs a special version of the TSO firmware. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007642
7643/* tp->lock is held. */
7644static int tg3_load_tso_firmware(struct tg3 *tp)
7645{
7646 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007647 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007648 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
7649 int err, i;
7650
7651 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
7652 return 0;
7653
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007654 fw_data = (void *)tp->fw->data;
7655
7656 /* Firmware blob starts with version numbers, followed by
7657 start address and length. We are setting complete length.
7658 length = end_address_of_bss - start_address_of_text.
7659 Remainder is the blob to be loaded contiguously
7660 from start address. */
7661
7662 info.fw_base = be32_to_cpu(fw_data[1]);
7663 cpu_scratch_size = tp->fw_len;
7664 info.fw_len = tp->fw->size - 12;
7665 info.fw_data = &fw_data[3];
7666
Linus Torvalds1da177e2005-04-16 15:20:36 -07007667 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007668 cpu_base = RX_CPU_BASE;
7669 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007670 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007671 cpu_base = TX_CPU_BASE;
7672 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
7673 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
7674 }
7675
7676 err = tg3_load_firmware_cpu(tp, cpu_base,
7677 cpu_scratch_base, cpu_scratch_size,
7678 &info);
7679 if (err)
7680 return err;
7681
7682 /* Now startup the cpu. */
7683 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007684 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007685
7686 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007687 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007688 break;
7689 tw32(cpu_base + CPU_STATE, 0xffffffff);
7690 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007691 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007692 udelay(1000);
7693 }
7694 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007695 netdev_err(tp->dev,
7696 "%s fails to set CPU PC, is %08x should be %08x\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007697 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007698 return -ENODEV;
7699 }
7700 tw32(cpu_base + CPU_STATE, 0xffffffff);
7701 tw32_f(cpu_base + CPU_MODE, 0x00000000);
7702 return 0;
7703}
7704
Linus Torvalds1da177e2005-04-16 15:20:36 -07007705
Linus Torvalds1da177e2005-04-16 15:20:36 -07007706static int tg3_set_mac_addr(struct net_device *dev, void *p)
7707{
7708 struct tg3 *tp = netdev_priv(dev);
7709 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007710 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007711
Michael Chanf9804dd2005-09-27 12:13:10 -07007712 if (!is_valid_ether_addr(addr->sa_data))
7713 return -EINVAL;
7714
Linus Torvalds1da177e2005-04-16 15:20:36 -07007715 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7716
Michael Chane75f7c92006-03-20 21:33:26 -08007717 if (!netif_running(dev))
7718 return 0;
7719
Michael Chan58712ef2006-04-29 18:58:01 -07007720 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007721 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007722
Michael Chan986e0ae2007-05-05 12:10:20 -07007723 addr0_high = tr32(MAC_ADDR_0_HIGH);
7724 addr0_low = tr32(MAC_ADDR_0_LOW);
7725 addr1_high = tr32(MAC_ADDR_1_HIGH);
7726 addr1_low = tr32(MAC_ADDR_1_LOW);
7727
7728 /* Skip MAC addr 1 if ASF is using it. */
7729 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7730 !(addr1_high == 0 && addr1_low == 0))
7731 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007732 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007733 spin_lock_bh(&tp->lock);
7734 __tg3_set_mac_addr(tp, skip_mac_1);
7735 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007736
Michael Chanb9ec6c12006-07-25 16:37:27 -07007737 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007738}
7739
7740/* tp->lock is held. */
7741static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7742 dma_addr_t mapping, u32 maxlen_flags,
7743 u32 nic_addr)
7744{
7745 tg3_write_mem(tp,
7746 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7747 ((u64) mapping >> 32));
7748 tg3_write_mem(tp,
7749 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7750 ((u64) mapping & 0xffffffff));
7751 tg3_write_mem(tp,
7752 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7753 maxlen_flags);
7754
7755 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7756 tg3_write_mem(tp,
7757 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7758 nic_addr);
7759}
7760
7761static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007762static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007763{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007764 int i;
7765
Matt Carlson19cfaec2009-12-03 08:36:20 +00007766 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007767 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7768 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7769 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007770 } else {
7771 tw32(HOSTCC_TXCOL_TICKS, 0);
7772 tw32(HOSTCC_TXMAX_FRAMES, 0);
7773 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007774 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007775
Matt Carlson20d73752010-07-11 09:31:41 +00007776 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007777 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
7778 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
7779 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
7780 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007781 tw32(HOSTCC_RXCOL_TICKS, 0);
7782 tw32(HOSTCC_RXMAX_FRAMES, 0);
7783 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07007784 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007785
David S. Miller15f98502005-05-18 22:49:26 -07007786 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7787 u32 val = ec->stats_block_coalesce_usecs;
7788
Matt Carlsonb6080e12009-09-01 13:12:00 +00007789 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
7790 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
7791
David S. Miller15f98502005-05-18 22:49:26 -07007792 if (!netif_carrier_ok(tp->dev))
7793 val = 0;
7794
7795 tw32(HOSTCC_STAT_COAL_TICKS, val);
7796 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007797
7798 for (i = 0; i < tp->irq_cnt - 1; i++) {
7799 u32 reg;
7800
7801 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
7802 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007803 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
7804 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007805 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
7806 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007807
7808 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7809 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
7810 tw32(reg, ec->tx_coalesce_usecs);
7811 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
7812 tw32(reg, ec->tx_max_coalesced_frames);
7813 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
7814 tw32(reg, ec->tx_max_coalesced_frames_irq);
7815 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007816 }
7817
7818 for (; i < tp->irq_max - 1; i++) {
7819 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007820 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007821 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007822
7823 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7824 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
7825 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
7826 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7827 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007828 }
David S. Miller15f98502005-05-18 22:49:26 -07007829}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007830
7831/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00007832static void tg3_rings_reset(struct tg3 *tp)
7833{
7834 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007835 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007836 struct tg3_napi *tnapi = &tp->napi[0];
7837
7838 /* Disable all transmit rings but the first. */
7839 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7840 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlson0a58d662011-04-05 14:22:45 +00007841 else if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlson3d377282010-10-14 10:37:39 +00007842 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlsonb703df62009-12-03 08:36:21 +00007843 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
7844 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007845 else
7846 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7847
7848 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7849 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
7850 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
7851 BDINFO_FLAGS_DISABLED);
7852
7853
7854 /* Disable all receive return rings but the first. */
Matt Carlson0a58d662011-04-05 14:22:45 +00007855 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007856 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
7857 else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007858 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007859 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7860 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00007861 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
7862 else
7863 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7864
7865 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7866 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
7867 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
7868 BDINFO_FLAGS_DISABLED);
7869
7870 /* Disable interrupts */
7871 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
7872
7873 /* Zero mailbox registers. */
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007874 if (tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00007875 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007876 tp->napi[i].tx_prod = 0;
7877 tp->napi[i].tx_cons = 0;
Matt Carlsonc2353a32010-01-20 16:58:08 +00007878 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
7879 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007880 tw32_rx_mbox(tp->napi[i].consmbox, 0);
7881 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
7882 }
Matt Carlsonc2353a32010-01-20 16:58:08 +00007883 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))
7884 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007885 } else {
7886 tp->napi[0].tx_prod = 0;
7887 tp->napi[0].tx_cons = 0;
7888 tw32_mailbox(tp->napi[0].prodmbox, 0);
7889 tw32_rx_mbox(tp->napi[0].consmbox, 0);
7890 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007891
7892 /* Make sure the NIC-based send BD rings are disabled. */
7893 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7894 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
7895 for (i = 0; i < 16; i++)
7896 tw32_tx_mbox(mbox + i * 8, 0);
7897 }
7898
7899 txrcb = NIC_SRAM_SEND_RCB;
7900 rxrcb = NIC_SRAM_RCV_RET_RCB;
7901
7902 /* Clear status block in ram. */
7903 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7904
7905 /* Set status block DMA address */
7906 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7907 ((u64) tnapi->status_mapping >> 32));
7908 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7909 ((u64) tnapi->status_mapping & 0xffffffff));
7910
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007911 if (tnapi->tx_ring) {
7912 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7913 (TG3_TX_RING_SIZE <<
7914 BDINFO_FLAGS_MAXLEN_SHIFT),
7915 NIC_SRAM_TX_BUFFER_DESC);
7916 txrcb += TG3_BDINFO_SIZE;
7917 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007918
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007919 if (tnapi->rx_rcb) {
7920 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007921 (tp->rx_ret_ring_mask + 1) <<
7922 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007923 rxrcb += TG3_BDINFO_SIZE;
7924 }
7925
7926 stblk = HOSTCC_STATBLCK_RING1;
7927
7928 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
7929 u64 mapping = (u64)tnapi->status_mapping;
7930 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
7931 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
7932
7933 /* Clear status block in ram. */
7934 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7935
Matt Carlson19cfaec2009-12-03 08:36:20 +00007936 if (tnapi->tx_ring) {
7937 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7938 (TG3_TX_RING_SIZE <<
7939 BDINFO_FLAGS_MAXLEN_SHIFT),
7940 NIC_SRAM_TX_BUFFER_DESC);
7941 txrcb += TG3_BDINFO_SIZE;
7942 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007943
7944 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007945 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007946 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7947
7948 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007949 rxrcb += TG3_BDINFO_SIZE;
7950 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007951}
7952
7953/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007954static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007955{
7956 u32 val, rdmac_mode;
7957 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00007958 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007959
7960 tg3_disable_ints(tp);
7961
7962 tg3_stop_fw(tp);
7963
7964 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
7965
Matt Carlson859a5882010-04-05 10:19:28 +00007966 if (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)
Michael Chane6de8ad2005-05-05 14:42:41 -07007967 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007968
Matt Carlson699c0192010-12-06 08:28:51 +00007969 /* Enable MAC control of LPI */
7970 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
7971 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
7972 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
7973 TG3_CPMU_EEE_LNKIDL_UART_IDL);
7974
7975 tw32_f(TG3_CPMU_EEE_CTRL,
7976 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
7977
Matt Carlsona386b902010-12-06 08:28:53 +00007978 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
7979 TG3_CPMU_EEEMD_LPI_IN_TX |
7980 TG3_CPMU_EEEMD_LPI_IN_RX |
7981 TG3_CPMU_EEEMD_EEE_ENABLE;
7982
7983 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
7984 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
7985
7986 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
7987 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
7988
7989 tw32_f(TG3_CPMU_EEE_MODE, val);
7990
7991 tw32_f(TG3_CPMU_EEE_DBTMR1,
7992 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
7993 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
7994
7995 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00007996 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00007997 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00007998 }
7999
Matt Carlson603f1172010-02-12 14:47:10 +00008000 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08008001 tg3_phy_reset(tp);
8002
Linus Torvalds1da177e2005-04-16 15:20:36 -07008003 err = tg3_chip_reset(tp);
8004 if (err)
8005 return err;
8006
8007 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
8008
Matt Carlsonbcb37f62008-11-03 16:52:09 -08008009 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008010 val = tr32(TG3_CPMU_CTRL);
8011 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
8012 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08008013
8014 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8015 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8016 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8017 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
8018
8019 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
8020 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
8021 val |= CPMU_LNK_AWARE_MACCLK_6_25;
8022 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
8023
8024 val = tr32(TG3_CPMU_HST_ACC);
8025 val &= ~CPMU_HST_ACC_MACCLK_MASK;
8026 val |= CPMU_HST_ACC_MACCLK_6_25;
8027 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07008028 }
8029
Matt Carlson33466d92009-04-20 06:57:41 +00008030 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
8031 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
8032 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
8033 PCIE_PWR_MGMT_L1_THRESH_4MS;
8034 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00008035
8036 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
8037 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
8038
8039 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00008040
Matt Carlsonf40386c2009-11-02 14:24:02 +00008041 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8042 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00008043 }
8044
Matt Carlson614b05902010-01-20 16:58:02 +00008045 if (tp->tg3_flags3 & TG3_FLG3_L1PLLPD_EN) {
8046 u32 grc_mode = tr32(GRC_MODE);
8047
8048 /* Access the lower 1K of PL PCIE block registers. */
8049 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8050 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
8051
8052 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
8053 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
8054 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
8055
8056 tw32(GRC_MODE, grc_mode);
8057 }
8058
Matt Carlson5093eed2010-11-24 08:31:45 +00008059 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
8060 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
8061 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00008062
Matt Carlson5093eed2010-11-24 08:31:45 +00008063 /* Access the lower 1K of PL PCIE block registers. */
8064 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8065 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00008066
Matt Carlson5093eed2010-11-24 08:31:45 +00008067 val = tr32(TG3_PCIE_TLDLPL_PORT +
8068 TG3_PCIE_PL_LO_PHYCTL5);
8069 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
8070 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00008071
Matt Carlson5093eed2010-11-24 08:31:45 +00008072 tw32(GRC_MODE, grc_mode);
8073 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00008074
8075 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8076 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8077 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8078 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008079 }
8080
Linus Torvalds1da177e2005-04-16 15:20:36 -07008081 /* This works around an issue with Athlon chipsets on
8082 * B3 tigon3 silicon. This bit has no effect on any
8083 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008084 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008085 */
Matt Carlson795d01c2007-10-07 23:28:17 -07008086 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)) {
8087 if (!(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
8088 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8089 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008091
8092 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
8093 (tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
8094 val = tr32(TG3PCI_PCISTATE);
8095 val |= PCISTATE_RETRY_SAME_DMA;
8096 tw32(TG3PCI_PCISTATE, val);
8097 }
8098
Matt Carlson0d3031d2007-10-10 18:02:43 -07008099 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
8100 /* Allow reads and writes to the
8101 * APE register and memory space.
8102 */
8103 val = tr32(TG3PCI_PCISTATE);
8104 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +00008105 PCISTATE_ALLOW_APE_SHMEM_WR |
8106 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008107 tw32(TG3PCI_PCISTATE, val);
8108 }
8109
Linus Torvalds1da177e2005-04-16 15:20:36 -07008110 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8111 /* Enable some hw fixes. */
8112 val = tr32(TG3PCI_MSI_DATA);
8113 val |= (1 << 26) | (1 << 28) | (1 << 29);
8114 tw32(TG3PCI_MSI_DATA, val);
8115 }
8116
8117 /* Descriptor ring init may make accesses to the
8118 * NIC SRAM area to setup the TX descriptors, so we
8119 * can only do this after the hardware has been
8120 * successfully reset.
8121 */
Michael Chan32d8c572006-07-25 16:38:29 -07008122 err = tg3_init_rings(tp);
8123 if (err)
8124 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008125
Matt Carlson1407deb2011-04-05 14:22:44 +00008126 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008127 val = tr32(TG3PCI_DMA_RW_CTRL) &
8128 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008129 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8130 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008131 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8132 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8133 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008134 /* This value is determined during the probe time DMA
8135 * engine test, tg3_test_dma.
8136 */
8137 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008139
8140 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8141 GRC_MODE_4X_NIC_SEND_RINGS |
8142 GRC_MODE_NO_TX_PHDR_CSUM |
8143 GRC_MODE_NO_RX_PHDR_CSUM);
8144 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07008145
8146 /* Pseudo-header checksum is done by hardware logic and not
8147 * the offload processers, so make the chip do the pseudo-
8148 * header checksums on receive. For transmit it is more
8149 * convenient to do the pseudo-header checksum in software
8150 * as Linux does that on transmit for us in all cases.
8151 */
8152 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008153
8154 tw32(GRC_MODE,
8155 tp->grc_mode |
8156 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8157
8158 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8159 val = tr32(GRC_MISC_CFG);
8160 val &= ~0xff;
8161 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8162 tw32(GRC_MISC_CFG, val);
8163
8164 /* Initialize MBUF/DESC pool. */
John W. Linvillecbf46852005-04-21 17:01:29 -07008165 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008166 /* Do nothing. */
8167 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8168 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8169 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8170 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8171 else
8172 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8173 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8174 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Matt Carlson859a5882010-04-05 10:19:28 +00008175 } else if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008176 int fw_len;
8177
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008178 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008179 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8180 tw32(BUFMGR_MB_POOL_ADDR,
8181 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8182 tw32(BUFMGR_MB_POOL_SIZE,
8183 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008185
Michael Chan0f893dc2005-07-25 12:30:38 -07008186 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008187 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8188 tp->bufmgr_config.mbuf_read_dma_low_water);
8189 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8190 tp->bufmgr_config.mbuf_mac_rx_low_water);
8191 tw32(BUFMGR_MB_HIGH_WATER,
8192 tp->bufmgr_config.mbuf_high_water);
8193 } else {
8194 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8195 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8196 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8197 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8198 tw32(BUFMGR_MB_HIGH_WATER,
8199 tp->bufmgr_config.mbuf_high_water_jumbo);
8200 }
8201 tw32(BUFMGR_DMA_LOW_WATER,
8202 tp->bufmgr_config.dma_low_water);
8203 tw32(BUFMGR_DMA_HIGH_WATER,
8204 tp->bufmgr_config.dma_high_water);
8205
Matt Carlsond309a462010-09-30 10:34:31 +00008206 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8207 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8208 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
8209 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008210 for (i = 0; i < 2000; i++) {
8211 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8212 break;
8213 udelay(10);
8214 }
8215 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008216 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008217 return -ENODEV;
8218 }
8219
8220 /* Setup replenish threshold. */
Michael Chanf92905d2006-06-29 20:14:29 -07008221 val = tp->rx_pending / 8;
8222 if (val == 0)
8223 val = 1;
8224 else if (val > tp->rx_std_max_post)
8225 val = tp->rx_std_max_post;
Michael Chanb5d37722006-09-27 16:06:21 -07008226 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
8227 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8228 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
8229
8230 if (val > (TG3_RX_INTERNAL_RING_SZ_5906 / 2))
8231 val = TG3_RX_INTERNAL_RING_SZ_5906 / 2;
8232 }
Michael Chanf92905d2006-06-29 20:14:29 -07008233
8234 tw32(RCVBDI_STD_THRESH, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008235
8236 /* Initialize TG3_BDINFO's at:
8237 * RCVDBDI_STD_BD: standard eth size rx ring
8238 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8239 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8240 *
8241 * like so:
8242 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8243 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8244 * ring attribute flags
8245 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8246 *
8247 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8248 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8249 *
8250 * The size of each ring is fixed in the firmware, but the location is
8251 * configurable.
8252 */
8253 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008254 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008255 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008256 ((u64) tpr->rx_std_mapping & 0xffffffff));
Matt Carlson0a58d662011-04-05 14:22:45 +00008257 if (!(tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00008258 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8259 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008260
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008261 /* Disable the mini ring */
8262 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008263 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8264 BDINFO_FLAGS_DISABLED);
8265
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008266 /* Program the jumbo buffer descriptor ring control
8267 * blocks on those devices that have them.
8268 */
Matt Carlsonbb18bb92011-03-09 16:58:19 +00008269 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson4d163b72011-01-25 15:58:48 +00008270 ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
8271 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008272 /* Setup replenish threshold. */
8273 tw32(RCVBDI_JUMBO_THRESH, tp->rx_jumbo_pending / 8);
8274
Michael Chan0f893dc2005-07-25 12:30:38 -07008275 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008276 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008277 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008278 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008279 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008280 val = TG3_RX_JMB_RING_SIZE(tp) <<
8281 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008282 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008283 val | BDINFO_FLAGS_USE_EXT_RECV);
Matt Carlsona50d0792010-06-05 17:24:37 +00008284 if (!(tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) ||
8285 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson87668d32009-11-13 13:03:34 +00008286 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8287 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008288 } else {
8289 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8290 BDINFO_FLAGS_DISABLED);
8291 }
8292
Matt Carlson1407deb2011-04-05 14:22:44 +00008293 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008294 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsonde9f5232011-04-05 14:22:43 +00008295 val = TG3_RX_STD_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008296 else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008297 val = TG3_RX_STD_MAX_SIZE_5717;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008298 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8299 val |= (TG3_RX_STD_DMA_SZ << 2);
8300 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008301 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008302 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008303 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008304
8305 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008306
Matt Carlson411da642009-11-13 13:03:46 +00008307 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00008308 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008309
Matt Carlson411da642009-11-13 13:03:46 +00008310 tpr->rx_jmb_prod_idx = (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) ?
Matt Carlson21f581a2009-08-28 14:00:25 +00008311 tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00008312 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008313
Matt Carlson1407deb2011-04-05 14:22:44 +00008314 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008315 tw32(STD_REPLENISH_LWM, 32);
8316 tw32(JMB_REPLENISH_LWM, 16);
8317 }
8318
Matt Carlson2d31eca2009-09-01 12:53:31 +00008319 tg3_rings_reset(tp);
8320
Linus Torvalds1da177e2005-04-16 15:20:36 -07008321 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008322 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008323
8324 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008325 tw32(MAC_RX_MTU_SIZE,
8326 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008327
8328 /* The slot time is changed by tg3_setup_phy if we
8329 * run at gigabit with half duplex.
8330 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00008331 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8332 (6 << TX_LENGTHS_IPG_SHIFT) |
8333 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8334
8335 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8336 val |= tr32(MAC_TX_LENGTHS) &
8337 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8338 TX_LENGTHS_CNT_DWN_VAL_MSK);
8339
8340 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008341
8342 /* Receive rules. */
8343 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8344 tw32(RCVLPC_CONFIG, 0x0181);
8345
8346 /* Calculate RDMAC_MODE setting early, we need it to determine
8347 * the RCVLPC_STATE_ENABLE mask.
8348 */
8349 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8350 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8351 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8352 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8353 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008354
Matt Carlsondeabaac2010-11-24 08:31:50 +00008355 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008356 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8357
Matt Carlson57e69832008-05-25 23:48:31 -07008358 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008359 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8360 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008361 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8362 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8363 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8364
Matt Carlsonc5908932011-03-09 16:58:25 +00008365 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8366 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008367 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008368 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008369 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8370 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
8371 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
8372 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8373 }
8374 }
8375
Michael Chan85e94ce2005-04-21 17:05:28 -07008376 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
8377 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8378
Linus Torvalds1da177e2005-04-16 15:20:36 -07008379 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlson027455a2008-12-21 20:19:30 -08008380 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8381
Matt Carlsone849cdc2009-11-13 13:03:38 +00008382 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
8383 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008384 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8385 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008386
Matt Carlsonf2096f92011-04-05 14:22:48 +00008387 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8388 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8389
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008390 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8391 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8392 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8393 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson1407deb2011-04-05 14:22:44 +00008394 (tp->tg3_flags3 & TG3_FLG3_57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008395 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00008396 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8397 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008398 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8399 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8400 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8401 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8402 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8403 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008404 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008405 tw32(TG3_RDMA_RSRVCTRL_REG,
8406 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8407 }
8408
Matt Carlsond78b59f2011-04-05 14:22:46 +00008409 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8410 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00008411 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8412 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8413 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8414 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8415 }
8416
Linus Torvalds1da177e2005-04-16 15:20:36 -07008417 /* Receive/send statistics. */
Michael Chan16613942006-06-29 20:15:13 -07008418 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
8419 val = tr32(RCVLPC_STATS_ENABLE);
8420 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8421 tw32(RCVLPC_STATS_ENABLE, val);
8422 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
8423 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008424 val = tr32(RCVLPC_STATS_ENABLE);
8425 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8426 tw32(RCVLPC_STATS_ENABLE, val);
8427 } else {
8428 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8429 }
8430 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8431 tw32(SNDDATAI_STATSENAB, 0xffffff);
8432 tw32(SNDDATAI_STATSCTRL,
8433 (SNDDATAI_SCTRL_ENABLE |
8434 SNDDATAI_SCTRL_FASTUPD));
8435
8436 /* Setup host coalescing engine. */
8437 tw32(HOSTCC_MODE, 0);
8438 for (i = 0; i < 2000; i++) {
8439 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8440 break;
8441 udelay(10);
8442 }
8443
Michael Chand244c892005-07-05 14:42:33 -07008444 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008445
Linus Torvalds1da177e2005-04-16 15:20:36 -07008446 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
8447 /* Status/statistics block address. See tg3_timer,
8448 * the tg3_periodic_fetch_stats call there, and
8449 * tg3_get_stats to see how this works for 5705/5750 chips.
8450 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008451 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8452 ((u64) tp->stats_mapping >> 32));
8453 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8454 ((u64) tp->stats_mapping & 0xffffffff));
8455 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008456
Linus Torvalds1da177e2005-04-16 15:20:36 -07008457 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008458
8459 /* Clear statistics and status block memory areas */
8460 for (i = NIC_SRAM_STATS_BLK;
8461 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8462 i += sizeof(u32)) {
8463 tg3_write_mem(tp, i, 0);
8464 udelay(40);
8465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008466 }
8467
8468 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8469
8470 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8471 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
8472 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8473 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8474
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008475 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8476 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07008477 /* reset to prevent losing 1st rx packet intermittently */
8478 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8479 udelay(10);
8480 }
8481
Matt Carlson3bda1252008-08-15 14:08:22 -07008482 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008483 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -07008484 else
8485 tp->mac_mode = 0;
8486 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07008487 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008488 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008489 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008490 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8491 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008492 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8493 udelay(40);
8494
Michael Chan314fba32005-04-21 17:07:04 -07008495 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Michael Chan9d26e212006-12-07 00:21:14 -08008496 * If TG3_FLG2_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008497 * register to preserve the GPIO settings for LOMs. The GPIOs,
8498 * whether used as inputs or outputs, are set by boot code after
8499 * reset.
8500 */
Michael Chan9d26e212006-12-07 00:21:14 -08008501 if (!(tp->tg3_flags2 & TG3_FLG2_IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008502 u32 gpio_mask;
8503
Michael Chan9d26e212006-12-07 00:21:14 -08008504 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8505 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8506 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008507
8508 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8509 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8510 GRC_LCLCTRL_GPIO_OUTPUT3;
8511
Michael Chanaf36e6b2006-03-23 01:28:06 -08008512 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8513 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8514
Gary Zambranoaaf84462007-05-05 11:51:45 -07008515 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008516 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8517
8518 /* GPIO1 must be driven high for eeprom write protect */
Michael Chan9d26e212006-12-07 00:21:14 -08008519 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT)
8520 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8521 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008523 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8524 udelay(100);
8525
Matt Carlson0583d522011-01-25 15:58:50 +00008526 if ((tp->tg3_flags2 & TG3_FLG2_USING_MSIX) &&
8527 tp->irq_cnt > 1) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008528 val = tr32(MSGINT_MODE);
8529 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
8530 tw32(MSGINT_MODE, val);
8531 }
8532
Linus Torvalds1da177e2005-04-16 15:20:36 -07008533 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
8534 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8535 udelay(40);
8536 }
8537
8538 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8539 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8540 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8541 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8542 WDMAC_MODE_LNGREAD_ENAB);
8543
Matt Carlsonc5908932011-03-09 16:58:25 +00008544 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8545 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Matt Carlson29ea0952009-08-25 10:07:54 +00008546 if ((tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008547 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8548 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8549 /* nothing */
8550 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Matt Carlsonc5908932011-03-09 16:58:25 +00008551 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008552 val |= WDMAC_MODE_RX_ACCEL;
8553 }
8554 }
8555
Michael Chand9ab5ad2006-03-20 22:27:35 -08008556 /* Enable host coalescing bug fix */
Matt Carlson321d32a2008-11-21 17:22:19 -08008557 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlsonf51f3562008-05-25 23:45:08 -07008558 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -08008559
Matt Carlson788a0352009-11-02 14:26:03 +00008560 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8561 val |= WDMAC_MODE_BURST_ALL_DATA;
8562
Linus Torvalds1da177e2005-04-16 15:20:36 -07008563 tw32_f(WDMAC_MODE, val);
8564 udelay(40);
8565
Matt Carlson9974a352007-10-07 23:27:28 -07008566 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
8567 u16 pcix_cmd;
8568
8569 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8570 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008571 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008572 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8573 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008574 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008575 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8576 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008577 }
Matt Carlson9974a352007-10-07 23:27:28 -07008578 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8579 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008580 }
8581
8582 tw32_f(RDMAC_MODE, rdmac_mode);
8583 udelay(40);
8584
8585 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
8586 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8587 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008588
8589 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8590 tw32(SNDDATAC_MODE,
8591 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8592 else
8593 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8594
Linus Torvalds1da177e2005-04-16 15:20:36 -07008595 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8596 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008597 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Matt Carlsonde9f5232011-04-05 14:22:43 +00008598 if (tp->tg3_flags3 & TG3_FLG3_LRG_PROD_RING_CAP)
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008599 val |= RCVDBDI_MODE_LRG_RING_SZ;
8600 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008601 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008602 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
8603 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008604 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Matt Carlson19cfaec2009-12-03 08:36:20 +00008605 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008606 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8607 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008608 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8609
8610 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8611 err = tg3_load_5701_a0_firmware_fix(tp);
8612 if (err)
8613 return err;
8614 }
8615
Linus Torvalds1da177e2005-04-16 15:20:36 -07008616 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
8617 err = tg3_load_tso_firmware(tp);
8618 if (err)
8619 return err;
8620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008621
8622 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008623
Matt Carlsonb1d05212010-06-05 17:24:31 +00008624 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
8625 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8626 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008627
8628 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8629 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
8630 tp->tx_mode &= ~val;
8631 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
8632 }
8633
Linus Torvalds1da177e2005-04-16 15:20:36 -07008634 tw32_f(MAC_TX_MODE, tp->tx_mode);
8635 udelay(100);
8636
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008637 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) {
8638 u32 reg = MAC_RSS_INDIR_TBL_0;
8639 u8 *ent = (u8 *)&val;
8640
8641 /* Setup the indirection table */
8642 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8643 int idx = i % sizeof(val);
8644
Matt Carlson5efeeea2010-07-11 09:31:40 +00008645 ent[idx] = i % (tp->irq_cnt - 1);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008646 if (idx == sizeof(val) - 1) {
8647 tw32(reg, val);
8648 reg += 4;
8649 }
8650 }
8651
8652 /* Setup the "secret" hash key. */
8653 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8654 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8655 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8656 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8657 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8658 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8659 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8660 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8661 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8662 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8663 }
8664
Linus Torvalds1da177e2005-04-16 15:20:36 -07008665 tp->rx_mode = RX_MODE_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08008666 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chanaf36e6b2006-03-23 01:28:06 -08008667 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8668
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008669 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)
8670 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8671 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8672 RX_MODE_RSS_IPV6_HASH_EN |
8673 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8674 RX_MODE_RSS_IPV4_HASH_EN |
8675 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8676
Linus Torvalds1da177e2005-04-16 15:20:36 -07008677 tw32_f(MAC_RX_MODE, tp->rx_mode);
8678 udelay(10);
8679
Linus Torvalds1da177e2005-04-16 15:20:36 -07008680 tw32(MAC_LED_CTRL, tp->led_ctrl);
8681
8682 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008683 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008684 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8685 udelay(10);
8686 }
8687 tw32_f(MAC_RX_MODE, tp->rx_mode);
8688 udelay(10);
8689
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008690 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008691 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008692 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008693 /* Set drive transmission level to 1.2V */
8694 /* only if the signal pre-emphasis bit is not set */
8695 val = tr32(MAC_SERDES_CFG);
8696 val &= 0xfffff000;
8697 val |= 0x880;
8698 tw32(MAC_SERDES_CFG, val);
8699 }
8700 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8701 tw32(MAC_SERDES_CFG, 0x616000);
8702 }
8703
8704 /* Prevent chip from dropping frames when flow control
8705 * is enabled.
8706 */
Matt Carlson666bc832010-01-20 16:58:03 +00008707 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8708 val = 1;
8709 else
8710 val = 2;
8711 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008712
8713 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008714 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008715 /* Use hardware link auto-negotiation */
8716 tp->tg3_flags2 |= TG3_FLG2_HW_AUTONEG;
8717 }
8718
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008719 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Michael Chand4d2c552006-03-20 17:47:20 -08008720 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)) {
8721 u32 tmp;
8722
8723 tmp = tr32(SERDES_RX_CTRL);
8724 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
8725 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
8726 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
8727 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8728 }
8729
Matt Carlsondd477002008-05-25 23:45:58 -07008730 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
Matt Carlson80096062010-08-02 11:26:06 +00008731 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
8732 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07008733 tp->link_config.speed = tp->link_config.orig_speed;
8734 tp->link_config.duplex = tp->link_config.orig_duplex;
8735 tp->link_config.autoneg = tp->link_config.orig_autoneg;
8736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008737
Matt Carlsondd477002008-05-25 23:45:58 -07008738 err = tg3_setup_phy(tp, 0);
8739 if (err)
8740 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008741
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008742 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
8743 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07008744 u32 tmp;
8745
8746 /* Clear CRC stats. */
8747 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
8748 tg3_writephy(tp, MII_TG3_TEST1,
8749 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00008750 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07008751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008752 }
8753 }
8754
8755 __tg3_set_rx_mode(tp->dev);
8756
8757 /* Initialize receive rules. */
8758 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
8759 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
8760 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
8761 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
8762
Michael Chan4cf78e42005-07-25 12:29:19 -07008763 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Michael Chana4e2b342005-10-26 15:46:52 -07008764 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008765 limit = 8;
8766 else
8767 limit = 16;
8768 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
8769 limit -= 4;
8770 switch (limit) {
8771 case 16:
8772 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
8773 case 15:
8774 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
8775 case 14:
8776 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
8777 case 13:
8778 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
8779 case 12:
8780 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
8781 case 11:
8782 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
8783 case 10:
8784 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
8785 case 9:
8786 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
8787 case 8:
8788 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
8789 case 7:
8790 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
8791 case 6:
8792 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
8793 case 5:
8794 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
8795 case 4:
8796 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
8797 case 3:
8798 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
8799 case 2:
8800 case 1:
8801
8802 default:
8803 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008805
Matt Carlson9ce768e2007-10-11 19:49:11 -07008806 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
8807 /* Write our heartbeat update interval to APE. */
8808 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
8809 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07008810
Linus Torvalds1da177e2005-04-16 15:20:36 -07008811 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
8812
Linus Torvalds1da177e2005-04-16 15:20:36 -07008813 return 0;
8814}
8815
8816/* Called at device open time to get the chip ready for
8817 * packet processing. Invoked with tp->lock held.
8818 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008819static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008820{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008821 tg3_switch_clocks(tp);
8822
8823 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
8824
Matt Carlson2f751b62008-08-04 23:17:34 -07008825 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008826}
8827
8828#define TG3_STAT_ADD32(PSTAT, REG) \
8829do { u32 __val = tr32(REG); \
8830 (PSTAT)->low += __val; \
8831 if ((PSTAT)->low < __val) \
8832 (PSTAT)->high += 1; \
8833} while (0)
8834
8835static void tg3_periodic_fetch_stats(struct tg3 *tp)
8836{
8837 struct tg3_hw_stats *sp = tp->hw_stats;
8838
8839 if (!netif_carrier_ok(tp->dev))
8840 return;
8841
8842 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
8843 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
8844 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
8845 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
8846 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
8847 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
8848 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
8849 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
8850 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
8851 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
8852 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
8853 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
8854 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
8855
8856 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
8857 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
8858 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
8859 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
8860 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
8861 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
8862 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
8863 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
8864 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
8865 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
8866 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
8867 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
8868 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
8869 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07008870
8871 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
8872 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
8873 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008874}
8875
8876static void tg3_timer(unsigned long __opaque)
8877{
8878 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008879
Michael Chanf475f162006-03-27 23:20:14 -08008880 if (tp->irq_sync)
8881 goto restart_timer;
8882
David S. Millerf47c11e2005-06-24 20:18:35 -07008883 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008884
David S. Millerfac9b832005-05-18 22:46:34 -07008885 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
8886 /* All of this garbage is because when using non-tagged
8887 * IRQ status the mailbox/status_block protocol the chip
8888 * uses with the cpu is race prone.
8889 */
Matt Carlson898a56f2009-08-28 14:02:40 +00008890 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07008891 tw32(GRC_LOCAL_CTRL,
8892 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
8893 } else {
8894 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008895 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07008896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008897
David S. Millerfac9b832005-05-18 22:46:34 -07008898 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
8899 tp->tg3_flags2 |= TG3_FLG2_RESTART_TIMER;
David S. Millerf47c11e2005-06-24 20:18:35 -07008900 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07008901 schedule_work(&tp->reset_task);
8902 return;
8903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008904 }
8905
Linus Torvalds1da177e2005-04-16 15:20:36 -07008906 /* This part only runs once per second. */
8907 if (!--tp->timer_counter) {
David S. Millerfac9b832005-05-18 22:46:34 -07008908 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
8909 tg3_periodic_fetch_stats(tp);
8910
Matt Carlson52b02d02010-10-14 10:37:41 +00008911 if (tp->setlpicnt && !--tp->setlpicnt) {
8912 u32 val = tr32(TG3_CPMU_EEE_MODE);
8913 tw32(TG3_CPMU_EEE_MODE,
8914 val | TG3_CPMU_EEEMD_LPI_ENABLE);
8915 }
8916
Linus Torvalds1da177e2005-04-16 15:20:36 -07008917 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
8918 u32 mac_stat;
8919 int phy_event;
8920
8921 mac_stat = tr32(MAC_STATUS);
8922
8923 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008924 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008925 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
8926 phy_event = 1;
8927 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
8928 phy_event = 1;
8929
8930 if (phy_event)
8931 tg3_setup_phy(tp, 0);
8932 } else if (tp->tg3_flags & TG3_FLAG_POLL_SERDES) {
8933 u32 mac_stat = tr32(MAC_STATUS);
8934 int need_setup = 0;
8935
8936 if (netif_carrier_ok(tp->dev) &&
8937 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
8938 need_setup = 1;
8939 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00008940 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008941 (mac_stat & (MAC_STATUS_PCS_SYNCED |
8942 MAC_STATUS_SIGNAL_DET))) {
8943 need_setup = 1;
8944 }
8945 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07008946 if (!tp->serdes_counter) {
8947 tw32_f(MAC_MODE,
8948 (tp->mac_mode &
8949 ~MAC_MODE_PORT_MODE_MASK));
8950 udelay(40);
8951 tw32_f(MAC_MODE, tp->mac_mode);
8952 udelay(40);
8953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008954 tg3_setup_phy(tp, 0);
8955 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008956 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson2138c002010-07-11 09:31:43 +00008957 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07008958 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00008959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008960
8961 tp->timer_counter = tp->timer_multiplier;
8962 }
8963
Michael Chan130b8e42006-09-27 16:00:40 -07008964 /* Heartbeat is only sent once every 2 seconds.
8965 *
8966 * The heartbeat is to tell the ASF firmware that the host
8967 * driver is still alive. In the event that the OS crashes,
8968 * ASF needs to reset the hardware to free up the FIFO space
8969 * that may be filled with rx packets destined for the host.
8970 * If the FIFO is full, ASF will no longer function properly.
8971 *
8972 * Unintended resets have been reported on real time kernels
8973 * where the timer doesn't run on time. Netpoll will also have
8974 * same problem.
8975 *
8976 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
8977 * to check the ring condition when the heartbeat is expiring
8978 * before doing the reset. This will prevent most unintended
8979 * resets.
8980 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008981 if (!--tp->asf_counter) {
Matt Carlsonbc7959b2008-08-15 14:08:55 -07008982 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
8983 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07008984 tg3_wait_for_event_ack(tp);
8985
Michael Chanbbadf502006-04-06 21:46:34 -07008986 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07008987 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07008988 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00008989 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
8990 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008991
8992 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008993 }
8994 tp->asf_counter = tp->asf_multiplier;
8995 }
8996
David S. Millerf47c11e2005-06-24 20:18:35 -07008997 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008998
Michael Chanf475f162006-03-27 23:20:14 -08008999restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07009000 tp->timer.expires = jiffies + tp->timer_offset;
9001 add_timer(&tp->timer);
9002}
9003
Matt Carlson4f125f42009-09-01 12:55:02 +00009004static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08009005{
David Howells7d12e782006-10-05 14:55:46 +01009006 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009007 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00009008 char *name;
9009 struct tg3_napi *tnapi = &tp->napi[irq_num];
9010
9011 if (tp->irq_cnt == 1)
9012 name = tp->dev->name;
9013 else {
9014 name = &tnapi->irq_lbl[0];
9015 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
9016 name[IFNAMSIZ-1] = 0;
9017 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009018
Matt Carlson679563f2009-09-01 12:55:46 +00009019 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08009020 fn = tg3_msi;
9021 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
9022 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009023 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009024 } else {
9025 fn = tg3_interrupt;
9026 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
9027 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009028 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009029 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009030
9031 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009032}
9033
Michael Chan79381092005-04-21 17:13:59 -07009034static int tg3_test_interrupt(struct tg3 *tp)
9035{
Matt Carlson09943a12009-08-28 14:01:57 +00009036 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07009037 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07009038 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009039 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07009040
Michael Chand4bc3922005-05-29 14:59:20 -07009041 if (!netif_running(dev))
9042 return -ENODEV;
9043
Michael Chan79381092005-04-21 17:13:59 -07009044 tg3_disable_ints(tp);
9045
Matt Carlson4f125f42009-09-01 12:55:02 +00009046 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009047
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009048 /*
9049 * Turn off MSI one shot mode. Otherwise this test has no
9050 * observable way to know whether the interrupt was delivered.
9051 */
Matt Carlson1407deb2011-04-05 14:22:44 +00009052 if ((tp->tg3_flags3 & TG3_FLG3_57765_PLUS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009053 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
9054 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
9055 tw32(MSGINT_MODE, val);
9056 }
9057
Matt Carlson4f125f42009-09-01 12:55:02 +00009058 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00009059 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009060 if (err)
9061 return err;
9062
Matt Carlson898a56f2009-08-28 14:02:40 +00009063 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07009064 tg3_enable_ints(tp);
9065
9066 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009067 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07009068
9069 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07009070 u32 int_mbox, misc_host_ctrl;
9071
Matt Carlson898a56f2009-08-28 14:02:40 +00009072 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07009073 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
9074
9075 if ((int_mbox != 0) ||
9076 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
9077 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07009078 break;
Michael Chanb16250e2006-09-27 16:10:14 -07009079 }
9080
Michael Chan79381092005-04-21 17:13:59 -07009081 msleep(10);
9082 }
9083
9084 tg3_disable_ints(tp);
9085
Matt Carlson4f125f42009-09-01 12:55:02 +00009086 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009087
Matt Carlson4f125f42009-09-01 12:55:02 +00009088 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009089
9090 if (err)
9091 return err;
9092
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009093 if (intr_ok) {
9094 /* Reenable MSI one shot mode. */
Matt Carlson1407deb2011-04-05 14:22:44 +00009095 if ((tp->tg3_flags3 & TG3_FLG3_57765_PLUS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009096 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
9097 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
9098 tw32(MSGINT_MODE, val);
9099 }
Michael Chan79381092005-04-21 17:13:59 -07009100 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009101 }
Michael Chan79381092005-04-21 17:13:59 -07009102
9103 return -EIO;
9104}
9105
9106/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
9107 * successfully restored
9108 */
9109static int tg3_test_msi(struct tg3 *tp)
9110{
Michael Chan79381092005-04-21 17:13:59 -07009111 int err;
9112 u16 pci_cmd;
9113
9114 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSI))
9115 return 0;
9116
9117 /* Turn off SERR reporting in case MSI terminates with Master
9118 * Abort.
9119 */
9120 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
9121 pci_write_config_word(tp->pdev, PCI_COMMAND,
9122 pci_cmd & ~PCI_COMMAND_SERR);
9123
9124 err = tg3_test_interrupt(tp);
9125
9126 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
9127
9128 if (!err)
9129 return 0;
9130
9131 /* other failures */
9132 if (err != -EIO)
9133 return err;
9134
9135 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009136 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
9137 "to INTx mode. Please report this failure to the PCI "
9138 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07009139
Matt Carlson4f125f42009-09-01 12:55:02 +00009140 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00009141
Michael Chan79381092005-04-21 17:13:59 -07009142 pci_disable_msi(tp->pdev);
9143
9144 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI;
Andre Detschdc8bf1b2010-04-26 07:27:07 +00009145 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07009146
Matt Carlson4f125f42009-09-01 12:55:02 +00009147 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009148 if (err)
9149 return err;
9150
9151 /* Need to reset the chip because the MSI cycle may have terminated
9152 * with Master Abort.
9153 */
David S. Millerf47c11e2005-06-24 20:18:35 -07009154 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009155
Michael Chan944d9802005-05-29 14:57:48 -07009156 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009157 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009158
David S. Millerf47c11e2005-06-24 20:18:35 -07009159 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009160
9161 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00009162 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07009163
9164 return err;
9165}
9166
Matt Carlson9e9fd122009-01-19 16:57:45 -08009167static int tg3_request_firmware(struct tg3 *tp)
9168{
9169 const __be32 *fw_data;
9170
9171 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009172 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
9173 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009174 return -ENOENT;
9175 }
9176
9177 fw_data = (void *)tp->fw->data;
9178
9179 /* Firmware blob starts with version numbers, followed by
9180 * start address and _full_ length including BSS sections
9181 * (which must be longer than the actual data, of course
9182 */
9183
9184 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9185 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009186 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9187 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009188 release_firmware(tp->fw);
9189 tp->fw = NULL;
9190 return -EINVAL;
9191 }
9192
9193 /* We no longer need firmware; we have it. */
9194 tp->fw_needed = NULL;
9195 return 0;
9196}
9197
Matt Carlson679563f2009-09-01 12:55:46 +00009198static bool tg3_enable_msix(struct tg3 *tp)
9199{
9200 int i, rc, cpus = num_online_cpus();
9201 struct msix_entry msix_ent[tp->irq_max];
9202
9203 if (cpus == 1)
9204 /* Just fallback to the simpler MSI mode. */
9205 return false;
9206
9207 /*
9208 * We want as many rx rings enabled as there are cpus.
9209 * The first MSIX vector only deals with link interrupts, etc,
9210 * so we add one to the number of vectors we are requesting.
9211 */
9212 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
9213
9214 for (i = 0; i < tp->irq_max; i++) {
9215 msix_ent[i].entry = i;
9216 msix_ent[i].vector = 0;
9217 }
9218
9219 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009220 if (rc < 0) {
9221 return false;
9222 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009223 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9224 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009225 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9226 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009227 tp->irq_cnt = rc;
9228 }
9229
9230 for (i = 0; i < tp->irq_max; i++)
9231 tp->napi[i].irq_vec = msix_ent[i].vector;
9232
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009233 netif_set_real_num_tx_queues(tp->dev, 1);
9234 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9235 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9236 pci_disable_msix(tp->pdev);
9237 return false;
9238 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009239
9240 if (tp->irq_cnt > 1) {
Matt Carlson2430b032010-06-05 17:24:34 +00009241 tp->tg3_flags3 |= TG3_FLG3_ENABLE_RSS;
Matt Carlsond78b59f2011-04-05 14:22:46 +00009242
9243 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9244 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb92b9042010-11-24 08:31:51 +00009245 tp->tg3_flags3 |= TG3_FLG3_ENABLE_TSS;
9246 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9247 }
9248 }
Matt Carlson2430b032010-06-05 17:24:34 +00009249
Matt Carlson679563f2009-09-01 12:55:46 +00009250 return true;
9251}
9252
Matt Carlson07b01732009-08-28 14:01:15 +00009253static void tg3_ints_init(struct tg3 *tp)
9254{
Matt Carlson679563f2009-09-01 12:55:46 +00009255 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI_OR_MSIX) &&
9256 !(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009257 /* All MSI supporting chips should support tagged
9258 * status. Assert that this is the case.
9259 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009260 netdev_warn(tp->dev,
9261 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009262 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009263 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009264
Matt Carlson679563f2009-09-01 12:55:46 +00009265 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) && tg3_enable_msix(tp))
9266 tp->tg3_flags2 |= TG3_FLG2_USING_MSIX;
9267 else if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI) &&
9268 pci_enable_msi(tp->pdev) == 0)
9269 tp->tg3_flags2 |= TG3_FLG2_USING_MSI;
9270
9271 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
9272 u32 msi_mode = tr32(MSGINT_MODE);
Matt Carlson0583d522011-01-25 15:58:50 +00009273 if ((tp->tg3_flags2 & TG3_FLG2_USING_MSIX) &&
9274 tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009275 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson679563f2009-09-01 12:55:46 +00009276 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9277 }
9278defcfg:
9279 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSIX)) {
9280 tp->irq_cnt = 1;
9281 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009282 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009283 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009284 }
Matt Carlson07b01732009-08-28 14:01:15 +00009285}
9286
9287static void tg3_ints_fini(struct tg3 *tp)
9288{
Matt Carlson679563f2009-09-01 12:55:46 +00009289 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
9290 pci_disable_msix(tp->pdev);
9291 else if (tp->tg3_flags2 & TG3_FLG2_USING_MSI)
9292 pci_disable_msi(tp->pdev);
9293 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI_OR_MSIX;
Matt Carlson774ee752010-08-02 11:25:56 +00009294 tp->tg3_flags3 &= ~(TG3_FLG3_ENABLE_RSS | TG3_FLG3_ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009295}
9296
Linus Torvalds1da177e2005-04-16 15:20:36 -07009297static int tg3_open(struct net_device *dev)
9298{
9299 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009300 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009301
Matt Carlson9e9fd122009-01-19 16:57:45 -08009302 if (tp->fw_needed) {
9303 err = tg3_request_firmware(tp);
9304 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9305 if (err)
9306 return err;
9307 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009308 netdev_warn(tp->dev, "TSO capability disabled\n");
Matt Carlson9e9fd122009-01-19 16:57:45 -08009309 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
9310 } else if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009311 netdev_notice(tp->dev, "TSO capability restored\n");
Matt Carlson9e9fd122009-01-19 16:57:45 -08009312 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
9313 }
9314 }
9315
Michael Chanc49a1562006-12-17 17:07:29 -08009316 netif_carrier_off(tp->dev);
9317
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009318 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -07009319 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009320 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009321
9322 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009323
Linus Torvalds1da177e2005-04-16 15:20:36 -07009324 tg3_disable_ints(tp);
9325 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
9326
David S. Millerf47c11e2005-06-24 20:18:35 -07009327 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009328
Matt Carlson679563f2009-09-01 12:55:46 +00009329 /*
9330 * Setup interrupts first so we know how
9331 * many NAPI resources to allocate
9332 */
9333 tg3_ints_init(tp);
9334
Linus Torvalds1da177e2005-04-16 15:20:36 -07009335 /* The placement of this call is tied
9336 * to the setup and use of Host TX descriptors.
9337 */
9338 err = tg3_alloc_consistent(tp);
9339 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009340 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009341
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009342 tg3_napi_init(tp);
9343
Matt Carlsonfed97812009-09-01 13:10:19 +00009344 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07009345
Matt Carlson4f125f42009-09-01 12:55:02 +00009346 for (i = 0; i < tp->irq_cnt; i++) {
9347 struct tg3_napi *tnapi = &tp->napi[i];
9348 err = tg3_request_irq(tp, i);
9349 if (err) {
9350 for (i--; i >= 0; i--)
9351 free_irq(tnapi->irq_vec, tnapi);
9352 break;
9353 }
9354 }
Matt Carlson07b01732009-08-28 14:01:15 +00009355
9356 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009357 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00009358
David S. Millerf47c11e2005-06-24 20:18:35 -07009359 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009360
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009361 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009362 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07009363 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009364 tg3_free_rings(tp);
9365 } else {
David S. Millerfac9b832005-05-18 22:46:34 -07009366 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
9367 tp->timer_offset = HZ;
9368 else
9369 tp->timer_offset = HZ / 10;
9370
9371 BUG_ON(tp->timer_offset > HZ);
9372 tp->timer_counter = tp->timer_multiplier =
9373 (HZ / tp->timer_offset);
9374 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07009375 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009376
9377 init_timer(&tp->timer);
9378 tp->timer.expires = jiffies + tp->timer_offset;
9379 tp->timer.data = (unsigned long) tp;
9380 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009381 }
9382
David S. Millerf47c11e2005-06-24 20:18:35 -07009383 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009384
Matt Carlson07b01732009-08-28 14:01:15 +00009385 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009386 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009387
Michael Chan79381092005-04-21 17:13:59 -07009388 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
9389 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07009390
Michael Chan79381092005-04-21 17:13:59 -07009391 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009392 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07009393 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07009394 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07009395 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009396
Matt Carlson679563f2009-09-01 12:55:46 +00009397 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07009398 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009399
Matt Carlson1407deb2011-04-05 14:22:44 +00009400 if (!(tp->tg3_flags3 & TG3_FLG3_57765_PLUS) &&
Matt Carlsonc885e822010-08-02 11:25:57 +00009401 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009402 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009403
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009404 tw32(PCIE_TRANSACTION_CFG,
9405 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009406 }
Michael Chan79381092005-04-21 17:13:59 -07009407 }
9408
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009409 tg3_phy_start(tp);
9410
David S. Millerf47c11e2005-06-24 20:18:35 -07009411 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009412
Michael Chan79381092005-04-21 17:13:59 -07009413 add_timer(&tp->timer);
9414 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009415 tg3_enable_ints(tp);
9416
David S. Millerf47c11e2005-06-24 20:18:35 -07009417 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009418
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009419 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009420
9421 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00009422
Matt Carlson679563f2009-09-01 12:55:46 +00009423err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00009424 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9425 struct tg3_napi *tnapi = &tp->napi[i];
9426 free_irq(tnapi->irq_vec, tnapi);
9427 }
Matt Carlson07b01732009-08-28 14:01:15 +00009428
Matt Carlson679563f2009-09-01 12:55:46 +00009429err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00009430 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009431 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009432 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00009433
9434err_out1:
9435 tg3_ints_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009436 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009437}
9438
Eric Dumazet511d2222010-07-07 20:44:24 +00009439static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
9440 struct rtnl_link_stats64 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009441static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
9442
9443static int tg3_close(struct net_device *dev)
9444{
Matt Carlson4f125f42009-09-01 12:55:02 +00009445 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009446 struct tg3 *tp = netdev_priv(dev);
9447
Matt Carlsonfed97812009-09-01 13:10:19 +00009448 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07009449 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08009450
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009451 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009452
9453 del_timer_sync(&tp->timer);
9454
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009455 tg3_phy_stop(tp);
9456
David S. Millerf47c11e2005-06-24 20:18:35 -07009457 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009458
9459 tg3_disable_ints(tp);
9460
Michael Chan944d9802005-05-29 14:57:48 -07009461 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009462 tg3_free_rings(tp);
Michael Chan5cf64b82007-05-05 12:11:21 -07009463 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009464
David S. Millerf47c11e2005-06-24 20:18:35 -07009465 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009466
Matt Carlson4f125f42009-09-01 12:55:02 +00009467 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9468 struct tg3_napi *tnapi = &tp->napi[i];
9469 free_irq(tnapi->irq_vec, tnapi);
9470 }
Matt Carlson07b01732009-08-28 14:01:15 +00009471
9472 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009473
Eric Dumazet511d2222010-07-07 20:44:24 +00009474 tg3_get_stats64(tp->dev, &tp->net_stats_prev);
9475
Linus Torvalds1da177e2005-04-16 15:20:36 -07009476 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9477 sizeof(tp->estats_prev));
9478
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009479 tg3_napi_fini(tp);
9480
Linus Torvalds1da177e2005-04-16 15:20:36 -07009481 tg3_free_consistent(tp);
9482
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009483 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -08009484
9485 netif_carrier_off(tp->dev);
9486
Linus Torvalds1da177e2005-04-16 15:20:36 -07009487 return 0;
9488}
9489
Eric Dumazet511d2222010-07-07 20:44:24 +00009490static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -07009491{
9492 return ((u64)val->high << 32) | ((u64)val->low);
9493}
9494
Eric Dumazet511d2222010-07-07 20:44:24 +00009495static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009496{
9497 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9498
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009499 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009500 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9501 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009502 u32 val;
9503
David S. Millerf47c11e2005-06-24 20:18:35 -07009504 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009505 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9506 tg3_writephy(tp, MII_TG3_TEST1,
9507 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009508 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009509 } else
9510 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009511 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009512
9513 tp->phy_crc_errors += val;
9514
9515 return tp->phy_crc_errors;
9516 }
9517
9518 return get_stat64(&hw_stats->rx_fcs_errors);
9519}
9520
9521#define ESTAT_ADD(member) \
9522 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +00009523 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009524
9525static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9526{
9527 struct tg3_ethtool_stats *estats = &tp->estats;
9528 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9529 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9530
9531 if (!hw_stats)
9532 return old_estats;
9533
9534 ESTAT_ADD(rx_octets);
9535 ESTAT_ADD(rx_fragments);
9536 ESTAT_ADD(rx_ucast_packets);
9537 ESTAT_ADD(rx_mcast_packets);
9538 ESTAT_ADD(rx_bcast_packets);
9539 ESTAT_ADD(rx_fcs_errors);
9540 ESTAT_ADD(rx_align_errors);
9541 ESTAT_ADD(rx_xon_pause_rcvd);
9542 ESTAT_ADD(rx_xoff_pause_rcvd);
9543 ESTAT_ADD(rx_mac_ctrl_rcvd);
9544 ESTAT_ADD(rx_xoff_entered);
9545 ESTAT_ADD(rx_frame_too_long_errors);
9546 ESTAT_ADD(rx_jabbers);
9547 ESTAT_ADD(rx_undersize_packets);
9548 ESTAT_ADD(rx_in_length_errors);
9549 ESTAT_ADD(rx_out_length_errors);
9550 ESTAT_ADD(rx_64_or_less_octet_packets);
9551 ESTAT_ADD(rx_65_to_127_octet_packets);
9552 ESTAT_ADD(rx_128_to_255_octet_packets);
9553 ESTAT_ADD(rx_256_to_511_octet_packets);
9554 ESTAT_ADD(rx_512_to_1023_octet_packets);
9555 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9556 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9557 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9558 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9559 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9560
9561 ESTAT_ADD(tx_octets);
9562 ESTAT_ADD(tx_collisions);
9563 ESTAT_ADD(tx_xon_sent);
9564 ESTAT_ADD(tx_xoff_sent);
9565 ESTAT_ADD(tx_flow_control);
9566 ESTAT_ADD(tx_mac_errors);
9567 ESTAT_ADD(tx_single_collisions);
9568 ESTAT_ADD(tx_mult_collisions);
9569 ESTAT_ADD(tx_deferred);
9570 ESTAT_ADD(tx_excessive_collisions);
9571 ESTAT_ADD(tx_late_collisions);
9572 ESTAT_ADD(tx_collide_2times);
9573 ESTAT_ADD(tx_collide_3times);
9574 ESTAT_ADD(tx_collide_4times);
9575 ESTAT_ADD(tx_collide_5times);
9576 ESTAT_ADD(tx_collide_6times);
9577 ESTAT_ADD(tx_collide_7times);
9578 ESTAT_ADD(tx_collide_8times);
9579 ESTAT_ADD(tx_collide_9times);
9580 ESTAT_ADD(tx_collide_10times);
9581 ESTAT_ADD(tx_collide_11times);
9582 ESTAT_ADD(tx_collide_12times);
9583 ESTAT_ADD(tx_collide_13times);
9584 ESTAT_ADD(tx_collide_14times);
9585 ESTAT_ADD(tx_collide_15times);
9586 ESTAT_ADD(tx_ucast_packets);
9587 ESTAT_ADD(tx_mcast_packets);
9588 ESTAT_ADD(tx_bcast_packets);
9589 ESTAT_ADD(tx_carrier_sense_errors);
9590 ESTAT_ADD(tx_discards);
9591 ESTAT_ADD(tx_errors);
9592
9593 ESTAT_ADD(dma_writeq_full);
9594 ESTAT_ADD(dma_write_prioq_full);
9595 ESTAT_ADD(rxbds_empty);
9596 ESTAT_ADD(rx_discards);
9597 ESTAT_ADD(rx_errors);
9598 ESTAT_ADD(rx_threshold_hit);
9599
9600 ESTAT_ADD(dma_readq_full);
9601 ESTAT_ADD(dma_read_prioq_full);
9602 ESTAT_ADD(tx_comp_queue_full);
9603
9604 ESTAT_ADD(ring_set_send_prod_index);
9605 ESTAT_ADD(ring_status_update);
9606 ESTAT_ADD(nic_irqs);
9607 ESTAT_ADD(nic_avoided_irqs);
9608 ESTAT_ADD(nic_tx_threshold_hit);
9609
9610 return estats;
9611}
9612
Eric Dumazet511d2222010-07-07 20:44:24 +00009613static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9614 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009615{
9616 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +00009617 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009618 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9619
9620 if (!hw_stats)
9621 return old_stats;
9622
9623 stats->rx_packets = old_stats->rx_packets +
9624 get_stat64(&hw_stats->rx_ucast_packets) +
9625 get_stat64(&hw_stats->rx_mcast_packets) +
9626 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009627
Linus Torvalds1da177e2005-04-16 15:20:36 -07009628 stats->tx_packets = old_stats->tx_packets +
9629 get_stat64(&hw_stats->tx_ucast_packets) +
9630 get_stat64(&hw_stats->tx_mcast_packets) +
9631 get_stat64(&hw_stats->tx_bcast_packets);
9632
9633 stats->rx_bytes = old_stats->rx_bytes +
9634 get_stat64(&hw_stats->rx_octets);
9635 stats->tx_bytes = old_stats->tx_bytes +
9636 get_stat64(&hw_stats->tx_octets);
9637
9638 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009639 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009640 stats->tx_errors = old_stats->tx_errors +
9641 get_stat64(&hw_stats->tx_errors) +
9642 get_stat64(&hw_stats->tx_mac_errors) +
9643 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9644 get_stat64(&hw_stats->tx_discards);
9645
9646 stats->multicast = old_stats->multicast +
9647 get_stat64(&hw_stats->rx_mcast_packets);
9648 stats->collisions = old_stats->collisions +
9649 get_stat64(&hw_stats->tx_collisions);
9650
9651 stats->rx_length_errors = old_stats->rx_length_errors +
9652 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9653 get_stat64(&hw_stats->rx_undersize_packets);
9654
9655 stats->rx_over_errors = old_stats->rx_over_errors +
9656 get_stat64(&hw_stats->rxbds_empty);
9657 stats->rx_frame_errors = old_stats->rx_frame_errors +
9658 get_stat64(&hw_stats->rx_align_errors);
9659 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9660 get_stat64(&hw_stats->tx_discards);
9661 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
9662 get_stat64(&hw_stats->tx_carrier_sense_errors);
9663
9664 stats->rx_crc_errors = old_stats->rx_crc_errors +
9665 calc_crc_errors(tp);
9666
John W. Linville4f63b872005-09-12 14:43:18 -07009667 stats->rx_missed_errors = old_stats->rx_missed_errors +
9668 get_stat64(&hw_stats->rx_discards);
9669
Eric Dumazetb0057c52010-10-10 19:55:52 +00009670 stats->rx_dropped = tp->rx_dropped;
9671
Linus Torvalds1da177e2005-04-16 15:20:36 -07009672 return stats;
9673}
9674
9675static inline u32 calc_crc(unsigned char *buf, int len)
9676{
9677 u32 reg;
9678 u32 tmp;
9679 int j, k;
9680
9681 reg = 0xffffffff;
9682
9683 for (j = 0; j < len; j++) {
9684 reg ^= buf[j];
9685
9686 for (k = 0; k < 8; k++) {
9687 tmp = reg & 0x01;
9688
9689 reg >>= 1;
9690
Matt Carlson859a5882010-04-05 10:19:28 +00009691 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009692 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009693 }
9694 }
9695
9696 return ~reg;
9697}
9698
9699static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9700{
9701 /* accept or reject all multicast frames */
9702 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9703 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9704 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9705 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9706}
9707
9708static void __tg3_set_rx_mode(struct net_device *dev)
9709{
9710 struct tg3 *tp = netdev_priv(dev);
9711 u32 rx_mode;
9712
9713 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9714 RX_MODE_KEEP_VLAN_TAG);
9715
Matt Carlsonbf933c82011-01-25 15:58:49 +00009716#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009717 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9718 * flag clear.
9719 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009720 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
9721 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9722#endif
9723
9724 if (dev->flags & IFF_PROMISC) {
9725 /* Promiscuous mode. */
9726 rx_mode |= RX_MODE_PROMISC;
9727 } else if (dev->flags & IFF_ALLMULTI) {
9728 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009729 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00009730 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009731 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009732 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009733 } else {
9734 /* Accept one or more multicast(s). */
Jiri Pirko22bedad2010-04-01 21:22:57 +00009735 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009736 u32 mc_filter[4] = { 0, };
9737 u32 regidx;
9738 u32 bit;
9739 u32 crc;
9740
Jiri Pirko22bedad2010-04-01 21:22:57 +00009741 netdev_for_each_mc_addr(ha, dev) {
9742 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009743 bit = ~crc & 0x7f;
9744 regidx = (bit & 0x60) >> 5;
9745 bit &= 0x1f;
9746 mc_filter[regidx] |= (1 << bit);
9747 }
9748
9749 tw32(MAC_HASH_REG_0, mc_filter[0]);
9750 tw32(MAC_HASH_REG_1, mc_filter[1]);
9751 tw32(MAC_HASH_REG_2, mc_filter[2]);
9752 tw32(MAC_HASH_REG_3, mc_filter[3]);
9753 }
9754
9755 if (rx_mode != tp->rx_mode) {
9756 tp->rx_mode = rx_mode;
9757 tw32_f(MAC_RX_MODE, rx_mode);
9758 udelay(10);
9759 }
9760}
9761
9762static void tg3_set_rx_mode(struct net_device *dev)
9763{
9764 struct tg3 *tp = netdev_priv(dev);
9765
Michael Chane75f7c92006-03-20 21:33:26 -08009766 if (!netif_running(dev))
9767 return;
9768
David S. Millerf47c11e2005-06-24 20:18:35 -07009769 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009770 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -07009771 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009772}
9773
Linus Torvalds1da177e2005-04-16 15:20:36 -07009774static int tg3_get_regs_len(struct net_device *dev)
9775{
Matt Carlson97bd8e42011-04-13 11:05:04 +00009776 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009777}
9778
9779static void tg3_get_regs(struct net_device *dev,
9780 struct ethtool_regs *regs, void *_p)
9781{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009782 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009783
9784 regs->version = 0;
9785
Matt Carlson97bd8e42011-04-13 11:05:04 +00009786 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009787
Matt Carlson80096062010-08-02 11:26:06 +00009788 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009789 return;
9790
David S. Millerf47c11e2005-06-24 20:18:35 -07009791 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009792
Matt Carlson97bd8e42011-04-13 11:05:04 +00009793 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009794
David S. Millerf47c11e2005-06-24 20:18:35 -07009795 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009796}
9797
9798static int tg3_get_eeprom_len(struct net_device *dev)
9799{
9800 struct tg3 *tp = netdev_priv(dev);
9801
9802 return tp->nvram_size;
9803}
9804
Linus Torvalds1da177e2005-04-16 15:20:36 -07009805static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9806{
9807 struct tg3 *tp = netdev_priv(dev);
9808 int ret;
9809 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -08009810 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009811 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009812
Matt Carlsondf259d82009-04-20 06:57:14 +00009813 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
9814 return -EINVAL;
9815
Matt Carlson80096062010-08-02 11:26:06 +00009816 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009817 return -EAGAIN;
9818
Linus Torvalds1da177e2005-04-16 15:20:36 -07009819 offset = eeprom->offset;
9820 len = eeprom->len;
9821 eeprom->len = 0;
9822
9823 eeprom->magic = TG3_EEPROM_MAGIC;
9824
9825 if (offset & 3) {
9826 /* adjustments to start on required 4 byte boundary */
9827 b_offset = offset & 3;
9828 b_count = 4 - b_offset;
9829 if (b_count > len) {
9830 /* i.e. offset=1 len=2 */
9831 b_count = len;
9832 }
Matt Carlsona9dc5292009-02-25 14:25:30 +00009833 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009834 if (ret)
9835 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +00009836 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009837 len -= b_count;
9838 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009839 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009840 }
9841
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009842 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009843 pd = &data[eeprom->len];
9844 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00009845 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009846 if (ret) {
9847 eeprom->len += i;
9848 return ret;
9849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009850 memcpy(pd + i, &val, 4);
9851 }
9852 eeprom->len += i;
9853
9854 if (len & 3) {
9855 /* read last bytes not ending on 4 byte boundary */
9856 pd = &data[eeprom->len];
9857 b_count = len & 3;
9858 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009859 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009860 if (ret)
9861 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009862 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009863 eeprom->len += b_count;
9864 }
9865 return 0;
9866}
9867
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009868static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009869
9870static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9871{
9872 struct tg3 *tp = netdev_priv(dev);
9873 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009874 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009875 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009876 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009877
Matt Carlson80096062010-08-02 11:26:06 +00009878 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009879 return -EAGAIN;
9880
Matt Carlsondf259d82009-04-20 06:57:14 +00009881 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
9882 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009883 return -EINVAL;
9884
9885 offset = eeprom->offset;
9886 len = eeprom->len;
9887
9888 if ((b_offset = (offset & 3))) {
9889 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +00009890 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009891 if (ret)
9892 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009893 len += b_offset;
9894 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -07009895 if (len < 4)
9896 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009897 }
9898
9899 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -07009900 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009901 /* adjustments to end on required 4 byte boundary */
9902 odd_len = 1;
9903 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009904 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009905 if (ret)
9906 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009907 }
9908
9909 buf = data;
9910 if (b_offset || odd_len) {
9911 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009912 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009913 return -ENOMEM;
9914 if (b_offset)
9915 memcpy(buf, &start, 4);
9916 if (odd_len)
9917 memcpy(buf+len-4, &end, 4);
9918 memcpy(buf + b_offset, data, eeprom->len);
9919 }
9920
9921 ret = tg3_nvram_write_block(tp, offset, len, buf);
9922
9923 if (buf != data)
9924 kfree(buf);
9925
9926 return ret;
9927}
9928
9929static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9930{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009931 struct tg3 *tp = netdev_priv(dev);
9932
9933 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009934 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009935 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009936 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009937 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9938 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009939 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009940
Linus Torvalds1da177e2005-04-16 15:20:36 -07009941 cmd->supported = (SUPPORTED_Autoneg);
9942
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009943 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009944 cmd->supported |= (SUPPORTED_1000baseT_Half |
9945 SUPPORTED_1000baseT_Full);
9946
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009947 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009948 cmd->supported |= (SUPPORTED_100baseT_Half |
9949 SUPPORTED_100baseT_Full |
9950 SUPPORTED_10baseT_Half |
9951 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -08009952 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -07009953 cmd->port = PORT_TP;
9954 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009955 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -07009956 cmd->port = PORT_FIBRE;
9957 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009958
Linus Torvalds1da177e2005-04-16 15:20:36 -07009959 cmd->advertising = tp->link_config.advertising;
9960 if (netif_running(dev)) {
9961 cmd->speed = tp->link_config.active_speed;
9962 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson64c22182010-10-14 10:37:44 +00009963 } else {
9964 cmd->speed = SPEED_INVALID;
9965 cmd->duplex = DUPLEX_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009966 }
Matt Carlson882e9792009-09-01 13:21:36 +00009967 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009968 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009969 cmd->autoneg = tp->link_config.autoneg;
9970 cmd->maxtxpkt = 0;
9971 cmd->maxrxpkt = 0;
9972 return 0;
9973}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009974
Linus Torvalds1da177e2005-04-16 15:20:36 -07009975static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9976{
9977 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009978
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009979 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009980 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009981 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009982 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009983 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9984 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009985 }
9986
Matt Carlson7e5856b2009-02-25 14:23:01 +00009987 if (cmd->autoneg != AUTONEG_ENABLE &&
9988 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -07009989 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009990
9991 if (cmd->autoneg == AUTONEG_DISABLE &&
9992 cmd->duplex != DUPLEX_FULL &&
9993 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -07009994 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009995
Matt Carlson7e5856b2009-02-25 14:23:01 +00009996 if (cmd->autoneg == AUTONEG_ENABLE) {
9997 u32 mask = ADVERTISED_Autoneg |
9998 ADVERTISED_Pause |
9999 ADVERTISED_Asym_Pause;
10000
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010001 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010002 mask |= ADVERTISED_1000baseT_Half |
10003 ADVERTISED_1000baseT_Full;
10004
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010005 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010006 mask |= ADVERTISED_100baseT_Half |
10007 ADVERTISED_100baseT_Full |
10008 ADVERTISED_10baseT_Half |
10009 ADVERTISED_10baseT_Full |
10010 ADVERTISED_TP;
10011 else
10012 mask |= ADVERTISED_FIBRE;
10013
10014 if (cmd->advertising & ~mask)
10015 return -EINVAL;
10016
10017 mask &= (ADVERTISED_1000baseT_Half |
10018 ADVERTISED_1000baseT_Full |
10019 ADVERTISED_100baseT_Half |
10020 ADVERTISED_100baseT_Full |
10021 ADVERTISED_10baseT_Half |
10022 ADVERTISED_10baseT_Full);
10023
10024 cmd->advertising &= mask;
10025 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010026 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
Matt Carlson7e5856b2009-02-25 14:23:01 +000010027 if (cmd->speed != SPEED_1000)
10028 return -EINVAL;
10029
10030 if (cmd->duplex != DUPLEX_FULL)
10031 return -EINVAL;
10032 } else {
10033 if (cmd->speed != SPEED_100 &&
10034 cmd->speed != SPEED_10)
10035 return -EINVAL;
10036 }
10037 }
10038
David S. Millerf47c11e2005-06-24 20:18:35 -070010039 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010040
10041 tp->link_config.autoneg = cmd->autoneg;
10042 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070010043 tp->link_config.advertising = (cmd->advertising |
10044 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010045 tp->link_config.speed = SPEED_INVALID;
10046 tp->link_config.duplex = DUPLEX_INVALID;
10047 } else {
10048 tp->link_config.advertising = 0;
10049 tp->link_config.speed = cmd->speed;
10050 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010051 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010052
Michael Chan24fcad62006-12-17 17:06:46 -080010053 tp->link_config.orig_speed = tp->link_config.speed;
10054 tp->link_config.orig_duplex = tp->link_config.duplex;
10055 tp->link_config.orig_autoneg = tp->link_config.autoneg;
10056
Linus Torvalds1da177e2005-04-16 15:20:36 -070010057 if (netif_running(dev))
10058 tg3_setup_phy(tp, 1);
10059
David S. Millerf47c11e2005-06-24 20:18:35 -070010060 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010061
Linus Torvalds1da177e2005-04-16 15:20:36 -070010062 return 0;
10063}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010064
Linus Torvalds1da177e2005-04-16 15:20:36 -070010065static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10066{
10067 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010068
Linus Torvalds1da177e2005-04-16 15:20:36 -070010069 strcpy(info->driver, DRV_MODULE_NAME);
10070 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -080010071 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010072 strcpy(info->bus_info, pci_name(tp->pdev));
10073}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010074
Linus Torvalds1da177e2005-04-16 15:20:36 -070010075static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10076{
10077 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010078
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010079 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
10080 device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070010081 wol->supported = WAKE_MAGIC;
10082 else
10083 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010084 wol->wolopts = 0;
Matt Carlson05ac4cb2008-11-03 16:53:46 -080010085 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) &&
10086 device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010087 wol->wolopts = WAKE_MAGIC;
10088 memset(&wol->sopass, 0, sizeof(wol->sopass));
10089}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010090
Linus Torvalds1da177e2005-04-16 15:20:36 -070010091static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10092{
10093 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010094 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010095
Linus Torvalds1da177e2005-04-16 15:20:36 -070010096 if (wol->wolopts & ~WAKE_MAGIC)
10097 return -EINVAL;
10098 if ((wol->wolopts & WAKE_MAGIC) &&
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010099 !((tp->tg3_flags & TG3_FLAG_WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010100 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010101
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010102 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
10103
David S. Millerf47c11e2005-06-24 20:18:35 -070010104 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010105 if (device_may_wakeup(dp))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010106 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010107 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070010108 tp->tg3_flags &= ~TG3_FLAG_WOL_ENABLE;
David S. Millerf47c11e2005-06-24 20:18:35 -070010109 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010110
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010111
Linus Torvalds1da177e2005-04-16 15:20:36 -070010112 return 0;
10113}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010114
Linus Torvalds1da177e2005-04-16 15:20:36 -070010115static u32 tg3_get_msglevel(struct net_device *dev)
10116{
10117 struct tg3 *tp = netdev_priv(dev);
10118 return tp->msg_enable;
10119}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010120
Linus Torvalds1da177e2005-04-16 15:20:36 -070010121static void tg3_set_msglevel(struct net_device *dev, u32 value)
10122{
10123 struct tg3 *tp = netdev_priv(dev);
10124 tp->msg_enable = value;
10125}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010126
Linus Torvalds1da177e2005-04-16 15:20:36 -070010127static int tg3_nway_reset(struct net_device *dev)
10128{
10129 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010130 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010131
Linus Torvalds1da177e2005-04-16 15:20:36 -070010132 if (!netif_running(dev))
10133 return -EAGAIN;
10134
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010135 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010136 return -EINVAL;
10137
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010138 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010139 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010140 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010141 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010142 } else {
10143 u32 bmcr;
10144
10145 spin_lock_bh(&tp->lock);
10146 r = -EINVAL;
10147 tg3_readphy(tp, MII_BMCR, &bmcr);
10148 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10149 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010150 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010151 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10152 BMCR_ANENABLE);
10153 r = 0;
10154 }
10155 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010156 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010157
Linus Torvalds1da177e2005-04-16 15:20:36 -070010158 return r;
10159}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010160
Linus Torvalds1da177e2005-04-16 15:20:36 -070010161static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10162{
10163 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010164
Matt Carlson2c49a442010-09-30 10:34:35 +000010165 ering->rx_max_pending = tp->rx_std_ring_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010166 ering->rx_mini_max_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -080010167 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
Matt Carlson2c49a442010-09-30 10:34:35 +000010168 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010169 else
10170 ering->rx_jumbo_max_pending = 0;
10171
10172 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010173
10174 ering->rx_pending = tp->rx_pending;
10175 ering->rx_mini_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -080010176 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
10177 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10178 else
10179 ering->rx_jumbo_pending = 0;
10180
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010181 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010182}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010183
Linus Torvalds1da177e2005-04-16 15:20:36 -070010184static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10185{
10186 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010187 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010188
Matt Carlson2c49a442010-09-30 10:34:35 +000010189 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10190 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010191 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10192 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Michael Chan7f62ad52007-02-20 23:25:40 -080010193 ((tp->tg3_flags2 & TG3_FLG2_TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010194 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010195 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010196
Michael Chanbbe832c2005-06-24 20:20:04 -070010197 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010198 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010199 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010200 irq_sync = 1;
10201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010202
Michael Chanbbe832c2005-06-24 20:20:04 -070010203 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010204
Linus Torvalds1da177e2005-04-16 15:20:36 -070010205 tp->rx_pending = ering->rx_pending;
10206
10207 if ((tp->tg3_flags2 & TG3_FLG2_MAX_RXPEND_64) &&
10208 tp->rx_pending > 63)
10209 tp->rx_pending = 63;
10210 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010211
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010212 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010213 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010214
10215 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010216 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010217 err = tg3_restart_hw(tp, 1);
10218 if (!err)
10219 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010220 }
10221
David S. Millerf47c11e2005-06-24 20:18:35 -070010222 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010223
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010224 if (irq_sync && !err)
10225 tg3_phy_start(tp);
10226
Michael Chanb9ec6c12006-07-25 16:37:27 -070010227 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010228}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010229
Linus Torvalds1da177e2005-04-16 15:20:36 -070010230static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10231{
10232 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010233
Linus Torvalds1da177e2005-04-16 15:20:36 -070010234 epause->autoneg = (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG) != 0;
Matt Carlson8d018622007-12-20 20:05:44 -080010235
Steve Glendinninge18ce342008-12-16 02:00:00 -080010236 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010237 epause->rx_pause = 1;
10238 else
10239 epause->rx_pause = 0;
10240
Steve Glendinninge18ce342008-12-16 02:00:00 -080010241 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010242 epause->tx_pause = 1;
10243 else
10244 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010245}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010246
Linus Torvalds1da177e2005-04-16 15:20:36 -070010247static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10248{
10249 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010250 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010251
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010252 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson27121682010-02-17 15:16:57 +000010253 u32 newadv;
10254 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010255
Matt Carlson27121682010-02-17 15:16:57 +000010256 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010257
Matt Carlson27121682010-02-17 15:16:57 +000010258 if (!(phydev->supported & SUPPORTED_Pause) ||
10259 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010260 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010261 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010262
Matt Carlson27121682010-02-17 15:16:57 +000010263 tp->link_config.flowctrl = 0;
10264 if (epause->rx_pause) {
10265 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010266
Matt Carlson27121682010-02-17 15:16:57 +000010267 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010268 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010269 newadv = ADVERTISED_Pause;
10270 } else
10271 newadv = ADVERTISED_Pause |
10272 ADVERTISED_Asym_Pause;
10273 } else if (epause->tx_pause) {
10274 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10275 newadv = ADVERTISED_Asym_Pause;
10276 } else
10277 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010278
Matt Carlson27121682010-02-17 15:16:57 +000010279 if (epause->autoneg)
10280 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
10281 else
10282 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
10283
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010284 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010285 u32 oldadv = phydev->advertising &
10286 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10287 if (oldadv != newadv) {
10288 phydev->advertising &=
10289 ~(ADVERTISED_Pause |
10290 ADVERTISED_Asym_Pause);
10291 phydev->advertising |= newadv;
10292 if (phydev->autoneg) {
10293 /*
10294 * Always renegotiate the link to
10295 * inform our link partner of our
10296 * flow control settings, even if the
10297 * flow control is forced. Let
10298 * tg3_adjust_link() do the final
10299 * flow control setup.
10300 */
10301 return phy_start_aneg(phydev);
10302 }
10303 }
10304
10305 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010306 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010307 } else {
10308 tp->link_config.orig_advertising &=
10309 ~(ADVERTISED_Pause |
10310 ADVERTISED_Asym_Pause);
10311 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010312 }
10313 } else {
10314 int irq_sync = 0;
10315
10316 if (netif_running(dev)) {
10317 tg3_netif_stop(tp);
10318 irq_sync = 1;
10319 }
10320
10321 tg3_full_lock(tp, irq_sync);
10322
10323 if (epause->autoneg)
10324 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
10325 else
10326 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
10327 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010328 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010329 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010330 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010331 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010332 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010333 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010334 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010335
10336 if (netif_running(dev)) {
10337 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10338 err = tg3_restart_hw(tp, 1);
10339 if (!err)
10340 tg3_netif_start(tp);
10341 }
10342
10343 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010345
Michael Chanb9ec6c12006-07-25 16:37:27 -070010346 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010347}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010348
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010349static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010350{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010351 switch (sset) {
10352 case ETH_SS_TEST:
10353 return TG3_NUM_TEST;
10354 case ETH_SS_STATS:
10355 return TG3_NUM_STATS;
10356 default:
10357 return -EOPNOTSUPP;
10358 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010359}
10360
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010361static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010362{
10363 switch (stringset) {
10364 case ETH_SS_STATS:
10365 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10366 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010367 case ETH_SS_TEST:
10368 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10369 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010370 default:
10371 WARN_ON(1); /* we need a WARN() */
10372 break;
10373 }
10374}
10375
stephen hemminger81b87092011-04-04 08:43:50 +000010376static int tg3_set_phys_id(struct net_device *dev,
10377 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070010378{
10379 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070010380
10381 if (!netif_running(tp->dev))
10382 return -EAGAIN;
10383
stephen hemminger81b87092011-04-04 08:43:50 +000010384 switch (state) {
10385 case ETHTOOL_ID_ACTIVE:
10386 return -EINVAL;
Michael Chan4009a932005-09-05 17:52:54 -070010387
stephen hemminger81b87092011-04-04 08:43:50 +000010388 case ETHTOOL_ID_ON:
10389 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10390 LED_CTRL_1000MBPS_ON |
10391 LED_CTRL_100MBPS_ON |
10392 LED_CTRL_10MBPS_ON |
10393 LED_CTRL_TRAFFIC_OVERRIDE |
10394 LED_CTRL_TRAFFIC_BLINK |
10395 LED_CTRL_TRAFFIC_LED);
10396 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010397
stephen hemminger81b87092011-04-04 08:43:50 +000010398 case ETHTOOL_ID_OFF:
10399 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10400 LED_CTRL_TRAFFIC_OVERRIDE);
10401 break;
Michael Chan4009a932005-09-05 17:52:54 -070010402
stephen hemminger81b87092011-04-04 08:43:50 +000010403 case ETHTOOL_ID_INACTIVE:
10404 tw32(MAC_LED_CTRL, tp->led_ctrl);
10405 break;
Michael Chan4009a932005-09-05 17:52:54 -070010406 }
stephen hemminger81b87092011-04-04 08:43:50 +000010407
Michael Chan4009a932005-09-05 17:52:54 -070010408 return 0;
10409}
10410
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010411static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010412 struct ethtool_stats *estats, u64 *tmp_stats)
10413{
10414 struct tg3 *tp = netdev_priv(dev);
10415 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10416}
10417
Michael Chan566f86a2005-05-29 14:56:58 -070010418#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010419#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10420#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10421#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Michael Chanb16250e2006-09-27 16:10:14 -070010422#define NVRAM_SELFBOOT_HW_SIZE 0x20
10423#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010424
10425static int tg3_test_nvram(struct tg3 *tp)
10426{
Al Virob9fc7dc2007-12-17 22:59:57 -080010427 u32 csum, magic;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010428 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010429 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010430
Matt Carlsondf259d82009-04-20 06:57:14 +000010431 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
10432 return 0;
10433
Matt Carlsone4f34112009-02-25 14:25:00 +000010434 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010435 return -EIO;
10436
Michael Chan1b277772006-03-20 22:27:48 -080010437 if (magic == TG3_EEPROM_MAGIC)
10438 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010439 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010440 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10441 TG3_EEPROM_SB_FORMAT_1) {
10442 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10443 case TG3_EEPROM_SB_REVISION_0:
10444 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10445 break;
10446 case TG3_EEPROM_SB_REVISION_2:
10447 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10448 break;
10449 case TG3_EEPROM_SB_REVISION_3:
10450 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10451 break;
10452 default:
10453 return 0;
10454 }
10455 } else
Michael Chan1b277772006-03-20 22:27:48 -080010456 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010457 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10458 size = NVRAM_SELFBOOT_HW_SIZE;
10459 else
Michael Chan1b277772006-03-20 22:27:48 -080010460 return -EIO;
10461
10462 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010463 if (buf == NULL)
10464 return -ENOMEM;
10465
Michael Chan1b277772006-03-20 22:27:48 -080010466 err = -EIO;
10467 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010468 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10469 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010470 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010471 }
Michael Chan1b277772006-03-20 22:27:48 -080010472 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010473 goto out;
10474
Michael Chan1b277772006-03-20 22:27:48 -080010475 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010476 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010477 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010478 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010479 u8 *buf8 = (u8 *) buf, csum8 = 0;
10480
Al Virob9fc7dc2007-12-17 22:59:57 -080010481 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010482 TG3_EEPROM_SB_REVISION_2) {
10483 /* For rev 2, the csum doesn't include the MBA. */
10484 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10485 csum8 += buf8[i];
10486 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10487 csum8 += buf8[i];
10488 } else {
10489 for (i = 0; i < size; i++)
10490 csum8 += buf8[i];
10491 }
Michael Chan1b277772006-03-20 22:27:48 -080010492
Adrian Bunkad96b482006-04-05 22:21:04 -070010493 if (csum8 == 0) {
10494 err = 0;
10495 goto out;
10496 }
10497
10498 err = -EIO;
10499 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010500 }
Michael Chan566f86a2005-05-29 14:56:58 -070010501
Al Virob9fc7dc2007-12-17 22:59:57 -080010502 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010503 TG3_EEPROM_MAGIC_HW) {
10504 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010505 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010506 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010507
10508 /* Separate the parity bits and the data bytes. */
10509 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10510 if ((i == 0) || (i == 8)) {
10511 int l;
10512 u8 msk;
10513
10514 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10515 parity[k++] = buf8[i] & msk;
10516 i++;
Matt Carlson859a5882010-04-05 10:19:28 +000010517 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010518 int l;
10519 u8 msk;
10520
10521 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10522 parity[k++] = buf8[i] & msk;
10523 i++;
10524
10525 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10526 parity[k++] = buf8[i] & msk;
10527 i++;
10528 }
10529 data[j++] = buf8[i];
10530 }
10531
10532 err = -EIO;
10533 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10534 u8 hw8 = hweight8(data[i]);
10535
10536 if ((hw8 & 0x1) && parity[i])
10537 goto out;
10538 else if (!(hw8 & 0x1) && !parity[i])
10539 goto out;
10540 }
10541 err = 0;
10542 goto out;
10543 }
10544
Matt Carlson01c3a392011-03-09 16:58:20 +000010545 err = -EIO;
10546
Michael Chan566f86a2005-05-29 14:56:58 -070010547 /* Bootstrap checksum at offset 0x10 */
10548 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000010549 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010550 goto out;
10551
10552 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10553 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000010554 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000010555 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010556
Matt Carlsond4894f32011-03-09 16:58:21 +000010557 for (i = 0; i < TG3_NVM_VPD_LEN; i += 4) {
10558 /* The data is in little-endian format in NVRAM.
10559 * Use the big-endian read routines to preserve
10560 * the byte order as it exists in NVRAM.
10561 */
10562 if (tg3_nvram_read_be32(tp, TG3_NVM_VPD_OFF + i, &buf[i/4]))
10563 goto out;
10564 }
10565
10566 i = pci_vpd_find_tag((u8 *)buf, 0, TG3_NVM_VPD_LEN,
10567 PCI_VPD_LRDT_RO_DATA);
10568 if (i > 0) {
10569 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
10570 if (j < 0)
10571 goto out;
10572
10573 if (i + PCI_VPD_LRDT_TAG_SIZE + j > TG3_NVM_VPD_LEN)
10574 goto out;
10575
10576 i += PCI_VPD_LRDT_TAG_SIZE;
10577 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
10578 PCI_VPD_RO_KEYWORD_CHKSUM);
10579 if (j > 0) {
10580 u8 csum8 = 0;
10581
10582 j += PCI_VPD_INFO_FLD_HDR_SIZE;
10583
10584 for (i = 0; i <= j; i++)
10585 csum8 += ((u8 *)buf)[i];
10586
10587 if (csum8)
10588 goto out;
10589 }
10590 }
10591
Michael Chan566f86a2005-05-29 14:56:58 -070010592 err = 0;
10593
10594out:
10595 kfree(buf);
10596 return err;
10597}
10598
Michael Chanca430072005-05-29 14:57:23 -070010599#define TG3_SERDES_TIMEOUT_SEC 2
10600#define TG3_COPPER_TIMEOUT_SEC 6
10601
10602static int tg3_test_link(struct tg3 *tp)
10603{
10604 int i, max;
10605
10606 if (!netif_running(tp->dev))
10607 return -ENODEV;
10608
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010609 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070010610 max = TG3_SERDES_TIMEOUT_SEC;
10611 else
10612 max = TG3_COPPER_TIMEOUT_SEC;
10613
10614 for (i = 0; i < max; i++) {
10615 if (netif_carrier_ok(tp->dev))
10616 return 0;
10617
10618 if (msleep_interruptible(1000))
10619 break;
10620 }
10621
10622 return -EIO;
10623}
10624
Michael Chana71116d2005-05-29 14:58:11 -070010625/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080010626static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070010627{
Michael Chanb16250e2006-09-27 16:10:14 -070010628 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070010629 u32 offset, read_mask, write_mask, val, save_val, read_val;
10630 static struct {
10631 u16 offset;
10632 u16 flags;
10633#define TG3_FL_5705 0x1
10634#define TG3_FL_NOT_5705 0x2
10635#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070010636#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070010637 u32 read_mask;
10638 u32 write_mask;
10639 } reg_tbl[] = {
10640 /* MAC Control Registers */
10641 { MAC_MODE, TG3_FL_NOT_5705,
10642 0x00000000, 0x00ef6f8c },
10643 { MAC_MODE, TG3_FL_5705,
10644 0x00000000, 0x01ef6b8c },
10645 { MAC_STATUS, TG3_FL_NOT_5705,
10646 0x03800107, 0x00000000 },
10647 { MAC_STATUS, TG3_FL_5705,
10648 0x03800100, 0x00000000 },
10649 { MAC_ADDR_0_HIGH, 0x0000,
10650 0x00000000, 0x0000ffff },
10651 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010652 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070010653 { MAC_RX_MTU_SIZE, 0x0000,
10654 0x00000000, 0x0000ffff },
10655 { MAC_TX_MODE, 0x0000,
10656 0x00000000, 0x00000070 },
10657 { MAC_TX_LENGTHS, 0x0000,
10658 0x00000000, 0x00003fff },
10659 { MAC_RX_MODE, TG3_FL_NOT_5705,
10660 0x00000000, 0x000007fc },
10661 { MAC_RX_MODE, TG3_FL_5705,
10662 0x00000000, 0x000007dc },
10663 { MAC_HASH_REG_0, 0x0000,
10664 0x00000000, 0xffffffff },
10665 { MAC_HASH_REG_1, 0x0000,
10666 0x00000000, 0xffffffff },
10667 { MAC_HASH_REG_2, 0x0000,
10668 0x00000000, 0xffffffff },
10669 { MAC_HASH_REG_3, 0x0000,
10670 0x00000000, 0xffffffff },
10671
10672 /* Receive Data and Receive BD Initiator Control Registers. */
10673 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
10674 0x00000000, 0xffffffff },
10675 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
10676 0x00000000, 0xffffffff },
10677 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
10678 0x00000000, 0x00000003 },
10679 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
10680 0x00000000, 0xffffffff },
10681 { RCVDBDI_STD_BD+0, 0x0000,
10682 0x00000000, 0xffffffff },
10683 { RCVDBDI_STD_BD+4, 0x0000,
10684 0x00000000, 0xffffffff },
10685 { RCVDBDI_STD_BD+8, 0x0000,
10686 0x00000000, 0xffff0002 },
10687 { RCVDBDI_STD_BD+0xc, 0x0000,
10688 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010689
Michael Chana71116d2005-05-29 14:58:11 -070010690 /* Receive BD Initiator Control Registers. */
10691 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
10692 0x00000000, 0xffffffff },
10693 { RCVBDI_STD_THRESH, TG3_FL_5705,
10694 0x00000000, 0x000003ff },
10695 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
10696 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010697
Michael Chana71116d2005-05-29 14:58:11 -070010698 /* Host Coalescing Control Registers. */
10699 { HOSTCC_MODE, TG3_FL_NOT_5705,
10700 0x00000000, 0x00000004 },
10701 { HOSTCC_MODE, TG3_FL_5705,
10702 0x00000000, 0x000000f6 },
10703 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
10704 0x00000000, 0xffffffff },
10705 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
10706 0x00000000, 0x000003ff },
10707 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
10708 0x00000000, 0xffffffff },
10709 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
10710 0x00000000, 0x000003ff },
10711 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
10712 0x00000000, 0xffffffff },
10713 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10714 0x00000000, 0x000000ff },
10715 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
10716 0x00000000, 0xffffffff },
10717 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10718 0x00000000, 0x000000ff },
10719 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
10720 0x00000000, 0xffffffff },
10721 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
10722 0x00000000, 0xffffffff },
10723 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10724 0x00000000, 0xffffffff },
10725 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10726 0x00000000, 0x000000ff },
10727 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10728 0x00000000, 0xffffffff },
10729 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10730 0x00000000, 0x000000ff },
10731 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
10732 0x00000000, 0xffffffff },
10733 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
10734 0x00000000, 0xffffffff },
10735 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
10736 0x00000000, 0xffffffff },
10737 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
10738 0x00000000, 0xffffffff },
10739 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
10740 0x00000000, 0xffffffff },
10741 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
10742 0xffffffff, 0x00000000 },
10743 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
10744 0xffffffff, 0x00000000 },
10745
10746 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070010747 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010748 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070010749 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010750 0x00000000, 0x007fffff },
10751 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
10752 0x00000000, 0x0000003f },
10753 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
10754 0x00000000, 0x000001ff },
10755 { BUFMGR_MB_HIGH_WATER, 0x0000,
10756 0x00000000, 0x000001ff },
10757 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
10758 0xffffffff, 0x00000000 },
10759 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
10760 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010761
Michael Chana71116d2005-05-29 14:58:11 -070010762 /* Mailbox Registers */
10763 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
10764 0x00000000, 0x000001ff },
10765 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
10766 0x00000000, 0x000001ff },
10767 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
10768 0x00000000, 0x000007ff },
10769 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
10770 0x00000000, 0x000001ff },
10771
10772 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
10773 };
10774
Michael Chanb16250e2006-09-27 16:10:14 -070010775 is_5705 = is_5750 = 0;
10776 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chana71116d2005-05-29 14:58:11 -070010777 is_5705 = 1;
Michael Chanb16250e2006-09-27 16:10:14 -070010778 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
10779 is_5750 = 1;
10780 }
Michael Chana71116d2005-05-29 14:58:11 -070010781
10782 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
10783 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
10784 continue;
10785
10786 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
10787 continue;
10788
10789 if ((tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
10790 (reg_tbl[i].flags & TG3_FL_NOT_5788))
10791 continue;
10792
Michael Chanb16250e2006-09-27 16:10:14 -070010793 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
10794 continue;
10795
Michael Chana71116d2005-05-29 14:58:11 -070010796 offset = (u32) reg_tbl[i].offset;
10797 read_mask = reg_tbl[i].read_mask;
10798 write_mask = reg_tbl[i].write_mask;
10799
10800 /* Save the original register content */
10801 save_val = tr32(offset);
10802
10803 /* Determine the read-only value. */
10804 read_val = save_val & read_mask;
10805
10806 /* Write zero to the register, then make sure the read-only bits
10807 * are not changed and the read/write bits are all zeros.
10808 */
10809 tw32(offset, 0);
10810
10811 val = tr32(offset);
10812
10813 /* Test the read-only and read/write bits. */
10814 if (((val & read_mask) != read_val) || (val & write_mask))
10815 goto out;
10816
10817 /* Write ones to all the bits defined by RdMask and WrMask, then
10818 * make sure the read-only bits are not changed and the
10819 * read/write bits are all ones.
10820 */
10821 tw32(offset, read_mask | write_mask);
10822
10823 val = tr32(offset);
10824
10825 /* Test the read-only bits. */
10826 if ((val & read_mask) != read_val)
10827 goto out;
10828
10829 /* Test the read/write bits. */
10830 if ((val & write_mask) != write_mask)
10831 goto out;
10832
10833 tw32(offset, save_val);
10834 }
10835
10836 return 0;
10837
10838out:
Michael Chan9f88f292006-12-07 00:22:54 -080010839 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000010840 netdev_err(tp->dev,
10841 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070010842 tw32(offset, save_val);
10843 return -EIO;
10844}
10845
Michael Chan7942e1d2005-05-29 14:58:36 -070010846static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
10847{
Arjan van de Venf71e1302006-03-03 21:33:57 -050010848 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070010849 int i;
10850 u32 j;
10851
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020010852 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070010853 for (j = 0; j < len; j += 4) {
10854 u32 val;
10855
10856 tg3_write_mem(tp, offset + j, test_pattern[i]);
10857 tg3_read_mem(tp, offset + j, &val);
10858 if (val != test_pattern[i])
10859 return -EIO;
10860 }
10861 }
10862 return 0;
10863}
10864
10865static int tg3_test_memory(struct tg3 *tp)
10866{
10867 static struct mem_entry {
10868 u32 offset;
10869 u32 len;
10870 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080010871 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070010872 { 0x00002000, 0x1c000},
10873 { 0xffffffff, 0x00000}
10874 }, mem_tbl_5705[] = {
10875 { 0x00000100, 0x0000c},
10876 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070010877 { 0x00004000, 0x00800},
10878 { 0x00006000, 0x01000},
10879 { 0x00008000, 0x02000},
10880 { 0x00010000, 0x0e000},
10881 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080010882 }, mem_tbl_5755[] = {
10883 { 0x00000200, 0x00008},
10884 { 0x00004000, 0x00800},
10885 { 0x00006000, 0x00800},
10886 { 0x00008000, 0x02000},
10887 { 0x00010000, 0x0c000},
10888 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070010889 }, mem_tbl_5906[] = {
10890 { 0x00000200, 0x00008},
10891 { 0x00004000, 0x00400},
10892 { 0x00006000, 0x00400},
10893 { 0x00008000, 0x01000},
10894 { 0x00010000, 0x01000},
10895 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010896 }, mem_tbl_5717[] = {
10897 { 0x00000200, 0x00008},
10898 { 0x00010000, 0x0a000},
10899 { 0x00020000, 0x13c00},
10900 { 0xffffffff, 0x00000}
10901 }, mem_tbl_57765[] = {
10902 { 0x00000200, 0x00008},
10903 { 0x00004000, 0x00800},
10904 { 0x00006000, 0x09800},
10905 { 0x00010000, 0x0a000},
10906 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070010907 };
10908 struct mem_entry *mem_tbl;
10909 int err = 0;
10910 int i;
10911
Matt Carlson0a58d662011-04-05 14:22:45 +000010912 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010913 mem_tbl = mem_tbl_5717;
10914 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
10915 mem_tbl = mem_tbl_57765;
10916 else if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlson321d32a2008-11-21 17:22:19 -080010917 mem_tbl = mem_tbl_5755;
10918 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
10919 mem_tbl = mem_tbl_5906;
10920 else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
10921 mem_tbl = mem_tbl_5705;
10922 else
Michael Chan7942e1d2005-05-29 14:58:36 -070010923 mem_tbl = mem_tbl_570x;
10924
10925 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000010926 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
10927 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070010928 break;
10929 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010930
Michael Chan7942e1d2005-05-29 14:58:36 -070010931 return err;
10932}
10933
Michael Chan9f40dea2005-09-05 17:53:06 -070010934#define TG3_MAC_LOOPBACK 0
10935#define TG3_PHY_LOOPBACK 1
10936
10937static int tg3_run_loopback(struct tg3 *tp, int loopback_mode)
Michael Chanc76949a2005-05-29 14:58:59 -070010938{
Michael Chan9f40dea2005-09-05 17:53:06 -070010939 u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010940 u32 desc_idx, coal_now;
Michael Chanc76949a2005-05-29 14:58:59 -070010941 struct sk_buff *skb, *rx_skb;
10942 u8 *tx_data;
10943 dma_addr_t map;
10944 int num_pkts, tx_len, rx_len, i, err;
10945 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000010946 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000010947 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070010948
Matt Carlsonc8873402010-02-12 14:47:11 +000010949 tnapi = &tp->napi[0];
10950 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010951 if (tp->irq_cnt > 1) {
Matt Carlson1da85aa2010-09-30 10:34:34 +000010952 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)
10953 rnapi = &tp->napi[1];
Matt Carlsonc8873402010-02-12 14:47:11 +000010954 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
10955 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010956 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010957 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000010958
Michael Chan9f40dea2005-09-05 17:53:06 -070010959 if (loopback_mode == TG3_MAC_LOOPBACK) {
Michael Chanc94e3942005-09-27 12:12:42 -070010960 /* HW errata - mac loopback fails in some cases on 5780.
10961 * Normal traffic and PHY loopback are not affected by
Matt Carlsonaba49f22011-01-25 15:58:53 +000010962 * errata. Also, the MAC loopback test is deprecated for
10963 * all newer ASIC revisions.
Michael Chanc94e3942005-09-27 12:12:42 -070010964 */
Matt Carlsonaba49f22011-01-25 15:58:53 +000010965 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
10966 (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT))
Michael Chanc94e3942005-09-27 12:12:42 -070010967 return 0;
10968
Matt Carlson49692ca2011-01-25 15:58:52 +000010969 mac_mode = tp->mac_mode &
10970 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
10971 mac_mode |= MAC_MODE_PORT_INT_LPBACK;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010972 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
10973 mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010974 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
Michael Chan3f7045c2006-09-27 16:02:29 -070010975 mac_mode |= MAC_MODE_PORT_MODE_MII;
10976 else
10977 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chan9f40dea2005-09-05 17:53:06 -070010978 tw32(MAC_MODE, mac_mode);
10979 } else if (loopback_mode == TG3_PHY_LOOPBACK) {
Michael Chan3f7045c2006-09-27 16:02:29 -070010980 u32 val;
10981
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010982 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +000010983 tg3_phy_fet_toggle_apd(tp, false);
Michael Chan5d64ad32006-12-07 00:19:40 -080010984 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
10985 } else
10986 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
Michael Chan3f7045c2006-09-27 16:02:29 -070010987
Matt Carlson9ef8ca92007-07-11 19:48:29 -070010988 tg3_phy_toggle_automdix(tp, 0);
10989
Michael Chan3f7045c2006-09-27 16:02:29 -070010990 tg3_writephy(tp, MII_BMCR, val);
Michael Chanc94e3942005-09-27 12:12:42 -070010991 udelay(40);
Michael Chan5d64ad32006-12-07 00:19:40 -080010992
Matt Carlson49692ca2011-01-25 15:58:52 +000010993 mac_mode = tp->mac_mode &
10994 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010995 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson1061b7c2010-02-12 14:47:12 +000010996 tg3_writephy(tp, MII_TG3_FET_PTEST,
10997 MII_TG3_FET_PTEST_FRC_TX_LINK |
10998 MII_TG3_FET_PTEST_FRC_TX_LOCK);
10999 /* The write needs to be flushed for the AC131 */
11000 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
11001 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
Michael Chan5d64ad32006-12-07 00:19:40 -080011002 mac_mode |= MAC_MODE_PORT_MODE_MII;
11003 } else
11004 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chanb16250e2006-09-27 16:10:14 -070011005
Michael Chanc94e3942005-09-27 12:12:42 -070011006 /* reset to prevent losing 1st rx packet intermittently */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011007 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Michael Chanc94e3942005-09-27 12:12:42 -070011008 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
11009 udelay(10);
11010 tw32_f(MAC_RX_MODE, tp->rx_mode);
11011 }
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011012 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlson79eb6902010-02-17 15:17:03 +000011013 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
11014 if (masked_phy_id == TG3_PHY_ID_BCM5401)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011015 mac_mode &= ~MAC_MODE_LINK_POLARITY;
Matt Carlson79eb6902010-02-17 15:17:03 +000011016 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011017 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chanff18ff02006-03-27 23:17:27 -080011018 tg3_writephy(tp, MII_TG3_EXT_CTRL,
11019 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
11020 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011021 tw32(MAC_MODE, mac_mode);
Matt Carlson49692ca2011-01-25 15:58:52 +000011022
11023 /* Wait for link */
11024 for (i = 0; i < 100; i++) {
11025 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
11026 break;
11027 mdelay(1);
11028 }
Matt Carlson859a5882010-04-05 10:19:28 +000011029 } else {
Michael Chan9f40dea2005-09-05 17:53:06 -070011030 return -EINVAL;
Matt Carlson859a5882010-04-05 10:19:28 +000011031 }
Michael Chanc76949a2005-05-29 14:58:59 -070011032
11033 err = -EIO;
11034
Michael Chanc76949a2005-05-29 14:58:59 -070011035 tx_len = 1514;
David S. Millera20e9c62006-07-31 22:38:16 -070011036 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070011037 if (!skb)
11038 return -ENOMEM;
11039
Michael Chanc76949a2005-05-29 14:58:59 -070011040 tx_data = skb_put(skb, tx_len);
11041 memcpy(tx_data, tp->dev->dev_addr, 6);
11042 memset(tx_data + 6, 0x0, 8);
11043
11044 tw32(MAC_RX_MTU_SIZE, tx_len + 4);
11045
11046 for (i = 14; i < tx_len; i++)
11047 tx_data[i] = (u8) (i & 0xff);
11048
Alexander Duyckf4188d82009-12-02 16:48:38 +000011049 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
11050 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000011051 dev_kfree_skb(skb);
11052 return -EIO;
11053 }
Michael Chanc76949a2005-05-29 14:58:59 -070011054
11055 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011056 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011057
11058 udelay(10);
11059
Matt Carlson898a56f2009-08-28 14:02:40 +000011060 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011061
Michael Chanc76949a2005-05-29 14:58:59 -070011062 num_pkts = 0;
11063
Alexander Duyckf4188d82009-12-02 16:48:38 +000011064 tg3_set_txd(tnapi, tnapi->tx_prod, map, tx_len, 0, 1);
Michael Chanc76949a2005-05-29 14:58:59 -070011065
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011066 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011067 num_pkts++;
11068
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011069 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11070 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011071
11072 udelay(10);
11073
Matt Carlson303fc922009-11-02 14:27:34 +000011074 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11075 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011076 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011077 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011078
11079 udelay(10);
11080
Matt Carlson898a56f2009-08-28 14:02:40 +000011081 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11082 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011083 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011084 (rx_idx == (rx_start_idx + num_pkts)))
11085 break;
11086 }
11087
Alexander Duyckf4188d82009-12-02 16:48:38 +000011088 pci_unmap_single(tp->pdev, map, tx_len, PCI_DMA_TODEVICE);
Michael Chanc76949a2005-05-29 14:58:59 -070011089 dev_kfree_skb(skb);
11090
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011091 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011092 goto out;
11093
11094 if (rx_idx != rx_start_idx + num_pkts)
11095 goto out;
11096
Matt Carlson72334482009-08-28 14:03:01 +000011097 desc = &rnapi->rx_rcb[rx_start_idx];
Michael Chanc76949a2005-05-29 14:58:59 -070011098 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11099 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
11100 if (opaque_key != RXD_OPAQUE_RING_STD)
11101 goto out;
11102
11103 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11104 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
11105 goto out;
11106
11107 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) - 4;
11108 if (rx_len != tx_len)
11109 goto out;
11110
Matt Carlson21f581a2009-08-28 14:00:25 +000011111 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
Michael Chanc76949a2005-05-29 14:58:59 -070011112
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +000011113 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx], mapping);
Michael Chanc76949a2005-05-29 14:58:59 -070011114 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len, PCI_DMA_FROMDEVICE);
11115
11116 for (i = 14; i < tx_len; i++) {
11117 if (*(rx_skb->data + i) != (u8) (i & 0xff))
11118 goto out;
11119 }
11120 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011121
Michael Chanc76949a2005-05-29 14:58:59 -070011122 /* tg3_free_rings will unmap and free the rx_skb */
11123out:
11124 return err;
11125}
11126
Michael Chan9f40dea2005-09-05 17:53:06 -070011127#define TG3_MAC_LOOPBACK_FAILED 1
11128#define TG3_PHY_LOOPBACK_FAILED 2
11129#define TG3_LOOPBACK_FAILED (TG3_MAC_LOOPBACK_FAILED | \
11130 TG3_PHY_LOOPBACK_FAILED)
11131
11132static int tg3_test_loopback(struct tg3 *tp)
11133{
11134 int err = 0;
Matt Carlsonab789042011-01-25 15:58:54 +000011135 u32 eee_cap, cpmuctrl = 0;
Michael Chan9f40dea2005-09-05 17:53:06 -070011136
11137 if (!netif_running(tp->dev))
11138 return TG3_LOOPBACK_FAILED;
11139
Matt Carlsonab789042011-01-25 15:58:54 +000011140 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11141 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11142
Michael Chanb9ec6c12006-07-25 16:37:27 -070011143 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011144 if (err) {
11145 err = TG3_LOOPBACK_FAILED;
11146 goto done;
11147 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011148
Matt Carlson6833c042008-11-21 17:18:59 -080011149 /* Turn off gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011150 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080011151 tg3_phy_toggle_apd(tp, false);
11152
Matt Carlson321d32a2008-11-21 17:22:19 -080011153 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070011154 int i;
11155 u32 status;
11156
11157 tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
11158
11159 /* Wait for up to 40 microseconds to acquire lock. */
11160 for (i = 0; i < 4; i++) {
11161 status = tr32(TG3_CPMU_MUTEX_GNT);
11162 if (status == CPMU_MUTEX_GNT_DRIVER)
11163 break;
11164 udelay(10);
11165 }
11166
Matt Carlsonab789042011-01-25 15:58:54 +000011167 if (status != CPMU_MUTEX_GNT_DRIVER) {
11168 err = TG3_LOOPBACK_FAILED;
11169 goto done;
11170 }
Matt Carlson9936bcf2007-10-10 18:03:07 -070011171
Matt Carlsonb2a5c192008-04-03 21:44:44 -070011172 /* Turn off link-based power management. */
Matt Carlsone8750932007-11-12 21:11:51 -080011173 cpmuctrl = tr32(TG3_CPMU_CTRL);
Matt Carlson109115e2008-05-02 16:48:59 -070011174 tw32(TG3_CPMU_CTRL,
11175 cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
11176 CPMU_CTRL_LINK_AWARE_MODE));
Matt Carlson9936bcf2007-10-10 18:03:07 -070011177 }
11178
Michael Chan9f40dea2005-09-05 17:53:06 -070011179 if (tg3_run_loopback(tp, TG3_MAC_LOOPBACK))
11180 err |= TG3_MAC_LOOPBACK_FAILED;
Matt Carlson9936bcf2007-10-10 18:03:07 -070011181
Matt Carlson321d32a2008-11-21 17:22:19 -080011182 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070011183 tw32(TG3_CPMU_CTRL, cpmuctrl);
11184
11185 /* Release the mutex */
11186 tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
11187 }
11188
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011189 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsondd477002008-05-25 23:45:58 -070011190 !(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
Michael Chan9f40dea2005-09-05 17:53:06 -070011191 if (tg3_run_loopback(tp, TG3_PHY_LOOPBACK))
11192 err |= TG3_PHY_LOOPBACK_FAILED;
11193 }
11194
Matt Carlson6833c042008-11-21 17:18:59 -080011195 /* Re-enable gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011196 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080011197 tg3_phy_toggle_apd(tp, true);
11198
Matt Carlsonab789042011-01-25 15:58:54 +000011199done:
11200 tp->phy_flags |= eee_cap;
11201
Michael Chan9f40dea2005-09-05 17:53:06 -070011202 return err;
11203}
11204
Michael Chan4cafd3f2005-05-29 14:56:34 -070011205static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11206 u64 *data)
11207{
Michael Chan566f86a2005-05-29 14:56:58 -070011208 struct tg3 *tp = netdev_priv(dev);
11209
Matt Carlson80096062010-08-02 11:26:06 +000011210 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011211 tg3_power_up(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011212
Michael Chan566f86a2005-05-29 14:56:58 -070011213 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
11214
11215 if (tg3_test_nvram(tp) != 0) {
11216 etest->flags |= ETH_TEST_FL_FAILED;
11217 data[0] = 1;
11218 }
Michael Chanca430072005-05-29 14:57:23 -070011219 if (tg3_test_link(tp) != 0) {
11220 etest->flags |= ETH_TEST_FL_FAILED;
11221 data[1] = 1;
11222 }
Michael Chana71116d2005-05-29 14:58:11 -070011223 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011224 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070011225
Michael Chanbbe832c2005-06-24 20:20:04 -070011226 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011227 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011228 tg3_netif_stop(tp);
11229 irq_sync = 1;
11230 }
11231
11232 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070011233
11234 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080011235 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011236 tg3_halt_cpu(tp, RX_CPU_BASE);
11237 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
11238 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080011239 if (!err)
11240 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011241
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011242 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad2006-03-20 22:27:35 -080011243 tg3_phy_reset(tp);
11244
Michael Chana71116d2005-05-29 14:58:11 -070011245 if (tg3_test_registers(tp) != 0) {
11246 etest->flags |= ETH_TEST_FL_FAILED;
11247 data[2] = 1;
11248 }
Michael Chan7942e1d2005-05-29 14:58:36 -070011249 if (tg3_test_memory(tp) != 0) {
11250 etest->flags |= ETH_TEST_FL_FAILED;
11251 data[3] = 1;
11252 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011253 if ((data[4] = tg3_test_loopback(tp)) != 0)
Michael Chanc76949a2005-05-29 14:58:59 -070011254 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070011255
David S. Millerf47c11e2005-06-24 20:18:35 -070011256 tg3_full_unlock(tp);
11257
Michael Chand4bc3922005-05-29 14:59:20 -070011258 if (tg3_test_interrupt(tp) != 0) {
11259 etest->flags |= ETH_TEST_FL_FAILED;
11260 data[5] = 1;
11261 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011262
11263 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070011264
Michael Chana71116d2005-05-29 14:58:11 -070011265 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11266 if (netif_running(dev)) {
11267 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011268 err2 = tg3_restart_hw(tp, 1);
11269 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070011270 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011271 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011272
11273 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011274
11275 if (irq_sync && !err2)
11276 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011277 }
Matt Carlson80096062010-08-02 11:26:06 +000011278 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011279 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011280
Michael Chan4cafd3f2005-05-29 14:56:34 -070011281}
11282
Linus Torvalds1da177e2005-04-16 15:20:36 -070011283static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
11284{
11285 struct mii_ioctl_data *data = if_mii(ifr);
11286 struct tg3 *tp = netdev_priv(dev);
11287 int err;
11288
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011289 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011290 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011291 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011292 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011293 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000011294 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011295 }
11296
Matt Carlson33f401a2010-04-05 10:19:27 +000011297 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011298 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000011299 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011300
11301 /* fallthru */
11302 case SIOCGMIIREG: {
11303 u32 mii_regval;
11304
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011305 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011306 break; /* We have no PHY */
11307
Matt Carlsonf746a312011-01-25 15:58:51 +000011308 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) ||
11309 ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
11310 !netif_running(dev)))
Michael Chanbc1c7562006-03-20 17:48:03 -080011311 return -EAGAIN;
11312
David S. Millerf47c11e2005-06-24 20:18:35 -070011313 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011314 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070011315 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011316
11317 data->val_out = mii_regval;
11318
11319 return err;
11320 }
11321
11322 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011323 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011324 break; /* We have no PHY */
11325
Matt Carlsonf746a312011-01-25 15:58:51 +000011326 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) ||
11327 ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
11328 !netif_running(dev)))
Michael Chanbc1c7562006-03-20 17:48:03 -080011329 return -EAGAIN;
11330
David S. Millerf47c11e2005-06-24 20:18:35 -070011331 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011332 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070011333 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011334
11335 return err;
11336
11337 default:
11338 /* do nothing */
11339 break;
11340 }
11341 return -EOPNOTSUPP;
11342}
11343
David S. Miller15f98502005-05-18 22:49:26 -070011344static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11345{
11346 struct tg3 *tp = netdev_priv(dev);
11347
11348 memcpy(ec, &tp->coal, sizeof(*ec));
11349 return 0;
11350}
11351
Michael Chand244c892005-07-05 14:42:33 -070011352static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11353{
11354 struct tg3 *tp = netdev_priv(dev);
11355 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11356 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11357
11358 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
11359 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11360 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11361 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11362 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11363 }
11364
11365 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11366 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11367 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11368 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11369 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11370 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11371 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11372 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11373 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11374 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11375 return -EINVAL;
11376
11377 /* No rx interrupts will be generated if both are zero */
11378 if ((ec->rx_coalesce_usecs == 0) &&
11379 (ec->rx_max_coalesced_frames == 0))
11380 return -EINVAL;
11381
11382 /* No tx interrupts will be generated if both are zero */
11383 if ((ec->tx_coalesce_usecs == 0) &&
11384 (ec->tx_max_coalesced_frames == 0))
11385 return -EINVAL;
11386
11387 /* Only copy relevant parameters, ignore all others. */
11388 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11389 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11390 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11391 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11392 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11393 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11394 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11395 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11396 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11397
11398 if (netif_running(dev)) {
11399 tg3_full_lock(tp, 0);
11400 __tg3_set_coalesce(tp, &tp->coal);
11401 tg3_full_unlock(tp);
11402 }
11403 return 0;
11404}
11405
Jeff Garzik7282d492006-09-13 14:30:00 -040011406static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011407 .get_settings = tg3_get_settings,
11408 .set_settings = tg3_set_settings,
11409 .get_drvinfo = tg3_get_drvinfo,
11410 .get_regs_len = tg3_get_regs_len,
11411 .get_regs = tg3_get_regs,
11412 .get_wol = tg3_get_wol,
11413 .set_wol = tg3_set_wol,
11414 .get_msglevel = tg3_get_msglevel,
11415 .set_msglevel = tg3_set_msglevel,
11416 .nway_reset = tg3_nway_reset,
11417 .get_link = ethtool_op_get_link,
11418 .get_eeprom_len = tg3_get_eeprom_len,
11419 .get_eeprom = tg3_get_eeprom,
11420 .set_eeprom = tg3_set_eeprom,
11421 .get_ringparam = tg3_get_ringparam,
11422 .set_ringparam = tg3_set_ringparam,
11423 .get_pauseparam = tg3_get_pauseparam,
11424 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011425 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011426 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000011427 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011428 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011429 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011430 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011431 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011432};
11433
11434static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11435{
Michael Chan1b277772006-03-20 22:27:48 -080011436 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011437
11438 tp->nvram_size = EEPROM_CHIP_SIZE;
11439
Matt Carlsone4f34112009-02-25 14:25:00 +000011440 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011441 return;
11442
Michael Chanb16250e2006-09-27 16:10:14 -070011443 if ((magic != TG3_EEPROM_MAGIC) &&
11444 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11445 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011446 return;
11447
11448 /*
11449 * Size the chip by reading offsets at increasing powers of two.
11450 * When we encounter our validation signature, we know the addressing
11451 * has wrapped around, and thus have our chip size.
11452 */
Michael Chan1b277772006-03-20 22:27:48 -080011453 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011454
11455 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011456 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011457 return;
11458
Michael Chan18201802006-03-20 22:29:15 -080011459 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011460 break;
11461
11462 cursize <<= 1;
11463 }
11464
11465 tp->nvram_size = cursize;
11466}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011467
Linus Torvalds1da177e2005-04-16 15:20:36 -070011468static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11469{
11470 u32 val;
11471
Matt Carlsondf259d82009-04-20 06:57:14 +000011472 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
11473 tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011474 return;
11475
11476 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011477 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011478 tg3_get_eeprom_size(tp);
11479 return;
11480 }
11481
Matt Carlson6d348f22009-02-25 14:25:52 +000011482 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011483 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011484 /* This is confusing. We want to operate on the
11485 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11486 * call will read from NVRAM and byteswap the data
11487 * according to the byteswapping settings for all
11488 * other register accesses. This ensures the data we
11489 * want will always reside in the lower 16-bits.
11490 * However, the data in NVRAM is in LE format, which
11491 * means the data from the NVRAM read will always be
11492 * opposite the endianness of the CPU. The 16-bit
11493 * byteswap then brings the data to CPU endianness.
11494 */
11495 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011496 return;
11497 }
11498 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070011499 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011500}
11501
11502static void __devinit tg3_get_nvram_info(struct tg3 *tp)
11503{
11504 u32 nvcfg1;
11505
11506 nvcfg1 = tr32(NVRAM_CFG1);
11507 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
11508 tp->tg3_flags2 |= TG3_FLG2_FLASH;
Matt Carlson8590a602009-08-28 12:29:16 +000011509 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011510 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11511 tw32(NVRAM_CFG1, nvcfg1);
11512 }
11513
Michael Chan4c987482005-09-05 17:52:38 -070011514 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) ||
Michael Chana4e2b342005-10-26 15:46:52 -070011515 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011516 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011517 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
11518 tp->nvram_jedecnum = JEDEC_ATMEL;
11519 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11520 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11521 break;
11522 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
11523 tp->nvram_jedecnum = JEDEC_ATMEL;
11524 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
11525 break;
11526 case FLASH_VENDOR_ATMEL_EEPROM:
11527 tp->nvram_jedecnum = JEDEC_ATMEL;
11528 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11529 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11530 break;
11531 case FLASH_VENDOR_ST:
11532 tp->nvram_jedecnum = JEDEC_ST;
11533 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
11534 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11535 break;
11536 case FLASH_VENDOR_SAIFUN:
11537 tp->nvram_jedecnum = JEDEC_SAIFUN;
11538 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
11539 break;
11540 case FLASH_VENDOR_SST_SMALL:
11541 case FLASH_VENDOR_SST_LARGE:
11542 tp->nvram_jedecnum = JEDEC_SST;
11543 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
11544 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011545 }
Matt Carlson8590a602009-08-28 12:29:16 +000011546 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011547 tp->nvram_jedecnum = JEDEC_ATMEL;
11548 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11549 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11550 }
11551}
11552
Matt Carlsona1b950d2009-09-01 13:20:17 +000011553static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
11554{
11555 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
11556 case FLASH_5752PAGE_SIZE_256:
11557 tp->nvram_pagesize = 256;
11558 break;
11559 case FLASH_5752PAGE_SIZE_512:
11560 tp->nvram_pagesize = 512;
11561 break;
11562 case FLASH_5752PAGE_SIZE_1K:
11563 tp->nvram_pagesize = 1024;
11564 break;
11565 case FLASH_5752PAGE_SIZE_2K:
11566 tp->nvram_pagesize = 2048;
11567 break;
11568 case FLASH_5752PAGE_SIZE_4K:
11569 tp->nvram_pagesize = 4096;
11570 break;
11571 case FLASH_5752PAGE_SIZE_264:
11572 tp->nvram_pagesize = 264;
11573 break;
11574 case FLASH_5752PAGE_SIZE_528:
11575 tp->nvram_pagesize = 528;
11576 break;
11577 }
11578}
11579
Michael Chan361b4ac2005-04-21 17:11:21 -070011580static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
11581{
11582 u32 nvcfg1;
11583
11584 nvcfg1 = tr32(NVRAM_CFG1);
11585
Michael Chane6af3012005-04-21 17:12:05 -070011586 /* NVRAM protection for TPM */
11587 if (nvcfg1 & (1 << 27))
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011588 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Michael Chane6af3012005-04-21 17:12:05 -070011589
Michael Chan361b4ac2005-04-21 17:11:21 -070011590 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011591 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
11592 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
11593 tp->nvram_jedecnum = JEDEC_ATMEL;
11594 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11595 break;
11596 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11597 tp->nvram_jedecnum = JEDEC_ATMEL;
11598 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11599 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11600 break;
11601 case FLASH_5752VENDOR_ST_M45PE10:
11602 case FLASH_5752VENDOR_ST_M45PE20:
11603 case FLASH_5752VENDOR_ST_M45PE40:
11604 tp->nvram_jedecnum = JEDEC_ST;
11605 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11606 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11607 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070011608 }
11609
11610 if (tp->tg3_flags2 & TG3_FLG2_FLASH) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000011611 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000011612 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070011613 /* For eeprom, set pagesize to maximum eeprom size */
11614 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11615
11616 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11617 tw32(NVRAM_CFG1, nvcfg1);
11618 }
11619}
11620
Michael Chand3c7b882006-03-23 01:28:25 -080011621static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
11622{
Matt Carlson989a9d22007-05-05 11:51:05 -070011623 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080011624
11625 nvcfg1 = tr32(NVRAM_CFG1);
11626
11627 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070011628 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011629 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson989a9d22007-05-05 11:51:05 -070011630 protect = 1;
11631 }
Michael Chand3c7b882006-03-23 01:28:25 -080011632
Matt Carlson989a9d22007-05-05 11:51:05 -070011633 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11634 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011635 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11636 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11637 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11638 case FLASH_5755VENDOR_ATMEL_FLASH_5:
11639 tp->nvram_jedecnum = JEDEC_ATMEL;
11640 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11641 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11642 tp->nvram_pagesize = 264;
11643 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
11644 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
11645 tp->nvram_size = (protect ? 0x3e200 :
11646 TG3_NVRAM_SIZE_512KB);
11647 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
11648 tp->nvram_size = (protect ? 0x1f200 :
11649 TG3_NVRAM_SIZE_256KB);
11650 else
11651 tp->nvram_size = (protect ? 0x1f200 :
11652 TG3_NVRAM_SIZE_128KB);
11653 break;
11654 case FLASH_5752VENDOR_ST_M45PE10:
11655 case FLASH_5752VENDOR_ST_M45PE20:
11656 case FLASH_5752VENDOR_ST_M45PE40:
11657 tp->nvram_jedecnum = JEDEC_ST;
11658 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11659 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11660 tp->nvram_pagesize = 256;
11661 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
11662 tp->nvram_size = (protect ?
11663 TG3_NVRAM_SIZE_64KB :
11664 TG3_NVRAM_SIZE_128KB);
11665 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
11666 tp->nvram_size = (protect ?
11667 TG3_NVRAM_SIZE_64KB :
11668 TG3_NVRAM_SIZE_256KB);
11669 else
11670 tp->nvram_size = (protect ?
11671 TG3_NVRAM_SIZE_128KB :
11672 TG3_NVRAM_SIZE_512KB);
11673 break;
Michael Chand3c7b882006-03-23 01:28:25 -080011674 }
11675}
11676
Michael Chan1b277772006-03-20 22:27:48 -080011677static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
11678{
11679 u32 nvcfg1;
11680
11681 nvcfg1 = tr32(NVRAM_CFG1);
11682
11683 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011684 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
11685 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11686 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
11687 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11688 tp->nvram_jedecnum = JEDEC_ATMEL;
11689 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11690 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080011691
Matt Carlson8590a602009-08-28 12:29:16 +000011692 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11693 tw32(NVRAM_CFG1, nvcfg1);
11694 break;
11695 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11696 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11697 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11698 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11699 tp->nvram_jedecnum = JEDEC_ATMEL;
11700 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11701 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11702 tp->nvram_pagesize = 264;
11703 break;
11704 case FLASH_5752VENDOR_ST_M45PE10:
11705 case FLASH_5752VENDOR_ST_M45PE20:
11706 case FLASH_5752VENDOR_ST_M45PE40:
11707 tp->nvram_jedecnum = JEDEC_ST;
11708 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11709 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11710 tp->nvram_pagesize = 256;
11711 break;
Michael Chan1b277772006-03-20 22:27:48 -080011712 }
11713}
11714
Matt Carlson6b91fa02007-10-10 18:01:09 -070011715static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
11716{
11717 u32 nvcfg1, protect = 0;
11718
11719 nvcfg1 = tr32(NVRAM_CFG1);
11720
11721 /* NVRAM protection for TPM */
11722 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011723 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011724 protect = 1;
11725 }
11726
11727 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11728 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011729 case FLASH_5761VENDOR_ATMEL_ADB021D:
11730 case FLASH_5761VENDOR_ATMEL_ADB041D:
11731 case FLASH_5761VENDOR_ATMEL_ADB081D:
11732 case FLASH_5761VENDOR_ATMEL_ADB161D:
11733 case FLASH_5761VENDOR_ATMEL_MDB021D:
11734 case FLASH_5761VENDOR_ATMEL_MDB041D:
11735 case FLASH_5761VENDOR_ATMEL_MDB081D:
11736 case FLASH_5761VENDOR_ATMEL_MDB161D:
11737 tp->nvram_jedecnum = JEDEC_ATMEL;
11738 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11739 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11740 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
11741 tp->nvram_pagesize = 256;
11742 break;
11743 case FLASH_5761VENDOR_ST_A_M45PE20:
11744 case FLASH_5761VENDOR_ST_A_M45PE40:
11745 case FLASH_5761VENDOR_ST_A_M45PE80:
11746 case FLASH_5761VENDOR_ST_A_M45PE16:
11747 case FLASH_5761VENDOR_ST_M_M45PE20:
11748 case FLASH_5761VENDOR_ST_M_M45PE40:
11749 case FLASH_5761VENDOR_ST_M_M45PE80:
11750 case FLASH_5761VENDOR_ST_M_M45PE16:
11751 tp->nvram_jedecnum = JEDEC_ST;
11752 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11753 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11754 tp->nvram_pagesize = 256;
11755 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011756 }
11757
11758 if (protect) {
11759 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
11760 } else {
11761 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011762 case FLASH_5761VENDOR_ATMEL_ADB161D:
11763 case FLASH_5761VENDOR_ATMEL_MDB161D:
11764 case FLASH_5761VENDOR_ST_A_M45PE16:
11765 case FLASH_5761VENDOR_ST_M_M45PE16:
11766 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
11767 break;
11768 case FLASH_5761VENDOR_ATMEL_ADB081D:
11769 case FLASH_5761VENDOR_ATMEL_MDB081D:
11770 case FLASH_5761VENDOR_ST_A_M45PE80:
11771 case FLASH_5761VENDOR_ST_M_M45PE80:
11772 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
11773 break;
11774 case FLASH_5761VENDOR_ATMEL_ADB041D:
11775 case FLASH_5761VENDOR_ATMEL_MDB041D:
11776 case FLASH_5761VENDOR_ST_A_M45PE40:
11777 case FLASH_5761VENDOR_ST_M_M45PE40:
11778 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11779 break;
11780 case FLASH_5761VENDOR_ATMEL_ADB021D:
11781 case FLASH_5761VENDOR_ATMEL_MDB021D:
11782 case FLASH_5761VENDOR_ST_A_M45PE20:
11783 case FLASH_5761VENDOR_ST_M_M45PE20:
11784 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11785 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011786 }
11787 }
11788}
11789
Michael Chanb5d37722006-09-27 16:06:21 -070011790static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
11791{
11792 tp->nvram_jedecnum = JEDEC_ATMEL;
11793 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11794 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11795}
11796
Matt Carlson321d32a2008-11-21 17:22:19 -080011797static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
11798{
11799 u32 nvcfg1;
11800
11801 nvcfg1 = tr32(NVRAM_CFG1);
11802
11803 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11804 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11805 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11806 tp->nvram_jedecnum = JEDEC_ATMEL;
11807 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11808 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11809
11810 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11811 tw32(NVRAM_CFG1, nvcfg1);
11812 return;
11813 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11814 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11815 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11816 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11817 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11818 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11819 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11820 tp->nvram_jedecnum = JEDEC_ATMEL;
11821 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11822 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11823
11824 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11825 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11826 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11827 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11828 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11829 break;
11830 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11831 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11832 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11833 break;
11834 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11835 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11836 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11837 break;
11838 }
11839 break;
11840 case FLASH_5752VENDOR_ST_M45PE10:
11841 case FLASH_5752VENDOR_ST_M45PE20:
11842 case FLASH_5752VENDOR_ST_M45PE40:
11843 tp->nvram_jedecnum = JEDEC_ST;
11844 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11845 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11846
11847 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11848 case FLASH_5752VENDOR_ST_M45PE10:
11849 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11850 break;
11851 case FLASH_5752VENDOR_ST_M45PE20:
11852 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11853 break;
11854 case FLASH_5752VENDOR_ST_M45PE40:
11855 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11856 break;
11857 }
11858 break;
11859 default:
Matt Carlsondf259d82009-04-20 06:57:14 +000011860 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
Matt Carlson321d32a2008-11-21 17:22:19 -080011861 return;
11862 }
11863
Matt Carlsona1b950d2009-09-01 13:20:17 +000011864 tg3_nvram_get_pagesize(tp, nvcfg1);
11865 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Matt Carlson321d32a2008-11-21 17:22:19 -080011866 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011867}
11868
11869
11870static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
11871{
11872 u32 nvcfg1;
11873
11874 nvcfg1 = tr32(NVRAM_CFG1);
11875
11876 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11877 case FLASH_5717VENDOR_ATMEL_EEPROM:
11878 case FLASH_5717VENDOR_MICRO_EEPROM:
11879 tp->nvram_jedecnum = JEDEC_ATMEL;
11880 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11881 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11882
11883 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11884 tw32(NVRAM_CFG1, nvcfg1);
11885 return;
11886 case FLASH_5717VENDOR_ATMEL_MDB011D:
11887 case FLASH_5717VENDOR_ATMEL_ADB011B:
11888 case FLASH_5717VENDOR_ATMEL_ADB011D:
11889 case FLASH_5717VENDOR_ATMEL_MDB021D:
11890 case FLASH_5717VENDOR_ATMEL_ADB021B:
11891 case FLASH_5717VENDOR_ATMEL_ADB021D:
11892 case FLASH_5717VENDOR_ATMEL_45USPT:
11893 tp->nvram_jedecnum = JEDEC_ATMEL;
11894 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11895 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11896
11897 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11898 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000011899 /* Detect size with tg3_nvram_get_size() */
11900 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011901 case FLASH_5717VENDOR_ATMEL_ADB021B:
11902 case FLASH_5717VENDOR_ATMEL_ADB021D:
11903 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11904 break;
11905 default:
11906 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11907 break;
11908 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011909 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011910 case FLASH_5717VENDOR_ST_M_M25PE10:
11911 case FLASH_5717VENDOR_ST_A_M25PE10:
11912 case FLASH_5717VENDOR_ST_M_M45PE10:
11913 case FLASH_5717VENDOR_ST_A_M45PE10:
11914 case FLASH_5717VENDOR_ST_M_M25PE20:
11915 case FLASH_5717VENDOR_ST_A_M25PE20:
11916 case FLASH_5717VENDOR_ST_M_M45PE20:
11917 case FLASH_5717VENDOR_ST_A_M45PE20:
11918 case FLASH_5717VENDOR_ST_25USPT:
11919 case FLASH_5717VENDOR_ST_45USPT:
11920 tp->nvram_jedecnum = JEDEC_ST;
11921 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11922 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11923
11924 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11925 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000011926 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000011927 /* Detect size with tg3_nvram_get_size() */
11928 break;
11929 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000011930 case FLASH_5717VENDOR_ST_A_M45PE20:
11931 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11932 break;
11933 default:
11934 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11935 break;
11936 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011937 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011938 default:
11939 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
11940 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080011941 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000011942
11943 tg3_nvram_get_pagesize(tp, nvcfg1);
11944 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
11945 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlson321d32a2008-11-21 17:22:19 -080011946}
11947
Matt Carlson9b91b5f2011-04-05 14:22:47 +000011948static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
11949{
11950 u32 nvcfg1, nvmpinstrp;
11951
11952 nvcfg1 = tr32(NVRAM_CFG1);
11953 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
11954
11955 switch (nvmpinstrp) {
11956 case FLASH_5720_EEPROM_HD:
11957 case FLASH_5720_EEPROM_LD:
11958 tp->nvram_jedecnum = JEDEC_ATMEL;
11959 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11960
11961 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11962 tw32(NVRAM_CFG1, nvcfg1);
11963 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
11964 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11965 else
11966 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
11967 return;
11968 case FLASH_5720VENDOR_M_ATMEL_DB011D:
11969 case FLASH_5720VENDOR_A_ATMEL_DB011B:
11970 case FLASH_5720VENDOR_A_ATMEL_DB011D:
11971 case FLASH_5720VENDOR_M_ATMEL_DB021D:
11972 case FLASH_5720VENDOR_A_ATMEL_DB021B:
11973 case FLASH_5720VENDOR_A_ATMEL_DB021D:
11974 case FLASH_5720VENDOR_M_ATMEL_DB041D:
11975 case FLASH_5720VENDOR_A_ATMEL_DB041B:
11976 case FLASH_5720VENDOR_A_ATMEL_DB041D:
11977 case FLASH_5720VENDOR_M_ATMEL_DB081D:
11978 case FLASH_5720VENDOR_A_ATMEL_DB081D:
11979 case FLASH_5720VENDOR_ATMEL_45USPT:
11980 tp->nvram_jedecnum = JEDEC_ATMEL;
11981 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11982 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11983
11984 switch (nvmpinstrp) {
11985 case FLASH_5720VENDOR_M_ATMEL_DB021D:
11986 case FLASH_5720VENDOR_A_ATMEL_DB021B:
11987 case FLASH_5720VENDOR_A_ATMEL_DB021D:
11988 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11989 break;
11990 case FLASH_5720VENDOR_M_ATMEL_DB041D:
11991 case FLASH_5720VENDOR_A_ATMEL_DB041B:
11992 case FLASH_5720VENDOR_A_ATMEL_DB041D:
11993 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11994 break;
11995 case FLASH_5720VENDOR_M_ATMEL_DB081D:
11996 case FLASH_5720VENDOR_A_ATMEL_DB081D:
11997 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
11998 break;
11999 default:
12000 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12001 break;
12002 }
12003 break;
12004 case FLASH_5720VENDOR_M_ST_M25PE10:
12005 case FLASH_5720VENDOR_M_ST_M45PE10:
12006 case FLASH_5720VENDOR_A_ST_M25PE10:
12007 case FLASH_5720VENDOR_A_ST_M45PE10:
12008 case FLASH_5720VENDOR_M_ST_M25PE20:
12009 case FLASH_5720VENDOR_M_ST_M45PE20:
12010 case FLASH_5720VENDOR_A_ST_M25PE20:
12011 case FLASH_5720VENDOR_A_ST_M45PE20:
12012 case FLASH_5720VENDOR_M_ST_M25PE40:
12013 case FLASH_5720VENDOR_M_ST_M45PE40:
12014 case FLASH_5720VENDOR_A_ST_M25PE40:
12015 case FLASH_5720VENDOR_A_ST_M45PE40:
12016 case FLASH_5720VENDOR_M_ST_M25PE80:
12017 case FLASH_5720VENDOR_M_ST_M45PE80:
12018 case FLASH_5720VENDOR_A_ST_M25PE80:
12019 case FLASH_5720VENDOR_A_ST_M45PE80:
12020 case FLASH_5720VENDOR_ST_25USPT:
12021 case FLASH_5720VENDOR_ST_45USPT:
12022 tp->nvram_jedecnum = JEDEC_ST;
12023 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
12024 tp->tg3_flags2 |= TG3_FLG2_FLASH;
12025
12026 switch (nvmpinstrp) {
12027 case FLASH_5720VENDOR_M_ST_M25PE20:
12028 case FLASH_5720VENDOR_M_ST_M45PE20:
12029 case FLASH_5720VENDOR_A_ST_M25PE20:
12030 case FLASH_5720VENDOR_A_ST_M45PE20:
12031 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12032 break;
12033 case FLASH_5720VENDOR_M_ST_M25PE40:
12034 case FLASH_5720VENDOR_M_ST_M45PE40:
12035 case FLASH_5720VENDOR_A_ST_M25PE40:
12036 case FLASH_5720VENDOR_A_ST_M45PE40:
12037 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12038 break;
12039 case FLASH_5720VENDOR_M_ST_M25PE80:
12040 case FLASH_5720VENDOR_M_ST_M45PE80:
12041 case FLASH_5720VENDOR_A_ST_M25PE80:
12042 case FLASH_5720VENDOR_A_ST_M45PE80:
12043 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12044 break;
12045 default:
12046 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12047 break;
12048 }
12049 break;
12050 default:
12051 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
12052 return;
12053 }
12054
12055 tg3_nvram_get_pagesize(tp, nvcfg1);
12056 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
12057 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
12058}
12059
Linus Torvalds1da177e2005-04-16 15:20:36 -070012060/* Chips other than 5700/5701 use the NVRAM for fetching info. */
12061static void __devinit tg3_nvram_init(struct tg3 *tp)
12062{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012063 tw32_f(GRC_EEPROM_ADDR,
12064 (EEPROM_ADDR_FSM_RESET |
12065 (EEPROM_DEFAULT_CLOCK_PERIOD <<
12066 EEPROM_ADDR_CLKPERD_SHIFT)));
12067
Michael Chan9d57f012006-12-07 00:23:25 -080012068 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012069
12070 /* Enable seeprom accesses. */
12071 tw32_f(GRC_LOCAL_CTRL,
12072 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
12073 udelay(100);
12074
12075 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12076 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
12077 tp->tg3_flags |= TG3_FLAG_NVRAM;
12078
Michael Chanec41c7d2006-01-17 02:40:55 -080012079 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000012080 netdev_warn(tp->dev,
12081 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000012082 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080012083 return;
12084 }
Michael Chane6af3012005-04-21 17:12:05 -070012085 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012086
Matt Carlson989a9d22007-05-05 11:51:05 -070012087 tp->nvram_size = 0;
12088
Michael Chan361b4ac2005-04-21 17:11:21 -070012089 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12090 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080012091 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
12092 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070012093 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012094 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12095 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080012096 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012097 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
12098 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070012099 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12100 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000012101 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
12102 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080012103 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012104 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12105 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000012106 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012107 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
12108 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070012109 else
12110 tg3_get_nvram_info(tp);
12111
Matt Carlson989a9d22007-05-05 11:51:05 -070012112 if (tp->nvram_size == 0)
12113 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012114
Michael Chane6af3012005-04-21 17:12:05 -070012115 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080012116 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012117
12118 } else {
12119 tp->tg3_flags &= ~(TG3_FLAG_NVRAM | TG3_FLAG_NVRAM_BUFFERED);
12120
12121 tg3_get_eeprom_size(tp);
12122 }
12123}
12124
Linus Torvalds1da177e2005-04-16 15:20:36 -070012125static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
12126 u32 offset, u32 len, u8 *buf)
12127{
12128 int i, j, rc = 0;
12129 u32 val;
12130
12131 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012132 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012133 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012134
12135 addr = offset + i;
12136
12137 memcpy(&data, buf + i, 4);
12138
Matt Carlson62cedd12009-04-20 14:52:29 -070012139 /*
12140 * The SEEPROM interface expects the data to always be opposite
12141 * the native endian format. We accomplish this by reversing
12142 * all the operations that would have been performed on the
12143 * data from a call to tg3_nvram_read_be32().
12144 */
12145 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012146
12147 val = tr32(GRC_EEPROM_ADDR);
12148 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
12149
12150 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
12151 EEPROM_ADDR_READ);
12152 tw32(GRC_EEPROM_ADDR, val |
12153 (0 << EEPROM_ADDR_DEVID_SHIFT) |
12154 (addr & EEPROM_ADDR_ADDR_MASK) |
12155 EEPROM_ADDR_START |
12156 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012157
Michael Chan9d57f012006-12-07 00:23:25 -080012158 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012159 val = tr32(GRC_EEPROM_ADDR);
12160
12161 if (val & EEPROM_ADDR_COMPLETE)
12162 break;
Michael Chan9d57f012006-12-07 00:23:25 -080012163 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012164 }
12165 if (!(val & EEPROM_ADDR_COMPLETE)) {
12166 rc = -EBUSY;
12167 break;
12168 }
12169 }
12170
12171 return rc;
12172}
12173
12174/* offset and length are dword aligned */
12175static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
12176 u8 *buf)
12177{
12178 int ret = 0;
12179 u32 pagesize = tp->nvram_pagesize;
12180 u32 pagemask = pagesize - 1;
12181 u32 nvram_cmd;
12182 u8 *tmp;
12183
12184 tmp = kmalloc(pagesize, GFP_KERNEL);
12185 if (tmp == NULL)
12186 return -ENOMEM;
12187
12188 while (len) {
12189 int j;
Michael Chane6af3012005-04-21 17:12:05 -070012190 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012191
12192 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012193
Linus Torvalds1da177e2005-04-16 15:20:36 -070012194 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012195 ret = tg3_nvram_read_be32(tp, phy_addr + j,
12196 (__be32 *) (tmp + j));
12197 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012198 break;
12199 }
12200 if (ret)
12201 break;
12202
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012203 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012204 size = pagesize;
12205 if (len < size)
12206 size = len;
12207
12208 len -= size;
12209
12210 memcpy(tmp + page_off, buf, size);
12211
12212 offset = offset + (pagesize - page_off);
12213
Michael Chane6af3012005-04-21 17:12:05 -070012214 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012215
12216 /*
12217 * Before we can erase the flash page, we need
12218 * to issue a special "write enable" command.
12219 */
12220 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12221
12222 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12223 break;
12224
12225 /* Erase the target page */
12226 tw32(NVRAM_ADDR, phy_addr);
12227
12228 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
12229 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
12230
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012231 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012232 break;
12233
12234 /* Issue another write enable to start the write. */
12235 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12236
12237 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12238 break;
12239
12240 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012241 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012242
Al Virob9fc7dc2007-12-17 22:59:57 -080012243 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000012244
Al Virob9fc7dc2007-12-17 22:59:57 -080012245 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012246
12247 tw32(NVRAM_ADDR, phy_addr + j);
12248
12249 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
12250 NVRAM_CMD_WR;
12251
12252 if (j == 0)
12253 nvram_cmd |= NVRAM_CMD_FIRST;
12254 else if (j == (pagesize - 4))
12255 nvram_cmd |= NVRAM_CMD_LAST;
12256
12257 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12258 break;
12259 }
12260 if (ret)
12261 break;
12262 }
12263
12264 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12265 tg3_nvram_exec_cmd(tp, nvram_cmd);
12266
12267 kfree(tmp);
12268
12269 return ret;
12270}
12271
12272/* offset and length are dword aligned */
12273static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
12274 u8 *buf)
12275{
12276 int i, ret = 0;
12277
12278 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012279 u32 page_off, phy_addr, nvram_cmd;
12280 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012281
12282 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080012283 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012284
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012285 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012286
Michael Chan18201802006-03-20 22:29:15 -080012287 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012288
12289 tw32(NVRAM_ADDR, phy_addr);
12290
12291 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
12292
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012293 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012294 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070012295 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012296 nvram_cmd |= NVRAM_CMD_LAST;
12297
12298 if (i == (len - 4))
12299 nvram_cmd |= NVRAM_CMD_LAST;
12300
Matt Carlson321d32a2008-11-21 17:22:19 -080012301 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
12302 !(tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070012303 (tp->nvram_jedecnum == JEDEC_ST) &&
12304 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012305
12306 if ((ret = tg3_nvram_exec_cmd(tp,
12307 NVRAM_CMD_WREN | NVRAM_CMD_GO |
12308 NVRAM_CMD_DONE)))
12309
12310 break;
12311 }
12312 if (!(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
12313 /* We always do complete word writes to eeprom. */
12314 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
12315 }
12316
12317 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12318 break;
12319 }
12320 return ret;
12321}
12322
12323/* offset and length are dword aligned */
12324static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
12325{
12326 int ret;
12327
Linus Torvalds1da177e2005-04-16 15:20:36 -070012328 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070012329 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
12330 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012331 udelay(40);
12332 }
12333
12334 if (!(tp->tg3_flags & TG3_FLAG_NVRAM)) {
12335 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a5882010-04-05 10:19:28 +000012336 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012337 u32 grc_mode;
12338
Michael Chanec41c7d2006-01-17 02:40:55 -080012339 ret = tg3_nvram_lock(tp);
12340 if (ret)
12341 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012342
Michael Chane6af3012005-04-21 17:12:05 -070012343 tg3_enable_nvram_access(tp);
12344 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +000012345 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012346 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012347
12348 grc_mode = tr32(GRC_MODE);
12349 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
12350
12351 if ((tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) ||
12352 !(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
12353
12354 ret = tg3_nvram_write_block_buffered(tp, offset, len,
12355 buf);
Matt Carlson859a5882010-04-05 10:19:28 +000012356 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012357 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
12358 buf);
12359 }
12360
12361 grc_mode = tr32(GRC_MODE);
12362 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
12363
Michael Chane6af3012005-04-21 17:12:05 -070012364 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012365 tg3_nvram_unlock(tp);
12366 }
12367
12368 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070012369 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012370 udelay(40);
12371 }
12372
12373 return ret;
12374}
12375
12376struct subsys_tbl_ent {
12377 u16 subsys_vendor, subsys_devid;
12378 u32 phy_id;
12379};
12380
Matt Carlson24daf2b2010-02-17 15:17:02 +000012381static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012382 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012383 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012384 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012385 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012386 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012387 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012388 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012389 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12390 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
12391 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012392 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012393 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012394 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012395 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12396 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
12397 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012398 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012399 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012400 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012401 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012402 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012403 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012404 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012405
12406 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012407 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012408 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012409 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012410 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012411 { TG3PCI_SUBVENDOR_ID_3COM,
12412 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
12413 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012414 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012415 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012416 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012417
12418 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012419 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012420 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012421 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012422 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012423 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012424 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012425 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012426 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012427
12428 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012429 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012430 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012431 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012432 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012433 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12434 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
12435 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012436 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012437 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012438 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012439
12440 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012441 { TG3PCI_SUBVENDOR_ID_IBM,
12442 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012443};
12444
Matt Carlson24daf2b2010-02-17 15:17:02 +000012445static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012446{
12447 int i;
12448
12449 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
12450 if ((subsys_id_to_phy_id[i].subsys_vendor ==
12451 tp->pdev->subsystem_vendor) &&
12452 (subsys_id_to_phy_id[i].subsys_devid ==
12453 tp->pdev->subsystem_device))
12454 return &subsys_id_to_phy_id[i];
12455 }
12456 return NULL;
12457}
12458
Michael Chan7d0c41e2005-04-21 17:06:20 -070012459static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012460{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012461 u32 val;
Michael Chancaf636c72006-03-22 01:05:31 -080012462 u16 pmcsr;
12463
12464 /* On some early chips the SRAM cannot be accessed in D3hot state,
12465 * so need make sure we're in D0.
12466 */
12467 pci_read_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, &pmcsr);
12468 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
12469 pci_write_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, pmcsr);
12470 msleep(1);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012471
12472 /* Make sure register accesses (indirect or otherwise)
12473 * will function correctly.
12474 */
12475 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12476 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012477
David S. Millerf49639e2006-06-09 11:58:36 -070012478 /* The memory arbiter has to be enabled in order for SRAM accesses
12479 * to succeed. Normally on powerup the tg3 chip firmware will make
12480 * sure it is enabled, but other entities such as system netboot
12481 * code might disable it.
12482 */
12483 val = tr32(MEMARB_MODE);
12484 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
12485
Matt Carlson79eb6902010-02-17 15:17:03 +000012486 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012487 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12488
Gary Zambranoa85feb82007-05-05 11:52:19 -070012489 /* Assume an onboard device and WOL capable by default. */
12490 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT | TG3_FLAG_WOL_CAP;
David S. Miller72b845e2006-03-14 14:11:48 -080012491
Michael Chanb5d37722006-09-27 16:06:21 -070012492 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012493 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Michael Chanb5d37722006-09-27 16:06:21 -070012494 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012495 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12496 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012497 val = tr32(VCPU_CFGSHDW);
12498 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Matt Carlson8ed5d972007-05-07 00:25:49 -070012499 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
Matt Carlson0527ba32007-10-10 18:03:30 -070012500 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Matt Carlson20232762008-12-21 20:18:56 -080012501 (val & VCPU_CFGSHDW_WOL_MAGPKT))
Matt Carlson0527ba32007-10-10 18:03:30 -070012502 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012503 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070012504 }
12505
Linus Torvalds1da177e2005-04-16 15:20:36 -070012506 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12507 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12508 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012509 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012510 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012511
12512 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12513 tp->nic_sram_data_cfg = nic_cfg;
12514
12515 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
12516 ver >>= NIC_SRAM_DATA_VER_SHIFT;
12517 if ((GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700) &&
12518 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) &&
12519 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703) &&
12520 (ver > 0) && (ver < 0x100))
12521 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
12522
Matt Carlsona9daf362008-05-25 23:49:44 -070012523 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
12524 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
12525
Linus Torvalds1da177e2005-04-16 15:20:36 -070012526 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
12527 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
12528 eeprom_phy_serdes = 1;
12529
12530 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
12531 if (nic_phy_id != 0) {
12532 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
12533 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
12534
12535 eeprom_phy_id = (id1 >> 16) << 10;
12536 eeprom_phy_id |= (id2 & 0xfc00) << 16;
12537 eeprom_phy_id |= (id2 & 0x03ff) << 0;
12538 } else
12539 eeprom_phy_id = 0;
12540
Michael Chan7d0c41e2005-04-21 17:06:20 -070012541 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070012542 if (eeprom_phy_serdes) {
Matt Carlsona50d0792010-06-05 17:24:37 +000012543 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012544 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000012545 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012546 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070012547 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070012548
John W. Linvillecbf46852005-04-21 17:01:29 -070012549 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012550 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
12551 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070012552 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070012553 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
12554
12555 switch (led_cfg) {
12556 default:
12557 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
12558 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12559 break;
12560
12561 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
12562 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12563 break;
12564
12565 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
12566 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070012567
12568 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
12569 * read on some older 5700/5701 bootcode.
12570 */
12571 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
12572 ASIC_REV_5700 ||
12573 GET_ASIC_REV(tp->pci_chip_rev_id) ==
12574 ASIC_REV_5701)
12575 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12576
Linus Torvalds1da177e2005-04-16 15:20:36 -070012577 break;
12578
12579 case SHASTA_EXT_LED_SHARED:
12580 tp->led_ctrl = LED_CTRL_MODE_SHARED;
12581 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
12582 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
12583 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12584 LED_CTRL_MODE_PHY_2);
12585 break;
12586
12587 case SHASTA_EXT_LED_MAC:
12588 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
12589 break;
12590
12591 case SHASTA_EXT_LED_COMBO:
12592 tp->led_ctrl = LED_CTRL_MODE_COMBO;
12593 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
12594 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12595 LED_CTRL_MODE_PHY_2);
12596 break;
12597
Stephen Hemminger855e1112008-04-16 16:37:28 -070012598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012599
12600 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12601 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
12602 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
12603 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12604
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012605 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
12606 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080012607
Michael Chan9d26e212006-12-07 00:21:14 -080012608 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012609 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012610 if ((tp->pdev->subsystem_vendor ==
12611 PCI_VENDOR_ID_ARIMA) &&
12612 (tp->pdev->subsystem_device == 0x205a ||
12613 tp->pdev->subsystem_device == 0x2063))
12614 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
12615 } else {
David S. Millerf49639e2006-06-09 11:58:36 -070012616 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012617 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012619
12620 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
12621 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
John W. Linvillecbf46852005-04-21 17:01:29 -070012622 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012623 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
12624 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012625
12626 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
12627 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Matt Carlson0d3031d2007-10-10 18:02:43 -070012628 tp->tg3_flags3 |= TG3_FLG3_ENABLE_APE;
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012629
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012630 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070012631 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
12632 tp->tg3_flags &= ~TG3_FLAG_WOL_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012633
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070012634 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012635 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE))
Matt Carlson0527ba32007-10-10 18:03:30 -070012636 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
12637
Linus Torvalds1da177e2005-04-16 15:20:36 -070012638 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012639 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012640
12641 /* serdes signal pre-emphasis in register 0x590 set by */
12642 /* bootcode if bit 18 is set */
12643 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012644 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070012645
Matt Carlson1407deb2011-04-05 14:22:44 +000012646 if (((tp->tg3_flags3 & TG3_FLG3_57765_PLUS) ||
Matt Carlson2e1e3292010-11-24 08:31:53 +000012647 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
12648 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX))) &&
Matt Carlson6833c042008-11-21 17:18:59 -080012649 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012650 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080012651
Matt Carlson8c69b1e2010-08-02 11:26:00 +000012652 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
12653 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlson1407deb2011-04-05 14:22:44 +000012654 !(tp->tg3_flags3 & TG3_FLG3_57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070012655 u32 cfg3;
12656
12657 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
12658 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
12659 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
12660 }
Matt Carlsona9daf362008-05-25 23:49:44 -070012661
Matt Carlson14417062010-02-17 15:16:59 +000012662 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
12663 tp->tg3_flags3 |= TG3_FLG3_RGMII_INBAND_DISABLE;
Matt Carlsona9daf362008-05-25 23:49:44 -070012664 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
12665 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_RX_EN;
12666 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
12667 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_TX_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012668 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012669done:
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000012670 if (tp->tg3_flags & TG3_FLAG_WOL_CAP)
12671 device_set_wakeup_enable(&tp->pdev->dev,
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012672 tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000012673 else
12674 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012675}
12676
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012677static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
12678{
12679 int i;
12680 u32 val;
12681
12682 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
12683 tw32(OTP_CTRL, cmd);
12684
12685 /* Wait for up to 1 ms for command to execute. */
12686 for (i = 0; i < 100; i++) {
12687 val = tr32(OTP_STATUS);
12688 if (val & OTP_STATUS_CMD_DONE)
12689 break;
12690 udelay(10);
12691 }
12692
12693 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
12694}
12695
12696/* Read the gphy configuration from the OTP region of the chip. The gphy
12697 * configuration is a 32-bit value that straddles the alignment boundary.
12698 * We do two 32-bit reads and then shift and merge the results.
12699 */
12700static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
12701{
12702 u32 bhalf_otp, thalf_otp;
12703
12704 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
12705
12706 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
12707 return 0;
12708
12709 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
12710
12711 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12712 return 0;
12713
12714 thalf_otp = tr32(OTP_READ_DATA);
12715
12716 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
12717
12718 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12719 return 0;
12720
12721 bhalf_otp = tr32(OTP_READ_DATA);
12722
12723 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
12724}
12725
Matt Carlsone256f8a2011-03-09 16:58:24 +000012726static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
12727{
12728 u32 adv = ADVERTISED_Autoneg |
12729 ADVERTISED_Pause;
12730
12731 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
12732 adv |= ADVERTISED_1000baseT_Half |
12733 ADVERTISED_1000baseT_Full;
12734
12735 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
12736 adv |= ADVERTISED_100baseT_Half |
12737 ADVERTISED_100baseT_Full |
12738 ADVERTISED_10baseT_Half |
12739 ADVERTISED_10baseT_Full |
12740 ADVERTISED_TP;
12741 else
12742 adv |= ADVERTISED_FIBRE;
12743
12744 tp->link_config.advertising = adv;
12745 tp->link_config.speed = SPEED_INVALID;
12746 tp->link_config.duplex = DUPLEX_INVALID;
12747 tp->link_config.autoneg = AUTONEG_ENABLE;
12748 tp->link_config.active_speed = SPEED_INVALID;
12749 tp->link_config.active_duplex = DUPLEX_INVALID;
12750 tp->link_config.orig_speed = SPEED_INVALID;
12751 tp->link_config.orig_duplex = DUPLEX_INVALID;
12752 tp->link_config.orig_autoneg = AUTONEG_INVALID;
12753}
12754
Michael Chan7d0c41e2005-04-21 17:06:20 -070012755static int __devinit tg3_phy_probe(struct tg3 *tp)
12756{
12757 u32 hw_phy_id_1, hw_phy_id_2;
12758 u32 hw_phy_id, hw_phy_id_masked;
12759 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012760
Matt Carlsone256f8a2011-03-09 16:58:24 +000012761 /* flow control autonegotiation is default behavior */
12762 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
12763 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
12764
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012765 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
12766 return tg3_phy_init(tp);
12767
Linus Torvalds1da177e2005-04-16 15:20:36 -070012768 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010012769 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012770 */
12771 err = 0;
Matt Carlson0d3031d2007-10-10 18:02:43 -070012772 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
12773 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000012774 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012775 } else {
12776 /* Now read the physical PHY_ID from the chip and verify
12777 * that it is sane. If it doesn't look good, we fall back
12778 * to either the hard-coded table based PHY_ID and failing
12779 * that the value found in the eeprom area.
12780 */
12781 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
12782 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
12783
12784 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
12785 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
12786 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
12787
Matt Carlson79eb6902010-02-17 15:17:03 +000012788 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012789 }
12790
Matt Carlson79eb6902010-02-17 15:17:03 +000012791 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012792 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000012793 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012794 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070012795 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012796 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012797 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000012798 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070012799 /* Do nothing, phy ID already set up in
12800 * tg3_get_eeprom_hw_cfg().
12801 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012802 } else {
12803 struct subsys_tbl_ent *p;
12804
12805 /* No eeprom signature? Try the hardcoded
12806 * subsys device table.
12807 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012808 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012809 if (!p)
12810 return -ENODEV;
12811
12812 tp->phy_id = p->phy_id;
12813 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000012814 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012815 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012816 }
12817 }
12818
Matt Carlsona6b68da2010-12-06 08:28:52 +000012819 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
12820 ((tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
12821 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
12822 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12823 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000012824 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
12825
Matt Carlsone256f8a2011-03-09 16:58:24 +000012826 tg3_phy_init_link_config(tp);
12827
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012828 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlson0d3031d2007-10-10 18:02:43 -070012829 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012830 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan3600d912006-12-07 00:21:48 -080012831 u32 bmsr, adv_reg, tg3_ctrl, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012832
12833 tg3_readphy(tp, MII_BMSR, &bmsr);
12834 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
12835 (bmsr & BMSR_LSTATUS))
12836 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012837
Linus Torvalds1da177e2005-04-16 15:20:36 -070012838 err = tg3_phy_reset(tp);
12839 if (err)
12840 return err;
12841
12842 adv_reg = (ADVERTISE_10HALF | ADVERTISE_10FULL |
12843 ADVERTISE_100HALF | ADVERTISE_100FULL |
12844 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
12845 tg3_ctrl = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012846 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012847 tg3_ctrl = (MII_TG3_CTRL_ADV_1000_HALF |
12848 MII_TG3_CTRL_ADV_1000_FULL);
12849 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
12850 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
12851 tg3_ctrl |= (MII_TG3_CTRL_AS_MASTER |
12852 MII_TG3_CTRL_ENABLE_AS_MASTER);
12853 }
12854
Michael Chan3600d912006-12-07 00:21:48 -080012855 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
12856 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
12857 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
12858 if (!tg3_copper_is_advertising_all(tp, mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012859 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
12860
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012861 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012862 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12863
12864 tg3_writephy(tp, MII_BMCR,
12865 BMCR_ANENABLE | BMCR_ANRESTART);
12866 }
12867 tg3_phy_set_wirespeed(tp);
12868
12869 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012870 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012871 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12872 }
12873
12874skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000012875 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012876 err = tg3_init_5401phy_dsp(tp);
12877 if (err)
12878 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012879
Linus Torvalds1da177e2005-04-16 15:20:36 -070012880 err = tg3_init_5401phy_dsp(tp);
12881 }
12882
Linus Torvalds1da177e2005-04-16 15:20:36 -070012883 return err;
12884}
12885
Matt Carlson184b8902010-04-05 10:19:25 +000012886static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012887{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000012888 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012889 unsigned int block_end, rosize, len;
Matt Carlson184b8902010-04-05 10:19:25 +000012890 int j, i = 0;
Michael Chan1b277772006-03-20 22:27:48 -080012891 u32 magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012892
Matt Carlsondf259d82009-04-20 06:57:14 +000012893 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
12894 tg3_nvram_read(tp, 0x0, &magic))
Matt Carlsona4a8bb12010-09-15 09:00:00 +000012895 goto out_no_vpd;
12896
12897 vpd_data = kmalloc(TG3_NVM_VPD_LEN, GFP_KERNEL);
12898 if (!vpd_data)
12899 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012900
Michael Chan18201802006-03-20 22:29:15 -080012901 if (magic == TG3_EEPROM_MAGIC) {
Matt Carlson141518c2009-12-03 08:36:22 +000012902 for (i = 0; i < TG3_NVM_VPD_LEN; i += 4) {
Michael Chan1b277772006-03-20 22:27:48 -080012903 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012904
Matt Carlson6d348f22009-02-25 14:25:52 +000012905 /* The data is in little-endian format in NVRAM.
12906 * Use the big-endian read routines to preserve
12907 * the byte order as it exists in NVRAM.
12908 */
Matt Carlson141518c2009-12-03 08:36:22 +000012909 if (tg3_nvram_read_be32(tp, TG3_NVM_VPD_OFF + i, &tmp))
Michael Chan1b277772006-03-20 22:27:48 -080012910 goto out_not_found;
12911
Matt Carlson6d348f22009-02-25 14:25:52 +000012912 memcpy(&vpd_data[i], &tmp, sizeof(tmp));
Michael Chan1b277772006-03-20 22:27:48 -080012913 }
12914 } else {
Matt Carlson94c982b2009-12-03 08:36:23 +000012915 ssize_t cnt;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012916 unsigned int pos = 0;
Michael Chan1b277772006-03-20 22:27:48 -080012917
Matt Carlson94c982b2009-12-03 08:36:23 +000012918 for (; pos < TG3_NVM_VPD_LEN && i < 3; i++, pos += cnt) {
12919 cnt = pci_read_vpd(tp->pdev, pos,
12920 TG3_NVM_VPD_LEN - pos,
12921 &vpd_data[pos]);
David Sterba824f5f32010-12-29 03:40:31 +000012922 if (cnt == -ETIMEDOUT || cnt == -EINTR)
Matt Carlson94c982b2009-12-03 08:36:23 +000012923 cnt = 0;
12924 else if (cnt < 0)
David S. Millerf49639e2006-06-09 11:58:36 -070012925 goto out_not_found;
Michael Chan1b277772006-03-20 22:27:48 -080012926 }
Matt Carlson94c982b2009-12-03 08:36:23 +000012927 if (pos != TG3_NVM_VPD_LEN)
12928 goto out_not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012929 }
12930
Matt Carlson4181b2c2010-02-26 14:04:45 +000012931 i = pci_vpd_find_tag(vpd_data, 0, TG3_NVM_VPD_LEN,
12932 PCI_VPD_LRDT_RO_DATA);
12933 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012934 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012935
12936 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
12937 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
12938 i += PCI_VPD_LRDT_TAG_SIZE;
12939
12940 if (block_end > TG3_NVM_VPD_LEN)
12941 goto out_not_found;
12942
Matt Carlson184b8902010-04-05 10:19:25 +000012943 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12944 PCI_VPD_RO_KEYWORD_MFR_ID);
12945 if (j > 0) {
12946 len = pci_vpd_info_field_size(&vpd_data[j]);
12947
12948 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12949 if (j + len > block_end || len != 4 ||
12950 memcmp(&vpd_data[j], "1028", 4))
12951 goto partno;
12952
12953 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12954 PCI_VPD_RO_KEYWORD_VENDOR0);
12955 if (j < 0)
12956 goto partno;
12957
12958 len = pci_vpd_info_field_size(&vpd_data[j]);
12959
12960 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12961 if (j + len > block_end)
12962 goto partno;
12963
12964 memcpy(tp->fw_ver, &vpd_data[j], len);
12965 strncat(tp->fw_ver, " bc ", TG3_NVM_VPD_LEN - len - 1);
12966 }
12967
12968partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000012969 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12970 PCI_VPD_RO_KEYWORD_PARTNO);
12971 if (i < 0)
12972 goto out_not_found;
12973
12974 len = pci_vpd_info_field_size(&vpd_data[i]);
12975
12976 i += PCI_VPD_INFO_FLD_HDR_SIZE;
12977 if (len > TG3_BPN_SIZE ||
12978 (len + i) > TG3_NVM_VPD_LEN)
12979 goto out_not_found;
12980
12981 memcpy(tp->board_part_number, &vpd_data[i], len);
12982
Linus Torvalds1da177e2005-04-16 15:20:36 -070012983out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000012984 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000012985 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000012986 return;
12987
12988out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000012989 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
12990 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
12991 strcpy(tp->board_part_number, "BCM5717");
12992 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
12993 strcpy(tp->board_part_number, "BCM5718");
12994 else
12995 goto nomatch;
12996 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
12997 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
12998 strcpy(tp->board_part_number, "BCM57780");
12999 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
13000 strcpy(tp->board_part_number, "BCM57760");
13001 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
13002 strcpy(tp->board_part_number, "BCM57790");
13003 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
13004 strcpy(tp->board_part_number, "BCM57788");
13005 else
13006 goto nomatch;
13007 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
13008 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
13009 strcpy(tp->board_part_number, "BCM57761");
13010 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
13011 strcpy(tp->board_part_number, "BCM57765");
13012 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
13013 strcpy(tp->board_part_number, "BCM57781");
13014 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
13015 strcpy(tp->board_part_number, "BCM57785");
13016 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
13017 strcpy(tp->board_part_number, "BCM57791");
13018 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13019 strcpy(tp->board_part_number, "BCM57795");
13020 else
13021 goto nomatch;
13022 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070013023 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000013024 } else {
13025nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070013026 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000013027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013028}
13029
Matt Carlson9c8a6202007-10-21 16:16:08 -070013030static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
13031{
13032 u32 val;
13033
Matt Carlsone4f34112009-02-25 14:25:00 +000013034 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013035 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013036 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013037 val != 0)
13038 return 0;
13039
13040 return 1;
13041}
13042
Matt Carlsonacd9c112009-02-25 14:26:33 +000013043static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13044{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013045 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000013046 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013047 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013048
13049 if (tg3_nvram_read(tp, 0xc, &offset) ||
13050 tg3_nvram_read(tp, 0x4, &start))
13051 return;
13052
13053 offset = tg3_nvram_logical_addr(tp, offset);
13054
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013055 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013056 return;
13057
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013058 if ((val & 0xfc000000) == 0x0c000000) {
13059 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013060 return;
13061
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013062 if (val == 0)
13063 newver = true;
13064 }
13065
Matt Carlson75f99362010-04-05 10:19:24 +000013066 dst_off = strlen(tp->fw_ver);
13067
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013068 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013069 if (TG3_VER_SIZE - dst_off < 16 ||
13070 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013071 return;
13072
13073 offset = offset + ver_offset - start;
13074 for (i = 0; i < 16; i += 4) {
13075 __be32 v;
13076 if (tg3_nvram_read_be32(tp, offset + i, &v))
13077 return;
13078
Matt Carlson75f99362010-04-05 10:19:24 +000013079 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013080 }
13081 } else {
13082 u32 major, minor;
13083
13084 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13085 return;
13086
13087 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13088 TG3_NVM_BCVER_MAJSFT;
13089 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013090 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13091 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013092 }
13093}
13094
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013095static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13096{
13097 u32 val, major, minor;
13098
13099 /* Use native endian representation */
13100 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13101 return;
13102
13103 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13104 TG3_NVM_HWSB_CFG1_MAJSFT;
13105 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13106 TG3_NVM_HWSB_CFG1_MINSFT;
13107
13108 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13109}
13110
Matt Carlsondfe00d72008-11-21 17:19:41 -080013111static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13112{
13113 u32 offset, major, minor, build;
13114
Matt Carlson75f99362010-04-05 10:19:24 +000013115 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013116
13117 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13118 return;
13119
13120 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13121 case TG3_EEPROM_SB_REVISION_0:
13122 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13123 break;
13124 case TG3_EEPROM_SB_REVISION_2:
13125 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13126 break;
13127 case TG3_EEPROM_SB_REVISION_3:
13128 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13129 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013130 case TG3_EEPROM_SB_REVISION_4:
13131 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13132 break;
13133 case TG3_EEPROM_SB_REVISION_5:
13134 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13135 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013136 case TG3_EEPROM_SB_REVISION_6:
13137 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13138 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013139 default:
13140 return;
13141 }
13142
Matt Carlsone4f34112009-02-25 14:25:00 +000013143 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013144 return;
13145
13146 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13147 TG3_EEPROM_SB_EDH_BLD_SHFT;
13148 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13149 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13150 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13151
13152 if (minor > 99 || build > 26)
13153 return;
13154
Matt Carlson75f99362010-04-05 10:19:24 +000013155 offset = strlen(tp->fw_ver);
13156 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13157 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013158
13159 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013160 offset = strlen(tp->fw_ver);
13161 if (offset < TG3_VER_SIZE - 1)
13162 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013163 }
13164}
13165
Matt Carlsonacd9c112009-02-25 14:26:33 +000013166static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013167{
13168 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013169 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013170
13171 for (offset = TG3_NVM_DIR_START;
13172 offset < TG3_NVM_DIR_END;
13173 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013174 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013175 return;
13176
13177 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13178 break;
13179 }
13180
13181 if (offset == TG3_NVM_DIR_END)
13182 return;
13183
13184 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
13185 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013186 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013187 return;
13188
Matt Carlsone4f34112009-02-25 14:25:00 +000013189 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013190 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013191 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013192 return;
13193
13194 offset += val - start;
13195
Matt Carlsonacd9c112009-02-25 14:26:33 +000013196 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013197
Matt Carlsonacd9c112009-02-25 14:26:33 +000013198 tp->fw_ver[vlen++] = ',';
13199 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013200
13201 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013202 __be32 v;
13203 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013204 return;
13205
Al Virob9fc7dc2007-12-17 22:59:57 -080013206 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013207
Matt Carlsonacd9c112009-02-25 14:26:33 +000013208 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13209 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013210 break;
13211 }
13212
Matt Carlsonacd9c112009-02-25 14:26:33 +000013213 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13214 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013215 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013216}
13217
Matt Carlson7fd76442009-02-25 14:27:20 +000013218static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13219{
13220 int vlen;
13221 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013222 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013223
13224 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) ||
13225 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
13226 return;
13227
13228 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13229 if (apedata != APE_SEG_SIG_MAGIC)
13230 return;
13231
13232 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13233 if (!(apedata & APE_FW_STATUS_READY))
13234 return;
13235
13236 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13237
Matt Carlsondc6d0742010-09-15 08:59:55 +000013238 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
13239 tp->tg3_flags3 |= TG3_FLG3_APE_HAS_NCSI;
Matt Carlsonecc79642010-08-02 11:26:01 +000013240 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013241 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013242 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013243 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013244
Matt Carlson7fd76442009-02-25 14:27:20 +000013245 vlen = strlen(tp->fw_ver);
13246
Matt Carlsonecc79642010-08-02 11:26:01 +000013247 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13248 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013249 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13250 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13251 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13252 (apedata & APE_FW_VERSION_BLDMSK));
13253}
13254
Matt Carlsonacd9c112009-02-25 14:26:33 +000013255static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13256{
13257 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013258 bool vpd_vers = false;
13259
13260 if (tp->fw_ver[0] != 0)
13261 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013262
Matt Carlsondf259d82009-04-20 06:57:14 +000013263 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) {
Matt Carlson75f99362010-04-05 10:19:24 +000013264 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013265 return;
13266 }
13267
Matt Carlsonacd9c112009-02-25 14:26:33 +000013268 if (tg3_nvram_read(tp, 0, &val))
13269 return;
13270
13271 if (val == TG3_EEPROM_MAGIC)
13272 tg3_read_bc_ver(tp);
13273 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13274 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013275 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13276 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013277 else
13278 return;
13279
13280 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson75f99362010-04-05 10:19:24 +000013281 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) || vpd_vers)
13282 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013283
13284 tg3_read_mgmtfw_ver(tp);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013285
Matt Carlson75f99362010-04-05 10:19:24 +000013286done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013287 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013288}
13289
Michael Chan7544b092007-05-05 13:08:32 -070013290static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
13291
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013292static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13293{
Matt Carlsonde9f5232011-04-05 14:22:43 +000013294 if (tp->tg3_flags3 & TG3_FLG3_LRG_PROD_RING_CAP)
13295 return TG3_RX_RET_MAX_SIZE_5717;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013296 else if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
13297 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013298 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013299 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013300 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013301}
13302
Matt Carlson41434702011-03-09 16:58:22 +000013303static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013304 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13305 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13306 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13307 { },
13308};
13309
Linus Torvalds1da177e2005-04-16 15:20:36 -070013310static int __devinit tg3_get_invariants(struct tg3 *tp)
13311{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013312 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013313 u32 pci_state_reg, grc_misc_cfg;
13314 u32 val;
13315 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013316 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013317
Linus Torvalds1da177e2005-04-16 15:20:36 -070013318 /* Force memory write invalidate off. If we leave it on,
13319 * then on 5700_BX chips we have to enable a workaround.
13320 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13321 * to match the cacheline size. The Broadcom driver have this
13322 * workaround but turns MWI off all the times so never uses
13323 * it. This seems to suggest that the workaround is insufficient.
13324 */
13325 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13326 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13327 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13328
13329 /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
13330 * has the register indirect write enable bit set before
13331 * we try to access any of the MMIO registers. It is also
13332 * critical that the PCI-X hw workaround situation is decided
13333 * before that as well.
13334 */
13335 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13336 &misc_ctrl_reg);
13337
13338 tp->pci_chip_rev_id = (misc_ctrl_reg >>
13339 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070013340 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13341 u32 prod_id_asic_rev;
13342
Matt Carlson5001e2f2009-11-13 13:03:51 +000013343 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13344 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013345 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
13346 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013347 pci_read_config_dword(tp->pdev,
13348 TG3PCI_GEN2_PRODID_ASICREV,
13349 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000013350 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13351 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13352 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13353 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13354 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13355 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13356 pci_read_config_dword(tp->pdev,
13357 TG3PCI_GEN15_PRODID_ASICREV,
13358 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013359 else
13360 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
13361 &prod_id_asic_rev);
13362
Matt Carlson321d32a2008-11-21 17:22:19 -080013363 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070013364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013365
Michael Chanff645be2005-04-21 17:09:53 -070013366 /* Wrong chip ID in 5752 A0. This code can be removed later
13367 * as A0 is not in production.
13368 */
13369 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13370 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13371
Michael Chan68929142005-08-09 20:17:14 -070013372 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
13373 * we need to disable memory and use config. cycles
13374 * only to access all registers. The 5702/03 chips
13375 * can mistakenly decode the special cycles from the
13376 * ICH chipsets as memory write cycles, causing corruption
13377 * of register and memory space. Only certain ICH bridges
13378 * will drive special cycles with non-zero data during the
13379 * address phase which can fall within the 5703's address
13380 * range. This is not an ICH bug as the PCI spec allows
13381 * non-zero address during special cycles. However, only
13382 * these ICH bridges are known to drive non-zero addresses
13383 * during special cycles.
13384 *
13385 * Since special cycles do not cross PCI bridges, we only
13386 * enable this workaround if the 5703 is on the secondary
13387 * bus of these ICH bridges.
13388 */
13389 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
13390 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
13391 static struct tg3_dev_id {
13392 u32 vendor;
13393 u32 device;
13394 u32 rev;
13395 } ich_chipsets[] = {
13396 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
13397 PCI_ANY_ID },
13398 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
13399 PCI_ANY_ID },
13400 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
13401 0xa },
13402 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
13403 PCI_ANY_ID },
13404 { },
13405 };
13406 struct tg3_dev_id *pci_id = &ich_chipsets[0];
13407 struct pci_dev *bridge = NULL;
13408
13409 while (pci_id->vendor != 0) {
13410 bridge = pci_get_device(pci_id->vendor, pci_id->device,
13411 bridge);
13412 if (!bridge) {
13413 pci_id++;
13414 continue;
13415 }
13416 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070013417 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070013418 continue;
13419 }
13420 if (bridge->subordinate &&
13421 (bridge->subordinate->number ==
13422 tp->pdev->bus->number)) {
13423
13424 tp->tg3_flags2 |= TG3_FLG2_ICH_WORKAROUND;
13425 pci_dev_put(bridge);
13426 break;
13427 }
13428 }
13429 }
13430
Matt Carlson41588ba2008-04-19 18:12:33 -070013431 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
13432 static struct tg3_dev_id {
13433 u32 vendor;
13434 u32 device;
13435 } bridge_chipsets[] = {
13436 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
13437 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
13438 { },
13439 };
13440 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
13441 struct pci_dev *bridge = NULL;
13442
13443 while (pci_id->vendor != 0) {
13444 bridge = pci_get_device(pci_id->vendor,
13445 pci_id->device,
13446 bridge);
13447 if (!bridge) {
13448 pci_id++;
13449 continue;
13450 }
13451 if (bridge->subordinate &&
13452 (bridge->subordinate->number <=
13453 tp->pdev->bus->number) &&
13454 (bridge->subordinate->subordinate >=
13455 tp->pdev->bus->number)) {
13456 tp->tg3_flags3 |= TG3_FLG3_5701_DMA_BUG;
13457 pci_dev_put(bridge);
13458 break;
13459 }
13460 }
13461 }
13462
Michael Chan4a29cc22006-03-19 13:21:12 -080013463 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
13464 * DMA addresses > 40-bit. This bridge may have other additional
13465 * 57xx devices behind it in some 4-port NIC designs for example.
13466 * Any tg3 device found behind the bridge will also need the 40-bit
13467 * DMA workaround.
13468 */
Michael Chana4e2b342005-10-26 15:46:52 -070013469 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
13470 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
13471 tp->tg3_flags2 |= TG3_FLG2_5780_CLASS;
Michael Chan4a29cc22006-03-19 13:21:12 -080013472 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
Michael Chan4cf78e42005-07-25 12:29:19 -070013473 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a5882010-04-05 10:19:28 +000013474 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080013475 struct pci_dev *bridge = NULL;
13476
13477 do {
13478 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
13479 PCI_DEVICE_ID_SERVERWORKS_EPB,
13480 bridge);
13481 if (bridge && bridge->subordinate &&
13482 (bridge->subordinate->number <=
13483 tp->pdev->bus->number) &&
13484 (bridge->subordinate->subordinate >=
13485 tp->pdev->bus->number)) {
13486 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
13487 pci_dev_put(bridge);
13488 break;
13489 }
13490 } while (bridge);
13491 }
Michael Chan4cf78e42005-07-25 12:29:19 -070013492
Linus Torvalds1da177e2005-04-16 15:20:36 -070013493 /* Initialize misc host control in PCI block. */
13494 tp->misc_host_ctrl |= (misc_ctrl_reg &
13495 MISC_HOST_CTRL_CHIPREV);
13496 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13497 tp->misc_host_ctrl);
13498
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013499 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
13500 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013501 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13502 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Michael Chan7544b092007-05-05 13:08:32 -070013503 tp->pdev_peer = tg3_find_peer(tp);
13504
Matt Carlsonc885e822010-08-02 11:25:57 +000013505 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013506 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13507 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Matt Carlson0a58d662011-04-05 14:22:45 +000013508 tp->tg3_flags3 |= TG3_FLG3_5717_PLUS;
13509
13510 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
13511 (tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
Matt Carlson1407deb2011-04-05 14:22:44 +000013512 tp->tg3_flags3 |= TG3_FLG3_57765_PLUS;
Matt Carlsonc885e822010-08-02 11:25:57 +000013513
Matt Carlson321d32a2008-11-21 17:22:19 -080013514 /* Intentionally exclude ASIC_REV_5906 */
13515 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad2006-03-20 22:27:35 -080013516 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013517 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013518 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013519 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013520 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson1407deb2011-04-05 14:22:44 +000013521 (tp->tg3_flags3 & TG3_FLG3_57765_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013522 tp->tg3_flags3 |= TG3_FLG3_5755_PLUS;
13523
13524 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13525 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070013526 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013527 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chana4e2b342005-10-26 15:46:52 -070013528 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
John W. Linville6708e5c2005-04-21 17:00:52 -070013529 tp->tg3_flags2 |= TG3_FLG2_5750_PLUS;
13530
John W. Linville1b440c562005-04-21 17:03:18 -070013531 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) ||
13532 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
13533 tp->tg3_flags2 |= TG3_FLG2_5705_PLUS;
13534
Matt Carlson027455a2008-12-21 20:19:30 -080013535 /* 5700 B0 chips do not support checksumming correctly due
13536 * to hardware bugs.
13537 */
Michał Mirosławdc668912011-04-07 03:35:07 +000013538 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
13539 u32 features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013540
Matt Carlson027455a2008-12-21 20:19:30 -080013541 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013542 features |= NETIF_F_IPV6_CSUM;
13543 tp->dev->features |= features;
Michał Mirosławdc668912011-04-07 03:35:07 +000013544 tp->dev->hw_features |= features;
13545 tp->dev->vlan_features |= features;
Matt Carlson027455a2008-12-21 20:19:30 -080013546 }
13547
Matt Carlson507399f2009-11-13 13:03:37 +000013548 /* Determine TSO capabilities */
Matt Carlson2866d952011-02-10 20:06:46 -080013549 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlson4d163b72011-01-25 15:58:48 +000013550 ; /* Do nothing. HW bug. */
Matt Carlson1407deb2011-04-05 14:22:44 +000013551 else if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS)
Matt Carlsone849cdc2009-11-13 13:03:38 +000013552 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_3;
13553 else if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
13554 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlson507399f2009-11-13 13:03:37 +000013555 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_2;
13556 else if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
13557 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_1 | TG3_FLG2_TSO_BUG;
13558 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13559 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
13560 tp->tg3_flags2 &= ~TG3_FLG2_TSO_BUG;
13561 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13562 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13563 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
13564 tp->tg3_flags2 |= TG3_FLG2_TSO_BUG;
13565 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13566 tp->fw_needed = FIRMWARE_TG3TSO5;
13567 else
13568 tp->fw_needed = FIRMWARE_TG3TSO;
13569 }
13570
13571 tp->irq_max = 1;
13572
Michael Chan5a6f3072006-03-20 22:28:05 -080013573 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Michael Chan7544b092007-05-05 13:08:32 -070013574 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSI;
13575 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
13576 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
13577 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
13578 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
13579 tp->pdev_peer == tp->pdev))
13580 tp->tg3_flags &= ~TG3_FLAG_SUPPORT_MSI;
13581
Matt Carlson321d32a2008-11-21 17:22:19 -080013582 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070013583 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080013584 tp->tg3_flags2 |= TG3_FLG2_1SHOT_MSI;
Michael Chan52c0fd82006-06-29 20:15:54 -070013585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013586
Matt Carlson1407deb2011-04-05 14:22:44 +000013587 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
Matt Carlson507399f2009-11-13 13:03:37 +000013588 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSIX;
13589 tp->irq_max = TG3_IRQ_MAX_VECS;
13590 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013591 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000013592
Matt Carlson615774f2009-11-13 13:03:39 +000013593 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsona50d0792010-06-05 17:24:37 +000013594 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson615774f2009-11-13 13:03:39 +000013595 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13596 tp->tg3_flags3 |= TG3_FLG3_SHORT_DMA_BUG;
13597 else if (!(tp->tg3_flags3 & TG3_FLG3_5755_PLUS)) {
13598 tp->tg3_flags3 |= TG3_FLG3_4G_DMA_BNDRY_BUG;
13599 tp->tg3_flags3 |= TG3_FLG3_40BIT_DMA_LIMIT_BUG;
Matt Carlson0e1406d2009-11-02 12:33:33 +000013600 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013601
Matt Carlson0a58d662011-04-05 14:22:45 +000013602 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlsonde9f5232011-04-05 14:22:43 +000013603 tp->tg3_flags3 |= TG3_FLG3_LRG_PROD_RING_CAP;
13604
Matt Carlson1407deb2011-04-05 14:22:44 +000013605 if ((tp->tg3_flags3 & TG3_FLG3_57765_PLUS) &&
Matt Carlson2866d952011-02-10 20:06:46 -080013606 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
Matt Carlsonb703df62009-12-03 08:36:21 +000013607 tp->tg3_flags3 |= TG3_FLG3_USE_JUMBO_BDFLAG;
13608
Matt Carlsonf51f3562008-05-25 23:45:08 -070013609 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsonc6cdf432010-04-05 10:19:26 +000013610 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
13611 (tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG))
Matt Carlson8f666b02009-08-28 13:58:24 +000013612 tp->tg3_flags |= TG3_FLAG_JUMBO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -070013613
Matt Carlson52f44902008-11-21 17:17:04 -080013614 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13615 &pci_state_reg);
13616
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013617 tp->pcie_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_EXP);
13618 if (tp->pcie_cap != 0) {
13619 u16 lnkctl;
13620
Linus Torvalds1da177e2005-04-16 15:20:36 -070013621 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013622
Matt Carlsoncf790032010-11-24 08:31:48 +000013623 tp->pcie_readrq = 4096;
Matt Carlsond78b59f2011-04-05 14:22:46 +000013624 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13625 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Matt Carlsonb4495ed2011-01-25 15:58:47 +000013626 tp->pcie_readrq = 2048;
Matt Carlsoncf790032010-11-24 08:31:48 +000013627
13628 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013629
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013630 pci_read_config_word(tp->pdev,
13631 tp->pcie_cap + PCI_EXP_LNKCTL,
13632 &lnkctl);
13633 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
13634 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanc7835a72006-11-15 21:14:42 -080013635 tp->tg3_flags2 &= ~TG3_FLG2_HW_TSO_2;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013636 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013637 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000013638 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
13639 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013640 tp->tg3_flags3 |= TG3_FLG3_CLKREQ_BUG;
Matt Carlson614b05902010-01-20 16:58:02 +000013641 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
13642 tp->tg3_flags3 |= TG3_FLG3_L1PLLPD_EN;
Michael Chanc7835a72006-11-15 21:14:42 -080013643 }
Matt Carlson52f44902008-11-21 17:17:04 -080013644 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -080013645 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson52f44902008-11-21 17:17:04 -080013646 } else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
13647 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
13648 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
13649 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000013650 dev_err(&tp->pdev->dev,
13651 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080013652 return -EIO;
13653 }
13654
13655 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
13656 tp->tg3_flags |= TG3_FLAG_PCIX_MODE;
13657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013658
Michael Chan399de502005-10-03 14:02:39 -070013659 /* If we have an AMD 762 or VIA K8T800 chipset, write
13660 * reordering to the mailbox registers done by the host
13661 * controller can cause major troubles. We read back from
13662 * every mailbox register write to force the writes to be
13663 * posted to the chip in order.
13664 */
Matt Carlson41434702011-03-09 16:58:22 +000013665 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Michael Chan399de502005-10-03 14:02:39 -070013666 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13667 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
13668
Matt Carlson69fc4052008-12-21 20:19:57 -080013669 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
13670 &tp->pci_cacheline_sz);
13671 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13672 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013673 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13674 tp->pci_lat_timer < 64) {
13675 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080013676 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13677 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013678 }
13679
Matt Carlson52f44902008-11-21 17:17:04 -080013680 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
13681 /* 5700 BX chips need to have their TX producer index
13682 * mailboxes written twice to workaround a bug.
13683 */
13684 tp->tg3_flags |= TG3_FLAG_TXD_MBOX_HWBUG;
Matt Carlson9974a352007-10-07 23:27:28 -070013685
Matt Carlson52f44902008-11-21 17:17:04 -080013686 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013687 *
13688 * The workaround is to use indirect register accesses
13689 * for all chip writes not to mailbox registers.
13690 */
Matt Carlson52f44902008-11-21 17:17:04 -080013691 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013692 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013693
13694 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13695
13696 /* The chip can have it's power management PCI config
13697 * space registers clobbered due to this bug.
13698 * So explicitly force the chip into D0 here.
13699 */
Matt Carlson9974a352007-10-07 23:27:28 -070013700 pci_read_config_dword(tp->pdev,
13701 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013702 &pm_reg);
13703 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
13704 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070013705 pci_write_config_dword(tp->pdev,
13706 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013707 pm_reg);
13708
13709 /* Also, force SERR#/PERR# in PCI command. */
13710 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13711 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
13712 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13713 }
13714 }
13715
Linus Torvalds1da177e2005-04-16 15:20:36 -070013716 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
13717 tp->tg3_flags |= TG3_FLAG_PCI_HIGH_SPEED;
13718 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
13719 tp->tg3_flags |= TG3_FLAG_PCI_32BIT;
13720
13721 /* Chip-specific fixup from Broadcom driver */
13722 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
13723 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
13724 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
13725 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
13726 }
13727
Michael Chan1ee582d2005-08-09 20:16:46 -070013728 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070013729 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013730 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070013731 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070013732 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013733 tp->write32_tx_mbox = tg3_write32;
13734 tp->write32_rx_mbox = tg3_write32;
13735
13736 /* Various workaround register access methods */
13737 if (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG)
13738 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013739 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
13740 ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
13741 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
13742 /*
13743 * Back to back register writes can cause problems on these
13744 * chips, the workaround is to read back all reg writes
13745 * except those to mailbox regs.
13746 *
13747 * See tg3_write_indirect_reg32().
13748 */
Michael Chan1ee582d2005-08-09 20:16:46 -070013749 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013750 }
13751
Michael Chan1ee582d2005-08-09 20:16:46 -070013752 if ((tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG) ||
13753 (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)) {
13754 tp->write32_tx_mbox = tg3_write32_tx_mbox;
13755 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
13756 tp->write32_rx_mbox = tg3_write_flush_reg32;
13757 }
Michael Chan20094932005-08-09 20:16:32 -070013758
Michael Chan68929142005-08-09 20:17:14 -070013759 if (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND) {
13760 tp->read32 = tg3_read_indirect_reg32;
13761 tp->write32 = tg3_write_indirect_reg32;
13762 tp->read32_mbox = tg3_read_indirect_mbox;
13763 tp->write32_mbox = tg3_write_indirect_mbox;
13764 tp->write32_tx_mbox = tg3_write_indirect_mbox;
13765 tp->write32_rx_mbox = tg3_write_indirect_mbox;
13766
13767 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070013768 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070013769
13770 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13771 pci_cmd &= ~PCI_COMMAND_MEMORY;
13772 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13773 }
Michael Chanb5d37722006-09-27 16:06:21 -070013774 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13775 tp->read32_mbox = tg3_read32_mbox_5906;
13776 tp->write32_mbox = tg3_write32_mbox_5906;
13777 tp->write32_tx_mbox = tg3_write32_mbox_5906;
13778 tp->write32_rx_mbox = tg3_write32_mbox_5906;
13779 }
Michael Chan68929142005-08-09 20:17:14 -070013780
Michael Chanbbadf502006-04-06 21:46:34 -070013781 if (tp->write32 == tg3_write_indirect_reg32 ||
13782 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13783 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070013784 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Michael Chanbbadf502006-04-06 21:46:34 -070013785 tp->tg3_flags |= TG3_FLAG_SRAM_USE_CONFIG;
13786
Michael Chan7d0c41e2005-04-21 17:06:20 -070013787 /* Get eeprom hw config before calling tg3_set_power_state().
Michael Chan9d26e212006-12-07 00:21:14 -080013788 * In particular, the TG3_FLG2_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070013789 * determined before calling tg3_set_power_state() so that
13790 * we know whether or not to switch out of Vaux power.
13791 * When the flag is set, it means that GPIO1 is used for eeprom
13792 * write protect and also implies that it is a LOM where GPIOs
13793 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013794 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070013795 tg3_get_eeprom_hw_cfg(tp);
13796
Matt Carlson0d3031d2007-10-10 18:02:43 -070013797 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
13798 /* Allow reads and writes to the
13799 * APE register and memory space.
13800 */
13801 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +000013802 PCISTATE_ALLOW_APE_SHMEM_WR |
13803 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070013804 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
13805 pci_state_reg);
13806 }
13807
Matt Carlson9936bcf2007-10-10 18:03:07 -070013808 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013809 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013810 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013811 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson1407deb2011-04-05 14:22:44 +000013812 (tp->tg3_flags3 & TG3_FLG3_57765_PLUS))
Matt Carlsond30cdd22007-10-07 23:28:35 -070013813 tp->tg3_flags |= TG3_FLAG_CPMU_PRESENT;
13814
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000013815 /* Set up tp->grc_local_ctrl before calling tg_power_up().
Michael Chan314fba32005-04-21 17:07:04 -070013816 * GPIO1 driven high will bring 5700's external PHY out of reset.
13817 * It is also used as eeprom write protect on LOMs.
13818 */
13819 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
13820 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13821 (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT))
13822 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
13823 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070013824 /* Unused GPIO3 must be driven as output on 5752 because there
13825 * are no pull-up resistors on unused GPIO pins.
13826 */
13827 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
13828 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070013829
Matt Carlson321d32a2008-11-21 17:22:19 -080013830 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000013831 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
13832 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Michael Chanaf36e6b2006-03-23 01:28:06 -080013833 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13834
Matt Carlson8d519ab2009-04-20 06:58:01 +000013835 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
13836 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070013837 /* Turn off the debug UART. */
13838 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13839 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
13840 /* Keep VMain power. */
13841 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
13842 GRC_LCLCTRL_GPIO_OUTPUT0;
13843 }
13844
Linus Torvalds1da177e2005-04-16 15:20:36 -070013845 /* Force the chip into D0. */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000013846 err = tg3_power_up(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013847 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000013848 dev_err(&tp->pdev->dev, "Transition to D0 failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070013849 return err;
13850 }
13851
Linus Torvalds1da177e2005-04-16 15:20:36 -070013852 /* Derive initial jumbo mode from MTU assigned in
13853 * ether_setup() via the alloc_etherdev() call
13854 */
Michael Chan0f893dc2005-07-25 12:30:38 -070013855 if (tp->dev->mtu > ETH_DATA_LEN &&
Michael Chana4e2b342005-10-26 15:46:52 -070013856 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan0f893dc2005-07-25 12:30:38 -070013857 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013858
13859 /* Determine WakeOnLan speed to use. */
13860 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13861 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
13862 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
13863 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
13864 tp->tg3_flags &= ~(TG3_FLAG_WOL_SPEED_100MB);
13865 } else {
13866 tp->tg3_flags |= TG3_FLAG_WOL_SPEED_100MB;
13867 }
13868
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013869 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013870 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013871
Linus Torvalds1da177e2005-04-16 15:20:36 -070013872 /* A few boards don't want Ethernet@WireSpeed phy feature */
13873 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13874 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
13875 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070013876 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013877 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
13878 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13879 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013880
13881 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
13882 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013883 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013884 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013885 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013886
Matt Carlson321d32a2008-11-21 17:22:19 -080013887 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013888 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080013889 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013890 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Matt Carlson1407deb2011-04-05 14:22:44 +000013891 !(tp->tg3_flags3 & TG3_FLG3_57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070013892 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013893 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013894 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13895 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080013896 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
13897 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013898 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080013899 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013900 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080013901 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013902 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070013903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013904
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013905 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13906 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
13907 tp->phy_otp = tg3_read_otp_phycfg(tp);
13908 if (tp->phy_otp == 0)
13909 tp->phy_otp = TG3_OTP_DEFAULT;
13910 }
13911
Matt Carlsonf51f3562008-05-25 23:45:08 -070013912 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)
Matt Carlson8ef21422008-05-02 16:47:53 -070013913 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
13914 else
13915 tp->mi_mode = MAC_MI_MODE_BASE;
13916
Linus Torvalds1da177e2005-04-16 15:20:36 -070013917 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013918 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
13919 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
13920 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
13921
Matt Carlson321d32a2008-11-21 17:22:19 -080013922 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
13923 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson57e69832008-05-25 23:48:31 -070013924 tp->tg3_flags3 |= TG3_FLG3_USE_PHYLIB;
13925
Matt Carlson158d7ab2008-05-29 01:37:54 -070013926 err = tg3_mdio_init(tp);
13927 if (err)
13928 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013929
13930 /* Initialize data/descriptor byte/word swapping. */
13931 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000013932 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
13933 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
13934 GRC_MODE_WORD_SWAP_B2HRX_DATA |
13935 GRC_MODE_B2HRX_ENABLE |
13936 GRC_MODE_HTX2B_ENABLE |
13937 GRC_MODE_HOST_STACKUP);
13938 else
13939 val &= GRC_MODE_HOST_STACKUP;
13940
Linus Torvalds1da177e2005-04-16 15:20:36 -070013941 tw32(GRC_MODE, val | tp->grc_mode);
13942
13943 tg3_switch_clocks(tp);
13944
13945 /* Clear this out for sanity. */
13946 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
13947
13948 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13949 &pci_state_reg);
13950 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
13951 (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) == 0) {
13952 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
13953
13954 if (chiprevid == CHIPREV_ID_5701_A0 ||
13955 chiprevid == CHIPREV_ID_5701_B0 ||
13956 chiprevid == CHIPREV_ID_5701_B2 ||
13957 chiprevid == CHIPREV_ID_5701_B5) {
13958 void __iomem *sram_base;
13959
13960 /* Write some dummy words into the SRAM status block
13961 * area, see if it reads back correctly. If the return
13962 * value is bad, force enable the PCIX workaround.
13963 */
13964 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
13965
13966 writel(0x00000000, sram_base);
13967 writel(0x00000000, sram_base + 4);
13968 writel(0xffffffff, sram_base + 4);
13969 if (readl(sram_base) != 0x00000000)
13970 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13971 }
13972 }
13973
13974 udelay(50);
13975 tg3_nvram_init(tp);
13976
13977 grc_misc_cfg = tr32(GRC_MISC_CFG);
13978 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
13979
Linus Torvalds1da177e2005-04-16 15:20:36 -070013980 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13981 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
13982 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
13983 tp->tg3_flags2 |= TG3_FLG2_IS_5788;
13984
David S. Millerfac9b832005-05-18 22:46:34 -070013985 if (!(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
13986 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700))
13987 tp->tg3_flags |= TG3_FLAG_TAGGED_STATUS;
13988 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
13989 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
13990 HOSTCC_MODE_CLRTICK_TXBD);
13991
13992 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
13993 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13994 tp->misc_host_ctrl);
13995 }
13996
Matt Carlson3bda1252008-08-15 14:08:22 -070013997 /* Preserve the APE MAC_MODE bits */
13998 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
Matt Carlsond2394e6b2010-11-24 08:31:47 +000013999 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070014000 else
14001 tp->mac_mode = TG3_DEF_MAC_MODE;
14002
Linus Torvalds1da177e2005-04-16 15:20:36 -070014003 /* these are limited to 10/100 only */
14004 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14005 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14006 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14007 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14008 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
14009 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
14010 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
14011 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14012 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080014013 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
14014 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014015 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000014016 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14017 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014018 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14019 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014020
14021 err = tg3_phy_probe(tp);
14022 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014023 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014024 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014025 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014026 }
14027
Matt Carlson184b8902010-04-05 10:19:25 +000014028 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080014029 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014030
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014031 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
14032 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014033 } else {
14034 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014035 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014036 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014037 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014038 }
14039
14040 /* 5700 {AX,BX} chips have a broken status block link
14041 * change bit implementation, so we must use the
14042 * status register in those cases.
14043 */
14044 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
14045 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
14046 else
14047 tp->tg3_flags &= ~TG3_FLAG_USE_LINKCHG_REG;
14048
14049 /* The led_ctrl is set during tg3_phy_probe, here we might
14050 * have to force the link status polling mechanism based
14051 * upon subsystem IDs.
14052 */
14053 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070014054 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014055 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
14056 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
14057 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014058 }
14059
14060 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014061 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014062 tp->tg3_flags |= TG3_FLAG_POLL_SERDES;
14063 else
14064 tp->tg3_flags &= ~TG3_FLAG_POLL_SERDES;
14065
Matt Carlsonbf933c82011-01-25 15:58:49 +000014066 tp->rx_offset = NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014067 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014068 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsond2757fc2010-04-12 06:58:27 +000014069 (tp->tg3_flags & TG3_FLAG_PCIX_MODE) != 0) {
Matt Carlsonbf933c82011-01-25 15:58:49 +000014070 tp->rx_offset = 0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014071#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000014072 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014073#endif
14074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014075
Matt Carlson2c49a442010-09-30 10:34:35 +000014076 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14077 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014078 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14079
Matt Carlson2c49a442010-09-30 10:34:35 +000014080 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070014081
14082 /* Increment the rx prod index on the rx std ring by at most
14083 * 8 for these chips to workaround hw errata.
14084 */
14085 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14086 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14087 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14088 tp->rx_std_max_post = 8;
14089
Matt Carlson8ed5d972007-05-07 00:25:49 -070014090 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND)
14091 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14092 PCIE_PWR_MGMT_L1_THRESH_MSK;
14093
Linus Torvalds1da177e2005-04-16 15:20:36 -070014094 return err;
14095}
14096
David S. Miller49b6e95f2007-03-29 01:38:42 -070014097#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014098static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14099{
14100 struct net_device *dev = tp->dev;
14101 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014102 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070014103 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014104 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014105
David S. Miller49b6e95f2007-03-29 01:38:42 -070014106 addr = of_get_property(dp, "local-mac-address", &len);
14107 if (addr && len == 6) {
14108 memcpy(dev->dev_addr, addr, 6);
14109 memcpy(dev->perm_addr, dev->dev_addr, 6);
14110 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014111 }
14112 return -ENODEV;
14113}
14114
14115static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14116{
14117 struct net_device *dev = tp->dev;
14118
14119 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070014120 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014121 return 0;
14122}
14123#endif
14124
14125static int __devinit tg3_get_device_address(struct tg3 *tp)
14126{
14127 struct net_device *dev = tp->dev;
14128 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080014129 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014130
David S. Miller49b6e95f2007-03-29 01:38:42 -070014131#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014132 if (!tg3_get_macaddr_sparc(tp))
14133 return 0;
14134#endif
14135
14136 mac_offset = 0x7c;
David S. Millerf49639e2006-06-09 11:58:36 -070014137 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
Michael Chana4e2b342005-10-26 15:46:52 -070014138 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014139 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14140 mac_offset = 0xcc;
14141 if (tg3_nvram_lock(tp))
14142 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14143 else
14144 tg3_nvram_unlock(tp);
Matt Carlson0a58d662011-04-05 14:22:45 +000014145 } else if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlsona50d0792010-06-05 17:24:37 +000014146 if (PCI_FUNC(tp->pdev->devfn) & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014147 mac_offset = 0xcc;
Matt Carlsona50d0792010-06-05 17:24:37 +000014148 if (PCI_FUNC(tp->pdev->devfn) > 1)
14149 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014150 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014151 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014152
14153 /* First try to get it from MAC address mailbox. */
14154 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14155 if ((hi >> 16) == 0x484b) {
14156 dev->dev_addr[0] = (hi >> 8) & 0xff;
14157 dev->dev_addr[1] = (hi >> 0) & 0xff;
14158
14159 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14160 dev->dev_addr[2] = (lo >> 24) & 0xff;
14161 dev->dev_addr[3] = (lo >> 16) & 0xff;
14162 dev->dev_addr[4] = (lo >> 8) & 0xff;
14163 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014164
Michael Chan008652b2006-03-27 23:14:53 -080014165 /* Some old bootcode may report a 0 MAC address in SRAM */
14166 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14167 }
14168 if (!addr_ok) {
14169 /* Next, try NVRAM. */
Matt Carlsondf259d82009-04-20 06:57:14 +000014170 if (!(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) &&
14171 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000014172 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070014173 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14174 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080014175 }
14176 /* Finally just fetch it out of the MAC control regs. */
14177 else {
14178 hi = tr32(MAC_ADDR_0_HIGH);
14179 lo = tr32(MAC_ADDR_0_LOW);
14180
14181 dev->dev_addr[5] = lo & 0xff;
14182 dev->dev_addr[4] = (lo >> 8) & 0xff;
14183 dev->dev_addr[3] = (lo >> 16) & 0xff;
14184 dev->dev_addr[2] = (lo >> 24) & 0xff;
14185 dev->dev_addr[1] = hi & 0xff;
14186 dev->dev_addr[0] = (hi >> 8) & 0xff;
14187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014188 }
14189
14190 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014191#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014192 if (!tg3_get_default_macaddr_sparc(tp))
14193 return 0;
14194#endif
14195 return -EINVAL;
14196 }
John W. Linville2ff43692005-09-12 14:44:20 -070014197 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014198 return 0;
14199}
14200
David S. Miller59e6b432005-05-18 22:50:10 -070014201#define BOUNDARY_SINGLE_CACHELINE 1
14202#define BOUNDARY_MULTI_CACHELINE 2
14203
14204static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14205{
14206 int cacheline_size;
14207 u8 byte;
14208 int goal;
14209
14210 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14211 if (byte == 0)
14212 cacheline_size = 1024;
14213 else
14214 cacheline_size = (int) byte * 4;
14215
14216 /* On 5703 and later chips, the boundary bits have no
14217 * effect.
14218 */
14219 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14220 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
14221 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
14222 goto out;
14223
14224#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14225 goal = BOUNDARY_MULTI_CACHELINE;
14226#else
14227#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14228 goal = BOUNDARY_SINGLE_CACHELINE;
14229#else
14230 goal = 0;
14231#endif
14232#endif
14233
Matt Carlson1407deb2011-04-05 14:22:44 +000014234 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014235 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14236 goto out;
14237 }
14238
David S. Miller59e6b432005-05-18 22:50:10 -070014239 if (!goal)
14240 goto out;
14241
14242 /* PCI controllers on most RISC systems tend to disconnect
14243 * when a device tries to burst across a cache-line boundary.
14244 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14245 *
14246 * Unfortunately, for PCI-E there are only limited
14247 * write-side controls for this, and thus for reads
14248 * we will still get the disconnects. We'll also waste
14249 * these PCI cycles for both read and write for chips
14250 * other than 5700 and 5701 which do not implement the
14251 * boundary bits.
14252 */
14253 if ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
14254 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
14255 switch (cacheline_size) {
14256 case 16:
14257 case 32:
14258 case 64:
14259 case 128:
14260 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14261 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14262 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14263 } else {
14264 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14265 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14266 }
14267 break;
14268
14269 case 256:
14270 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14271 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14272 break;
14273
14274 default:
14275 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14276 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14277 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014278 }
David S. Miller59e6b432005-05-18 22:50:10 -070014279 } else if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14280 switch (cacheline_size) {
14281 case 16:
14282 case 32:
14283 case 64:
14284 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14285 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14286 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14287 break;
14288 }
14289 /* fallthrough */
14290 case 128:
14291 default:
14292 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14293 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14294 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014295 }
David S. Miller59e6b432005-05-18 22:50:10 -070014296 } else {
14297 switch (cacheline_size) {
14298 case 16:
14299 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14300 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14301 DMA_RWCTRL_WRITE_BNDRY_16);
14302 break;
14303 }
14304 /* fallthrough */
14305 case 32:
14306 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14307 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14308 DMA_RWCTRL_WRITE_BNDRY_32);
14309 break;
14310 }
14311 /* fallthrough */
14312 case 64:
14313 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14314 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14315 DMA_RWCTRL_WRITE_BNDRY_64);
14316 break;
14317 }
14318 /* fallthrough */
14319 case 128:
14320 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14321 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14322 DMA_RWCTRL_WRITE_BNDRY_128);
14323 break;
14324 }
14325 /* fallthrough */
14326 case 256:
14327 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14328 DMA_RWCTRL_WRITE_BNDRY_256);
14329 break;
14330 case 512:
14331 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14332 DMA_RWCTRL_WRITE_BNDRY_512);
14333 break;
14334 case 1024:
14335 default:
14336 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14337 DMA_RWCTRL_WRITE_BNDRY_1024);
14338 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014339 }
David S. Miller59e6b432005-05-18 22:50:10 -070014340 }
14341
14342out:
14343 return val;
14344}
14345
Linus Torvalds1da177e2005-04-16 15:20:36 -070014346static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
14347{
14348 struct tg3_internal_buffer_desc test_desc;
14349 u32 sram_dma_descs;
14350 int i, ret;
14351
14352 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
14353
14354 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
14355 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
14356 tw32(RDMAC_STATUS, 0);
14357 tw32(WDMAC_STATUS, 0);
14358
14359 tw32(BUFMGR_MODE, 0);
14360 tw32(FTQ_RESET, 0);
14361
14362 test_desc.addr_hi = ((u64) buf_dma) >> 32;
14363 test_desc.addr_lo = buf_dma & 0xffffffff;
14364 test_desc.nic_mbuf = 0x00002100;
14365 test_desc.len = size;
14366
14367 /*
14368 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
14369 * the *second* time the tg3 driver was getting loaded after an
14370 * initial scan.
14371 *
14372 * Broadcom tells me:
14373 * ...the DMA engine is connected to the GRC block and a DMA
14374 * reset may affect the GRC block in some unpredictable way...
14375 * The behavior of resets to individual blocks has not been tested.
14376 *
14377 * Broadcom noted the GRC reset will also reset all sub-components.
14378 */
14379 if (to_device) {
14380 test_desc.cqid_sqid = (13 << 8) | 2;
14381
14382 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
14383 udelay(40);
14384 } else {
14385 test_desc.cqid_sqid = (16 << 8) | 7;
14386
14387 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
14388 udelay(40);
14389 }
14390 test_desc.flags = 0x00000005;
14391
14392 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
14393 u32 val;
14394
14395 val = *(((u32 *)&test_desc) + i);
14396 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
14397 sram_dma_descs + (i * sizeof(u32)));
14398 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
14399 }
14400 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
14401
Matt Carlson859a5882010-04-05 10:19:28 +000014402 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014403 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a5882010-04-05 10:19:28 +000014404 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014405 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014406
14407 ret = -ENODEV;
14408 for (i = 0; i < 40; i++) {
14409 u32 val;
14410
14411 if (to_device)
14412 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
14413 else
14414 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
14415 if ((val & 0xffff) == sram_dma_descs) {
14416 ret = 0;
14417 break;
14418 }
14419
14420 udelay(100);
14421 }
14422
14423 return ret;
14424}
14425
David S. Millerded73402005-05-23 13:59:47 -070014426#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070014427
Matt Carlson41434702011-03-09 16:58:22 +000014428static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080014429 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14430 { },
14431};
14432
Linus Torvalds1da177e2005-04-16 15:20:36 -070014433static int __devinit tg3_test_dma(struct tg3 *tp)
14434{
14435 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070014436 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014437 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014438
Matt Carlson4bae65c2010-11-24 08:31:52 +000014439 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
14440 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014441 if (!buf) {
14442 ret = -ENOMEM;
14443 goto out_nofree;
14444 }
14445
14446 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
14447 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
14448
David S. Miller59e6b432005-05-18 22:50:10 -070014449 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014450
Matt Carlson1407deb2011-04-05 14:22:44 +000014451 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS)
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014452 goto out;
14453
Linus Torvalds1da177e2005-04-16 15:20:36 -070014454 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14455 /* DMA read watermark not used on PCIE */
14456 tp->dma_rwctrl |= 0x00180000;
14457 } else if (!(tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070014458 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14459 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014460 tp->dma_rwctrl |= 0x003f0000;
14461 else
14462 tp->dma_rwctrl |= 0x003f000f;
14463 } else {
14464 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14465 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
14466 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080014467 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014468
Michael Chan4a29cc22006-03-19 13:21:12 -080014469 /* If the 5704 is behind the EPB bridge, we can
14470 * do the less restrictive ONE_DMA workaround for
14471 * better performance.
14472 */
14473 if ((tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) &&
14474 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14475 tp->dma_rwctrl |= 0x8000;
14476 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014477 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
14478
Michael Chan49afdeb2007-02-13 12:17:03 -080014479 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
14480 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070014481 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080014482 tp->dma_rwctrl |=
14483 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
14484 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
14485 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070014486 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
14487 /* 5780 always in PCIX mode */
14488 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070014489 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
14490 /* 5714 always in PCIX mode */
14491 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014492 } else {
14493 tp->dma_rwctrl |= 0x001b000f;
14494 }
14495 }
14496
14497 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14498 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14499 tp->dma_rwctrl &= 0xfffffff0;
14500
14501 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14502 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
14503 /* Remove this if it causes problems for some boards. */
14504 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
14505
14506 /* On 5700/5701 chips, we need to set this bit.
14507 * Otherwise the chip will issue cacheline transactions
14508 * to streamable DMA memory with not all the byte
14509 * enables turned on. This is an error on several
14510 * RISC PCI controllers, in particular sparc64.
14511 *
14512 * On 5703/5704 chips, this bit has been reassigned
14513 * a different meaning. In particular, it is used
14514 * on those chips to enable a PCI-X workaround.
14515 */
14516 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
14517 }
14518
14519 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14520
14521#if 0
14522 /* Unneeded, already done by tg3_get_invariants. */
14523 tg3_switch_clocks(tp);
14524#endif
14525
Linus Torvalds1da177e2005-04-16 15:20:36 -070014526 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14527 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
14528 goto out;
14529
David S. Miller59e6b432005-05-18 22:50:10 -070014530 /* It is best to perform DMA test with maximum write burst size
14531 * to expose the 5700/5701 write DMA bug.
14532 */
14533 saved_dma_rwctrl = tp->dma_rwctrl;
14534 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14535 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14536
Linus Torvalds1da177e2005-04-16 15:20:36 -070014537 while (1) {
14538 u32 *p = buf, i;
14539
14540 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
14541 p[i] = i;
14542
14543 /* Send the buffer to the chip. */
14544 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
14545 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000014546 dev_err(&tp->pdev->dev,
14547 "%s: Buffer write failed. err = %d\n",
14548 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014549 break;
14550 }
14551
14552#if 0
14553 /* validate data reached card RAM correctly. */
14554 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14555 u32 val;
14556 tg3_read_mem(tp, 0x2100 + (i*4), &val);
14557 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000014558 dev_err(&tp->pdev->dev,
14559 "%s: Buffer corrupted on device! "
14560 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014561 /* ret = -ENODEV here? */
14562 }
14563 p[i] = 0;
14564 }
14565#endif
14566 /* Now read it back. */
14567 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
14568 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014569 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
14570 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014571 break;
14572 }
14573
14574 /* Verify it. */
14575 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14576 if (p[i] == i)
14577 continue;
14578
David S. Miller59e6b432005-05-18 22:50:10 -070014579 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14580 DMA_RWCTRL_WRITE_BNDRY_16) {
14581 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014582 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
14583 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14584 break;
14585 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000014586 dev_err(&tp->pdev->dev,
14587 "%s: Buffer corrupted on read back! "
14588 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014589 ret = -ENODEV;
14590 goto out;
14591 }
14592 }
14593
14594 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
14595 /* Success. */
14596 ret = 0;
14597 break;
14598 }
14599 }
David S. Miller59e6b432005-05-18 22:50:10 -070014600 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14601 DMA_RWCTRL_WRITE_BNDRY_16) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014602
David S. Miller59e6b432005-05-18 22:50:10 -070014603 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070014604 * now look for chipsets that are known to expose the
14605 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070014606 */
Matt Carlson41434702011-03-09 16:58:22 +000014607 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014608 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14609 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a5882010-04-05 10:19:28 +000014610 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014611 /* Safe to use the calculated DMA boundary. */
14612 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a5882010-04-05 10:19:28 +000014613 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070014614
David S. Miller59e6b432005-05-18 22:50:10 -070014615 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014617
14618out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000014619 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014620out_nofree:
14621 return ret;
14622}
14623
Linus Torvalds1da177e2005-04-16 15:20:36 -070014624static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
14625{
Matt Carlson1407deb2011-04-05 14:22:44 +000014626 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
Matt Carlson666bc832010-01-20 16:58:03 +000014627 tp->bufmgr_config.mbuf_read_dma_low_water =
14628 DEFAULT_MB_RDMA_LOW_WATER_5705;
14629 tp->bufmgr_config.mbuf_mac_rx_low_water =
14630 DEFAULT_MB_MACRX_LOW_WATER_57765;
14631 tp->bufmgr_config.mbuf_high_water =
14632 DEFAULT_MB_HIGH_WATER_57765;
14633
14634 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14635 DEFAULT_MB_RDMA_LOW_WATER_5705;
14636 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14637 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
14638 tp->bufmgr_config.mbuf_high_water_jumbo =
14639 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
14640 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chanfdfec172005-07-25 12:31:48 -070014641 tp->bufmgr_config.mbuf_read_dma_low_water =
14642 DEFAULT_MB_RDMA_LOW_WATER_5705;
14643 tp->bufmgr_config.mbuf_mac_rx_low_water =
14644 DEFAULT_MB_MACRX_LOW_WATER_5705;
14645 tp->bufmgr_config.mbuf_high_water =
14646 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070014647 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14648 tp->bufmgr_config.mbuf_mac_rx_low_water =
14649 DEFAULT_MB_MACRX_LOW_WATER_5906;
14650 tp->bufmgr_config.mbuf_high_water =
14651 DEFAULT_MB_HIGH_WATER_5906;
14652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014653
Michael Chanfdfec172005-07-25 12:31:48 -070014654 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14655 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
14656 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14657 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
14658 tp->bufmgr_config.mbuf_high_water_jumbo =
14659 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
14660 } else {
14661 tp->bufmgr_config.mbuf_read_dma_low_water =
14662 DEFAULT_MB_RDMA_LOW_WATER;
14663 tp->bufmgr_config.mbuf_mac_rx_low_water =
14664 DEFAULT_MB_MACRX_LOW_WATER;
14665 tp->bufmgr_config.mbuf_high_water =
14666 DEFAULT_MB_HIGH_WATER;
14667
14668 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14669 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
14670 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14671 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
14672 tp->bufmgr_config.mbuf_high_water_jumbo =
14673 DEFAULT_MB_HIGH_WATER_JUMBO;
14674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014675
14676 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
14677 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
14678}
14679
14680static char * __devinit tg3_phy_string(struct tg3 *tp)
14681{
Matt Carlson79eb6902010-02-17 15:17:03 +000014682 switch (tp->phy_id & TG3_PHY_ID_MASK) {
14683 case TG3_PHY_ID_BCM5400: return "5400";
14684 case TG3_PHY_ID_BCM5401: return "5401";
14685 case TG3_PHY_ID_BCM5411: return "5411";
14686 case TG3_PHY_ID_BCM5701: return "5701";
14687 case TG3_PHY_ID_BCM5703: return "5703";
14688 case TG3_PHY_ID_BCM5704: return "5704";
14689 case TG3_PHY_ID_BCM5705: return "5705";
14690 case TG3_PHY_ID_BCM5750: return "5750";
14691 case TG3_PHY_ID_BCM5752: return "5752";
14692 case TG3_PHY_ID_BCM5714: return "5714";
14693 case TG3_PHY_ID_BCM5780: return "5780";
14694 case TG3_PHY_ID_BCM5755: return "5755";
14695 case TG3_PHY_ID_BCM5787: return "5787";
14696 case TG3_PHY_ID_BCM5784: return "5784";
14697 case TG3_PHY_ID_BCM5756: return "5722/5756";
14698 case TG3_PHY_ID_BCM5906: return "5906";
14699 case TG3_PHY_ID_BCM5761: return "5761";
14700 case TG3_PHY_ID_BCM5718C: return "5718C";
14701 case TG3_PHY_ID_BCM5718S: return "5718S";
14702 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000014703 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000014704 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000014705 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070014706 case 0: return "serdes";
14707 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070014708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014709}
14710
Michael Chanf9804dd2005-09-27 12:13:10 -070014711static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
14712{
14713 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14714 strcpy(str, "PCI Express");
14715 return str;
14716 } else if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
14717 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
14718
14719 strcpy(str, "PCIX:");
14720
14721 if ((clock_ctrl == 7) ||
14722 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
14723 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
14724 strcat(str, "133MHz");
14725 else if (clock_ctrl == 0)
14726 strcat(str, "33MHz");
14727 else if (clock_ctrl == 2)
14728 strcat(str, "50MHz");
14729 else if (clock_ctrl == 4)
14730 strcat(str, "66MHz");
14731 else if (clock_ctrl == 6)
14732 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070014733 } else {
14734 strcpy(str, "PCI:");
14735 if (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED)
14736 strcat(str, "66MHz");
14737 else
14738 strcat(str, "33MHz");
14739 }
14740 if (tp->tg3_flags & TG3_FLAG_PCI_32BIT)
14741 strcat(str, ":32-bit");
14742 else
14743 strcat(str, ":64-bit");
14744 return str;
14745}
14746
Michael Chan8c2dc7e2005-12-19 16:26:02 -080014747static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014748{
14749 struct pci_dev *peer;
14750 unsigned int func, devnr = tp->pdev->devfn & ~7;
14751
14752 for (func = 0; func < 8; func++) {
14753 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14754 if (peer && peer != tp->pdev)
14755 break;
14756 pci_dev_put(peer);
14757 }
Michael Chan16fe9d72005-12-13 21:09:54 -080014758 /* 5704 can be configured in single-port mode, set peer to
14759 * tp->pdev in that case.
14760 */
14761 if (!peer) {
14762 peer = tp->pdev;
14763 return peer;
14764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014765
14766 /*
14767 * We don't need to keep the refcount elevated; there's no way
14768 * to remove one half of this device without removing the other
14769 */
14770 pci_dev_put(peer);
14771
14772 return peer;
14773}
14774
David S. Miller15f98502005-05-18 22:49:26 -070014775static void __devinit tg3_init_coal(struct tg3 *tp)
14776{
14777 struct ethtool_coalesce *ec = &tp->coal;
14778
14779 memset(ec, 0, sizeof(*ec));
14780 ec->cmd = ETHTOOL_GCOALESCE;
14781 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
14782 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
14783 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
14784 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
14785 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
14786 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
14787 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
14788 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
14789 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
14790
14791 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
14792 HOSTCC_MODE_CLRTICK_TXBD)) {
14793 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
14794 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
14795 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
14796 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
14797 }
Michael Chand244c892005-07-05 14:42:33 -070014798
14799 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
14800 ec->rx_coalesce_usecs_irq = 0;
14801 ec->tx_coalesce_usecs_irq = 0;
14802 ec->stats_block_coalesce_usecs = 0;
14803 }
David S. Miller15f98502005-05-18 22:49:26 -070014804}
14805
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014806static const struct net_device_ops tg3_netdev_ops = {
14807 .ndo_open = tg3_open,
14808 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080014809 .ndo_start_xmit = tg3_start_xmit,
Eric Dumazet511d2222010-07-07 20:44:24 +000014810 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -080014811 .ndo_validate_addr = eth_validate_addr,
14812 .ndo_set_multicast_list = tg3_set_rx_mode,
14813 .ndo_set_mac_address = tg3_set_mac_addr,
14814 .ndo_do_ioctl = tg3_ioctl,
14815 .ndo_tx_timeout = tg3_tx_timeout,
14816 .ndo_change_mtu = tg3_change_mtu,
Michał Mirosławdc668912011-04-07 03:35:07 +000014817 .ndo_fix_features = tg3_fix_features,
Stephen Hemminger00829822008-11-20 20:14:53 -080014818#ifdef CONFIG_NET_POLL_CONTROLLER
14819 .ndo_poll_controller = tg3_poll_controller,
14820#endif
14821};
14822
14823static const struct net_device_ops tg3_netdev_ops_dma_bug = {
14824 .ndo_open = tg3_open,
14825 .ndo_stop = tg3_close,
14826 .ndo_start_xmit = tg3_start_xmit_dma_bug,
Eric Dumazet511d2222010-07-07 20:44:24 +000014827 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014828 .ndo_validate_addr = eth_validate_addr,
14829 .ndo_set_multicast_list = tg3_set_rx_mode,
14830 .ndo_set_mac_address = tg3_set_mac_addr,
14831 .ndo_do_ioctl = tg3_ioctl,
14832 .ndo_tx_timeout = tg3_tx_timeout,
14833 .ndo_change_mtu = tg3_change_mtu,
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014834#ifdef CONFIG_NET_POLL_CONTROLLER
14835 .ndo_poll_controller = tg3_poll_controller,
14836#endif
14837};
14838
Linus Torvalds1da177e2005-04-16 15:20:36 -070014839static int __devinit tg3_init_one(struct pci_dev *pdev,
14840 const struct pci_device_id *ent)
14841{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014842 struct net_device *dev;
14843 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000014844 int i, err, pm_cap;
14845 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070014846 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080014847 u64 dma_mask, persist_dma_mask;
Michał Mirosławdc668912011-04-07 03:35:07 +000014848 u32 hw_features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014849
Joe Perches05dbe002010-02-17 19:44:19 +000014850 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014851
14852 err = pci_enable_device(pdev);
14853 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014854 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014855 return err;
14856 }
14857
Linus Torvalds1da177e2005-04-16 15:20:36 -070014858 err = pci_request_regions(pdev, DRV_MODULE_NAME);
14859 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014860 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014861 goto err_out_disable_pdev;
14862 }
14863
14864 pci_set_master(pdev);
14865
14866 /* Find power-management capability. */
14867 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
14868 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000014869 dev_err(&pdev->dev,
14870 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014871 err = -EIO;
14872 goto err_out_free_res;
14873 }
14874
Matt Carlsonfe5f5782009-09-01 13:09:39 +000014875 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014876 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000014877 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014878 err = -ENOMEM;
14879 goto err_out_free_res;
14880 }
14881
Linus Torvalds1da177e2005-04-16 15:20:36 -070014882 SET_NETDEV_DEV(dev, &pdev->dev);
14883
Linus Torvalds1da177e2005-04-16 15:20:36 -070014884 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014885
14886 tp = netdev_priv(dev);
14887 tp->pdev = pdev;
14888 tp->dev = dev;
14889 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014890 tp->rx_mode = TG3_DEF_RX_MODE;
14891 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070014892
Linus Torvalds1da177e2005-04-16 15:20:36 -070014893 if (tg3_debug > 0)
14894 tp->msg_enable = tg3_debug;
14895 else
14896 tp->msg_enable = TG3_DEF_MSG_ENABLE;
14897
14898 /* The word/byte swap controls here control register access byte
14899 * swapping. DMA data byte swapping is controlled in the GRC_MODE
14900 * setting below.
14901 */
14902 tp->misc_host_ctrl =
14903 MISC_HOST_CTRL_MASK_PCI_INT |
14904 MISC_HOST_CTRL_WORD_SWAP |
14905 MISC_HOST_CTRL_INDIR_ACCESS |
14906 MISC_HOST_CTRL_PCISTATE_RW;
14907
14908 /* The NONFRM (non-frame) byte/word swap controls take effect
14909 * on descriptor entries, anything which isn't packet data.
14910 *
14911 * The StrongARM chips on the board (one for tx, one for rx)
14912 * are running in big-endian mode.
14913 */
14914 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
14915 GRC_MODE_WSWAP_NONFRM_DATA);
14916#ifdef __BIG_ENDIAN
14917 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
14918#endif
14919 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014920 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000014921 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014922
Matt Carlsond5fe4882008-11-21 17:20:32 -080014923 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010014924 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014925 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014926 err = -ENOMEM;
14927 goto err_out_free_dev;
14928 }
14929
Linus Torvalds1da177e2005-04-16 15:20:36 -070014930 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
14931 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014932
Linus Torvalds1da177e2005-04-16 15:20:36 -070014933 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014934 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014935 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014936
14937 err = tg3_get_invariants(tp);
14938 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014939 dev_err(&pdev->dev,
14940 "Problem fetching invariants of chip, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014941 goto err_out_iounmap;
14942 }
14943
Matt Carlson615774f2009-11-13 13:03:39 +000014944 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Matt Carlson0a58d662011-04-05 14:22:45 +000014945 !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
Stephen Hemminger00829822008-11-20 20:14:53 -080014946 dev->netdev_ops = &tg3_netdev_ops;
14947 else
14948 dev->netdev_ops = &tg3_netdev_ops_dma_bug;
14949
14950
Michael Chan4a29cc22006-03-19 13:21:12 -080014951 /* The EPB bridge inside 5714, 5715, and 5780 and any
14952 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080014953 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
14954 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
14955 * do DMA address check in tg3_start_xmit().
14956 */
Michael Chan4a29cc22006-03-19 13:21:12 -080014957 if (tp->tg3_flags2 & TG3_FLG2_IS_5788)
Yang Hongyang284901a2009-04-06 19:01:15 -070014958 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Michael Chan4a29cc22006-03-19 13:21:12 -080014959 else if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070014960 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080014961#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070014962 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014963#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080014964 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070014965 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014966
14967 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070014968 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080014969 err = pci_set_dma_mask(pdev, dma_mask);
14970 if (!err) {
14971 dev->features |= NETIF_F_HIGHDMA;
14972 err = pci_set_consistent_dma_mask(pdev,
14973 persist_dma_mask);
14974 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014975 dev_err(&pdev->dev, "Unable to obtain 64 bit "
14976 "DMA for consistent allocations\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080014977 goto err_out_iounmap;
14978 }
14979 }
14980 }
Yang Hongyang284901a2009-04-06 19:01:15 -070014981 if (err || dma_mask == DMA_BIT_MASK(32)) {
14982 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080014983 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014984 dev_err(&pdev->dev,
14985 "No usable DMA configuration, aborting\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080014986 goto err_out_iounmap;
14987 }
14988 }
14989
Michael Chanfdfec172005-07-25 12:31:48 -070014990 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014991
Matt Carlson507399f2009-11-13 13:03:37 +000014992 /* Selectively allow TSO based on operating conditions */
14993 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) ||
14994 (tp->fw_needed && !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)))
14995 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
14996 else {
14997 tp->tg3_flags2 &= ~(TG3_FLG2_TSO_CAPABLE | TG3_FLG2_TSO_BUG);
14998 tp->fw_needed = NULL;
14999 }
15000
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015001 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
Matt Carlson9e9fd122009-01-19 16:57:45 -080015002 tp->fw_needed = FIRMWARE_TG3;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015003
Michael Chan4e3a7aa2006-03-20 17:47:44 -080015004 /* TSO is on by default on chips that support hardware TSO.
15005 * Firmware TSO on older chips gives lower performance, so it
15006 * is off by default, but can be enabled using ethtool.
15007 */
Matt Carlsone849cdc2009-11-13 13:03:38 +000015008 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) &&
Michał Mirosławdc668912011-04-07 03:35:07 +000015009 (dev->features & NETIF_F_IP_CSUM))
15010 hw_features |= NETIF_F_TSO;
Matt Carlsone849cdc2009-11-13 13:03:38 +000015011 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
15012 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3)) {
Michał Mirosławdc668912011-04-07 03:35:07 +000015013 if (dev->features & NETIF_F_IPV6_CSUM)
15014 hw_features |= NETIF_F_TSO6;
Matt Carlsone849cdc2009-11-13 13:03:38 +000015015 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
15016 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070015017 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15018 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080015019 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000015020 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
15021 hw_features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070015022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015023
Michał Mirosławdc668912011-04-07 03:35:07 +000015024 dev->hw_features |= hw_features;
15025 dev->features |= hw_features;
15026 dev->vlan_features |= hw_features;
15027
Linus Torvalds1da177e2005-04-16 15:20:36 -070015028 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
15029 !(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
15030 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
15031 tp->tg3_flags2 |= TG3_FLG2_MAX_RXPEND_64;
15032 tp->rx_pending = 63;
15033 }
15034
Linus Torvalds1da177e2005-04-16 15:20:36 -070015035 err = tg3_get_device_address(tp);
15036 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015037 dev_err(&pdev->dev,
15038 "Could not obtain valid ethernet address, aborting\n");
Matt Carlson026a6c22009-12-03 08:36:24 +000015039 goto err_out_iounmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015040 }
15041
Matt Carlson0d3031d2007-10-10 18:02:43 -070015042 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
Matt Carlson63532392008-11-03 16:49:57 -080015043 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
Al Viro79ea13c2008-01-24 02:06:46 -080015044 if (!tp->aperegs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015045 dev_err(&pdev->dev,
15046 "Cannot map APE registers, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015047 err = -ENOMEM;
Matt Carlson026a6c22009-12-03 08:36:24 +000015048 goto err_out_iounmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015049 }
15050
15051 tg3_ape_lock_init(tp);
Matt Carlson7fd76442009-02-25 14:27:20 +000015052
15053 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
15054 tg3_read_dash_ver(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015055 }
15056
Matt Carlsonc88864d2007-11-12 21:07:01 -080015057 /*
15058 * Reset chip in case UNDI or EFI driver did not shutdown
15059 * DMA self test will enable WDMAC and we'll see (spurious)
15060 * pending DMA on the PCI bus at that point.
15061 */
15062 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15063 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15064 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15065 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15066 }
15067
15068 err = tg3_test_dma(tp);
15069 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015070 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080015071 goto err_out_apeunmap;
15072 }
15073
Matt Carlson78f90dc2009-11-13 13:03:42 +000015074 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15075 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15076 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000015077 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000015078 struct tg3_napi *tnapi = &tp->napi[i];
15079
15080 tnapi->tp = tp;
15081 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15082
15083 tnapi->int_mbox = intmbx;
15084 if (i < 4)
15085 intmbx += 0x8;
15086 else
15087 intmbx += 0x4;
15088
15089 tnapi->consmbox = rcvmbx;
15090 tnapi->prodmbox = sndmbx;
15091
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015092 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015093 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015094 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000015095 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000015096
15097 if (!(tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX))
15098 break;
15099
15100 /*
15101 * If we support MSIX, we'll be using RSS. If we're using
15102 * RSS, the first vector only handles link interrupts and the
15103 * remaining vectors handle rx and tx interrupts. Reuse the
15104 * mailbox values for the next iteration. The values we setup
15105 * above are still useful for the single vectored mode.
15106 */
15107 if (!i)
15108 continue;
15109
15110 rcvmbx += 0x8;
15111
15112 if (sndmbx & 0x4)
15113 sndmbx -= 0x4;
15114 else
15115 sndmbx += 0xc;
15116 }
15117
Matt Carlsonc88864d2007-11-12 21:07:01 -080015118 tg3_init_coal(tp);
15119
Michael Chanc49a1562006-12-17 17:07:29 -080015120 pci_set_drvdata(pdev, dev);
15121
Linus Torvalds1da177e2005-04-16 15:20:36 -070015122 err = register_netdev(dev);
15123 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015124 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015125 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015126 }
15127
Joe Perches05dbe002010-02-17 19:44:19 +000015128 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15129 tp->board_part_number,
15130 tp->pci_chip_rev_id,
15131 tg3_bus_string(tp, str),
15132 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015133
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015134 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000015135 struct phy_device *phydev;
15136 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000015137 netdev_info(dev,
15138 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015139 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015140 } else {
15141 char *ethtype;
15142
15143 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15144 ethtype = "10/100Base-TX";
15145 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15146 ethtype = "1000Base-SX";
15147 else
15148 ethtype = "10/100/1000Base-T";
15149
Matt Carlson5129c3a2010-04-05 10:19:23 +000015150 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015151 "(WireSpeed[%d])\n", tg3_phy_string(tp), ethtype,
15152 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0);
15153 }
Matt Carlsondf59c942008-11-03 16:52:56 -080015154
Joe Perches05dbe002010-02-17 19:44:19 +000015155 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000015156 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches05dbe002010-02-17 19:44:19 +000015157 (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015158 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches05dbe002010-02-17 19:44:19 +000015159 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0,
15160 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) != 0);
15161 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15162 tp->dma_rwctrl,
15163 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15164 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015165
15166 return 0;
15167
Matt Carlson0d3031d2007-10-10 18:02:43 -070015168err_out_apeunmap:
15169 if (tp->aperegs) {
15170 iounmap(tp->aperegs);
15171 tp->aperegs = NULL;
15172 }
15173
Linus Torvalds1da177e2005-04-16 15:20:36 -070015174err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070015175 if (tp->regs) {
15176 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015177 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015179
15180err_out_free_dev:
15181 free_netdev(dev);
15182
15183err_out_free_res:
15184 pci_release_regions(pdev);
15185
15186err_out_disable_pdev:
15187 pci_disable_device(pdev);
15188 pci_set_drvdata(pdev, NULL);
15189 return err;
15190}
15191
15192static void __devexit tg3_remove_one(struct pci_dev *pdev)
15193{
15194 struct net_device *dev = pci_get_drvdata(pdev);
15195
15196 if (dev) {
15197 struct tg3 *tp = netdev_priv(dev);
15198
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015199 if (tp->fw)
15200 release_firmware(tp->fw);
15201
Tejun Heo23f333a2010-12-12 16:45:14 +010015202 cancel_work_sync(&tp->reset_task);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015203
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015204 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
15205 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015206 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015207 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015208
Linus Torvalds1da177e2005-04-16 15:20:36 -070015209 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015210 if (tp->aperegs) {
15211 iounmap(tp->aperegs);
15212 tp->aperegs = NULL;
15213 }
Michael Chan68929142005-08-09 20:17:14 -070015214 if (tp->regs) {
15215 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015216 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015218 free_netdev(dev);
15219 pci_release_regions(pdev);
15220 pci_disable_device(pdev);
15221 pci_set_drvdata(pdev, NULL);
15222 }
15223}
15224
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015225#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015226static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015227{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015228 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015229 struct net_device *dev = pci_get_drvdata(pdev);
15230 struct tg3 *tp = netdev_priv(dev);
15231 int err;
15232
15233 if (!netif_running(dev))
15234 return 0;
15235
Tejun Heo23f333a2010-12-12 16:45:14 +010015236 flush_work_sync(&tp->reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015237 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015238 tg3_netif_stop(tp);
15239
15240 del_timer_sync(&tp->timer);
15241
David S. Millerf47c11e2005-06-24 20:18:35 -070015242 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015243 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015244 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015245
15246 netif_device_detach(dev);
15247
David S. Millerf47c11e2005-06-24 20:18:35 -070015248 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015249 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan6a9eba12005-12-13 21:08:58 -080015250 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
David S. Millerf47c11e2005-06-24 20:18:35 -070015251 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015252
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015253 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015254 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015255 int err2;
15256
David S. Millerf47c11e2005-06-24 20:18:35 -070015257 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015258
Michael Chan6a9eba12005-12-13 21:08:58 -080015259 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015260 err2 = tg3_restart_hw(tp, 1);
15261 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015262 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015263
15264 tp->timer.expires = jiffies + tp->timer_offset;
15265 add_timer(&tp->timer);
15266
15267 netif_device_attach(dev);
15268 tg3_netif_start(tp);
15269
Michael Chanb9ec6c12006-07-25 16:37:27 -070015270out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015271 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015272
15273 if (!err2)
15274 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015275 }
15276
15277 return err;
15278}
15279
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015280static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015281{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015282 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015283 struct net_device *dev = pci_get_drvdata(pdev);
15284 struct tg3 *tp = netdev_priv(dev);
15285 int err;
15286
15287 if (!netif_running(dev))
15288 return 0;
15289
Linus Torvalds1da177e2005-04-16 15:20:36 -070015290 netif_device_attach(dev);
15291
David S. Millerf47c11e2005-06-24 20:18:35 -070015292 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015293
Michael Chan6a9eba12005-12-13 21:08:58 -080015294 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Michael Chanb9ec6c12006-07-25 16:37:27 -070015295 err = tg3_restart_hw(tp, 1);
15296 if (err)
15297 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015298
15299 tp->timer.expires = jiffies + tp->timer_offset;
15300 add_timer(&tp->timer);
15301
Linus Torvalds1da177e2005-04-16 15:20:36 -070015302 tg3_netif_start(tp);
15303
Michael Chanb9ec6c12006-07-25 16:37:27 -070015304out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015305 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015306
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015307 if (!err)
15308 tg3_phy_start(tp);
15309
Michael Chanb9ec6c12006-07-25 16:37:27 -070015310 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015311}
15312
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015313static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015314#define TG3_PM_OPS (&tg3_pm_ops)
15315
15316#else
15317
15318#define TG3_PM_OPS NULL
15319
15320#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015321
Linus Torvalds1da177e2005-04-16 15:20:36 -070015322static struct pci_driver tg3_driver = {
15323 .name = DRV_MODULE_NAME,
15324 .id_table = tg3_pci_tbl,
15325 .probe = tg3_init_one,
15326 .remove = __devexit_p(tg3_remove_one),
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015327 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015328};
15329
15330static int __init tg3_init(void)
15331{
Jeff Garzik29917622006-08-19 17:48:59 -040015332 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015333}
15334
15335static void __exit tg3_cleanup(void)
15336{
15337 pci_unregister_driver(&tg3_driver);
15338}
15339
15340module_init(tg3_init);
15341module_exit(tg3_cleanup);