blob: a079e745a071e95aa7314de28b2f3e114af6f1ad [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * tg3.c: Broadcom Tigon3 ethernet driver.
3 *
4 * Copyright (C) 2001, 2002, 2003, 2004 David S. Miller (davem@redhat.com)
5 * Copyright (C) 2001, 2002, 2003 Jeff Garzik (jgarzik@pobox.com)
6 * Copyright (C) 2004 Sun Microsystems Inc.
Matt Carlsonb86fb2c2011-01-25 15:58:57 +00007 * Copyright (C) 2005-2011 Broadcom Corporation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Firmware is:
Michael Chan49cabf42005-06-06 15:15:17 -070010 * Derived from proprietary unpublished source code,
11 * Copyright (C) 2000-2003 Broadcom Corporation.
12 *
13 * Permission is hereby granted for the distribution of this firmware
14 * data in hexadecimal or equivalent format, provided this copyright
15 * notice is accompanying it.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <linux/module.h>
20#include <linux/moduleparam.h>
Matt Carlson6867c842010-07-11 09:31:44 +000021#include <linux/stringify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/kernel.h>
23#include <linux/types.h>
24#include <linux/compiler.h>
25#include <linux/slab.h>
26#include <linux/delay.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020027#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/init.h>
29#include <linux/ioport.h>
30#include <linux/pci.h>
31#include <linux/netdevice.h>
32#include <linux/etherdevice.h>
33#include <linux/skbuff.h>
34#include <linux/ethtool.h>
Matt Carlson3110f5f52010-12-06 08:28:50 +000035#include <linux/mdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/mii.h>
Matt Carlson158d7ab2008-05-29 01:37:54 -070037#include <linux/phy.h>
Matt Carlsona9daf362008-05-25 23:49:44 -070038#include <linux/brcmphy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/if_vlan.h>
40#include <linux/ip.h>
41#include <linux/tcp.h>
42#include <linux/workqueue.h>
Michael Chan61487482005-09-05 17:53:19 -070043#include <linux/prefetch.h>
Tobias Klauserf9a5f7d2005-10-29 15:09:26 +020044#include <linux/dma-mapping.h>
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080045#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#include <net/checksum.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030048#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#include <asm/system.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000051#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <asm/byteorder.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000053#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
David S. Miller49b6e95f2007-03-29 01:38:42 -070055#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <asm/idprom.h>
David S. Miller49b6e95f2007-03-29 01:38:42 -070057#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#endif
59
Matt Carlson63532392008-11-03 16:49:57 -080060#define BAR_0 0
61#define BAR_2 2
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include "tg3.h"
64
65#define DRV_MODULE_NAME "tg3"
Matt Carlson6867c842010-07-11 09:31:44 +000066#define TG3_MAJ_NUM 3
Matt Carlsonb86fb2c2011-01-25 15:58:57 +000067#define TG3_MIN_NUM 117
Matt Carlson6867c842010-07-11 09:31:44 +000068#define DRV_MODULE_VERSION \
69 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Matt Carlsonb86fb2c2011-01-25 15:58:57 +000070#define DRV_MODULE_RELDATE "January 25, 2011"
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72#define TG3_DEF_MAC_MODE 0
73#define TG3_DEF_RX_MODE 0
74#define TG3_DEF_TX_MODE 0
75#define TG3_DEF_MSG_ENABLE \
76 (NETIF_MSG_DRV | \
77 NETIF_MSG_PROBE | \
78 NETIF_MSG_LINK | \
79 NETIF_MSG_TIMER | \
80 NETIF_MSG_IFDOWN | \
81 NETIF_MSG_IFUP | \
82 NETIF_MSG_RX_ERR | \
83 NETIF_MSG_TX_ERR)
84
85/* length of time before we decide the hardware is borked,
86 * and dev->tx_timeout() should be called to fix the problem
87 */
88#define TG3_TX_TIMEOUT (5 * HZ)
89
90/* hardware minimum and maximum for a single frame's data payload */
91#define TG3_MIN_MTU 60
92#define TG3_MAX_MTU(tp) \
Matt Carlson8f666b02009-08-28 13:58:24 +000093 ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95/* These numbers seem to be hard coded in the NIC firmware somehow.
96 * You can't change the ring sizes, but you can change where you place
97 * them in the NIC onboard memory.
98 */
Matt Carlson7cb32cf2010-09-30 10:34:36 +000099#define TG3_RX_STD_RING_SIZE(tp) \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000100 ((tp->tg3_flags3 & TG3_FLG3_LRG_PROD_RING_CAP) ? \
101 TG3_RX_STD_MAX_SIZE_5717 : TG3_RX_STD_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#define TG3_DEF_RX_RING_PENDING 200
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000103#define TG3_RX_JMB_RING_SIZE(tp) \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000104 ((tp->tg3_flags3 & TG3_FLG3_LRG_PROD_RING_CAP) ? \
105 TG3_RX_JMB_MAX_SIZE_5717 : TG3_RX_JMB_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#define TG3_DEF_RX_JUMBO_RING_PENDING 100
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000107#define TG3_RSS_INDIR_TBL_SIZE 128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109/* Do not place this n-ring entries value into the tp struct itself,
110 * we really want to expose these constants to GCC so that modulo et
111 * al. operations are done with shifts and masks instead of with
112 * hw multiply/modulo instructions. Another solution would be to
113 * replace things like '% foo' with '& (foo - 1)'.
114 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116#define TG3_TX_RING_SIZE 512
117#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
118
Matt Carlson2c49a442010-09-30 10:34:35 +0000119#define TG3_RX_STD_RING_BYTES(tp) \
120 (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
121#define TG3_RX_JMB_RING_BYTES(tp) \
122 (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
123#define TG3_RX_RCB_RING_BYTES(tp) \
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000124 (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
126 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
128
Matt Carlson287be122009-08-28 13:58:46 +0000129#define TG3_DMA_BYTE_ENAB 64
130
131#define TG3_RX_STD_DMA_SZ 1536
132#define TG3_RX_JMB_DMA_SZ 9046
133
134#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
135
136#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
137#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Matt Carlson2c49a442010-09-30 10:34:35 +0000139#define TG3_RX_STD_BUFF_RING_SIZE(tp) \
140 (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000141
Matt Carlson2c49a442010-09-30 10:34:35 +0000142#define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
143 (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000144
Matt Carlsond2757fc2010-04-12 06:58:27 +0000145/* Due to a hardware bug, the 5701 can only DMA to memory addresses
146 * that are at least dword aligned when used in PCIX mode. The driver
147 * works around this bug by double copying the packet. This workaround
148 * is built into the normal double copy length check for efficiency.
149 *
150 * However, the double copy is only necessary on those architectures
151 * where unaligned memory accesses are inefficient. For those architectures
152 * where unaligned memory accesses incur little penalty, we can reintegrate
153 * the 5701 in the normal rx path. Doing so saves a device structure
154 * dereference by hardcoding the double copy threshold in place.
155 */
156#define TG3_RX_COPY_THRESHOLD 256
157#if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
158 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
159#else
160 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
161#endif
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000164#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Matt Carlsonad829262008-11-21 17:16:16 -0800166#define TG3_RAW_IP_ALIGN 2
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168/* number of ETHTOOL_GSTATS u64's */
169#define TG3_NUM_STATS (sizeof(struct tg3_ethtool_stats)/sizeof(u64))
170
Michael Chan4cafd3f2005-05-29 14:56:34 -0700171#define TG3_NUM_TEST 6
172
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000173#define TG3_FW_UPDATE_TIMEOUT_SEC 5
174
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800175#define FIRMWARE_TG3 "tigon/tg3.bin"
176#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
177#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179static char version[] __devinitdata =
Joe Perches05dbe002010-02-17 19:44:19 +0000180 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
183MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
184MODULE_LICENSE("GPL");
185MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800186MODULE_FIRMWARE(FIRMWARE_TG3);
187MODULE_FIRMWARE(FIRMWARE_TG3TSO);
188MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
191module_param(tg3_debug, int, 0);
192MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
193
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000194static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700195 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
196 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
197 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
198 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
199 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
200 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
201 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
202 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
203 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
204 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
205 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
206 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
207 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
208 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
209 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
210 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
211 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
212 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
213 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901)},
214 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2)},
215 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
216 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700217 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700218 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700219 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700220 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
221 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F)},
222 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
223 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
224 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
225 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
226 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F)},
227 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
228 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
229 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
230 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700231 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700232 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
233 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
234 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Michael Chan676917d2006-12-07 00:20:22 -0800235 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700236 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
237 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
238 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
239 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
240 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Matt Carlson321d32a2008-11-21 17:22:19 -0800254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
255 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
256 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790)},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000257 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000258 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
259 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000260 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
261 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
262 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
263 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
264 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791)},
265 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795)},
Matt Carlson302b5002010-06-05 17:24:38 +0000266 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700267 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
268 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
269 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
270 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
271 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
272 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
273 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
274 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275};
276
277MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
278
Andreas Mohr50da8592006-08-14 23:54:30 -0700279static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 const char string[ETH_GSTRING_LEN];
281} ethtool_stats_keys[TG3_NUM_STATS] = {
282 { "rx_octets" },
283 { "rx_fragments" },
284 { "rx_ucast_packets" },
285 { "rx_mcast_packets" },
286 { "rx_bcast_packets" },
287 { "rx_fcs_errors" },
288 { "rx_align_errors" },
289 { "rx_xon_pause_rcvd" },
290 { "rx_xoff_pause_rcvd" },
291 { "rx_mac_ctrl_rcvd" },
292 { "rx_xoff_entered" },
293 { "rx_frame_too_long_errors" },
294 { "rx_jabbers" },
295 { "rx_undersize_packets" },
296 { "rx_in_length_errors" },
297 { "rx_out_length_errors" },
298 { "rx_64_or_less_octet_packets" },
299 { "rx_65_to_127_octet_packets" },
300 { "rx_128_to_255_octet_packets" },
301 { "rx_256_to_511_octet_packets" },
302 { "rx_512_to_1023_octet_packets" },
303 { "rx_1024_to_1522_octet_packets" },
304 { "rx_1523_to_2047_octet_packets" },
305 { "rx_2048_to_4095_octet_packets" },
306 { "rx_4096_to_8191_octet_packets" },
307 { "rx_8192_to_9022_octet_packets" },
308
309 { "tx_octets" },
310 { "tx_collisions" },
311
312 { "tx_xon_sent" },
313 { "tx_xoff_sent" },
314 { "tx_flow_control" },
315 { "tx_mac_errors" },
316 { "tx_single_collisions" },
317 { "tx_mult_collisions" },
318 { "tx_deferred" },
319 { "tx_excessive_collisions" },
320 { "tx_late_collisions" },
321 { "tx_collide_2times" },
322 { "tx_collide_3times" },
323 { "tx_collide_4times" },
324 { "tx_collide_5times" },
325 { "tx_collide_6times" },
326 { "tx_collide_7times" },
327 { "tx_collide_8times" },
328 { "tx_collide_9times" },
329 { "tx_collide_10times" },
330 { "tx_collide_11times" },
331 { "tx_collide_12times" },
332 { "tx_collide_13times" },
333 { "tx_collide_14times" },
334 { "tx_collide_15times" },
335 { "tx_ucast_packets" },
336 { "tx_mcast_packets" },
337 { "tx_bcast_packets" },
338 { "tx_carrier_sense_errors" },
339 { "tx_discards" },
340 { "tx_errors" },
341
342 { "dma_writeq_full" },
343 { "dma_write_prioq_full" },
344 { "rxbds_empty" },
345 { "rx_discards" },
346 { "rx_errors" },
347 { "rx_threshold_hit" },
348
349 { "dma_readq_full" },
350 { "dma_read_prioq_full" },
351 { "tx_comp_queue_full" },
352
353 { "ring_set_send_prod_index" },
354 { "ring_status_update" },
355 { "nic_irqs" },
356 { "nic_avoided_irqs" },
357 { "nic_tx_threshold_hit" }
358};
359
Andreas Mohr50da8592006-08-14 23:54:30 -0700360static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700361 const char string[ETH_GSTRING_LEN];
362} ethtool_test_keys[TG3_NUM_TEST] = {
363 { "nvram test (online) " },
364 { "link test (online) " },
365 { "register test (offline)" },
366 { "memory test (offline)" },
367 { "loopback test (offline)" },
368 { "interrupt test (offline)" },
369};
370
Michael Chanb401e9e2005-12-19 16:27:04 -0800371static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
372{
373 writel(val, tp->regs + off);
374}
375
376static u32 tg3_read32(struct tg3 *tp, u32 off)
377{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000378 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800379}
380
Matt Carlson0d3031d2007-10-10 18:02:43 -0700381static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
382{
383 writel(val, tp->aperegs + off);
384}
385
386static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
387{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000388 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700389}
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
392{
Michael Chan68929142005-08-09 20:17:14 -0700393 unsigned long flags;
394
395 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700396 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
397 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700398 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700399}
400
401static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
402{
403 writel(val, tp->regs + off);
404 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
Michael Chan68929142005-08-09 20:17:14 -0700407static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
408{
409 unsigned long flags;
410 u32 val;
411
412 spin_lock_irqsave(&tp->indirect_lock, flags);
413 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
414 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
415 spin_unlock_irqrestore(&tp->indirect_lock, flags);
416 return val;
417}
418
419static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
420{
421 unsigned long flags;
422
423 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
424 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
425 TG3_64BIT_REG_LOW, val);
426 return;
427 }
Matt Carlson66711e62009-11-13 13:03:49 +0000428 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700429 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
430 TG3_64BIT_REG_LOW, val);
431 return;
432 }
433
434 spin_lock_irqsave(&tp->indirect_lock, flags);
435 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
436 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
437 spin_unlock_irqrestore(&tp->indirect_lock, flags);
438
439 /* In indirect mode when disabling interrupts, we also need
440 * to clear the interrupt bit in the GRC local ctrl register.
441 */
442 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
443 (val == 0x1)) {
444 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
445 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
446 }
447}
448
449static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
450{
451 unsigned long flags;
452 u32 val;
453
454 spin_lock_irqsave(&tp->indirect_lock, flags);
455 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
456 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
457 spin_unlock_irqrestore(&tp->indirect_lock, flags);
458 return val;
459}
460
Michael Chanb401e9e2005-12-19 16:27:04 -0800461/* usec_wait specifies the wait time in usec when writing to certain registers
462 * where it is unsafe to read back the register without some delay.
463 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
464 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
465 */
466static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Michael Chanb401e9e2005-12-19 16:27:04 -0800468 if ((tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) ||
469 (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
470 /* Non-posted methods */
471 tp->write32(tp, off, val);
472 else {
473 /* Posted method */
474 tg3_write32(tp, off, val);
475 if (usec_wait)
476 udelay(usec_wait);
477 tp->read32(tp, off);
478 }
479 /* Wait again after the read for the posted method to guarantee that
480 * the wait time is met.
481 */
482 if (usec_wait)
483 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
Michael Chan09ee9292005-08-09 20:17:00 -0700486static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
487{
488 tp->write32_mbox(tp, off, val);
Michael Chan68929142005-08-09 20:17:14 -0700489 if (!(tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) &&
490 !(tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
491 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700492}
493
Michael Chan20094932005-08-09 20:16:32 -0700494static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 void __iomem *mbox = tp->regs + off;
497 writel(val, mbox);
498 if (tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG)
499 writel(val, mbox);
500 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
501 readl(mbox);
502}
503
Michael Chanb5d37722006-09-27 16:06:21 -0700504static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
505{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000506 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700507}
508
509static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
510{
511 writel(val, tp->regs + off + GRCMBOX_BASE);
512}
513
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000514#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700515#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000516#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
517#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
518#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700519
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000520#define tw32(reg, val) tp->write32(tp, reg, val)
521#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
522#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
523#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
526{
Michael Chan68929142005-08-09 20:17:14 -0700527 unsigned long flags;
528
Michael Chanb5d37722006-09-27 16:06:21 -0700529 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
530 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
531 return;
532
Michael Chan68929142005-08-09 20:17:14 -0700533 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chanbbadf502006-04-06 21:46:34 -0700534 if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
535 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
536 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Michael Chanbbadf502006-04-06 21:46:34 -0700538 /* Always leave this as zero. */
539 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
540 } else {
541 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
542 tw32_f(TG3PCI_MEM_WIN_DATA, val);
543
544 /* Always leave this as zero. */
545 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
546 }
Michael Chan68929142005-08-09 20:17:14 -0700547 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
550static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
551{
Michael Chan68929142005-08-09 20:17:14 -0700552 unsigned long flags;
553
Michael Chanb5d37722006-09-27 16:06:21 -0700554 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
555 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
556 *val = 0;
557 return;
558 }
559
Michael Chan68929142005-08-09 20:17:14 -0700560 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chanbbadf502006-04-06 21:46:34 -0700561 if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
562 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
563 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Michael Chanbbadf502006-04-06 21:46:34 -0700565 /* Always leave this as zero. */
566 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
567 } else {
568 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
569 *val = tr32(TG3PCI_MEM_WIN_DATA);
570
571 /* Always leave this as zero. */
572 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
573 }
Michael Chan68929142005-08-09 20:17:14 -0700574 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
Matt Carlson0d3031d2007-10-10 18:02:43 -0700577static void tg3_ape_lock_init(struct tg3 *tp)
578{
579 int i;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000580 u32 regbase;
581
582 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
583 regbase = TG3_APE_LOCK_GRANT;
584 else
585 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700586
587 /* Make sure the driver hasn't any stale locks. */
588 for (i = 0; i < 8; i++)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000589 tg3_ape_write32(tp, regbase + 4 * i, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700590}
591
592static int tg3_ape_lock(struct tg3 *tp, int locknum)
593{
594 int i, off;
595 int ret = 0;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000596 u32 status, req, gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700597
598 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
599 return 0;
600
601 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000602 case TG3_APE_LOCK_GRC:
603 case TG3_APE_LOCK_MEM:
604 break;
605 default:
606 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700607 }
608
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000609 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
610 req = TG3_APE_LOCK_REQ;
611 gnt = TG3_APE_LOCK_GRANT;
612 } else {
613 req = TG3_APE_PER_LOCK_REQ;
614 gnt = TG3_APE_PER_LOCK_GRANT;
615 }
616
Matt Carlson0d3031d2007-10-10 18:02:43 -0700617 off = 4 * locknum;
618
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000619 tg3_ape_write32(tp, req + off, APE_LOCK_REQ_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700620
621 /* Wait for up to 1 millisecond to acquire lock. */
622 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000623 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700624 if (status == APE_LOCK_GRANT_DRIVER)
625 break;
626 udelay(10);
627 }
628
629 if (status != APE_LOCK_GRANT_DRIVER) {
630 /* Revoke the lock request. */
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000631 tg3_ape_write32(tp, gnt + off,
Matt Carlson0d3031d2007-10-10 18:02:43 -0700632 APE_LOCK_GRANT_DRIVER);
633
634 ret = -EBUSY;
635 }
636
637 return ret;
638}
639
640static void tg3_ape_unlock(struct tg3 *tp, int locknum)
641{
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000642 u32 gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700643
644 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
645 return;
646
647 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000648 case TG3_APE_LOCK_GRC:
649 case TG3_APE_LOCK_MEM:
650 break;
651 default:
652 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700653 }
654
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000655 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
656 gnt = TG3_APE_LOCK_GRANT;
657 else
658 gnt = TG3_APE_PER_LOCK_GRANT;
659
660 tg3_ape_write32(tp, gnt + 4 * locknum, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700661}
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663static void tg3_disable_ints(struct tg3 *tp)
664{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000665 int i;
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 tw32(TG3PCI_MISC_HOST_CTRL,
668 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000669 for (i = 0; i < tp->irq_max; i++)
670 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673static void tg3_enable_ints(struct tg3 *tp)
674{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000675 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000676
Michael Chanbbe832c2005-06-24 20:20:04 -0700677 tp->irq_sync = 0;
678 wmb();
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 tw32(TG3PCI_MISC_HOST_CTRL,
681 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000682
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000683 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000684 for (i = 0; i < tp->irq_cnt; i++) {
685 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000686
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000687 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
688 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
689 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
690
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000691 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000692 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000693
694 /* Force an initial interrupt */
695 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
696 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
697 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
698 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000699 tw32(HOSTCC_MODE, tp->coal_now);
700
701 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
Matt Carlson17375d22009-08-28 14:02:18 +0000704static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700705{
Matt Carlson17375d22009-08-28 14:02:18 +0000706 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000707 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700708 unsigned int work_exists = 0;
709
710 /* check for phy events */
711 if (!(tp->tg3_flags &
712 (TG3_FLAG_USE_LINKCHG_REG |
713 TG3_FLAG_POLL_SERDES))) {
714 if (sblk->status & SD_STATUS_LINK_CHG)
715 work_exists = 1;
716 }
717 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000718 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000719 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700720 work_exists = 1;
721
722 return work_exists;
723}
724
Matt Carlson17375d22009-08-28 14:02:18 +0000725/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700726 * similar to tg3_enable_ints, but it accurately determines whether there
727 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400728 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 */
Matt Carlson17375d22009-08-28 14:02:18 +0000730static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Matt Carlson17375d22009-08-28 14:02:18 +0000732 struct tg3 *tp = tnapi->tp;
733
Matt Carlson898a56f2009-08-28 14:02:40 +0000734 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 mmiowb();
736
David S. Millerfac9b832005-05-18 22:46:34 -0700737 /* When doing tagged status, this work check is unnecessary.
738 * The last_tag we write above tells the chip which piece of
739 * work we've completed.
740 */
741 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
Matt Carlson17375d22009-08-28 14:02:18 +0000742 tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700743 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000744 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747static void tg3_switch_clocks(struct tg3 *tp)
748{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000749 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 u32 orig_clock_ctrl;
751
Matt Carlson795d01c2007-10-07 23:28:17 -0700752 if ((tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
753 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700754 return;
755
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000756 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 orig_clock_ctrl = clock_ctrl;
759 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
760 CLOCK_CTRL_CLKRUN_OENABLE |
761 0x1f);
762 tp->pci_clock_ctrl = clock_ctrl;
763
764 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
765 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800766 tw32_wait_f(TG3PCI_CLOCK_CTRL,
767 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
769 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800770 tw32_wait_f(TG3PCI_CLOCK_CTRL,
771 clock_ctrl |
772 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
773 40);
774 tw32_wait_f(TG3PCI_CLOCK_CTRL,
775 clock_ctrl | (CLOCK_CTRL_ALTCLK),
776 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800778 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779}
780
781#define PHY_BUSY_LOOPS 5000
782
783static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
784{
785 u32 frame_val;
786 unsigned int loops;
787 int ret;
788
789 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
790 tw32_f(MAC_MI_MODE,
791 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
792 udelay(80);
793 }
794
795 *val = 0x0;
796
Matt Carlson882e9792009-09-01 13:21:36 +0000797 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 MI_COM_PHY_ADDR_MASK);
799 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
800 MI_COM_REG_ADDR_MASK);
801 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 tw32_f(MAC_MI_COM, frame_val);
804
805 loops = PHY_BUSY_LOOPS;
806 while (loops != 0) {
807 udelay(10);
808 frame_val = tr32(MAC_MI_COM);
809
810 if ((frame_val & MI_COM_BUSY) == 0) {
811 udelay(5);
812 frame_val = tr32(MAC_MI_COM);
813 break;
814 }
815 loops -= 1;
816 }
817
818 ret = -EBUSY;
819 if (loops != 0) {
820 *val = frame_val & MI_COM_DATA_MASK;
821 ret = 0;
822 }
823
824 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
825 tw32_f(MAC_MI_MODE, tp->mi_mode);
826 udelay(80);
827 }
828
829 return ret;
830}
831
832static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
833{
834 u32 frame_val;
835 unsigned int loops;
836 int ret;
837
Matt Carlsonf07e9af2010-08-02 11:26:07 +0000838 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Michael Chanb5d37722006-09-27 16:06:21 -0700839 (reg == MII_TG3_CTRL || reg == MII_TG3_AUX_CTRL))
840 return 0;
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
843 tw32_f(MAC_MI_MODE,
844 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
845 udelay(80);
846 }
847
Matt Carlson882e9792009-09-01 13:21:36 +0000848 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 MI_COM_PHY_ADDR_MASK);
850 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
851 MI_COM_REG_ADDR_MASK);
852 frame_val |= (val & MI_COM_DATA_MASK);
853 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 tw32_f(MAC_MI_COM, frame_val);
856
857 loops = PHY_BUSY_LOOPS;
858 while (loops != 0) {
859 udelay(10);
860 frame_val = tr32(MAC_MI_COM);
861 if ((frame_val & MI_COM_BUSY) == 0) {
862 udelay(5);
863 frame_val = tr32(MAC_MI_COM);
864 break;
865 }
866 loops -= 1;
867 }
868
869 ret = -EBUSY;
870 if (loops != 0)
871 ret = 0;
872
873 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
874 tw32_f(MAC_MI_MODE, tp->mi_mode);
875 udelay(80);
876 }
877
878 return ret;
879}
880
Matt Carlson95e28692008-05-25 23:44:14 -0700881static int tg3_bmcr_reset(struct tg3 *tp)
882{
883 u32 phy_control;
884 int limit, err;
885
886 /* OK, reset it, and poll the BMCR_RESET bit until it
887 * clears or we time out.
888 */
889 phy_control = BMCR_RESET;
890 err = tg3_writephy(tp, MII_BMCR, phy_control);
891 if (err != 0)
892 return -EBUSY;
893
894 limit = 5000;
895 while (limit--) {
896 err = tg3_readphy(tp, MII_BMCR, &phy_control);
897 if (err != 0)
898 return -EBUSY;
899
900 if ((phy_control & BMCR_RESET) == 0) {
901 udelay(40);
902 break;
903 }
904 udelay(10);
905 }
Roel Kluind4675b52009-02-12 16:33:27 -0800906 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -0700907 return -EBUSY;
908
909 return 0;
910}
911
Matt Carlson158d7ab2008-05-29 01:37:54 -0700912static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
913{
Francois Romieu3d165432009-01-19 16:56:50 -0800914 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700915 u32 val;
916
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000917 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700918
919 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000920 val = -EIO;
921
922 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700923
924 return val;
925}
926
927static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
928{
Francois Romieu3d165432009-01-19 16:56:50 -0800929 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000930 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700931
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000932 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700933
934 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000935 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700936
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000937 spin_unlock_bh(&tp->lock);
938
939 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700940}
941
942static int tg3_mdio_reset(struct mii_bus *bp)
943{
944 return 0;
945}
946
Matt Carlson9c61d6b2008-11-03 16:54:56 -0800947static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -0700948{
949 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800950 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -0700951
Matt Carlson3f0e3ad2009-11-02 14:24:36 +0000952 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800953 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +0000954 case PHY_ID_BCM50610:
955 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800956 val = MAC_PHYCFG2_50610_LED_MODES;
957 break;
Matt Carlson6a443a02010-02-17 15:17:04 +0000958 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800959 val = MAC_PHYCFG2_AC131_LED_MODES;
960 break;
Matt Carlson6a443a02010-02-17 15:17:04 +0000961 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800962 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
963 break;
Matt Carlson6a443a02010-02-17 15:17:04 +0000964 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800965 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
966 break;
967 default:
Matt Carlsona9daf362008-05-25 23:49:44 -0700968 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800969 }
970
971 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
972 tw32(MAC_PHYCFG2, val);
973
974 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +0000975 val &= ~(MAC_PHYCFG1_RGMII_INT |
976 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
977 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800978 tw32(MAC_PHYCFG1, val);
979
980 return;
981 }
982
Matt Carlson14417062010-02-17 15:16:59 +0000983 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800984 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
985 MAC_PHYCFG2_FMODE_MASK_MASK |
986 MAC_PHYCFG2_GMODE_MASK_MASK |
987 MAC_PHYCFG2_ACT_MASK_MASK |
988 MAC_PHYCFG2_QUAL_MASK_MASK |
989 MAC_PHYCFG2_INBAND_ENABLE;
990
991 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -0700992
Matt Carlsonbb85fbb2009-08-25 10:09:07 +0000993 val = tr32(MAC_PHYCFG1);
994 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
995 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Matt Carlson14417062010-02-17 15:16:59 +0000996 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)) {
Matt Carlsona9daf362008-05-25 23:49:44 -0700997 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
998 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
999 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1000 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1001 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001002 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1003 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1004 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001005
Matt Carlsona9daf362008-05-25 23:49:44 -07001006 val = tr32(MAC_EXT_RGMII_MODE);
1007 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1008 MAC_RGMII_MODE_RX_QUALITY |
1009 MAC_RGMII_MODE_RX_ACTIVITY |
1010 MAC_RGMII_MODE_RX_ENG_DET |
1011 MAC_RGMII_MODE_TX_ENABLE |
1012 MAC_RGMII_MODE_TX_LOWPWR |
1013 MAC_RGMII_MODE_TX_RESET);
Matt Carlson14417062010-02-17 15:16:59 +00001014 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)) {
Matt Carlsona9daf362008-05-25 23:49:44 -07001015 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1016 val |= MAC_RGMII_MODE_RX_INT_B |
1017 MAC_RGMII_MODE_RX_QUALITY |
1018 MAC_RGMII_MODE_RX_ACTIVITY |
1019 MAC_RGMII_MODE_RX_ENG_DET;
1020 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1021 val |= MAC_RGMII_MODE_TX_ENABLE |
1022 MAC_RGMII_MODE_TX_LOWPWR |
1023 MAC_RGMII_MODE_TX_RESET;
1024 }
1025 tw32(MAC_EXT_RGMII_MODE, val);
1026}
1027
Matt Carlson158d7ab2008-05-29 01:37:54 -07001028static void tg3_mdio_start(struct tg3 *tp)
1029{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001030 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1031 tw32_f(MAC_MI_MODE, tp->mi_mode);
1032 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001033
Matt Carlson9ea48182010-02-17 15:17:01 +00001034 if ((tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) &&
1035 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1036 tg3_mdio_config_5785(tp);
1037}
1038
1039static int tg3_mdio_init(struct tg3 *tp)
1040{
1041 int i;
1042 u32 reg;
1043 struct phy_device *phydev;
1044
Matt Carlson0a58d662011-04-05 14:22:45 +00001045 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001046 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001047
Matt Carlson9c7df912010-06-05 17:24:36 +00001048 tp->phy_addr = PCI_FUNC(tp->pdev->devfn) + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001049
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001050 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1051 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1052 else
1053 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1054 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001055 if (is_serdes)
1056 tp->phy_addr += 7;
1057 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001058 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001059
Matt Carlson158d7ab2008-05-29 01:37:54 -07001060 tg3_mdio_start(tp);
1061
1062 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) ||
1063 (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED))
1064 return 0;
1065
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001066 tp->mdio_bus = mdiobus_alloc();
1067 if (tp->mdio_bus == NULL)
1068 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001069
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001070 tp->mdio_bus->name = "tg3 mdio bus";
1071 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001072 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001073 tp->mdio_bus->priv = tp;
1074 tp->mdio_bus->parent = &tp->pdev->dev;
1075 tp->mdio_bus->read = &tg3_mdio_read;
1076 tp->mdio_bus->write = &tg3_mdio_write;
1077 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001078 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001079 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001080
1081 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001082 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001083
1084 /* The bus registration will look for all the PHYs on the mdio bus.
1085 * Unfortunately, it does not ensure the PHY is powered up before
1086 * accessing the PHY ID registers. A chip reset is the
1087 * quickest way to bring the device back to an operational state..
1088 */
1089 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1090 tg3_bmcr_reset(tp);
1091
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001092 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001093 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001094 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001095 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001096 return i;
1097 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001098
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001099 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001100
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001101 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001102 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001103 mdiobus_unregister(tp->mdio_bus);
1104 mdiobus_free(tp->mdio_bus);
1105 return -ENODEV;
1106 }
1107
1108 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001109 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001110 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001111 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001112 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001113 case PHY_ID_BCM50610:
1114 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001115 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001116 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001117 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001118 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson14417062010-02-17 15:16:59 +00001119 if (tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)
Matt Carlsona9daf362008-05-25 23:49:44 -07001120 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
1121 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1122 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
1123 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1124 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001125 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001126 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001127 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001128 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001129 case PHY_ID_RTL8201E:
1130 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001131 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001132 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001133 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001134 break;
1135 }
1136
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001137 tp->tg3_flags3 |= TG3_FLG3_MDIOBUS_INITED;
1138
1139 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1140 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001141
1142 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001143}
1144
1145static void tg3_mdio_fini(struct tg3 *tp)
1146{
1147 if (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) {
1148 tp->tg3_flags3 &= ~TG3_FLG3_MDIOBUS_INITED;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001149 mdiobus_unregister(tp->mdio_bus);
1150 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001151 }
1152}
1153
Matt Carlsonddfc87b2010-10-14 10:37:40 +00001154static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
1155{
1156 int err;
1157
1158 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1159 if (err)
1160 goto done;
1161
1162 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1163 if (err)
1164 goto done;
1165
1166 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1167 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1168 if (err)
1169 goto done;
1170
1171 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
1172
1173done:
1174 return err;
1175}
1176
1177static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
1178{
1179 int err;
1180
1181 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1182 if (err)
1183 goto done;
1184
1185 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1186 if (err)
1187 goto done;
1188
1189 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1190 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1191 if (err)
1192 goto done;
1193
1194 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
1195
1196done:
1197 return err;
1198}
1199
Matt Carlson95e28692008-05-25 23:44:14 -07001200/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001201static inline void tg3_generate_fw_event(struct tg3 *tp)
1202{
1203 u32 val;
1204
1205 val = tr32(GRC_RX_CPU_EVENT);
1206 val |= GRC_RX_CPU_DRIVER_EVENT;
1207 tw32_f(GRC_RX_CPU_EVENT, val);
1208
1209 tp->last_event_jiffies = jiffies;
1210}
1211
1212#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1213
1214/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001215static void tg3_wait_for_event_ack(struct tg3 *tp)
1216{
1217 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001218 unsigned int delay_cnt;
1219 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001220
Matt Carlson4ba526c2008-08-15 14:10:04 -07001221 /* If enough time has passed, no wait is necessary. */
1222 time_remain = (long)(tp->last_event_jiffies + 1 +
1223 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1224 (long)jiffies;
1225 if (time_remain < 0)
1226 return;
1227
1228 /* Check if we can shorten the wait time. */
1229 delay_cnt = jiffies_to_usecs(time_remain);
1230 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1231 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1232 delay_cnt = (delay_cnt >> 3) + 1;
1233
1234 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001235 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1236 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001237 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001238 }
1239}
1240
1241/* tp->lock is held. */
1242static void tg3_ump_link_report(struct tg3 *tp)
1243{
1244 u32 reg;
1245 u32 val;
1246
1247 if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
1248 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
1249 return;
1250
1251 tg3_wait_for_event_ack(tp);
1252
1253 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1254
1255 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1256
1257 val = 0;
1258 if (!tg3_readphy(tp, MII_BMCR, &reg))
1259 val = reg << 16;
1260 if (!tg3_readphy(tp, MII_BMSR, &reg))
1261 val |= (reg & 0xffff);
1262 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1263
1264 val = 0;
1265 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1266 val = reg << 16;
1267 if (!tg3_readphy(tp, MII_LPA, &reg))
1268 val |= (reg & 0xffff);
1269 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1270
1271 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001272 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001273 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1274 val = reg << 16;
1275 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1276 val |= (reg & 0xffff);
1277 }
1278 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1279
1280 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1281 val = reg << 16;
1282 else
1283 val = 0;
1284 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1285
Matt Carlson4ba526c2008-08-15 14:10:04 -07001286 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001287}
1288
1289static void tg3_link_report(struct tg3 *tp)
1290{
1291 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001292 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001293 tg3_ump_link_report(tp);
1294 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001295 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1296 (tp->link_config.active_speed == SPEED_1000 ?
1297 1000 :
1298 (tp->link_config.active_speed == SPEED_100 ?
1299 100 : 10)),
1300 (tp->link_config.active_duplex == DUPLEX_FULL ?
1301 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001302
Joe Perches05dbe002010-02-17 19:44:19 +00001303 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1304 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1305 "on" : "off",
1306 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1307 "on" : "off");
Matt Carlson95e28692008-05-25 23:44:14 -07001308 tg3_ump_link_report(tp);
1309 }
1310}
1311
1312static u16 tg3_advert_flowctrl_1000T(u8 flow_ctrl)
1313{
1314 u16 miireg;
1315
Steve Glendinninge18ce342008-12-16 02:00:00 -08001316 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001317 miireg = ADVERTISE_PAUSE_CAP;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001318 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001319 miireg = ADVERTISE_PAUSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001320 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001321 miireg = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1322 else
1323 miireg = 0;
1324
1325 return miireg;
1326}
1327
1328static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1329{
1330 u16 miireg;
1331
Steve Glendinninge18ce342008-12-16 02:00:00 -08001332 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001333 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001334 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001335 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001336 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001337 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1338 else
1339 miireg = 0;
1340
1341 return miireg;
1342}
1343
Matt Carlson95e28692008-05-25 23:44:14 -07001344static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1345{
1346 u8 cap = 0;
1347
1348 if (lcladv & ADVERTISE_1000XPAUSE) {
1349 if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1350 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001351 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001352 else if (rmtadv & LPA_1000XPAUSE_ASYM)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001353 cap = FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001354 } else {
1355 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001356 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001357 }
1358 } else if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1359 if ((rmtadv & LPA_1000XPAUSE) && (rmtadv & LPA_1000XPAUSE_ASYM))
Steve Glendinninge18ce342008-12-16 02:00:00 -08001360 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001361 }
1362
1363 return cap;
1364}
1365
Matt Carlsonf51f3562008-05-25 23:45:08 -07001366static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001367{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001368 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001369 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001370 u32 old_rx_mode = tp->rx_mode;
1371 u32 old_tx_mode = tp->tx_mode;
1372
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001373 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001374 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001375 else
1376 autoneg = tp->link_config.autoneg;
1377
1378 if (autoneg == AUTONEG_ENABLE &&
Matt Carlson95e28692008-05-25 23:44:14 -07001379 (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001380 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001381 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001382 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001383 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001384 } else
1385 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001386
Matt Carlsonf51f3562008-05-25 23:45:08 -07001387 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001388
Steve Glendinninge18ce342008-12-16 02:00:00 -08001389 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001390 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1391 else
1392 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1393
Matt Carlsonf51f3562008-05-25 23:45:08 -07001394 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001395 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001396
Steve Glendinninge18ce342008-12-16 02:00:00 -08001397 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001398 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1399 else
1400 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1401
Matt Carlsonf51f3562008-05-25 23:45:08 -07001402 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001403 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001404}
1405
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001406static void tg3_adjust_link(struct net_device *dev)
1407{
1408 u8 oldflowctrl, linkmesg = 0;
1409 u32 mac_mode, lcl_adv, rmt_adv;
1410 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001411 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001412
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001413 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001414
1415 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1416 MAC_MODE_HALF_DUPLEX);
1417
1418 oldflowctrl = tp->link_config.active_flowctrl;
1419
1420 if (phydev->link) {
1421 lcl_adv = 0;
1422 rmt_adv = 0;
1423
1424 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1425 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001426 else if (phydev->speed == SPEED_1000 ||
1427 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001428 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001429 else
1430 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001431
1432 if (phydev->duplex == DUPLEX_HALF)
1433 mac_mode |= MAC_MODE_HALF_DUPLEX;
1434 else {
1435 lcl_adv = tg3_advert_flowctrl_1000T(
1436 tp->link_config.flowctrl);
1437
1438 if (phydev->pause)
1439 rmt_adv = LPA_PAUSE_CAP;
1440 if (phydev->asym_pause)
1441 rmt_adv |= LPA_PAUSE_ASYM;
1442 }
1443
1444 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1445 } else
1446 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1447
1448 if (mac_mode != tp->mac_mode) {
1449 tp->mac_mode = mac_mode;
1450 tw32_f(MAC_MODE, tp->mac_mode);
1451 udelay(40);
1452 }
1453
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001454 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1455 if (phydev->speed == SPEED_10)
1456 tw32(MAC_MI_STAT,
1457 MAC_MI_STAT_10MBPS_MODE |
1458 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1459 else
1460 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1461 }
1462
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001463 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1464 tw32(MAC_TX_LENGTHS,
1465 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1466 (6 << TX_LENGTHS_IPG_SHIFT) |
1467 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1468 else
1469 tw32(MAC_TX_LENGTHS,
1470 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1471 (6 << TX_LENGTHS_IPG_SHIFT) |
1472 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1473
1474 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1475 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1476 phydev->speed != tp->link_config.active_speed ||
1477 phydev->duplex != tp->link_config.active_duplex ||
1478 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001479 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001480
1481 tp->link_config.active_speed = phydev->speed;
1482 tp->link_config.active_duplex = phydev->duplex;
1483
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001484 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001485
1486 if (linkmesg)
1487 tg3_link_report(tp);
1488}
1489
1490static int tg3_phy_init(struct tg3 *tp)
1491{
1492 struct phy_device *phydev;
1493
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001494 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001495 return 0;
1496
1497 /* Bring the PHY back to a known state. */
1498 tg3_bmcr_reset(tp);
1499
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001500 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001501
1502 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad352008-11-10 13:55:14 -08001503 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001504 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001505 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001506 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001507 return PTR_ERR(phydev);
1508 }
1509
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001510 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001511 switch (phydev->interface) {
1512 case PHY_INTERFACE_MODE_GMII:
1513 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001514 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08001515 phydev->supported &= (PHY_GBIT_FEATURES |
1516 SUPPORTED_Pause |
1517 SUPPORTED_Asym_Pause);
1518 break;
1519 }
1520 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001521 case PHY_INTERFACE_MODE_MII:
1522 phydev->supported &= (PHY_BASIC_FEATURES |
1523 SUPPORTED_Pause |
1524 SUPPORTED_Asym_Pause);
1525 break;
1526 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001527 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001528 return -EINVAL;
1529 }
1530
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001531 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001532
1533 phydev->advertising = phydev->supported;
1534
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001535 return 0;
1536}
1537
1538static void tg3_phy_start(struct tg3 *tp)
1539{
1540 struct phy_device *phydev;
1541
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001542 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001543 return;
1544
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001545 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001546
Matt Carlson80096062010-08-02 11:26:06 +00001547 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
1548 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001549 phydev->speed = tp->link_config.orig_speed;
1550 phydev->duplex = tp->link_config.orig_duplex;
1551 phydev->autoneg = tp->link_config.orig_autoneg;
1552 phydev->advertising = tp->link_config.orig_advertising;
1553 }
1554
1555 phy_start(phydev);
1556
1557 phy_start_aneg(phydev);
1558}
1559
1560static void tg3_phy_stop(struct tg3 *tp)
1561{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001562 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001563 return;
1564
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001565 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001566}
1567
1568static void tg3_phy_fini(struct tg3 *tp)
1569{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001570 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001571 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001572 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001573 }
1574}
1575
Matt Carlson52b02d02010-10-14 10:37:41 +00001576static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
1577{
1578 int err;
1579
1580 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1581 if (!err)
1582 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
1583
1584 return err;
1585}
1586
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00001587static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001588{
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00001589 int err;
1590
1591 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1592 if (!err)
1593 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1594
1595 return err;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001596}
1597
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001598static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1599{
1600 u32 phytest;
1601
1602 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1603 u32 phy;
1604
1605 tg3_writephy(tp, MII_TG3_FET_TEST,
1606 phytest | MII_TG3_FET_SHADOW_EN);
1607 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1608 if (enable)
1609 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1610 else
1611 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1612 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1613 }
1614 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1615 }
1616}
1617
Matt Carlson6833c042008-11-21 17:18:59 -08001618static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1619{
1620 u32 reg;
1621
Matt Carlsonecf14102010-01-20 16:58:05 +00001622 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlson0a58d662011-04-05 14:22:45 +00001623 ((tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001624 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001625 return;
1626
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001627 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001628 tg3_phy_fet_toggle_apd(tp, enable);
1629 return;
1630 }
1631
Matt Carlson6833c042008-11-21 17:18:59 -08001632 reg = MII_TG3_MISC_SHDW_WREN |
1633 MII_TG3_MISC_SHDW_SCR5_SEL |
1634 MII_TG3_MISC_SHDW_SCR5_LPED |
1635 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1636 MII_TG3_MISC_SHDW_SCR5_SDTL |
1637 MII_TG3_MISC_SHDW_SCR5_C125OE;
1638 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1639 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1640
1641 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1642
1643
1644 reg = MII_TG3_MISC_SHDW_WREN |
1645 MII_TG3_MISC_SHDW_APD_SEL |
1646 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1647 if (enable)
1648 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1649
1650 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1651}
1652
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001653static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
1654{
1655 u32 phy;
1656
1657 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001658 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001659 return;
1660
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001661 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001662 u32 ephy;
1663
Matt Carlson535ef6e2009-08-25 10:09:36 +00001664 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
1665 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
1666
1667 tg3_writephy(tp, MII_TG3_FET_TEST,
1668 ephy | MII_TG3_FET_SHADOW_EN);
1669 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001670 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00001671 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001672 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00001673 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1674 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001675 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00001676 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001677 }
1678 } else {
1679 phy = MII_TG3_AUXCTL_MISC_RDSEL_MISC |
1680 MII_TG3_AUXCTL_SHDWSEL_MISC;
1681 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, phy) &&
1682 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &phy)) {
1683 if (enable)
1684 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1685 else
1686 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1687 phy |= MII_TG3_AUXCTL_MISC_WREN;
1688 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1689 }
1690 }
1691}
1692
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693static void tg3_phy_set_wirespeed(struct tg3 *tp)
1694{
1695 u32 val;
1696
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001697 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 return;
1699
1700 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x7007) &&
1701 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &val))
1702 tg3_writephy(tp, MII_TG3_AUX_CTRL,
1703 (val | (1 << 15) | (1 << 4)));
1704}
1705
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001706static void tg3_phy_apply_otp(struct tg3 *tp)
1707{
1708 u32 otp, phy;
1709
1710 if (!tp->phy_otp)
1711 return;
1712
1713 otp = tp->phy_otp;
1714
1715 /* Enable SM_DSP clock and tx 6dB coding. */
1716 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1717 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
1718 MII_TG3_AUXCTL_ACTL_TX_6DB;
1719 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1720
1721 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
1722 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
1723 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
1724
1725 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
1726 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
1727 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
1728
1729 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
1730 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
1731 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
1732
1733 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
1734 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
1735
1736 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
1737 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
1738
1739 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
1740 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
1741 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
1742
1743 /* Turn off SM_DSP clock. */
1744 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1745 MII_TG3_AUXCTL_ACTL_TX_6DB;
1746 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1747}
1748
Matt Carlson52b02d02010-10-14 10:37:41 +00001749static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
1750{
1751 u32 val;
1752
1753 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
1754 return;
1755
1756 tp->setlpicnt = 0;
1757
1758 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
1759 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00001760 tp->link_config.active_duplex == DUPLEX_FULL &&
1761 (tp->link_config.active_speed == SPEED_100 ||
1762 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00001763 u32 eeectl;
1764
1765 if (tp->link_config.active_speed == SPEED_1000)
1766 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
1767 else
1768 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
1769
1770 tw32(TG3_CPMU_EEE_CTRL, eeectl);
1771
Matt Carlson3110f5f52010-12-06 08:28:50 +00001772 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
1773 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00001774
Matt Carlson21a00ab2011-01-25 15:58:55 +00001775 switch (val) {
1776 case TG3_CL45_D7_EEERES_STAT_LP_1000T:
1777 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
1778 case ASIC_REV_5717:
1779 case ASIC_REV_5719:
1780 case ASIC_REV_57765:
1781 /* Enable SM_DSP clock and tx 6dB coding. */
1782 val = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1783 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
1784 MII_TG3_AUXCTL_ACTL_TX_6DB;
1785 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
1786
1787 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
1788
1789 /* Turn off SM_DSP clock. */
1790 val = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1791 MII_TG3_AUXCTL_ACTL_TX_6DB;
1792 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
1793 }
1794 /* Fallthrough */
1795 case TG3_CL45_D7_EEERES_STAT_LP_100TX:
Matt Carlson52b02d02010-10-14 10:37:41 +00001796 tp->setlpicnt = 2;
Matt Carlson21a00ab2011-01-25 15:58:55 +00001797 }
Matt Carlson52b02d02010-10-14 10:37:41 +00001798 }
1799
1800 if (!tp->setlpicnt) {
1801 val = tr32(TG3_CPMU_EEE_MODE);
1802 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
1803 }
1804}
1805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806static int tg3_wait_macro_done(struct tg3 *tp)
1807{
1808 int limit = 100;
1809
1810 while (limit--) {
1811 u32 tmp32;
1812
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001813 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 if ((tmp32 & 0x1000) == 0)
1815 break;
1816 }
1817 }
Roel Kluind4675b52009-02-12 16:33:27 -08001818 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 return -EBUSY;
1820
1821 return 0;
1822}
1823
1824static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
1825{
1826 static const u32 test_pat[4][6] = {
1827 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
1828 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
1829 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
1830 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
1831 };
1832 int chan;
1833
1834 for (chan = 0; chan < 4; chan++) {
1835 int i;
1836
1837 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1838 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001839 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
1841 for (i = 0; i < 6; i++)
1842 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
1843 test_pat[chan][i]);
1844
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001845 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 if (tg3_wait_macro_done(tp)) {
1847 *resetp = 1;
1848 return -EBUSY;
1849 }
1850
1851 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1852 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001853 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 if (tg3_wait_macro_done(tp)) {
1855 *resetp = 1;
1856 return -EBUSY;
1857 }
1858
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001859 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 if (tg3_wait_macro_done(tp)) {
1861 *resetp = 1;
1862 return -EBUSY;
1863 }
1864
1865 for (i = 0; i < 6; i += 2) {
1866 u32 low, high;
1867
1868 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
1869 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
1870 tg3_wait_macro_done(tp)) {
1871 *resetp = 1;
1872 return -EBUSY;
1873 }
1874 low &= 0x7fff;
1875 high &= 0x000f;
1876 if (low != test_pat[chan][i] ||
1877 high != test_pat[chan][i+1]) {
1878 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
1879 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
1880 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
1881
1882 return -EBUSY;
1883 }
1884 }
1885 }
1886
1887 return 0;
1888}
1889
1890static int tg3_phy_reset_chanpat(struct tg3 *tp)
1891{
1892 int chan;
1893
1894 for (chan = 0; chan < 4; chan++) {
1895 int i;
1896
1897 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1898 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001899 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 for (i = 0; i < 6; i++)
1901 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001902 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 if (tg3_wait_macro_done(tp))
1904 return -EBUSY;
1905 }
1906
1907 return 0;
1908}
1909
1910static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
1911{
1912 u32 reg32, phy9_orig;
1913 int retries, do_phy_reset, err;
1914
1915 retries = 10;
1916 do_phy_reset = 1;
1917 do {
1918 if (do_phy_reset) {
1919 err = tg3_bmcr_reset(tp);
1920 if (err)
1921 return err;
1922 do_phy_reset = 0;
1923 }
1924
1925 /* Disable transmitter and interrupt. */
1926 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
1927 continue;
1928
1929 reg32 |= 0x3000;
1930 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1931
1932 /* Set full-duplex, 1000 mbps. */
1933 tg3_writephy(tp, MII_BMCR,
1934 BMCR_FULLDPLX | TG3_BMCR_SPEED1000);
1935
1936 /* Set to master mode. */
1937 if (tg3_readphy(tp, MII_TG3_CTRL, &phy9_orig))
1938 continue;
1939
1940 tg3_writephy(tp, MII_TG3_CTRL,
1941 (MII_TG3_CTRL_AS_MASTER |
1942 MII_TG3_CTRL_ENABLE_AS_MASTER));
1943
1944 /* Enable SM_DSP_CLOCK and 6dB. */
1945 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1946
1947 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00001948 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
1950 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
1951 if (!err)
1952 break;
1953 } while (--retries);
1954
1955 err = tg3_phy_reset_chanpat(tp);
1956 if (err)
1957 return err;
1958
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00001959 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
1961 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001962 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
1964 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
1965 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
1966 /* Set Extended packet length bit for jumbo frames */
1967 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4400);
Matt Carlson859a588792010-04-05 10:19:28 +00001968 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1970 }
1971
1972 tg3_writephy(tp, MII_TG3_CTRL, phy9_orig);
1973
1974 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
1975 reg32 &= ~0x3000;
1976 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1977 } else if (!err)
1978 err = -EBUSY;
1979
1980 return err;
1981}
1982
1983/* This will reset the tigon3 PHY if there is no valid
1984 * link unless the FORCE argument is non-zero.
1985 */
1986static int tg3_phy_reset(struct tg3 *tp)
1987{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00001988 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 int err;
1990
Michael Chan60189dd2006-12-17 17:08:07 -08001991 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08001992 val = tr32(GRC_MISC_CFG);
1993 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
1994 udelay(40);
1995 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00001996 err = tg3_readphy(tp, MII_BMSR, &val);
1997 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 if (err != 0)
1999 return -EBUSY;
2000
Michael Chanc8e1e822006-04-29 18:55:17 -07002001 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
2002 netif_carrier_off(tp->dev);
2003 tg3_link_report(tp);
2004 }
2005
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2007 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2008 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2009 err = tg3_phy_reset_5703_4_5(tp);
2010 if (err)
2011 return err;
2012 goto out;
2013 }
2014
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002015 cpmuctrl = 0;
2016 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2017 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2018 cpmuctrl = tr32(TG3_CPMU_CTRL);
2019 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2020 tw32(TG3_CPMU_CTRL,
2021 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2022 }
2023
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 err = tg3_bmcr_reset(tp);
2025 if (err)
2026 return err;
2027
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002028 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002029 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2030 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002031
2032 tw32(TG3_CPMU_CTRL, cpmuctrl);
2033 }
2034
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002035 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2036 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002037 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2038 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2039 CPMU_LSPD_1000MB_MACCLK_12_5) {
2040 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2041 udelay(40);
2042 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2043 }
2044 }
2045
Matt Carlson0a58d662011-04-05 14:22:45 +00002046 if ((tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002047 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002048 return 0;
2049
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002050 tg3_phy_apply_otp(tp);
2051
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002052 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002053 tg3_phy_toggle_apd(tp, true);
2054 else
2055 tg3_phy_toggle_apd(tp, false);
2056
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057out:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002058 if (tp->phy_flags & TG3_PHYFLG_ADC_BUG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002060 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2061 tg3_phydsp_write(tp, 0x000a, 0x0323);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
2063 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002064 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002065 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2066 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002068 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002070 tg3_phydsp_write(tp, 0x000a, 0x310b);
2071 tg3_phydsp_write(tp, 0x201f, 0x9506);
2072 tg3_phydsp_write(tp, 0x401f, 0x14e2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002074 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Michael Chanc424cb22006-04-29 18:56:34 -07002075 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
2076 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002077 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
Michael Chanc1d2a192007-01-08 19:57:20 -08002078 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2079 tg3_writephy(tp, MII_TG3_TEST1,
2080 MII_TG3_TEST1_TRIM_EN | 0x4);
2081 } else
2082 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
Michael Chanc424cb22006-04-29 18:56:34 -07002083 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
2084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 /* Set Extended packet length bit (bit 14) on all chips that */
2086 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002087 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 /* Cannot do read-modify-write on 5401 */
2089 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
Matt Carlson8f666b02009-08-28 13:58:24 +00002090 } else if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 /* Set bit 14 with read-modify-write to preserve other bits */
2092 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0007) &&
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002093 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &val))
2094 tg3_writephy(tp, MII_TG3_AUX_CTRL, val | 0x4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 }
2096
2097 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2098 * jumbo frames transmission.
2099 */
Matt Carlson8f666b02009-08-28 13:58:24 +00002100 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002101 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002102 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002103 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 }
2105
Michael Chan715116a2006-09-27 16:09:25 -07002106 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002107 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002108 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002109 }
2110
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002111 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 tg3_phy_set_wirespeed(tp);
2113 return 0;
2114}
2115
2116static void tg3_frob_aux_power(struct tg3 *tp)
2117{
Matt Carlson683644b2011-03-09 16:58:23 +00002118 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
Matt Carlson334355a2010-01-20 16:58:10 +00002120 /* The GPIOs do something completely different on 57765. */
2121 if ((tp->tg3_flags2 & TG3_FLG2_IS_NIC) == 0 ||
Matt Carlsona50d0792010-06-05 17:24:37 +00002122 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson334355a2010-01-20 16:58:10 +00002123 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 return;
2125
Matt Carlson683644b2011-03-09 16:58:23 +00002126 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2127 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +00002128 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2129 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) &&
Matt Carlson683644b2011-03-09 16:58:23 +00002130 tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002131 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002133 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002134
Michael Chanbc1c7562006-03-20 17:48:03 -08002135 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002136 if (dev_peer) {
2137 struct tg3 *tp_peer = netdev_priv(dev_peer);
2138
2139 if (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE)
2140 return;
2141
2142 if ((tp_peer->tg3_flags & TG3_FLAG_WOL_ENABLE) ||
2143 (tp_peer->tg3_flags & TG3_FLAG_ENABLE_ASF))
2144 need_vaux = true;
2145 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
Matt Carlson683644b2011-03-09 16:58:23 +00002148 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) ||
2149 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
2150 need_vaux = true;
2151
2152 if (need_vaux) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2154 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002155 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2156 (GRC_LCLCTRL_GPIO_OE0 |
2157 GRC_LCLCTRL_GPIO_OE1 |
2158 GRC_LCLCTRL_GPIO_OE2 |
2159 GRC_LCLCTRL_GPIO_OUTPUT0 |
2160 GRC_LCLCTRL_GPIO_OUTPUT1),
2161 100);
Matt Carlson8d519ab2009-04-20 06:58:01 +00002162 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2163 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -07002164 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2165 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2166 GRC_LCLCTRL_GPIO_OE1 |
2167 GRC_LCLCTRL_GPIO_OE2 |
2168 GRC_LCLCTRL_GPIO_OUTPUT0 |
2169 GRC_LCLCTRL_GPIO_OUTPUT1 |
2170 tp->grc_local_ctrl;
2171 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2172
2173 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2174 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2175
2176 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2177 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 } else {
2179 u32 no_gpio2;
Michael Chandc56b7d2005-12-19 16:26:28 -08002180 u32 grc_local_ctrl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
Michael Chandc56b7d2005-12-19 16:26:28 -08002182 /* Workaround to prevent overdrawing Amps. */
2183 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2184 ASIC_REV_5714) {
2185 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chanb401e9e2005-12-19 16:27:04 -08002186 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2187 grc_local_ctrl, 100);
Michael Chandc56b7d2005-12-19 16:26:28 -08002188 }
2189
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 /* On 5753 and variants, GPIO2 cannot be used. */
2191 no_gpio2 = tp->nic_sram_data_cfg &
2192 NIC_SRAM_DATA_CFG_NO_GPIO2;
2193
Michael Chandc56b7d2005-12-19 16:26:28 -08002194 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 GRC_LCLCTRL_GPIO_OE1 |
2196 GRC_LCLCTRL_GPIO_OE2 |
2197 GRC_LCLCTRL_GPIO_OUTPUT1 |
2198 GRC_LCLCTRL_GPIO_OUTPUT2;
2199 if (no_gpio2) {
2200 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2201 GRC_LCLCTRL_GPIO_OUTPUT2);
2202 }
Michael Chanb401e9e2005-12-19 16:27:04 -08002203 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2204 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
2206 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2207
Michael Chanb401e9e2005-12-19 16:27:04 -08002208 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2209 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
2211 if (!no_gpio2) {
2212 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chanb401e9e2005-12-19 16:27:04 -08002213 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2214 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 }
2216 }
2217 } else {
2218 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
2219 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002220 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2221 (GRC_LCLCTRL_GPIO_OE1 |
2222 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
Michael Chanb401e9e2005-12-19 16:27:04 -08002224 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2225 GRC_LCLCTRL_GPIO_OE1, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Michael Chanb401e9e2005-12-19 16:27:04 -08002227 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2228 (GRC_LCLCTRL_GPIO_OE1 |
2229 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 }
2231 }
2232}
2233
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002234static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2235{
2236 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2237 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002238 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002239 if (speed != SPEED_10)
2240 return 1;
2241 } else if (speed == SPEED_10)
2242 return 1;
2243
2244 return 0;
2245}
2246
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247static int tg3_setup_phy(struct tg3 *, int);
2248
2249#define RESET_KIND_SHUTDOWN 0
2250#define RESET_KIND_INIT 1
2251#define RESET_KIND_SUSPEND 2
2252
2253static void tg3_write_sig_post_reset(struct tg3 *, int);
2254static int tg3_halt_cpu(struct tg3 *, u32);
2255
Matt Carlson0a459aa2008-11-03 16:54:15 -08002256static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002257{
Matt Carlsonce057f02007-11-12 21:08:03 -08002258 u32 val;
2259
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002260 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002261 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2262 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2263 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2264
2265 sg_dig_ctrl |=
2266 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2267 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2268 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2269 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002270 return;
Michael Chan51297242007-02-13 12:17:57 -08002271 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002272
Michael Chan60189dd2006-12-17 17:08:07 -08002273 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002274 tg3_bmcr_reset(tp);
2275 val = tr32(GRC_MISC_CFG);
2276 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2277 udelay(40);
2278 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002279 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002280 u32 phytest;
2281 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2282 u32 phy;
2283
2284 tg3_writephy(tp, MII_ADVERTISE, 0);
2285 tg3_writephy(tp, MII_BMCR,
2286 BMCR_ANENABLE | BMCR_ANRESTART);
2287
2288 tg3_writephy(tp, MII_TG3_FET_TEST,
2289 phytest | MII_TG3_FET_SHADOW_EN);
2290 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2291 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2292 tg3_writephy(tp,
2293 MII_TG3_FET_SHDW_AUXMODE4,
2294 phy);
2295 }
2296 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2297 }
2298 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002299 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002300 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2301 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002302
2303 tg3_writephy(tp, MII_TG3_AUX_CTRL,
2304 MII_TG3_AUXCTL_SHDWSEL_PWRCTL |
2305 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2306 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2307 MII_TG3_AUXCTL_PCTL_VREG_11V);
Michael Chan715116a2006-09-27 16:09:25 -07002308 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002309
Michael Chan15c3b692006-03-22 01:06:52 -08002310 /* The PHY should not be powered down on some chips because
2311 * of bugs.
2312 */
2313 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2314 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2315 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002316 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Michael Chan15c3b692006-03-22 01:06:52 -08002317 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002318
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002319 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2320 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002321 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2322 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2323 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2324 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2325 }
2326
Michael Chan15c3b692006-03-22 01:06:52 -08002327 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2328}
2329
Matt Carlson3f007892008-11-03 16:51:36 -08002330/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002331static int tg3_nvram_lock(struct tg3 *tp)
2332{
2333 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2334 int i;
2335
2336 if (tp->nvram_lock_cnt == 0) {
2337 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2338 for (i = 0; i < 8000; i++) {
2339 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2340 break;
2341 udelay(20);
2342 }
2343 if (i == 8000) {
2344 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2345 return -ENODEV;
2346 }
2347 }
2348 tp->nvram_lock_cnt++;
2349 }
2350 return 0;
2351}
2352
2353/* tp->lock is held. */
2354static void tg3_nvram_unlock(struct tg3 *tp)
2355{
2356 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2357 if (tp->nvram_lock_cnt > 0)
2358 tp->nvram_lock_cnt--;
2359 if (tp->nvram_lock_cnt == 0)
2360 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2361 }
2362}
2363
2364/* tp->lock is held. */
2365static void tg3_enable_nvram_access(struct tg3 *tp)
2366{
2367 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +00002368 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002369 u32 nvaccess = tr32(NVRAM_ACCESS);
2370
2371 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2372 }
2373}
2374
2375/* tp->lock is held. */
2376static void tg3_disable_nvram_access(struct tg3 *tp)
2377{
2378 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +00002379 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002380 u32 nvaccess = tr32(NVRAM_ACCESS);
2381
2382 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2383 }
2384}
2385
2386static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2387 u32 offset, u32 *val)
2388{
2389 u32 tmp;
2390 int i;
2391
2392 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2393 return -EINVAL;
2394
2395 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2396 EEPROM_ADDR_DEVID_MASK |
2397 EEPROM_ADDR_READ);
2398 tw32(GRC_EEPROM_ADDR,
2399 tmp |
2400 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2401 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2402 EEPROM_ADDR_ADDR_MASK) |
2403 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2404
2405 for (i = 0; i < 1000; i++) {
2406 tmp = tr32(GRC_EEPROM_ADDR);
2407
2408 if (tmp & EEPROM_ADDR_COMPLETE)
2409 break;
2410 msleep(1);
2411 }
2412 if (!(tmp & EEPROM_ADDR_COMPLETE))
2413 return -EBUSY;
2414
Matt Carlson62cedd12009-04-20 14:52:29 -07002415 tmp = tr32(GRC_EEPROM_DATA);
2416
2417 /*
2418 * The data will always be opposite the native endian
2419 * format. Perform a blind byteswap to compensate.
2420 */
2421 *val = swab32(tmp);
2422
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002423 return 0;
2424}
2425
2426#define NVRAM_CMD_TIMEOUT 10000
2427
2428static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2429{
2430 int i;
2431
2432 tw32(NVRAM_CMD, nvram_cmd);
2433 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2434 udelay(10);
2435 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2436 udelay(10);
2437 break;
2438 }
2439 }
2440
2441 if (i == NVRAM_CMD_TIMEOUT)
2442 return -EBUSY;
2443
2444 return 0;
2445}
2446
2447static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2448{
2449 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2450 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2451 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2452 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2453 (tp->nvram_jedecnum == JEDEC_ATMEL))
2454
2455 addr = ((addr / tp->nvram_pagesize) <<
2456 ATMEL_AT45DB0X1B_PAGE_POS) +
2457 (addr % tp->nvram_pagesize);
2458
2459 return addr;
2460}
2461
2462static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2463{
2464 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2465 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2466 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2467 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2468 (tp->nvram_jedecnum == JEDEC_ATMEL))
2469
2470 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2471 tp->nvram_pagesize) +
2472 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2473
2474 return addr;
2475}
2476
Matt Carlsone4f34112009-02-25 14:25:00 +00002477/* NOTE: Data read in from NVRAM is byteswapped according to
2478 * the byteswapping settings for all other register accesses.
2479 * tg3 devices are BE devices, so on a BE machine, the data
2480 * returned will be exactly as it is seen in NVRAM. On a LE
2481 * machine, the 32-bit value will be byteswapped.
2482 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002483static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2484{
2485 int ret;
2486
2487 if (!(tp->tg3_flags & TG3_FLAG_NVRAM))
2488 return tg3_nvram_read_using_eeprom(tp, offset, val);
2489
2490 offset = tg3_nvram_phys_addr(tp, offset);
2491
2492 if (offset > NVRAM_ADDR_MSK)
2493 return -EINVAL;
2494
2495 ret = tg3_nvram_lock(tp);
2496 if (ret)
2497 return ret;
2498
2499 tg3_enable_nvram_access(tp);
2500
2501 tw32(NVRAM_ADDR, offset);
2502 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2503 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2504
2505 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002506 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002507
2508 tg3_disable_nvram_access(tp);
2509
2510 tg3_nvram_unlock(tp);
2511
2512 return ret;
2513}
2514
Matt Carlsona9dc5292009-02-25 14:25:30 +00002515/* Ensures NVRAM data is in bytestream format. */
2516static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002517{
2518 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002519 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002520 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002521 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002522 return res;
2523}
2524
2525/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08002526static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2527{
2528 u32 addr_high, addr_low;
2529 int i;
2530
2531 addr_high = ((tp->dev->dev_addr[0] << 8) |
2532 tp->dev->dev_addr[1]);
2533 addr_low = ((tp->dev->dev_addr[2] << 24) |
2534 (tp->dev->dev_addr[3] << 16) |
2535 (tp->dev->dev_addr[4] << 8) |
2536 (tp->dev->dev_addr[5] << 0));
2537 for (i = 0; i < 4; i++) {
2538 if (i == 1 && skip_mac_1)
2539 continue;
2540 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2541 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2542 }
2543
2544 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2545 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2546 for (i = 0; i < 12; i++) {
2547 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2548 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2549 }
2550 }
2551
2552 addr_high = (tp->dev->dev_addr[0] +
2553 tp->dev->dev_addr[1] +
2554 tp->dev->dev_addr[2] +
2555 tp->dev->dev_addr[3] +
2556 tp->dev->dev_addr[4] +
2557 tp->dev->dev_addr[5]) &
2558 TX_BACKOFF_SEED_MASK;
2559 tw32(MAC_TX_BACKOFF_SEED, addr_high);
2560}
2561
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002562static void tg3_enable_register_access(struct tg3 *tp)
2563{
2564 /*
2565 * Make sure register accesses (indirect or otherwise) will function
2566 * correctly.
2567 */
2568 pci_write_config_dword(tp->pdev,
2569 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
2570}
2571
2572static int tg3_power_up(struct tg3 *tp)
2573{
2574 tg3_enable_register_access(tp);
2575
2576 pci_set_power_state(tp->pdev, PCI_D0);
2577
2578 /* Switch out of Vaux if it is a NIC */
2579 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
2580 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl, 100);
2581
2582 return 0;
2583}
2584
2585static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586{
2587 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002588 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002590 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002591
2592 /* Restore the CLKREQ setting. */
2593 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
2594 u16 lnkctl;
2595
2596 pci_read_config_word(tp->pdev,
2597 tp->pcie_cap + PCI_EXP_LNKCTL,
2598 &lnkctl);
2599 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2600 pci_write_config_word(tp->pdev,
2601 tp->pcie_cap + PCI_EXP_LNKCTL,
2602 lnkctl);
2603 }
2604
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2606 tw32(TG3PCI_MISC_HOST_CTRL,
2607 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2608
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002609 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002610 (tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
2611
Matt Carlsondd477002008-05-25 23:45:58 -07002612 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002613 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002614 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00002615 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002616 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002617 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002618
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002619 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002620
Matt Carlson80096062010-08-02 11:26:06 +00002621 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002622
2623 tp->link_config.orig_speed = phydev->speed;
2624 tp->link_config.orig_duplex = phydev->duplex;
2625 tp->link_config.orig_autoneg = phydev->autoneg;
2626 tp->link_config.orig_advertising = phydev->advertising;
2627
2628 advertising = ADVERTISED_TP |
2629 ADVERTISED_Pause |
2630 ADVERTISED_Autoneg |
2631 ADVERTISED_10baseT_Half;
2632
2633 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002634 device_should_wake) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002635 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2636 advertising |=
2637 ADVERTISED_100baseT_Half |
2638 ADVERTISED_100baseT_Full |
2639 ADVERTISED_10baseT_Full;
2640 else
2641 advertising |= ADVERTISED_10baseT_Full;
2642 }
2643
2644 phydev->advertising = advertising;
2645
2646 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002647
2648 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00002649 if (phyid != PHY_ID_BCMAC131) {
2650 phyid &= PHY_BCM_OUI_MASK;
2651 if (phyid == PHY_BCM_OUI_1 ||
2652 phyid == PHY_BCM_OUI_2 ||
2653 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08002654 do_low_power = true;
2655 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002656 }
Matt Carlsondd477002008-05-25 23:45:58 -07002657 } else {
Matt Carlson20232762008-12-21 20:18:56 -08002658 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002659
Matt Carlson80096062010-08-02 11:26:06 +00002660 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
2661 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07002662 tp->link_config.orig_speed = tp->link_config.speed;
2663 tp->link_config.orig_duplex = tp->link_config.duplex;
2664 tp->link_config.orig_autoneg = tp->link_config.autoneg;
2665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002667 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Matt Carlsondd477002008-05-25 23:45:58 -07002668 tp->link_config.speed = SPEED_10;
2669 tp->link_config.duplex = DUPLEX_HALF;
2670 tp->link_config.autoneg = AUTONEG_ENABLE;
2671 tg3_setup_phy(tp, 0);
2672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 }
2674
Michael Chanb5d37722006-09-27 16:06:21 -07002675 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2676 u32 val;
2677
2678 val = tr32(GRC_VCPU_EXT_CTRL);
2679 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
2680 } else if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08002681 int i;
2682 u32 val;
2683
2684 for (i = 0; i < 200; i++) {
2685 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2686 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2687 break;
2688 msleep(1);
2689 }
2690 }
Gary Zambranoa85feb82007-05-05 11:52:19 -07002691 if (tp->tg3_flags & TG3_FLAG_WOL_CAP)
2692 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2693 WOL_DRV_STATE_SHUTDOWN |
2694 WOL_DRV_WOL |
2695 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08002696
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002697 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 u32 mac_mode;
2699
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002700 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002701 if (do_low_power) {
Matt Carlsondd477002008-05-25 23:45:58 -07002702 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x5a);
2703 udelay(40);
2704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002706 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07002707 mac_mode = MAC_MODE_PORT_MODE_GMII;
2708 else
2709 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002711 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2712 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2713 ASIC_REV_5700) {
2714 u32 speed = (tp->tg3_flags &
2715 TG3_FLAG_WOL_SPEED_100MB) ?
2716 SPEED_100 : SPEED_10;
2717 if (tg3_5700_link_polarity(tp, speed))
2718 mac_mode |= MAC_MODE_LINK_POLARITY;
2719 else
2720 mac_mode &= ~MAC_MODE_LINK_POLARITY;
2721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 } else {
2723 mac_mode = MAC_MODE_PORT_MODE_TBI;
2724 }
2725
John W. Linvillecbf46852005-04-21 17:01:29 -07002726 if (!(tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 tw32(MAC_LED_CTRL, tp->led_ctrl);
2728
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002729 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
2730 if (((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
2731 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) &&
2732 ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
2733 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)))
2734 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
Matt Carlsond2394e6b2010-11-24 08:31:47 +00002736 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
2737 mac_mode |= MAC_MODE_APE_TX_EN |
2738 MAC_MODE_APE_RX_EN |
2739 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07002740
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 tw32_f(MAC_MODE, mac_mode);
2742 udelay(100);
2743
2744 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2745 udelay(10);
2746 }
2747
2748 if (!(tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB) &&
2749 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2750 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2751 u32 base_val;
2752
2753 base_val = tp->pci_clock_ctrl;
2754 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2755 CLOCK_CTRL_TXCLK_DISABLE);
2756
Michael Chanb401e9e2005-12-19 16:27:04 -08002757 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
2758 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Michael Chand7b0a852007-02-13 12:17:38 -08002759 } else if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
Matt Carlson795d01c2007-10-07 23:28:17 -07002760 (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
Michael Chand7b0a852007-02-13 12:17:38 -08002761 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)) {
Michael Chan4cf78e42005-07-25 12:29:19 -07002762 /* do nothing */
Michael Chan85e94ce2005-04-21 17:05:28 -07002763 } else if (!((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF))) {
2765 u32 newbits1, newbits2;
2766
2767 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2768 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2769 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
2770 CLOCK_CTRL_TXCLK_DISABLE |
2771 CLOCK_CTRL_ALTCLK);
2772 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2773 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
2774 newbits1 = CLOCK_CTRL_625_CORE;
2775 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
2776 } else {
2777 newbits1 = CLOCK_CTRL_ALTCLK;
2778 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2779 }
2780
Michael Chanb401e9e2005-12-19 16:27:04 -08002781 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
2782 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
Michael Chanb401e9e2005-12-19 16:27:04 -08002784 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
2785 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
2787 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
2788 u32 newbits3;
2789
2790 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2791 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2792 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
2793 CLOCK_CTRL_TXCLK_DISABLE |
2794 CLOCK_CTRL_44MHZ_CORE);
2795 } else {
2796 newbits3 = CLOCK_CTRL_44MHZ_CORE;
2797 }
2798
Michael Chanb401e9e2005-12-19 16:27:04 -08002799 tw32_wait_f(TG3PCI_CLOCK_CTRL,
2800 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 }
2802 }
2803
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002804 if (!(device_should_wake) &&
Matt Carlson22435842008-11-21 17:21:13 -08002805 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08002806 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08002807
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 tg3_frob_aux_power(tp);
2809
2810 /* Workaround for unstable PLL clock */
2811 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
2812 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
2813 u32 val = tr32(0x7d00);
2814
2815 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
2816 tw32(0x7d00, val);
Michael Chan6921d202005-12-13 21:15:53 -08002817 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08002818 int err;
2819
2820 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08002822 if (!err)
2823 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08002824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 }
2826
Michael Chanbbadf502006-04-06 21:46:34 -07002827 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
2828
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 return 0;
2830}
2831
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002832static void tg3_power_down(struct tg3 *tp)
2833{
2834 tg3_power_down_prepare(tp);
2835
2836 pci_wake_from_d3(tp->pdev, tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
2837 pci_set_power_state(tp->pdev, PCI_D3hot);
2838}
2839
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
2841{
2842 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
2843 case MII_TG3_AUX_STAT_10HALF:
2844 *speed = SPEED_10;
2845 *duplex = DUPLEX_HALF;
2846 break;
2847
2848 case MII_TG3_AUX_STAT_10FULL:
2849 *speed = SPEED_10;
2850 *duplex = DUPLEX_FULL;
2851 break;
2852
2853 case MII_TG3_AUX_STAT_100HALF:
2854 *speed = SPEED_100;
2855 *duplex = DUPLEX_HALF;
2856 break;
2857
2858 case MII_TG3_AUX_STAT_100FULL:
2859 *speed = SPEED_100;
2860 *duplex = DUPLEX_FULL;
2861 break;
2862
2863 case MII_TG3_AUX_STAT_1000HALF:
2864 *speed = SPEED_1000;
2865 *duplex = DUPLEX_HALF;
2866 break;
2867
2868 case MII_TG3_AUX_STAT_1000FULL:
2869 *speed = SPEED_1000;
2870 *duplex = DUPLEX_FULL;
2871 break;
2872
2873 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002874 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07002875 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
2876 SPEED_10;
2877 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
2878 DUPLEX_HALF;
2879 break;
2880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 *speed = SPEED_INVALID;
2882 *duplex = DUPLEX_INVALID;
2883 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885}
2886
2887static void tg3_phy_copper_begin(struct tg3 *tp)
2888{
2889 u32 new_adv;
2890 int i;
2891
Matt Carlson80096062010-08-02 11:26:06 +00002892 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 /* Entering low power mode. Disable gigabit and
2894 * 100baseT advertisements.
2895 */
2896 tg3_writephy(tp, MII_TG3_CTRL, 0);
2897
2898 new_adv = (ADVERTISE_10HALF | ADVERTISE_10FULL |
2899 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
2900 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2901 new_adv |= (ADVERTISE_100HALF | ADVERTISE_100FULL);
2902
2903 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2904 } else if (tp->link_config.speed == SPEED_INVALID) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002905 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 tp->link_config.advertising &=
2907 ~(ADVERTISED_1000baseT_Half |
2908 ADVERTISED_1000baseT_Full);
2909
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002910 new_adv = ADVERTISE_CSMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 if (tp->link_config.advertising & ADVERTISED_10baseT_Half)
2912 new_adv |= ADVERTISE_10HALF;
2913 if (tp->link_config.advertising & ADVERTISED_10baseT_Full)
2914 new_adv |= ADVERTISE_10FULL;
2915 if (tp->link_config.advertising & ADVERTISED_100baseT_Half)
2916 new_adv |= ADVERTISE_100HALF;
2917 if (tp->link_config.advertising & ADVERTISED_100baseT_Full)
2918 new_adv |= ADVERTISE_100FULL;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002919
2920 new_adv |= tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2921
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2923
2924 if (tp->link_config.advertising &
2925 (ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full)) {
2926 new_adv = 0;
2927 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
2928 new_adv |= MII_TG3_CTRL_ADV_1000_HALF;
2929 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
2930 new_adv |= MII_TG3_CTRL_ADV_1000_FULL;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002931 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2933 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0))
2934 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2935 MII_TG3_CTRL_ENABLE_AS_MASTER);
2936 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
2937 } else {
2938 tg3_writephy(tp, MII_TG3_CTRL, 0);
2939 }
2940 } else {
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002941 new_adv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2942 new_adv |= ADVERTISE_CSMA;
2943
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 /* Asking for a specific link mode. */
2945 if (tp->link_config.speed == SPEED_1000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2947
2948 if (tp->link_config.duplex == DUPLEX_FULL)
2949 new_adv = MII_TG3_CTRL_ADV_1000_FULL;
2950 else
2951 new_adv = MII_TG3_CTRL_ADV_1000_HALF;
2952 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2953 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
2954 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2955 MII_TG3_CTRL_ENABLE_AS_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 if (tp->link_config.speed == SPEED_100) {
2958 if (tp->link_config.duplex == DUPLEX_FULL)
2959 new_adv |= ADVERTISE_100FULL;
2960 else
2961 new_adv |= ADVERTISE_100HALF;
2962 } else {
2963 if (tp->link_config.duplex == DUPLEX_FULL)
2964 new_adv |= ADVERTISE_10FULL;
2965 else
2966 new_adv |= ADVERTISE_10HALF;
2967 }
2968 tg3_writephy(tp, MII_ADVERTISE, new_adv);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002969
2970 new_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002972
2973 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 }
2975
Matt Carlson52b02d02010-10-14 10:37:41 +00002976 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
Matt Carlsona6b68da2010-12-06 08:28:52 +00002977 u32 val;
Matt Carlson52b02d02010-10-14 10:37:41 +00002978
2979 tw32(TG3_CPMU_EEE_MODE,
2980 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2981
2982 /* Enable SM_DSP clock and tx 6dB coding. */
2983 val = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
2984 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
2985 MII_TG3_AUXCTL_ACTL_TX_6DB;
2986 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
2987
Matt Carlson21a00ab2011-01-25 15:58:55 +00002988 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
2989 case ASIC_REV_5717:
2990 case ASIC_REV_57765:
2991 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
2992 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
2993 MII_TG3_DSP_CH34TP2_HIBW01);
2994 /* Fall through */
2995 case ASIC_REV_5719:
2996 val = MII_TG3_DSP_TAP26_ALNOKO |
2997 MII_TG3_DSP_TAP26_RMRXSTO |
2998 MII_TG3_DSP_TAP26_OPCSINPT;
2999 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
3000 }
Matt Carlson52b02d02010-10-14 10:37:41 +00003001
Matt Carlsona6b68da2010-12-06 08:28:52 +00003002 val = 0;
Matt Carlson52b02d02010-10-14 10:37:41 +00003003 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3004 /* Advertise 100-BaseTX EEE ability */
3005 if (tp->link_config.advertising &
Matt Carlson3110f5f52010-12-06 08:28:50 +00003006 ADVERTISED_100baseT_Full)
3007 val |= MDIO_AN_EEE_ADV_100TX;
Matt Carlson52b02d02010-10-14 10:37:41 +00003008 /* Advertise 1000-BaseT EEE ability */
3009 if (tp->link_config.advertising &
Matt Carlson3110f5f52010-12-06 08:28:50 +00003010 ADVERTISED_1000baseT_Full)
3011 val |= MDIO_AN_EEE_ADV_1000T;
Matt Carlson52b02d02010-10-14 10:37:41 +00003012 }
Matt Carlson3110f5f52010-12-06 08:28:50 +00003013 tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlson52b02d02010-10-14 10:37:41 +00003014
3015 /* Turn off SM_DSP clock. */
3016 val = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
3017 MII_TG3_AUXCTL_ACTL_TX_6DB;
3018 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
3019 }
3020
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
3022 tp->link_config.speed != SPEED_INVALID) {
3023 u32 bmcr, orig_bmcr;
3024
3025 tp->link_config.active_speed = tp->link_config.speed;
3026 tp->link_config.active_duplex = tp->link_config.duplex;
3027
3028 bmcr = 0;
3029 switch (tp->link_config.speed) {
3030 default:
3031 case SPEED_10:
3032 break;
3033
3034 case SPEED_100:
3035 bmcr |= BMCR_SPEED100;
3036 break;
3037
3038 case SPEED_1000:
3039 bmcr |= TG3_BMCR_SPEED1000;
3040 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042
3043 if (tp->link_config.duplex == DUPLEX_FULL)
3044 bmcr |= BMCR_FULLDPLX;
3045
3046 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3047 (bmcr != orig_bmcr)) {
3048 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3049 for (i = 0; i < 1500; i++) {
3050 u32 tmp;
3051
3052 udelay(10);
3053 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3054 tg3_readphy(tp, MII_BMSR, &tmp))
3055 continue;
3056 if (!(tmp & BMSR_LSTATUS)) {
3057 udelay(40);
3058 break;
3059 }
3060 }
3061 tg3_writephy(tp, MII_BMCR, bmcr);
3062 udelay(40);
3063 }
3064 } else {
3065 tg3_writephy(tp, MII_BMCR,
3066 BMCR_ANENABLE | BMCR_ANRESTART);
3067 }
3068}
3069
3070static int tg3_init_5401phy_dsp(struct tg3 *tp)
3071{
3072 int err;
3073
3074 /* Turn off tap power management. */
3075 /* Set Extended packet length bit */
3076 err = tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
3077
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003078 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3079 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3080 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3081 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3082 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083
3084 udelay(40);
3085
3086 return err;
3087}
3088
Michael Chan3600d912006-12-07 00:21:48 -08003089static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090{
Michael Chan3600d912006-12-07 00:21:48 -08003091 u32 adv_reg, all_mask = 0;
3092
3093 if (mask & ADVERTISED_10baseT_Half)
3094 all_mask |= ADVERTISE_10HALF;
3095 if (mask & ADVERTISED_10baseT_Full)
3096 all_mask |= ADVERTISE_10FULL;
3097 if (mask & ADVERTISED_100baseT_Half)
3098 all_mask |= ADVERTISE_100HALF;
3099 if (mask & ADVERTISED_100baseT_Full)
3100 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
3102 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
3103 return 0;
3104
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 if ((adv_reg & all_mask) != all_mask)
3106 return 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003107 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 u32 tg3_ctrl;
3109
Michael Chan3600d912006-12-07 00:21:48 -08003110 all_mask = 0;
3111 if (mask & ADVERTISED_1000baseT_Half)
3112 all_mask |= ADVERTISE_1000HALF;
3113 if (mask & ADVERTISED_1000baseT_Full)
3114 all_mask |= ADVERTISE_1000FULL;
3115
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 if (tg3_readphy(tp, MII_TG3_CTRL, &tg3_ctrl))
3117 return 0;
3118
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 if ((tg3_ctrl & all_mask) != all_mask)
3120 return 0;
3121 }
3122 return 1;
3123}
3124
Matt Carlsonef167e22007-12-20 20:10:01 -08003125static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
3126{
3127 u32 curadv, reqadv;
3128
3129 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3130 return 1;
3131
3132 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3133 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3134
3135 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3136 if (curadv != reqadv)
3137 return 0;
3138
3139 if (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)
3140 tg3_readphy(tp, MII_LPA, rmtadv);
3141 } else {
3142 /* Reprogram the advertisement register, even if it
3143 * does not affect the current link. If the link
3144 * gets renegotiated in the future, we can save an
3145 * additional renegotiation cycle by advertising
3146 * it correctly in the first place.
3147 */
3148 if (curadv != reqadv) {
3149 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3150 ADVERTISE_PAUSE_ASYM);
3151 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3152 }
3153 }
3154
3155 return 1;
3156}
3157
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3159{
3160 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003161 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08003162 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 u16 current_speed;
3164 u8 current_duplex;
3165 int i, err;
3166
3167 tw32(MAC_EVENT, 0);
3168
3169 tw32_f(MAC_STATUS,
3170 (MAC_STATUS_SYNC_CHANGED |
3171 MAC_STATUS_CFG_CHANGED |
3172 MAC_STATUS_MI_COMPLETION |
3173 MAC_STATUS_LNKSTATE_CHANGED));
3174 udelay(40);
3175
Matt Carlson8ef21422008-05-02 16:47:53 -07003176 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3177 tw32_f(MAC_MI_MODE,
3178 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3179 udelay(80);
3180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181
3182 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x02);
3183
3184 /* Some third-party PHYs need to be reset on link going
3185 * down.
3186 */
3187 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3188 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3189 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3190 netif_carrier_ok(tp->dev)) {
3191 tg3_readphy(tp, MII_BMSR, &bmsr);
3192 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3193 !(bmsr & BMSR_LSTATUS))
3194 force_reset = 1;
3195 }
3196 if (force_reset)
3197 tg3_phy_reset(tp);
3198
Matt Carlson79eb6902010-02-17 15:17:03 +00003199 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 tg3_readphy(tp, MII_BMSR, &bmsr);
3201 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
3202 !(tp->tg3_flags & TG3_FLAG_INIT_COMPLETE))
3203 bmsr = 0;
3204
3205 if (!(bmsr & BMSR_LSTATUS)) {
3206 err = tg3_init_5401phy_dsp(tp);
3207 if (err)
3208 return err;
3209
3210 tg3_readphy(tp, MII_BMSR, &bmsr);
3211 for (i = 0; i < 1000; i++) {
3212 udelay(10);
3213 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3214 (bmsr & BMSR_LSTATUS)) {
3215 udelay(40);
3216 break;
3217 }
3218 }
3219
Matt Carlson79eb6902010-02-17 15:17:03 +00003220 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3221 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 !(bmsr & BMSR_LSTATUS) &&
3223 tp->link_config.active_speed == SPEED_1000) {
3224 err = tg3_phy_reset(tp);
3225 if (!err)
3226 err = tg3_init_5401phy_dsp(tp);
3227 if (err)
3228 return err;
3229 }
3230 }
3231 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3232 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3233 /* 5701 {A0,B0} CRC bug workaround */
3234 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00003235 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3236 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
3237 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 }
3239
3240 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003241 tg3_readphy(tp, MII_TG3_ISTAT, &val);
3242 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003244 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003246 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3248
3249 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3250 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3251 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3252 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3253 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3254 else
3255 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3256 }
3257
3258 current_link_up = 0;
3259 current_speed = SPEED_INVALID;
3260 current_duplex = DUPLEX_INVALID;
3261
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003262 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4007);
3264 tg3_readphy(tp, MII_TG3_AUX_CTRL, &val);
3265 if (!(val & (1 << 10))) {
3266 val |= (1 << 10);
3267 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
3268 goto relink;
3269 }
3270 }
3271
3272 bmsr = 0;
3273 for (i = 0; i < 100; i++) {
3274 tg3_readphy(tp, MII_BMSR, &bmsr);
3275 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3276 (bmsr & BMSR_LSTATUS))
3277 break;
3278 udelay(40);
3279 }
3280
3281 if (bmsr & BMSR_LSTATUS) {
3282 u32 aux_stat, bmcr;
3283
3284 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3285 for (i = 0; i < 2000; i++) {
3286 udelay(10);
3287 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3288 aux_stat)
3289 break;
3290 }
3291
3292 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3293 &current_speed,
3294 &current_duplex);
3295
3296 bmcr = 0;
3297 for (i = 0; i < 200; i++) {
3298 tg3_readphy(tp, MII_BMCR, &bmcr);
3299 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3300 continue;
3301 if (bmcr && bmcr != 0x7fff)
3302 break;
3303 udelay(10);
3304 }
3305
Matt Carlsonef167e22007-12-20 20:10:01 -08003306 lcl_adv = 0;
3307 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308
Matt Carlsonef167e22007-12-20 20:10:01 -08003309 tp->link_config.active_speed = current_speed;
3310 tp->link_config.active_duplex = current_duplex;
3311
3312 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3313 if ((bmcr & BMCR_ANENABLE) &&
3314 tg3_copper_is_advertising_all(tp,
3315 tp->link_config.advertising)) {
3316 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3317 &rmt_adv))
3318 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 }
3320 } else {
3321 if (!(bmcr & BMCR_ANENABLE) &&
3322 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003323 tp->link_config.duplex == current_duplex &&
3324 tp->link_config.flowctrl ==
3325 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 }
3328 }
3329
Matt Carlsonef167e22007-12-20 20:10:01 -08003330 if (current_link_up == 1 &&
3331 tp->link_config.active_duplex == DUPLEX_FULL)
3332 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 }
3334
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335relink:
Matt Carlson80096062010-08-02 11:26:06 +00003336 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 tg3_phy_copper_begin(tp);
3338
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003339 tg3_readphy(tp, MII_BMSR, &bmsr);
3340 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3341 (bmsr & BMSR_LSTATUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 current_link_up = 1;
3343 }
3344
3345 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3346 if (current_link_up == 1) {
3347 if (tp->link_config.active_speed == SPEED_100 ||
3348 tp->link_config.active_speed == SPEED_10)
3349 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3350 else
3351 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003352 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003353 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3354 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3356
3357 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3358 if (tp->link_config.active_duplex == DUPLEX_HALF)
3359 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3360
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003362 if (current_link_up == 1 &&
3363 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003365 else
3366 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 }
3368
3369 /* ??? Without this setting Netgear GA302T PHY does not
3370 * ??? send/receive packets...
3371 */
Matt Carlson79eb6902010-02-17 15:17:03 +00003372 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3374 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3375 tw32_f(MAC_MI_MODE, tp->mi_mode);
3376 udelay(80);
3377 }
3378
3379 tw32_f(MAC_MODE, tp->mac_mode);
3380 udelay(40);
3381
Matt Carlson52b02d02010-10-14 10:37:41 +00003382 tg3_phy_eee_adjust(tp, current_link_up);
3383
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
3385 /* Polled via timer. */
3386 tw32_f(MAC_EVENT, 0);
3387 } else {
3388 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3389 }
3390 udelay(40);
3391
3392 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3393 current_link_up == 1 &&
3394 tp->link_config.active_speed == SPEED_1000 &&
3395 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) ||
3396 (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED))) {
3397 udelay(120);
3398 tw32_f(MAC_STATUS,
3399 (MAC_STATUS_SYNC_CHANGED |
3400 MAC_STATUS_CFG_CHANGED));
3401 udelay(40);
3402 tg3_write_mem(tp,
3403 NIC_SRAM_FIRMWARE_MBOX,
3404 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3405 }
3406
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003407 /* Prevent send BD corruption. */
3408 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
3409 u16 oldlnkctl, newlnkctl;
3410
3411 pci_read_config_word(tp->pdev,
3412 tp->pcie_cap + PCI_EXP_LNKCTL,
3413 &oldlnkctl);
3414 if (tp->link_config.active_speed == SPEED_100 ||
3415 tp->link_config.active_speed == SPEED_10)
3416 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3417 else
3418 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3419 if (newlnkctl != oldlnkctl)
3420 pci_write_config_word(tp->pdev,
3421 tp->pcie_cap + PCI_EXP_LNKCTL,
3422 newlnkctl);
3423 }
3424
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 if (current_link_up != netif_carrier_ok(tp->dev)) {
3426 if (current_link_up)
3427 netif_carrier_on(tp->dev);
3428 else
3429 netif_carrier_off(tp->dev);
3430 tg3_link_report(tp);
3431 }
3432
3433 return 0;
3434}
3435
3436struct tg3_fiber_aneginfo {
3437 int state;
3438#define ANEG_STATE_UNKNOWN 0
3439#define ANEG_STATE_AN_ENABLE 1
3440#define ANEG_STATE_RESTART_INIT 2
3441#define ANEG_STATE_RESTART 3
3442#define ANEG_STATE_DISABLE_LINK_OK 4
3443#define ANEG_STATE_ABILITY_DETECT_INIT 5
3444#define ANEG_STATE_ABILITY_DETECT 6
3445#define ANEG_STATE_ACK_DETECT_INIT 7
3446#define ANEG_STATE_ACK_DETECT 8
3447#define ANEG_STATE_COMPLETE_ACK_INIT 9
3448#define ANEG_STATE_COMPLETE_ACK 10
3449#define ANEG_STATE_IDLE_DETECT_INIT 11
3450#define ANEG_STATE_IDLE_DETECT 12
3451#define ANEG_STATE_LINK_OK 13
3452#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3453#define ANEG_STATE_NEXT_PAGE_WAIT 15
3454
3455 u32 flags;
3456#define MR_AN_ENABLE 0x00000001
3457#define MR_RESTART_AN 0x00000002
3458#define MR_AN_COMPLETE 0x00000004
3459#define MR_PAGE_RX 0x00000008
3460#define MR_NP_LOADED 0x00000010
3461#define MR_TOGGLE_TX 0x00000020
3462#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3463#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3464#define MR_LP_ADV_SYM_PAUSE 0x00000100
3465#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3466#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3467#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3468#define MR_LP_ADV_NEXT_PAGE 0x00001000
3469#define MR_TOGGLE_RX 0x00002000
3470#define MR_NP_RX 0x00004000
3471
3472#define MR_LINK_OK 0x80000000
3473
3474 unsigned long link_time, cur_time;
3475
3476 u32 ability_match_cfg;
3477 int ability_match_count;
3478
3479 char ability_match, idle_match, ack_match;
3480
3481 u32 txconfig, rxconfig;
3482#define ANEG_CFG_NP 0x00000080
3483#define ANEG_CFG_ACK 0x00000040
3484#define ANEG_CFG_RF2 0x00000020
3485#define ANEG_CFG_RF1 0x00000010
3486#define ANEG_CFG_PS2 0x00000001
3487#define ANEG_CFG_PS1 0x00008000
3488#define ANEG_CFG_HD 0x00004000
3489#define ANEG_CFG_FD 0x00002000
3490#define ANEG_CFG_INVAL 0x00001f06
3491
3492};
3493#define ANEG_OK 0
3494#define ANEG_DONE 1
3495#define ANEG_TIMER_ENAB 2
3496#define ANEG_FAILED -1
3497
3498#define ANEG_STATE_SETTLE_TIME 10000
3499
3500static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3501 struct tg3_fiber_aneginfo *ap)
3502{
Matt Carlson5be73b42007-12-20 20:09:29 -08003503 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 unsigned long delta;
3505 u32 rx_cfg_reg;
3506 int ret;
3507
3508 if (ap->state == ANEG_STATE_UNKNOWN) {
3509 ap->rxconfig = 0;
3510 ap->link_time = 0;
3511 ap->cur_time = 0;
3512 ap->ability_match_cfg = 0;
3513 ap->ability_match_count = 0;
3514 ap->ability_match = 0;
3515 ap->idle_match = 0;
3516 ap->ack_match = 0;
3517 }
3518 ap->cur_time++;
3519
3520 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3521 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3522
3523 if (rx_cfg_reg != ap->ability_match_cfg) {
3524 ap->ability_match_cfg = rx_cfg_reg;
3525 ap->ability_match = 0;
3526 ap->ability_match_count = 0;
3527 } else {
3528 if (++ap->ability_match_count > 1) {
3529 ap->ability_match = 1;
3530 ap->ability_match_cfg = rx_cfg_reg;
3531 }
3532 }
3533 if (rx_cfg_reg & ANEG_CFG_ACK)
3534 ap->ack_match = 1;
3535 else
3536 ap->ack_match = 0;
3537
3538 ap->idle_match = 0;
3539 } else {
3540 ap->idle_match = 1;
3541 ap->ability_match_cfg = 0;
3542 ap->ability_match_count = 0;
3543 ap->ability_match = 0;
3544 ap->ack_match = 0;
3545
3546 rx_cfg_reg = 0;
3547 }
3548
3549 ap->rxconfig = rx_cfg_reg;
3550 ret = ANEG_OK;
3551
Matt Carlson33f401a2010-04-05 10:19:27 +00003552 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 case ANEG_STATE_UNKNOWN:
3554 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3555 ap->state = ANEG_STATE_AN_ENABLE;
3556
3557 /* fallthru */
3558 case ANEG_STATE_AN_ENABLE:
3559 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3560 if (ap->flags & MR_AN_ENABLE) {
3561 ap->link_time = 0;
3562 ap->cur_time = 0;
3563 ap->ability_match_cfg = 0;
3564 ap->ability_match_count = 0;
3565 ap->ability_match = 0;
3566 ap->idle_match = 0;
3567 ap->ack_match = 0;
3568
3569 ap->state = ANEG_STATE_RESTART_INIT;
3570 } else {
3571 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3572 }
3573 break;
3574
3575 case ANEG_STATE_RESTART_INIT:
3576 ap->link_time = ap->cur_time;
3577 ap->flags &= ~(MR_NP_LOADED);
3578 ap->txconfig = 0;
3579 tw32(MAC_TX_AUTO_NEG, 0);
3580 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3581 tw32_f(MAC_MODE, tp->mac_mode);
3582 udelay(40);
3583
3584 ret = ANEG_TIMER_ENAB;
3585 ap->state = ANEG_STATE_RESTART;
3586
3587 /* fallthru */
3588 case ANEG_STATE_RESTART:
3589 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00003590 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00003592 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 break;
3595
3596 case ANEG_STATE_DISABLE_LINK_OK:
3597 ret = ANEG_DONE;
3598 break;
3599
3600 case ANEG_STATE_ABILITY_DETECT_INIT:
3601 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003602 ap->txconfig = ANEG_CFG_FD;
3603 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3604 if (flowctrl & ADVERTISE_1000XPAUSE)
3605 ap->txconfig |= ANEG_CFG_PS1;
3606 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3607 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3609 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3610 tw32_f(MAC_MODE, tp->mac_mode);
3611 udelay(40);
3612
3613 ap->state = ANEG_STATE_ABILITY_DETECT;
3614 break;
3615
3616 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00003617 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 break;
3620
3621 case ANEG_STATE_ACK_DETECT_INIT:
3622 ap->txconfig |= ANEG_CFG_ACK;
3623 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3624 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3625 tw32_f(MAC_MODE, tp->mac_mode);
3626 udelay(40);
3627
3628 ap->state = ANEG_STATE_ACK_DETECT;
3629
3630 /* fallthru */
3631 case ANEG_STATE_ACK_DETECT:
3632 if (ap->ack_match != 0) {
3633 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3634 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3635 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3636 } else {
3637 ap->state = ANEG_STATE_AN_ENABLE;
3638 }
3639 } else if (ap->ability_match != 0 &&
3640 ap->rxconfig == 0) {
3641 ap->state = ANEG_STATE_AN_ENABLE;
3642 }
3643 break;
3644
3645 case ANEG_STATE_COMPLETE_ACK_INIT:
3646 if (ap->rxconfig & ANEG_CFG_INVAL) {
3647 ret = ANEG_FAILED;
3648 break;
3649 }
3650 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3651 MR_LP_ADV_HALF_DUPLEX |
3652 MR_LP_ADV_SYM_PAUSE |
3653 MR_LP_ADV_ASYM_PAUSE |
3654 MR_LP_ADV_REMOTE_FAULT1 |
3655 MR_LP_ADV_REMOTE_FAULT2 |
3656 MR_LP_ADV_NEXT_PAGE |
3657 MR_TOGGLE_RX |
3658 MR_NP_RX);
3659 if (ap->rxconfig & ANEG_CFG_FD)
3660 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3661 if (ap->rxconfig & ANEG_CFG_HD)
3662 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3663 if (ap->rxconfig & ANEG_CFG_PS1)
3664 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3665 if (ap->rxconfig & ANEG_CFG_PS2)
3666 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3667 if (ap->rxconfig & ANEG_CFG_RF1)
3668 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3669 if (ap->rxconfig & ANEG_CFG_RF2)
3670 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3671 if (ap->rxconfig & ANEG_CFG_NP)
3672 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3673
3674 ap->link_time = ap->cur_time;
3675
3676 ap->flags ^= (MR_TOGGLE_TX);
3677 if (ap->rxconfig & 0x0008)
3678 ap->flags |= MR_TOGGLE_RX;
3679 if (ap->rxconfig & ANEG_CFG_NP)
3680 ap->flags |= MR_NP_RX;
3681 ap->flags |= MR_PAGE_RX;
3682
3683 ap->state = ANEG_STATE_COMPLETE_ACK;
3684 ret = ANEG_TIMER_ENAB;
3685 break;
3686
3687 case ANEG_STATE_COMPLETE_ACK:
3688 if (ap->ability_match != 0 &&
3689 ap->rxconfig == 0) {
3690 ap->state = ANEG_STATE_AN_ENABLE;
3691 break;
3692 }
3693 delta = ap->cur_time - ap->link_time;
3694 if (delta > ANEG_STATE_SETTLE_TIME) {
3695 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3696 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3697 } else {
3698 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3699 !(ap->flags & MR_NP_RX)) {
3700 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3701 } else {
3702 ret = ANEG_FAILED;
3703 }
3704 }
3705 }
3706 break;
3707
3708 case ANEG_STATE_IDLE_DETECT_INIT:
3709 ap->link_time = ap->cur_time;
3710 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3711 tw32_f(MAC_MODE, tp->mac_mode);
3712 udelay(40);
3713
3714 ap->state = ANEG_STATE_IDLE_DETECT;
3715 ret = ANEG_TIMER_ENAB;
3716 break;
3717
3718 case ANEG_STATE_IDLE_DETECT:
3719 if (ap->ability_match != 0 &&
3720 ap->rxconfig == 0) {
3721 ap->state = ANEG_STATE_AN_ENABLE;
3722 break;
3723 }
3724 delta = ap->cur_time - ap->link_time;
3725 if (delta > ANEG_STATE_SETTLE_TIME) {
3726 /* XXX another gem from the Broadcom driver :( */
3727 ap->state = ANEG_STATE_LINK_OK;
3728 }
3729 break;
3730
3731 case ANEG_STATE_LINK_OK:
3732 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3733 ret = ANEG_DONE;
3734 break;
3735
3736 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3737 /* ??? unimplemented */
3738 break;
3739
3740 case ANEG_STATE_NEXT_PAGE_WAIT:
3741 /* ??? unimplemented */
3742 break;
3743
3744 default:
3745 ret = ANEG_FAILED;
3746 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748
3749 return ret;
3750}
3751
Matt Carlson5be73b42007-12-20 20:09:29 -08003752static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753{
3754 int res = 0;
3755 struct tg3_fiber_aneginfo aninfo;
3756 int status = ANEG_FAILED;
3757 unsigned int tick;
3758 u32 tmp;
3759
3760 tw32_f(MAC_TX_AUTO_NEG, 0);
3761
3762 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3763 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3764 udelay(40);
3765
3766 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3767 udelay(40);
3768
3769 memset(&aninfo, 0, sizeof(aninfo));
3770 aninfo.flags |= MR_AN_ENABLE;
3771 aninfo.state = ANEG_STATE_UNKNOWN;
3772 aninfo.cur_time = 0;
3773 tick = 0;
3774 while (++tick < 195000) {
3775 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3776 if (status == ANEG_DONE || status == ANEG_FAILED)
3777 break;
3778
3779 udelay(1);
3780 }
3781
3782 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3783 tw32_f(MAC_MODE, tp->mac_mode);
3784 udelay(40);
3785
Matt Carlson5be73b42007-12-20 20:09:29 -08003786 *txflags = aninfo.txconfig;
3787 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788
3789 if (status == ANEG_DONE &&
3790 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3791 MR_LP_ADV_FULL_DUPLEX)))
3792 res = 1;
3793
3794 return res;
3795}
3796
3797static void tg3_init_bcm8002(struct tg3 *tp)
3798{
3799 u32 mac_status = tr32(MAC_STATUS);
3800 int i;
3801
3802 /* Reset when initting first time or we have a link. */
3803 if ((tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) &&
3804 !(mac_status & MAC_STATUS_PCS_SYNCED))
3805 return;
3806
3807 /* Set PLL lock range. */
3808 tg3_writephy(tp, 0x16, 0x8007);
3809
3810 /* SW reset */
3811 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
3812
3813 /* Wait for reset to complete. */
3814 /* XXX schedule_timeout() ... */
3815 for (i = 0; i < 500; i++)
3816 udelay(10);
3817
3818 /* Config mode; select PMA/Ch 1 regs. */
3819 tg3_writephy(tp, 0x10, 0x8411);
3820
3821 /* Enable auto-lock and comdet, select txclk for tx. */
3822 tg3_writephy(tp, 0x11, 0x0a10);
3823
3824 tg3_writephy(tp, 0x18, 0x00a0);
3825 tg3_writephy(tp, 0x16, 0x41ff);
3826
3827 /* Assert and deassert POR. */
3828 tg3_writephy(tp, 0x13, 0x0400);
3829 udelay(40);
3830 tg3_writephy(tp, 0x13, 0x0000);
3831
3832 tg3_writephy(tp, 0x11, 0x0a50);
3833 udelay(40);
3834 tg3_writephy(tp, 0x11, 0x0a10);
3835
3836 /* Wait for signal to stabilize */
3837 /* XXX schedule_timeout() ... */
3838 for (i = 0; i < 15000; i++)
3839 udelay(10);
3840
3841 /* Deselect the channel register so we can read the PHYID
3842 * later.
3843 */
3844 tg3_writephy(tp, 0x10, 0x8011);
3845}
3846
3847static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
3848{
Matt Carlson82cd3d12007-12-20 20:09:00 -08003849 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 u32 sg_dig_ctrl, sg_dig_status;
3851 u32 serdes_cfg, expected_sg_dig_ctrl;
3852 int workaround, port_a;
3853 int current_link_up;
3854
3855 serdes_cfg = 0;
3856 expected_sg_dig_ctrl = 0;
3857 workaround = 0;
3858 port_a = 1;
3859 current_link_up = 0;
3860
3861 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
3862 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
3863 workaround = 1;
3864 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
3865 port_a = 0;
3866
3867 /* preserve bits 0-11,13,14 for signal pre-emphasis */
3868 /* preserve bits 20-23 for voltage regulator */
3869 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
3870 }
3871
3872 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3873
3874 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003875 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 if (workaround) {
3877 u32 val = serdes_cfg;
3878
3879 if (port_a)
3880 val |= 0xc010000;
3881 else
3882 val |= 0x4010000;
3883 tw32_f(MAC_SERDES_CFG, val);
3884 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003885
3886 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 }
3888 if (mac_status & MAC_STATUS_PCS_SYNCED) {
3889 tg3_setup_flow_control(tp, 0, 0);
3890 current_link_up = 1;
3891 }
3892 goto out;
3893 }
3894
3895 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003896 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897
Matt Carlson82cd3d12007-12-20 20:09:00 -08003898 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3899 if (flowctrl & ADVERTISE_1000XPAUSE)
3900 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
3901 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3902 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903
3904 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003905 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07003906 tp->serdes_counter &&
3907 ((mac_status & (MAC_STATUS_PCS_SYNCED |
3908 MAC_STATUS_RCVD_CFG)) ==
3909 MAC_STATUS_PCS_SYNCED)) {
3910 tp->serdes_counter--;
3911 current_link_up = 1;
3912 goto out;
3913 }
3914restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 if (workaround)
3916 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003917 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 udelay(5);
3919 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
3920
Michael Chan3d3ebe72006-09-27 15:59:15 -07003921 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003922 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
3924 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003925 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 mac_status = tr32(MAC_STATUS);
3927
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003928 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08003930 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931
Matt Carlson82cd3d12007-12-20 20:09:00 -08003932 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
3933 local_adv |= ADVERTISE_1000XPAUSE;
3934 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
3935 local_adv |= ADVERTISE_1000XPSE_ASYM;
3936
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003937 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003938 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003939 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003940 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941
3942 tg3_setup_flow_control(tp, local_adv, remote_adv);
3943 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003944 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003945 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003946 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003947 if (tp->serdes_counter)
3948 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 else {
3950 if (workaround) {
3951 u32 val = serdes_cfg;
3952
3953 if (port_a)
3954 val |= 0xc010000;
3955 else
3956 val |= 0x4010000;
3957
3958 tw32_f(MAC_SERDES_CFG, val);
3959 }
3960
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003961 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 udelay(40);
3963
3964 /* Link parallel detection - link is up */
3965 /* only if we have PCS_SYNC and not */
3966 /* receiving config code words */
3967 mac_status = tr32(MAC_STATUS);
3968 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
3969 !(mac_status & MAC_STATUS_RCVD_CFG)) {
3970 tg3_setup_flow_control(tp, 0, 0);
3971 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003972 tp->phy_flags |=
3973 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003974 tp->serdes_counter =
3975 SERDES_PARALLEL_DET_TIMEOUT;
3976 } else
3977 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 }
3979 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07003980 } else {
3981 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003982 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 }
3984
3985out:
3986 return current_link_up;
3987}
3988
3989static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
3990{
3991 int current_link_up = 0;
3992
Michael Chan5cf64b8a2007-05-05 12:11:21 -07003993 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995
3996 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08003997 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003999
Matt Carlson5be73b42007-12-20 20:09:29 -08004000 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4001 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002
Matt Carlson5be73b42007-12-20 20:09:29 -08004003 if (txflags & ANEG_CFG_PS1)
4004 local_adv |= ADVERTISE_1000XPAUSE;
4005 if (txflags & ANEG_CFG_PS2)
4006 local_adv |= ADVERTISE_1000XPSE_ASYM;
4007
4008 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4009 remote_adv |= LPA_1000XPAUSE;
4010 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4011 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012
4013 tg3_setup_flow_control(tp, local_adv, remote_adv);
4014
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 current_link_up = 1;
4016 }
4017 for (i = 0; i < 30; i++) {
4018 udelay(20);
4019 tw32_f(MAC_STATUS,
4020 (MAC_STATUS_SYNC_CHANGED |
4021 MAC_STATUS_CFG_CHANGED));
4022 udelay(40);
4023 if ((tr32(MAC_STATUS) &
4024 (MAC_STATUS_SYNC_CHANGED |
4025 MAC_STATUS_CFG_CHANGED)) == 0)
4026 break;
4027 }
4028
4029 mac_status = tr32(MAC_STATUS);
4030 if (current_link_up == 0 &&
4031 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4032 !(mac_status & MAC_STATUS_RCVD_CFG))
4033 current_link_up = 1;
4034 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004035 tg3_setup_flow_control(tp, 0, 0);
4036
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 /* Forcing 1000FD link up. */
4038 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039
4040 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4041 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004042
4043 tw32_f(MAC_MODE, tp->mac_mode);
4044 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 }
4046
4047out:
4048 return current_link_up;
4049}
4050
4051static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4052{
4053 u32 orig_pause_cfg;
4054 u16 orig_active_speed;
4055 u8 orig_active_duplex;
4056 u32 mac_status;
4057 int current_link_up;
4058 int i;
4059
Matt Carlson8d018622007-12-20 20:05:44 -08004060 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 orig_active_speed = tp->link_config.active_speed;
4062 orig_active_duplex = tp->link_config.active_duplex;
4063
4064 if (!(tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG) &&
4065 netif_carrier_ok(tp->dev) &&
4066 (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)) {
4067 mac_status = tr32(MAC_STATUS);
4068 mac_status &= (MAC_STATUS_PCS_SYNCED |
4069 MAC_STATUS_SIGNAL_DET |
4070 MAC_STATUS_CFG_CHANGED |
4071 MAC_STATUS_RCVD_CFG);
4072 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4073 MAC_STATUS_SIGNAL_DET)) {
4074 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4075 MAC_STATUS_CFG_CHANGED));
4076 return 0;
4077 }
4078 }
4079
4080 tw32_f(MAC_TX_AUTO_NEG, 0);
4081
4082 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4083 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
4084 tw32_f(MAC_MODE, tp->mac_mode);
4085 udelay(40);
4086
Matt Carlson79eb6902010-02-17 15:17:03 +00004087 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 tg3_init_bcm8002(tp);
4089
4090 /* Enable link change event even when serdes polling. */
4091 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4092 udelay(40);
4093
4094 current_link_up = 0;
4095 mac_status = tr32(MAC_STATUS);
4096
4097 if (tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG)
4098 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4099 else
4100 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4101
Matt Carlson898a56f2009-08-28 14:02:40 +00004102 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00004104 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105
4106 for (i = 0; i < 100; i++) {
4107 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4108 MAC_STATUS_CFG_CHANGED));
4109 udelay(5);
4110 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07004111 MAC_STATUS_CFG_CHANGED |
4112 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 break;
4114 }
4115
4116 mac_status = tr32(MAC_STATUS);
4117 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4118 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004119 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4120 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 tw32_f(MAC_MODE, (tp->mac_mode |
4122 MAC_MODE_SEND_CONFIGS));
4123 udelay(1);
4124 tw32_f(MAC_MODE, tp->mac_mode);
4125 }
4126 }
4127
4128 if (current_link_up == 1) {
4129 tp->link_config.active_speed = SPEED_1000;
4130 tp->link_config.active_duplex = DUPLEX_FULL;
4131 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4132 LED_CTRL_LNKLED_OVERRIDE |
4133 LED_CTRL_1000MBPS_ON));
4134 } else {
4135 tp->link_config.active_speed = SPEED_INVALID;
4136 tp->link_config.active_duplex = DUPLEX_INVALID;
4137 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4138 LED_CTRL_LNKLED_OVERRIDE |
4139 LED_CTRL_TRAFFIC_OVERRIDE));
4140 }
4141
4142 if (current_link_up != netif_carrier_ok(tp->dev)) {
4143 if (current_link_up)
4144 netif_carrier_on(tp->dev);
4145 else
4146 netif_carrier_off(tp->dev);
4147 tg3_link_report(tp);
4148 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004149 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 if (orig_pause_cfg != now_pause_cfg ||
4151 orig_active_speed != tp->link_config.active_speed ||
4152 orig_active_duplex != tp->link_config.active_duplex)
4153 tg3_link_report(tp);
4154 }
4155
4156 return 0;
4157}
4158
Michael Chan747e8f82005-07-25 12:33:22 -07004159static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4160{
4161 int current_link_up, err = 0;
4162 u32 bmsr, bmcr;
4163 u16 current_speed;
4164 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004165 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004166
4167 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4168 tw32_f(MAC_MODE, tp->mac_mode);
4169 udelay(40);
4170
4171 tw32(MAC_EVENT, 0);
4172
4173 tw32_f(MAC_STATUS,
4174 (MAC_STATUS_SYNC_CHANGED |
4175 MAC_STATUS_CFG_CHANGED |
4176 MAC_STATUS_MI_COMPLETION |
4177 MAC_STATUS_LNKSTATE_CHANGED));
4178 udelay(40);
4179
4180 if (force_reset)
4181 tg3_phy_reset(tp);
4182
4183 current_link_up = 0;
4184 current_speed = SPEED_INVALID;
4185 current_duplex = DUPLEX_INVALID;
4186
4187 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4188 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004189 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4190 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4191 bmsr |= BMSR_LSTATUS;
4192 else
4193 bmsr &= ~BMSR_LSTATUS;
4194 }
Michael Chan747e8f82005-07-25 12:33:22 -07004195
4196 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4197
4198 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004199 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004200 /* do nothing, just check for link up at the end */
4201 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4202 u32 adv, new_adv;
4203
4204 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4205 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4206 ADVERTISE_1000XPAUSE |
4207 ADVERTISE_1000XPSE_ASYM |
4208 ADVERTISE_SLCT);
4209
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004210 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004211
4212 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4213 new_adv |= ADVERTISE_1000XHALF;
4214 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4215 new_adv |= ADVERTISE_1000XFULL;
4216
4217 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4218 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4219 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4220 tg3_writephy(tp, MII_BMCR, bmcr);
4221
4222 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004223 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004224 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004225
4226 return err;
4227 }
4228 } else {
4229 u32 new_bmcr;
4230
4231 bmcr &= ~BMCR_SPEED1000;
4232 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4233
4234 if (tp->link_config.duplex == DUPLEX_FULL)
4235 new_bmcr |= BMCR_FULLDPLX;
4236
4237 if (new_bmcr != bmcr) {
4238 /* BMCR_SPEED1000 is a reserved bit that needs
4239 * to be set on write.
4240 */
4241 new_bmcr |= BMCR_SPEED1000;
4242
4243 /* Force a linkdown */
4244 if (netif_carrier_ok(tp->dev)) {
4245 u32 adv;
4246
4247 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4248 adv &= ~(ADVERTISE_1000XFULL |
4249 ADVERTISE_1000XHALF |
4250 ADVERTISE_SLCT);
4251 tg3_writephy(tp, MII_ADVERTISE, adv);
4252 tg3_writephy(tp, MII_BMCR, bmcr |
4253 BMCR_ANRESTART |
4254 BMCR_ANENABLE);
4255 udelay(10);
4256 netif_carrier_off(tp->dev);
4257 }
4258 tg3_writephy(tp, MII_BMCR, new_bmcr);
4259 bmcr = new_bmcr;
4260 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4261 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004262 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4263 ASIC_REV_5714) {
4264 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4265 bmsr |= BMSR_LSTATUS;
4266 else
4267 bmsr &= ~BMSR_LSTATUS;
4268 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004269 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004270 }
4271 }
4272
4273 if (bmsr & BMSR_LSTATUS) {
4274 current_speed = SPEED_1000;
4275 current_link_up = 1;
4276 if (bmcr & BMCR_FULLDPLX)
4277 current_duplex = DUPLEX_FULL;
4278 else
4279 current_duplex = DUPLEX_HALF;
4280
Matt Carlsonef167e22007-12-20 20:10:01 -08004281 local_adv = 0;
4282 remote_adv = 0;
4283
Michael Chan747e8f82005-07-25 12:33:22 -07004284 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004285 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004286
4287 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4288 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4289 common = local_adv & remote_adv;
4290 if (common & (ADVERTISE_1000XHALF |
4291 ADVERTISE_1000XFULL)) {
4292 if (common & ADVERTISE_1000XFULL)
4293 current_duplex = DUPLEX_FULL;
4294 else
4295 current_duplex = DUPLEX_HALF;
Matt Carlson57d8b882010-06-05 17:24:35 +00004296 } else if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
4297 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00004298 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07004299 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00004300 }
Michael Chan747e8f82005-07-25 12:33:22 -07004301 }
4302 }
4303
Matt Carlsonef167e22007-12-20 20:10:01 -08004304 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4305 tg3_setup_flow_control(tp, local_adv, remote_adv);
4306
Michael Chan747e8f82005-07-25 12:33:22 -07004307 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4308 if (tp->link_config.active_duplex == DUPLEX_HALF)
4309 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4310
4311 tw32_f(MAC_MODE, tp->mac_mode);
4312 udelay(40);
4313
4314 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4315
4316 tp->link_config.active_speed = current_speed;
4317 tp->link_config.active_duplex = current_duplex;
4318
4319 if (current_link_up != netif_carrier_ok(tp->dev)) {
4320 if (current_link_up)
4321 netif_carrier_on(tp->dev);
4322 else {
4323 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004324 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004325 }
4326 tg3_link_report(tp);
4327 }
4328 return err;
4329}
4330
4331static void tg3_serdes_parallel_detect(struct tg3 *tp)
4332{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004333 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004334 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004335 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004336 return;
4337 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004338
Michael Chan747e8f82005-07-25 12:33:22 -07004339 if (!netif_carrier_ok(tp->dev) &&
4340 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4341 u32 bmcr;
4342
4343 tg3_readphy(tp, MII_BMCR, &bmcr);
4344 if (bmcr & BMCR_ANENABLE) {
4345 u32 phy1, phy2;
4346
4347 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004348 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
4349 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07004350
4351 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004352 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4353 MII_TG3_DSP_EXP1_INT_STAT);
4354 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
4355 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004356
4357 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4358 /* We have signal detect and not receiving
4359 * config code words, link is up by parallel
4360 * detection.
4361 */
4362
4363 bmcr &= ~BMCR_ANENABLE;
4364 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4365 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004366 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004367 }
4368 }
Matt Carlson859a588792010-04-05 10:19:28 +00004369 } else if (netif_carrier_ok(tp->dev) &&
4370 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004371 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004372 u32 phy2;
4373
4374 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004375 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4376 MII_TG3_DSP_EXP1_INT_STAT);
4377 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004378 if (phy2 & 0x20) {
4379 u32 bmcr;
4380
4381 /* Config code words received, turn on autoneg. */
4382 tg3_readphy(tp, MII_BMCR, &bmcr);
4383 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4384
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004385 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004386
4387 }
4388 }
4389}
4390
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4392{
4393 int err;
4394
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004395 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004397 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07004398 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00004399 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401
Matt Carlsonbcb37f62008-11-03 16:52:09 -08004402 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08004403 u32 val, scale;
4404
4405 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4406 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4407 scale = 65;
4408 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4409 scale = 6;
4410 else
4411 scale = 12;
4412
4413 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4414 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4415 tw32(GRC_MISC_CFG, val);
4416 }
4417
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 if (tp->link_config.active_speed == SPEED_1000 &&
4419 tp->link_config.active_duplex == DUPLEX_HALF)
4420 tw32(MAC_TX_LENGTHS,
4421 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4422 (6 << TX_LENGTHS_IPG_SHIFT) |
4423 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
4424 else
4425 tw32(MAC_TX_LENGTHS,
4426 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4427 (6 << TX_LENGTHS_IPG_SHIFT) |
4428 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
4429
4430 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
4431 if (netif_carrier_ok(tp->dev)) {
4432 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07004433 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 } else {
4435 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4436 }
4437 }
4438
Matt Carlson8ed5d972007-05-07 00:25:49 -07004439 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND) {
4440 u32 val = tr32(PCIE_PWR_MGMT_THRESH);
4441 if (!netif_carrier_ok(tp->dev))
4442 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4443 tp->pwrmgmt_thresh;
4444 else
4445 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4446 tw32(PCIE_PWR_MGMT_THRESH, val);
4447 }
4448
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 return err;
4450}
4451
Matt Carlson66cfd1b2010-09-30 10:34:30 +00004452static inline int tg3_irq_sync(struct tg3 *tp)
4453{
4454 return tp->irq_sync;
4455}
4456
Michael Chandf3e6542006-05-26 17:48:07 -07004457/* This is called whenever we suspect that the system chipset is re-
4458 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4459 * is bogus tx completions. We try to recover by setting the
4460 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4461 * in the workqueue.
4462 */
4463static void tg3_tx_recover(struct tg3 *tp)
4464{
4465 BUG_ON((tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) ||
4466 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4467
Matt Carlson5129c3a2010-04-05 10:19:23 +00004468 netdev_warn(tp->dev,
4469 "The system may be re-ordering memory-mapped I/O "
4470 "cycles to the network device, attempting to recover. "
4471 "Please report the problem to the driver maintainer "
4472 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07004473
4474 spin_lock(&tp->lock);
Michael Chandf3e6542006-05-26 17:48:07 -07004475 tp->tg3_flags |= TG3_FLAG_TX_RECOVERY_PENDING;
Michael Chandf3e6542006-05-26 17:48:07 -07004476 spin_unlock(&tp->lock);
4477}
4478
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004479static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07004480{
Matt Carlsonf65aac12010-08-02 11:26:03 +00004481 /* Tell compiler to fetch tx indices from memory. */
4482 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004483 return tnapi->tx_pending -
4484 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07004485}
4486
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487/* Tigon3 never reports partial packet sends. So we do not
4488 * need special logic to handle SKBs that have not had all
4489 * of their frags sent yet, like SunGEM does.
4490 */
Matt Carlson17375d22009-08-28 14:02:18 +00004491static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492{
Matt Carlson17375d22009-08-28 14:02:18 +00004493 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004494 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004495 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004496 struct netdev_queue *txq;
4497 int index = tnapi - tp->napi;
4498
Matt Carlson19cfaec2009-12-03 08:36:20 +00004499 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004500 index--;
4501
4502 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503
4504 while (sw_idx != hw_idx) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00004505 struct ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004507 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508
Michael Chandf3e6542006-05-26 17:48:07 -07004509 if (unlikely(skb == NULL)) {
4510 tg3_tx_recover(tp);
4511 return;
4512 }
4513
Alexander Duyckf4188d82009-12-02 16:48:38 +00004514 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004515 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004516 skb_headlen(skb),
4517 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518
4519 ri->skb = NULL;
4520
4521 sw_idx = NEXT_TX(sw_idx);
4522
4523 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004524 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004525 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4526 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00004527
4528 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004529 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004530 skb_shinfo(skb)->frags[i].size,
4531 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 sw_idx = NEXT_TX(sw_idx);
4533 }
4534
David S. Millerf47c11e2005-06-24 20:18:35 -07004535 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004536
4537 if (unlikely(tx_bug)) {
4538 tg3_tx_recover(tp);
4539 return;
4540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 }
4542
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004543 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544
Michael Chan1b2a7202006-08-07 21:46:02 -07004545 /* Need to make the tx_cons update visible to tg3_start_xmit()
4546 * before checking for netif_queue_stopped(). Without the
4547 * memory barrier, there is a small possibility that tg3_start_xmit()
4548 * will miss it and cause the queue to be stopped forever.
4549 */
4550 smp_mb();
4551
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004552 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004553 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004554 __netif_tx_lock(txq, smp_processor_id());
4555 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004556 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004557 netif_tx_wake_queue(txq);
4558 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07004559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560}
4561
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004562static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
4563{
4564 if (!ri->skb)
4565 return;
4566
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004567 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004568 map_sz, PCI_DMA_FROMDEVICE);
4569 dev_kfree_skb_any(ri->skb);
4570 ri->skb = NULL;
4571}
4572
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573/* Returns size of skb allocated or < 0 on error.
4574 *
4575 * We only need to fill in the address because the other members
4576 * of the RX descriptor are invariant, see tg3_init_rings.
4577 *
4578 * Note the purposeful assymetry of cpu vs. chip accesses. For
4579 * posting buffers we only dirty the first cache line of the RX
4580 * descriptor (containing the address). Whereas for the RX status
4581 * buffers the cpu only reads the last cacheline of the RX descriptor
4582 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4583 */
Matt Carlson86b21e52009-11-13 13:03:45 +00004584static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00004585 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586{
4587 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00004588 struct ring_info *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 struct sk_buff *skb;
4590 dma_addr_t mapping;
4591 int skb_size, dest_idx;
4592
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 switch (opaque_key) {
4594 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004595 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00004596 desc = &tpr->rx_std[dest_idx];
4597 map = &tpr->rx_std_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004598 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 break;
4600
4601 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004602 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004603 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004604 map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004605 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 break;
4607
4608 default:
4609 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611
4612 /* Do not overwrite any of the map or rp information
4613 * until we are sure we can commit to a new buffer.
4614 *
4615 * Callers depend upon this behavior and assume that
4616 * we leave everything unchanged if we fail.
4617 */
Matt Carlson287be122009-08-28 13:58:46 +00004618 skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 if (skb == NULL)
4620 return -ENOMEM;
4621
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 skb_reserve(skb, tp->rx_offset);
4623
Matt Carlson287be122009-08-28 13:58:46 +00004624 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00004626 if (pci_dma_mapping_error(tp->pdev, mapping)) {
4627 dev_kfree_skb(skb);
4628 return -EIO;
4629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630
4631 map->skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004632 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 desc->addr_hi = ((u64)mapping >> 32);
4635 desc->addr_lo = ((u64)mapping & 0xffffffff);
4636
4637 return skb_size;
4638}
4639
4640/* We only need to move over in the address because the other
4641 * members of the RX descriptor are invariant. See notes above
4642 * tg3_alloc_rx_skb for full details.
4643 */
Matt Carlsona3896162009-11-13 13:03:44 +00004644static void tg3_recycle_rx(struct tg3_napi *tnapi,
4645 struct tg3_rx_prodring_set *dpr,
4646 u32 opaque_key, int src_idx,
4647 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648{
Matt Carlson17375d22009-08-28 14:02:18 +00004649 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4651 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004652 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004653 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654
4655 switch (opaque_key) {
4656 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004657 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004658 dest_desc = &dpr->rx_std[dest_idx];
4659 dest_map = &dpr->rx_std_buffers[dest_idx];
4660 src_desc = &spr->rx_std[src_idx];
4661 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 break;
4663
4664 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004665 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004666 dest_desc = &dpr->rx_jmb[dest_idx].std;
4667 dest_map = &dpr->rx_jmb_buffers[dest_idx];
4668 src_desc = &spr->rx_jmb[src_idx].std;
4669 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 break;
4671
4672 default:
4673 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675
4676 dest_map->skb = src_map->skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004677 dma_unmap_addr_set(dest_map, mapping,
4678 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 dest_desc->addr_hi = src_desc->addr_hi;
4680 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00004681
4682 /* Ensure that the update to the skb happens after the physical
4683 * addresses have been transferred to the new BD location.
4684 */
4685 smp_wmb();
4686
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 src_map->skb = NULL;
4688}
4689
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690/* The RX ring scheme is composed of multiple rings which post fresh
4691 * buffers to the chip, and one special ring the chip uses to report
4692 * status back to the host.
4693 *
4694 * The special ring reports the status of received packets to the
4695 * host. The chip does not write into the original descriptor the
4696 * RX buffer was obtained from. The chip simply takes the original
4697 * descriptor as provided by the host, updates the status and length
4698 * field, then writes this into the next status ring entry.
4699 *
4700 * Each ring the host uses to post buffers to the chip is described
4701 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
4702 * it is first placed into the on-chip ram. When the packet's length
4703 * is known, it walks down the TG3_BDINFO entries to select the ring.
4704 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
4705 * which is within the range of the new packet's length is chosen.
4706 *
4707 * The "separate ring for rx status" scheme may sound queer, but it makes
4708 * sense from a cache coherency perspective. If only the host writes
4709 * to the buffer post rings, and only the chip writes to the rx status
4710 * rings, then cache lines never move beyond shared-modified state.
4711 * If both the host and chip were to write into the same ring, cache line
4712 * eviction could occur since both entities want it in an exclusive state.
4713 */
Matt Carlson17375d22009-08-28 14:02:18 +00004714static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715{
Matt Carlson17375d22009-08-28 14:02:18 +00004716 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07004717 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004718 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00004719 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07004720 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004722 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004724 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 /*
4726 * We need to order the read of hw_idx and the read of
4727 * the opaque cookie.
4728 */
4729 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 work_mask = 0;
4731 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004732 std_prod_idx = tpr->rx_std_prod_idx;
4733 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00004735 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00004736 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 unsigned int len;
4738 struct sk_buff *skb;
4739 dma_addr_t dma_addr;
4740 u32 opaque_key, desc_idx, *post_ptr;
4741
4742 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
4743 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
4744 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004745 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004746 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004747 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004748 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07004749 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004751 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004752 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004753 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004754 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00004755 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757
4758 work_mask |= opaque_key;
4759
4760 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
4761 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
4762 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00004763 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 desc_idx, *post_ptr);
4765 drop_it_no_recycle:
4766 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00004767 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 goto next_pkt;
4769 }
4770
Matt Carlsonad829262008-11-21 17:16:16 -08004771 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
4772 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773
Matt Carlsond2757fc2010-04-12 06:58:27 +00004774 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 int skb_size;
4776
Matt Carlson86b21e52009-11-13 13:03:45 +00004777 skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00004778 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 if (skb_size < 0)
4780 goto drop_it;
4781
Matt Carlson287be122009-08-28 13:58:46 +00004782 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 PCI_DMA_FROMDEVICE);
4784
Matt Carlson61e800c2010-02-17 15:16:54 +00004785 /* Ensure that the update to the skb happens
4786 * after the usage of the old DMA mapping.
4787 */
4788 smp_wmb();
4789
4790 ri->skb = NULL;
4791
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 skb_put(skb, len);
4793 } else {
4794 struct sk_buff *copy_skb;
4795
Matt Carlsona3896162009-11-13 13:03:44 +00004796 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 desc_idx, *post_ptr);
4798
Matt Carlsonbf933c82011-01-25 15:58:49 +00004799 copy_skb = netdev_alloc_skb(tp->dev, len +
Matt Carlson9dc7a112010-04-12 06:58:28 +00004800 TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 if (copy_skb == NULL)
4802 goto drop_it_no_recycle;
4803
Matt Carlsonbf933c82011-01-25 15:58:49 +00004804 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 skb_put(copy_skb, len);
4806 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03004807 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
4809
4810 /* We'll reuse the original ring buffer. */
4811 skb = copy_skb;
4812 }
4813
4814 if ((tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) &&
4815 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
4816 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
4817 >> RXD_TCPCSUM_SHIFT) == 0xffff))
4818 skb->ip_summed = CHECKSUM_UNNECESSARY;
4819 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07004820 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821
4822 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004823
4824 if (len > (tp->dev->mtu + ETH_HLEN) &&
4825 skb->protocol != htons(ETH_P_8021Q)) {
4826 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00004827 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004828 }
4829
Matt Carlson9dc7a112010-04-12 06:58:28 +00004830 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00004831 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
4832 __vlan_hwaccel_put_tag(skb,
4833 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00004834
Matt Carlsonbf933c82011-01-25 15:58:49 +00004835 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 received++;
4838 budget--;
4839
4840next_pkt:
4841 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07004842
4843 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00004844 tpr->rx_std_prod_idx = std_prod_idx &
4845 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00004846 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4847 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07004848 work_mask &= ~RXD_OPAQUE_RING_STD;
4849 rx_std_posted = 0;
4850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07004852 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00004853 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07004854
4855 /* Refresh hw_idx to see if there is new work */
4856 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004857 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07004858 rmb();
4859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 }
4861
4862 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00004863 tnapi->rx_rcb_ptr = sw_idx;
4864 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865
4866 /* Refill RX ring(s). */
Matt Carlsone4af1af2010-02-12 14:47:05 +00004867 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004868 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00004869 tpr->rx_std_prod_idx = std_prod_idx &
4870 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004871 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4872 tpr->rx_std_prod_idx);
4873 }
4874 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00004875 tpr->rx_jmb_prod_idx = jmb_prod_idx &
4876 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004877 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
4878 tpr->rx_jmb_prod_idx);
4879 }
4880 mmiowb();
4881 } else if (work_mask) {
4882 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
4883 * updated before the producer indices can be updated.
4884 */
4885 smp_wmb();
4886
Matt Carlson2c49a442010-09-30 10:34:35 +00004887 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
4888 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004889
Matt Carlsone4af1af2010-02-12 14:47:05 +00004890 if (tnapi != &tp->napi[1])
4891 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893
4894 return received;
4895}
4896
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004897static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 /* handle link change and other phy events */
4900 if (!(tp->tg3_flags &
4901 (TG3_FLAG_USE_LINKCHG_REG |
4902 TG3_FLAG_POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004903 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
4904
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 if (sblk->status & SD_STATUS_LINK_CHG) {
4906 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004907 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07004908 spin_lock(&tp->lock);
Matt Carlsondd477002008-05-25 23:45:58 -07004909 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
4910 tw32_f(MAC_STATUS,
4911 (MAC_STATUS_SYNC_CHANGED |
4912 MAC_STATUS_CFG_CHANGED |
4913 MAC_STATUS_MI_COMPLETION |
4914 MAC_STATUS_LNKSTATE_CHANGED));
4915 udelay(40);
4916 } else
4917 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07004918 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 }
4920 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004921}
4922
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004923static int tg3_rx_prodring_xfer(struct tg3 *tp,
4924 struct tg3_rx_prodring_set *dpr,
4925 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004926{
4927 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004928 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004929
4930 while (1) {
4931 src_prod_idx = spr->rx_std_prod_idx;
4932
4933 /* Make sure updates to the rx_std_buffers[] entries and the
4934 * standard producer index are seen in the correct order.
4935 */
4936 smp_rmb();
4937
4938 if (spr->rx_std_cons_idx == src_prod_idx)
4939 break;
4940
4941 if (spr->rx_std_cons_idx < src_prod_idx)
4942 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
4943 else
Matt Carlson2c49a442010-09-30 10:34:35 +00004944 cpycnt = tp->rx_std_ring_mask + 1 -
4945 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004946
Matt Carlson2c49a442010-09-30 10:34:35 +00004947 cpycnt = min(cpycnt,
4948 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004949
4950 si = spr->rx_std_cons_idx;
4951 di = dpr->rx_std_prod_idx;
4952
Matt Carlsone92967b2010-02-12 14:47:06 +00004953 for (i = di; i < di + cpycnt; i++) {
4954 if (dpr->rx_std_buffers[i].skb) {
4955 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004956 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00004957 break;
4958 }
4959 }
4960
4961 if (!cpycnt)
4962 break;
4963
4964 /* Ensure that updates to the rx_std_buffers ring and the
4965 * shadowed hardware producer ring from tg3_recycle_skb() are
4966 * ordered correctly WRT the skb check above.
4967 */
4968 smp_rmb();
4969
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004970 memcpy(&dpr->rx_std_buffers[di],
4971 &spr->rx_std_buffers[si],
4972 cpycnt * sizeof(struct ring_info));
4973
4974 for (i = 0; i < cpycnt; i++, di++, si++) {
4975 struct tg3_rx_buffer_desc *sbd, *dbd;
4976 sbd = &spr->rx_std[si];
4977 dbd = &dpr->rx_std[di];
4978 dbd->addr_hi = sbd->addr_hi;
4979 dbd->addr_lo = sbd->addr_lo;
4980 }
4981
Matt Carlson2c49a442010-09-30 10:34:35 +00004982 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
4983 tp->rx_std_ring_mask;
4984 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
4985 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004986 }
4987
4988 while (1) {
4989 src_prod_idx = spr->rx_jmb_prod_idx;
4990
4991 /* Make sure updates to the rx_jmb_buffers[] entries and
4992 * the jumbo producer index are seen in the correct order.
4993 */
4994 smp_rmb();
4995
4996 if (spr->rx_jmb_cons_idx == src_prod_idx)
4997 break;
4998
4999 if (spr->rx_jmb_cons_idx < src_prod_idx)
5000 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
5001 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005002 cpycnt = tp->rx_jmb_ring_mask + 1 -
5003 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005004
5005 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00005006 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005007
5008 si = spr->rx_jmb_cons_idx;
5009 di = dpr->rx_jmb_prod_idx;
5010
Matt Carlsone92967b2010-02-12 14:47:06 +00005011 for (i = di; i < di + cpycnt; i++) {
5012 if (dpr->rx_jmb_buffers[i].skb) {
5013 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005014 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005015 break;
5016 }
5017 }
5018
5019 if (!cpycnt)
5020 break;
5021
5022 /* Ensure that updates to the rx_jmb_buffers ring and the
5023 * shadowed hardware producer ring from tg3_recycle_skb() are
5024 * ordered correctly WRT the skb check above.
5025 */
5026 smp_rmb();
5027
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005028 memcpy(&dpr->rx_jmb_buffers[di],
5029 &spr->rx_jmb_buffers[si],
5030 cpycnt * sizeof(struct ring_info));
5031
5032 for (i = 0; i < cpycnt; i++, di++, si++) {
5033 struct tg3_rx_buffer_desc *sbd, *dbd;
5034 sbd = &spr->rx_jmb[si].std;
5035 dbd = &dpr->rx_jmb[di].std;
5036 dbd->addr_hi = sbd->addr_hi;
5037 dbd->addr_lo = sbd->addr_lo;
5038 }
5039
Matt Carlson2c49a442010-09-30 10:34:35 +00005040 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
5041 tp->rx_jmb_ring_mask;
5042 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
5043 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005044 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005045
5046 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005047}
5048
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005049static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
5050{
5051 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052
5053 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005054 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00005055 tg3_tx(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005056 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
Michael Chan4fd7ab52007-10-12 01:39:50 -07005057 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 }
5059
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 /* run RX thread, within the bounds set by NAPI.
5061 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005062 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005064 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00005065 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005067 if ((tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005068 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005069 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00005070 u32 std_prod_idx = dpr->rx_std_prod_idx;
5071 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005072
Matt Carlsone4af1af2010-02-12 14:47:05 +00005073 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005074 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00005075 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005076
5077 wmb();
5078
Matt Carlsone4af1af2010-02-12 14:47:05 +00005079 if (std_prod_idx != dpr->rx_std_prod_idx)
5080 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5081 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005082
Matt Carlsone4af1af2010-02-12 14:47:05 +00005083 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5084 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5085 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005086
5087 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005088
5089 if (err)
5090 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005091 }
5092
David S. Miller6f535762007-10-11 18:08:29 -07005093 return work_done;
5094}
David S. Millerf7383c22005-05-18 22:50:53 -07005095
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005096static int tg3_poll_msix(struct napi_struct *napi, int budget)
5097{
5098 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5099 struct tg3 *tp = tnapi->tp;
5100 int work_done = 0;
5101 struct tg3_hw_status *sblk = tnapi->hw_status;
5102
5103 while (1) {
5104 work_done = tg3_poll_work(tnapi, work_done, budget);
5105
5106 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5107 goto tx_recovery;
5108
5109 if (unlikely(work_done >= budget))
5110 break;
5111
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005112 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005113 * to tell the hw how much work has been processed,
5114 * so we must read it before checking for more work.
5115 */
5116 tnapi->last_tag = sblk->status_tag;
5117 tnapi->last_irq_tag = tnapi->last_tag;
5118 rmb();
5119
5120 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005121 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5122 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005123 napi_complete(napi);
5124 /* Reenable interrupts. */
5125 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5126 mmiowb();
5127 break;
5128 }
5129 }
5130
5131 return work_done;
5132
5133tx_recovery:
5134 /* work_done is guaranteed to be less than budget. */
5135 napi_complete(napi);
5136 schedule_work(&tp->reset_task);
5137 return work_done;
5138}
5139
David S. Miller6f535762007-10-11 18:08:29 -07005140static int tg3_poll(struct napi_struct *napi, int budget)
5141{
Matt Carlson8ef04422009-08-28 14:01:37 +00005142 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5143 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07005144 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00005145 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07005146
5147 while (1) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005148 tg3_poll_link(tp);
5149
Matt Carlson17375d22009-08-28 14:02:18 +00005150 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07005151
5152 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5153 goto tx_recovery;
5154
5155 if (unlikely(work_done >= budget))
5156 break;
5157
Michael Chan4fd7ab52007-10-12 01:39:50 -07005158 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
Matt Carlson17375d22009-08-28 14:02:18 +00005159 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07005160 * to tell the hw how much work has been processed,
5161 * so we must read it before checking for more work.
5162 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005163 tnapi->last_tag = sblk->status_tag;
5164 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07005165 rmb();
5166 } else
5167 sblk->status &= ~SD_STATUS_UPDATED;
5168
Matt Carlson17375d22009-08-28 14:02:18 +00005169 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005170 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00005171 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005172 break;
5173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 }
5175
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005176 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07005177
5178tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07005179 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08005180 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07005181 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07005182 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183}
5184
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005185static void tg3_napi_disable(struct tg3 *tp)
5186{
5187 int i;
5188
5189 for (i = tp->irq_cnt - 1; i >= 0; i--)
5190 napi_disable(&tp->napi[i].napi);
5191}
5192
5193static void tg3_napi_enable(struct tg3 *tp)
5194{
5195 int i;
5196
5197 for (i = 0; i < tp->irq_cnt; i++)
5198 napi_enable(&tp->napi[i].napi);
5199}
5200
5201static void tg3_napi_init(struct tg3 *tp)
5202{
5203 int i;
5204
5205 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
5206 for (i = 1; i < tp->irq_cnt; i++)
5207 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
5208}
5209
5210static void tg3_napi_fini(struct tg3 *tp)
5211{
5212 int i;
5213
5214 for (i = 0; i < tp->irq_cnt; i++)
5215 netif_napi_del(&tp->napi[i].napi);
5216}
5217
5218static inline void tg3_netif_stop(struct tg3 *tp)
5219{
5220 tp->dev->trans_start = jiffies; /* prevent tx timeout */
5221 tg3_napi_disable(tp);
5222 netif_tx_disable(tp->dev);
5223}
5224
5225static inline void tg3_netif_start(struct tg3 *tp)
5226{
5227 /* NOTE: unconditional netif_tx_wake_all_queues is only
5228 * appropriate so long as all callers are assured to
5229 * have free tx slots (such as after tg3_init_hw)
5230 */
5231 netif_tx_wake_all_queues(tp->dev);
5232
5233 tg3_napi_enable(tp);
5234 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
5235 tg3_enable_ints(tp);
5236}
5237
David S. Millerf47c11e2005-06-24 20:18:35 -07005238static void tg3_irq_quiesce(struct tg3 *tp)
5239{
Matt Carlson4f125f42009-09-01 12:55:02 +00005240 int i;
5241
David S. Millerf47c11e2005-06-24 20:18:35 -07005242 BUG_ON(tp->irq_sync);
5243
5244 tp->irq_sync = 1;
5245 smp_mb();
5246
Matt Carlson4f125f42009-09-01 12:55:02 +00005247 for (i = 0; i < tp->irq_cnt; i++)
5248 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07005249}
5250
David S. Millerf47c11e2005-06-24 20:18:35 -07005251/* Fully shutdown all tg3 driver activity elsewhere in the system.
5252 * If irq_sync is non-zero, then the IRQ handler must be synchronized
5253 * with as well. Most of the time, this is not necessary except when
5254 * shutting down the device.
5255 */
5256static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
5257{
Michael Chan46966542007-07-11 19:47:19 -07005258 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07005259 if (irq_sync)
5260 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005261}
5262
5263static inline void tg3_full_unlock(struct tg3 *tp)
5264{
David S. Millerf47c11e2005-06-24 20:18:35 -07005265 spin_unlock_bh(&tp->lock);
5266}
5267
Michael Chanfcfa0a32006-03-20 22:28:41 -08005268/* One-shot MSI handler - Chip automatically disables interrupt
5269 * after sending MSI so driver doesn't have to do it.
5270 */
David Howells7d12e782006-10-05 14:55:46 +01005271static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08005272{
Matt Carlson09943a12009-08-28 14:01:57 +00005273 struct tg3_napi *tnapi = dev_id;
5274 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08005275
Matt Carlson898a56f2009-08-28 14:02:40 +00005276 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005277 if (tnapi->rx_rcb)
5278 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005279
5280 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005281 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005282
5283 return IRQ_HANDLED;
5284}
5285
Michael Chan88b06bc22005-04-21 17:13:25 -07005286/* MSI ISR - No need to check for interrupt sharing and no need to
5287 * flush status block and interrupt mailbox. PCI ordering rules
5288 * guarantee that MSI will arrive after the status block.
5289 */
David Howells7d12e782006-10-05 14:55:46 +01005290static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07005291{
Matt Carlson09943a12009-08-28 14:01:57 +00005292 struct tg3_napi *tnapi = dev_id;
5293 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07005294
Matt Carlson898a56f2009-08-28 14:02:40 +00005295 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005296 if (tnapi->rx_rcb)
5297 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07005298 /*
David S. Millerfac9b832005-05-18 22:46:34 -07005299 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07005300 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07005301 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07005302 * NIC to stop sending us irqs, engaging "in-intr-handler"
5303 * event coalescing.
5304 */
5305 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07005306 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005307 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07005308
Michael Chan88b06bc22005-04-21 17:13:25 -07005309 return IRQ_RETVAL(1);
5310}
5311
David Howells7d12e782006-10-05 14:55:46 +01005312static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313{
Matt Carlson09943a12009-08-28 14:01:57 +00005314 struct tg3_napi *tnapi = dev_id;
5315 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005316 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317 unsigned int handled = 1;
5318
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 /* In INTx mode, it is possible for the interrupt to arrive at
5320 * the CPU before the status block posted prior to the interrupt.
5321 * Reading the PCI State register will confirm whether the
5322 * interrupt is ours and will flush the status block.
5323 */
Michael Chand18edcb2007-03-24 20:57:11 -07005324 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
5325 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5326 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5327 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005328 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07005329 }
Michael Chand18edcb2007-03-24 20:57:11 -07005330 }
5331
5332 /*
5333 * Writing any value to intr-mbox-0 clears PCI INTA# and
5334 * chip-internal interrupt pending events.
5335 * Writing non-zero to intr-mbox-0 additional tells the
5336 * NIC to stop sending us irqs, engaging "in-intr-handler"
5337 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005338 *
5339 * Flush the mailbox to de-assert the IRQ immediately to prevent
5340 * spurious interrupts. The flush impacts performance but
5341 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005342 */
Michael Chanc04cb342007-05-07 00:26:15 -07005343 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07005344 if (tg3_irq_sync(tp))
5345 goto out;
5346 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00005347 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00005348 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00005349 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07005350 } else {
5351 /* No work, shared interrupt perhaps? re-enable
5352 * interrupts, and flush that PCI write
5353 */
5354 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
5355 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07005356 }
David S. Millerf47c11e2005-06-24 20:18:35 -07005357out:
David S. Millerfac9b832005-05-18 22:46:34 -07005358 return IRQ_RETVAL(handled);
5359}
5360
David Howells7d12e782006-10-05 14:55:46 +01005361static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07005362{
Matt Carlson09943a12009-08-28 14:01:57 +00005363 struct tg3_napi *tnapi = dev_id;
5364 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005365 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07005366 unsigned int handled = 1;
5367
David S. Millerfac9b832005-05-18 22:46:34 -07005368 /* In INTx mode, it is possible for the interrupt to arrive at
5369 * the CPU before the status block posted prior to the interrupt.
5370 * Reading the PCI State register will confirm whether the
5371 * interrupt is ours and will flush the status block.
5372 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005373 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Michael Chand18edcb2007-03-24 20:57:11 -07005374 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5375 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5376 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005377 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 }
Michael Chand18edcb2007-03-24 20:57:11 -07005379 }
5380
5381 /*
5382 * writing any value to intr-mbox-0 clears PCI INTA# and
5383 * chip-internal interrupt pending events.
5384 * writing non-zero to intr-mbox-0 additional tells the
5385 * NIC to stop sending us irqs, engaging "in-intr-handler"
5386 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005387 *
5388 * Flush the mailbox to de-assert the IRQ immediately to prevent
5389 * spurious interrupts. The flush impacts performance but
5390 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005391 */
Michael Chanc04cb342007-05-07 00:26:15 -07005392 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00005393
5394 /*
5395 * In a shared interrupt configuration, sometimes other devices'
5396 * interrupts will scream. We record the current status tag here
5397 * so that the above check can report that the screaming interrupts
5398 * are unhandled. Eventually they will be silenced.
5399 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005400 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00005401
Michael Chand18edcb2007-03-24 20:57:11 -07005402 if (tg3_irq_sync(tp))
5403 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00005404
Matt Carlson72334482009-08-28 14:03:01 +00005405 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00005406
Matt Carlson09943a12009-08-28 14:01:57 +00005407 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00005408
David S. Millerf47c11e2005-06-24 20:18:35 -07005409out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410 return IRQ_RETVAL(handled);
5411}
5412
Michael Chan79381092005-04-21 17:13:59 -07005413/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01005414static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07005415{
Matt Carlson09943a12009-08-28 14:01:57 +00005416 struct tg3_napi *tnapi = dev_id;
5417 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005418 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07005419
Michael Chanf9804dd2005-09-27 12:13:10 -07005420 if ((sblk->status & SD_STATUS_UPDATED) ||
5421 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07005422 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07005423 return IRQ_RETVAL(1);
5424 }
5425 return IRQ_RETVAL(0);
5426}
5427
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07005428static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07005429static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430
Michael Chanb9ec6c12006-07-25 16:37:27 -07005431/* Restart hardware after configuration changes, self-test, etc.
5432 * Invoked with tp->lock held.
5433 */
5434static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07005435 __releases(tp->lock)
5436 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005437{
5438 int err;
5439
5440 err = tg3_init_hw(tp, reset_phy);
5441 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00005442 netdev_err(tp->dev,
5443 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07005444 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
5445 tg3_full_unlock(tp);
5446 del_timer_sync(&tp->timer);
5447 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00005448 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005449 dev_close(tp->dev);
5450 tg3_full_lock(tp, 0);
5451 }
5452 return err;
5453}
5454
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455#ifdef CONFIG_NET_POLL_CONTROLLER
5456static void tg3_poll_controller(struct net_device *dev)
5457{
Matt Carlson4f125f42009-09-01 12:55:02 +00005458 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07005459 struct tg3 *tp = netdev_priv(dev);
5460
Matt Carlson4f125f42009-09-01 12:55:02 +00005461 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00005462 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463}
5464#endif
5465
David Howellsc4028952006-11-22 14:57:56 +00005466static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467{
David Howellsc4028952006-11-22 14:57:56 +00005468 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005469 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470 unsigned int restart_timer;
5471
Michael Chan7faa0062006-02-02 17:29:28 -08005472 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08005473
5474 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08005475 tg3_full_unlock(tp);
5476 return;
5477 }
5478
5479 tg3_full_unlock(tp);
5480
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005481 tg3_phy_stop(tp);
5482
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 tg3_netif_stop(tp);
5484
David S. Millerf47c11e2005-06-24 20:18:35 -07005485 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486
5487 restart_timer = tp->tg3_flags2 & TG3_FLG2_RESTART_TIMER;
5488 tp->tg3_flags2 &= ~TG3_FLG2_RESTART_TIMER;
5489
Michael Chandf3e6542006-05-26 17:48:07 -07005490 if (tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING) {
5491 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5492 tp->write32_rx_mbox = tg3_write_flush_reg32;
5493 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
5494 tp->tg3_flags &= ~TG3_FLAG_TX_RECOVERY_PENDING;
5495 }
5496
Michael Chan944d9802005-05-29 14:57:48 -07005497 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005498 err = tg3_init_hw(tp, 1);
5499 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005500 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501
5502 tg3_netif_start(tp);
5503
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 if (restart_timer)
5505 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08005506
Michael Chanb9ec6c12006-07-25 16:37:27 -07005507out:
Michael Chan7faa0062006-02-02 17:29:28 -08005508 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005509
5510 if (!err)
5511 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512}
5513
Michael Chanb0408752007-02-13 12:18:30 -08005514static void tg3_dump_short_state(struct tg3 *tp)
5515{
Joe Perches05dbe002010-02-17 19:44:19 +00005516 netdev_err(tp->dev, "DEBUG: MAC_TX_STATUS[%08x] MAC_RX_STATUS[%08x]\n",
5517 tr32(MAC_TX_STATUS), tr32(MAC_RX_STATUS));
5518 netdev_err(tp->dev, "DEBUG: RDMAC_STATUS[%08x] WDMAC_STATUS[%08x]\n",
5519 tr32(RDMAC_STATUS), tr32(WDMAC_STATUS));
Michael Chanb0408752007-02-13 12:18:30 -08005520}
5521
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522static void tg3_tx_timeout(struct net_device *dev)
5523{
5524 struct tg3 *tp = netdev_priv(dev);
5525
Michael Chanb0408752007-02-13 12:18:30 -08005526 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00005527 netdev_err(dev, "transmit timed out, resetting\n");
Michael Chanb0408752007-02-13 12:18:30 -08005528 tg3_dump_short_state(tp);
5529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530
5531 schedule_work(&tp->reset_task);
5532}
5533
Michael Chanc58ec932005-09-17 00:46:27 -07005534/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5535static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5536{
5537 u32 base = (u32) mapping & 0xffffffff;
5538
Eric Dumazet807540b2010-09-23 05:40:09 +00005539 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07005540}
5541
Michael Chan72f2afb2006-03-06 19:28:35 -08005542/* Test for DMA addresses > 40-bit */
5543static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5544 int len)
5545{
5546#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Michael Chan6728a8e2006-03-27 23:16:49 -08005547 if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG)
Eric Dumazet807540b2010-09-23 05:40:09 +00005548 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08005549 return 0;
5550#else
5551 return 0;
5552#endif
5553}
5554
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005555static void tg3_set_txd(struct tg3_napi *, int, dma_addr_t, int, u32, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556
Michael Chan72f2afb2006-03-06 19:28:35 -08005557/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005558static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
5559 struct sk_buff *skb, u32 last_plus_one,
5560 u32 *start, u32 base_flags, u32 mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561{
Matt Carlson24f4efd2009-11-13 13:03:35 +00005562 struct tg3 *tp = tnapi->tp;
Matt Carlson41588ba2008-04-19 18:12:33 -07005563 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07005564 dma_addr_t new_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 u32 entry = *start;
Michael Chanc58ec932005-09-17 00:46:27 -07005566 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567
Matt Carlson41588ba2008-04-19 18:12:33 -07005568 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
5569 new_skb = skb_copy(skb, GFP_ATOMIC);
5570 else {
5571 int more_headroom = 4 - ((unsigned long)skb->data & 3);
5572
5573 new_skb = skb_copy_expand(skb,
5574 skb_headroom(skb) + more_headroom,
5575 skb_tailroom(skb), GFP_ATOMIC);
5576 }
5577
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07005579 ret = -1;
5580 } else {
5581 /* New SKB is guaranteed to be linear. */
5582 entry = *start;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005583 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
5584 PCI_DMA_TODEVICE);
5585 /* Make sure the mapping succeeded */
5586 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
5587 ret = -1;
5588 dev_kfree_skb(new_skb);
5589 new_skb = NULL;
David S. Miller90079ce2008-09-11 04:52:51 -07005590
Michael Chanc58ec932005-09-17 00:46:27 -07005591 /* Make sure new skb does not cross any 4G boundaries.
5592 * Drop the packet if it does.
5593 */
Alexander Duyckf4188d82009-12-02 16:48:38 +00005594 } else if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5595 tg3_4g_overflow_test(new_addr, new_skb->len)) {
5596 pci_unmap_single(tp->pdev, new_addr, new_skb->len,
5597 PCI_DMA_TODEVICE);
Michael Chanc58ec932005-09-17 00:46:27 -07005598 ret = -1;
5599 dev_kfree_skb(new_skb);
5600 new_skb = NULL;
5601 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005602 tg3_set_txd(tnapi, entry, new_addr, new_skb->len,
Michael Chanc58ec932005-09-17 00:46:27 -07005603 base_flags, 1 | (mss << 1));
5604 *start = NEXT_TX(entry);
5605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606 }
5607
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608 /* Now clean up the sw ring entries. */
5609 i = 0;
5610 while (entry != last_plus_one) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00005611 int len;
5612
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005613 if (i == 0)
Alexander Duyckf4188d82009-12-02 16:48:38 +00005614 len = skb_headlen(skb);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005615 else
Alexander Duyckf4188d82009-12-02 16:48:38 +00005616 len = skb_shinfo(skb)->frags[i-1].size;
5617
5618 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005619 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00005620 mapping),
5621 len, PCI_DMA_TODEVICE);
5622 if (i == 0) {
5623 tnapi->tx_buffers[entry].skb = new_skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005624 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00005625 new_addr);
5626 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005627 tnapi->tx_buffers[entry].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629 entry = NEXT_TX(entry);
5630 i++;
5631 }
5632
5633 dev_kfree_skb(skb);
5634
Michael Chanc58ec932005-09-17 00:46:27 -07005635 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636}
5637
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005638static void tg3_set_txd(struct tg3_napi *tnapi, int entry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 dma_addr_t mapping, int len, u32 flags,
5640 u32 mss_and_is_end)
5641{
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005642 struct tg3_tx_buffer_desc *txd = &tnapi->tx_ring[entry];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643 int is_end = (mss_and_is_end & 0x1);
5644 u32 mss = (mss_and_is_end >> 1);
5645 u32 vlan_tag = 0;
5646
5647 if (is_end)
5648 flags |= TXD_FLAG_END;
5649 if (flags & TXD_FLAG_VLAN) {
5650 vlan_tag = flags >> 16;
5651 flags &= 0xffff;
5652 }
5653 vlan_tag |= (mss << TXD_MSS_SHIFT);
5654
5655 txd->addr_hi = ((u64) mapping >> 32);
5656 txd->addr_lo = ((u64) mapping & 0xffffffff);
5657 txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
5658 txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
5659}
5660
Michael Chan5a6f3072006-03-20 22:28:05 -08005661/* hard_start_xmit for devices that don't have any bugs and
Matt Carlsone849cdc2009-11-13 13:03:38 +00005662 * support TG3_FLG2_HW_TSO_2 and TG3_FLG2_HW_TSO_3 only.
Michael Chan5a6f3072006-03-20 22:28:05 -08005663 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005664static netdev_tx_t tg3_start_xmit(struct sk_buff *skb,
5665 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666{
5667 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 u32 len, entry, base_flags, mss;
David S. Miller90079ce2008-09-11 04:52:51 -07005669 dma_addr_t mapping;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005670 struct tg3_napi *tnapi;
5671 struct netdev_queue *txq;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005672 unsigned int i, last;
5673
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005674 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5675 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Matt Carlson19cfaec2009-12-03 08:36:20 +00005676 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005677 tnapi++;
Michael Chan5a6f3072006-03-20 22:28:05 -08005678
Michael Chan00b70502006-06-17 21:58:45 -07005679 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005680 * and TX reclaim runs via tp->napi.poll inside of a software
Michael Chan5a6f3072006-03-20 22:28:05 -08005681 * interrupt. Furthermore, IRQ processing runs lockless so we have
5682 * no IRQ context deadlocks to worry about either. Rejoice!
5683 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005684 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005685 if (!netif_tx_queue_stopped(txq)) {
5686 netif_tx_stop_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005687
5688 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00005689 netdev_err(dev,
5690 "BUG! Tx Ring full when queue awake!\n");
Michael Chan5a6f3072006-03-20 22:28:05 -08005691 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005692 return NETDEV_TX_BUSY;
5693 }
5694
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005695 entry = tnapi->tx_prod;
Michael Chan5a6f3072006-03-20 22:28:05 -08005696 base_flags = 0;
Matt Carlsonbe98da62010-07-11 09:31:46 +00005697 mss = skb_shinfo(skb)->gso_size;
5698 if (mss) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005699 int tcp_opt_len, ip_tcp_len;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005700 u32 hdrlen;
Michael Chan5a6f3072006-03-20 22:28:05 -08005701
5702 if (skb_header_cloned(skb) &&
5703 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5704 dev_kfree_skb(skb);
5705 goto out_unlock;
5706 }
5707
Matt Carlson02e96082010-09-15 08:59:59 +00005708 if (skb_is_gso_v6(skb)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005709 hdrlen = skb_headlen(skb) - ETH_HLEN;
Matt Carlson02e96082010-09-15 08:59:59 +00005710 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005711 struct iphdr *iph = ip_hdr(skb);
5712
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005713 tcp_opt_len = tcp_optlen(skb);
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03005714 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
Michael Chanb0026622006-07-03 19:42:14 -07005715
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005716 iph->check = 0;
5717 iph->tot_len = htons(mss + ip_tcp_len + tcp_opt_len);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005718 hdrlen = ip_tcp_len + tcp_opt_len;
Michael Chanb0026622006-07-03 19:42:14 -07005719 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005720
Matt Carlsone849cdc2009-11-13 13:03:38 +00005721 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005722 mss |= (hdrlen & 0xc) << 12;
5723 if (hdrlen & 0x10)
5724 base_flags |= 0x00000010;
5725 base_flags |= (hdrlen & 0x3e0) << 5;
5726 } else
5727 mss |= hdrlen << 9;
5728
Michael Chan5a6f3072006-03-20 22:28:05 -08005729 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5730 TXD_FLAG_CPU_POST_DMA);
5731
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005732 tcp_hdr(skb)->check = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08005733
Matt Carlson859a588792010-04-05 10:19:28 +00005734 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005735 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson859a588792010-04-05 10:19:28 +00005736 }
5737
Jesse Grosseab6d182010-10-20 13:56:03 +00005738 if (vlan_tx_tag_present(skb))
Michael Chan5a6f3072006-03-20 22:28:05 -08005739 base_flags |= (TXD_FLAG_VLAN |
5740 (vlan_tx_tag_get(skb) << 16));
Michael Chan5a6f3072006-03-20 22:28:05 -08005741
Alexander Duyckf4188d82009-12-02 16:48:38 +00005742 len = skb_headlen(skb);
5743
5744 /* Queue skb data, a.k.a. the main skb fragment. */
5745 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
5746 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07005747 dev_kfree_skb(skb);
5748 goto out_unlock;
5749 }
5750
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005751 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005752 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005753
Matt Carlsonb703df62009-12-03 08:36:21 +00005754 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
Matt Carlson8fc2f992010-12-06 08:28:49 +00005755 !mss && skb->len > VLAN_ETH_FRAME_LEN)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005756 base_flags |= TXD_FLAG_JMB_PKT;
5757
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005758 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Michael Chan5a6f3072006-03-20 22:28:05 -08005759 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5760
5761 entry = NEXT_TX(entry);
5762
5763 /* Now loop through additional data fragments, and queue them. */
5764 if (skb_shinfo(skb)->nr_frags > 0) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005765 last = skb_shinfo(skb)->nr_frags - 1;
5766 for (i = 0; i <= last; i++) {
5767 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5768
5769 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005770 mapping = pci_map_page(tp->pdev,
5771 frag->page,
5772 frag->page_offset,
5773 len, PCI_DMA_TODEVICE);
5774 if (pci_dma_mapping_error(tp->pdev, mapping))
5775 goto dma_error;
5776
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005777 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005778 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00005779 mapping);
Michael Chan5a6f3072006-03-20 22:28:05 -08005780
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005781 tg3_set_txd(tnapi, entry, mapping, len,
Michael Chan5a6f3072006-03-20 22:28:05 -08005782 base_flags, (i == last) | (mss << 1));
5783
5784 entry = NEXT_TX(entry);
5785 }
5786 }
5787
5788 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005789 tw32_tx_mbox(tnapi->prodmbox, entry);
Michael Chan5a6f3072006-03-20 22:28:05 -08005790
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005791 tnapi->tx_prod = entry;
5792 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005793 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00005794
5795 /* netif_tx_stop_queue() must be done before checking
5796 * checking tx index in tg3_tx_avail() below, because in
5797 * tg3_tx(), we update tx index before checking for
5798 * netif_tx_queue_stopped().
5799 */
5800 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005801 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005802 netif_tx_wake_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005803 }
5804
5805out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005806 mmiowb();
Michael Chan5a6f3072006-03-20 22:28:05 -08005807
5808 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005809
5810dma_error:
5811 last = i;
5812 entry = tnapi->tx_prod;
5813 tnapi->tx_buffers[entry].skb = NULL;
5814 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005815 dma_unmap_addr(&tnapi->tx_buffers[entry], mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005816 skb_headlen(skb),
5817 PCI_DMA_TODEVICE);
5818 for (i = 0; i <= last; i++) {
5819 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5820 entry = NEXT_TX(entry);
5821
5822 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005823 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00005824 mapping),
5825 frag->size, PCI_DMA_TODEVICE);
5826 }
5827
5828 dev_kfree_skb(skb);
5829 return NETDEV_TX_OK;
Michael Chan5a6f3072006-03-20 22:28:05 -08005830}
5831
Stephen Hemminger613573252009-08-31 19:50:58 +00005832static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *,
5833 struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07005834
5835/* Use GSO to workaround a rare TSO bug that may be triggered when the
5836 * TSO header is greater than 80 bytes.
5837 */
5838static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
5839{
5840 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005841 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07005842
5843 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005844 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07005845 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00005846
5847 /* netif_tx_stop_queue() must be done before checking
5848 * checking tx index in tg3_tx_avail() below, because in
5849 * tg3_tx(), we update tx index before checking for
5850 * netif_tx_queue_stopped().
5851 */
5852 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005853 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08005854 return NETDEV_TX_BUSY;
5855
5856 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005857 }
5858
5859 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07005860 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07005861 goto tg3_tso_bug_end;
5862
5863 do {
5864 nskb = segs;
5865 segs = segs->next;
5866 nskb->next = NULL;
5867 tg3_start_xmit_dma_bug(nskb, tp->dev);
5868 } while (segs);
5869
5870tg3_tso_bug_end:
5871 dev_kfree_skb(skb);
5872
5873 return NETDEV_TX_OK;
5874}
Michael Chan52c0fd82006-06-29 20:15:54 -07005875
Michael Chan5a6f3072006-03-20 22:28:05 -08005876/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
5877 * support TG3_FLG2_HW_TSO_1 or firmware TSO only.
5878 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005879static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *skb,
5880 struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08005881{
5882 struct tg3 *tp = netdev_priv(dev);
Michael Chan5a6f3072006-03-20 22:28:05 -08005883 u32 len, entry, base_flags, mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884 int would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07005885 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005886 struct tg3_napi *tnapi;
5887 struct netdev_queue *txq;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005888 unsigned int i, last;
5889
Matt Carlson24f4efd2009-11-13 13:03:35 +00005890 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5891 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Matt Carlson19cfaec2009-12-03 08:36:20 +00005892 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlson24f4efd2009-11-13 13:03:35 +00005893 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894
Michael Chan00b70502006-06-17 21:58:45 -07005895 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005896 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07005897 * interrupt. Furthermore, IRQ processing runs lockless so we have
5898 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005900 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00005901 if (!netif_tx_queue_stopped(txq)) {
5902 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005903
5904 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00005905 netdev_err(dev,
5906 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 return NETDEV_TX_BUSY;
5909 }
5910
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005911 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07005913 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005915
Matt Carlsonbe98da62010-07-11 09:31:46 +00005916 mss = skb_shinfo(skb)->gso_size;
5917 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005918 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00005919 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920
5921 if (skb_header_cloned(skb) &&
5922 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5923 dev_kfree_skb(skb);
5924 goto out_unlock;
5925 }
5926
Matt Carlson34195c32010-07-11 09:31:42 +00005927 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005928 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929
Matt Carlson02e96082010-09-15 08:59:59 +00005930 if (skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00005931 hdr_len = skb_headlen(skb) - ETH_HLEN;
5932 } else {
5933 u32 ip_tcp_len;
5934
5935 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
5936 hdr_len = ip_tcp_len + tcp_opt_len;
5937
5938 iph->check = 0;
5939 iph->tot_len = htons(mss + hdr_len);
5940 }
5941
Michael Chan52c0fd82006-06-29 20:15:54 -07005942 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Michael Chan7f62ad52007-02-20 23:25:40 -08005943 (tp->tg3_flags2 & TG3_FLG2_TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00005944 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07005945
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5947 TXD_FLAG_CPU_POST_DMA);
5948
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005950 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005952 } else
5953 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
5954 iph->daddr, 0,
5955 IPPROTO_TCP,
5956 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957
Matt Carlson615774f2009-11-13 13:03:39 +00005958 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
5959 mss |= (hdr_len & 0xc) << 12;
5960 if (hdr_len & 0x10)
5961 base_flags |= 0x00000010;
5962 base_flags |= (hdr_len & 0x3e0) << 5;
5963 } else if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_2)
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005964 mss |= hdr_len << 9;
5965 else if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_1) ||
5966 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005967 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968 int tsflags;
5969
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005970 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971 mss |= (tsflags << 11);
5972 }
5973 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005974 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975 int tsflags;
5976
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005977 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978 base_flags |= tsflags << 12;
5979 }
5980 }
5981 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00005982
Jesse Grosseab6d182010-10-20 13:56:03 +00005983 if (vlan_tx_tag_present(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984 base_flags |= (TXD_FLAG_VLAN |
5985 (vlan_tx_tag_get(skb) << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986
Matt Carlsonb703df62009-12-03 08:36:21 +00005987 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
Matt Carlson8fc2f992010-12-06 08:28:49 +00005988 !mss && skb->len > VLAN_ETH_FRAME_LEN)
Matt Carlson615774f2009-11-13 13:03:39 +00005989 base_flags |= TXD_FLAG_JMB_PKT;
5990
Alexander Duyckf4188d82009-12-02 16:48:38 +00005991 len = skb_headlen(skb);
5992
5993 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
5994 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07005995 dev_kfree_skb(skb);
5996 goto out_unlock;
5997 }
5998
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005999 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006000 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001
6002 would_hit_hwbug = 0;
6003
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006004 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) && len <= 8)
6005 would_hit_hwbug = 1;
6006
Matt Carlson0e1406d2009-11-02 12:33:33 +00006007 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
6008 tg3_4g_overflow_test(mapping, len))
Matt Carlson41588ba2008-04-19 18:12:33 -07006009 would_hit_hwbug = 1;
Matt Carlson0e1406d2009-11-02 12:33:33 +00006010
6011 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
6012 tg3_40bit_overflow_test(tp, mapping, len))
6013 would_hit_hwbug = 1;
6014
6015 if (tp->tg3_flags3 & TG3_FLG3_5701_DMA_BUG)
Michael Chanc58ec932005-09-17 00:46:27 -07006016 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006018 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
6020
6021 entry = NEXT_TX(entry);
6022
6023 /* Now loop through additional data fragments, and queue them. */
6024 if (skb_shinfo(skb)->nr_frags > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025 last = skb_shinfo(skb)->nr_frags - 1;
6026 for (i = 0; i <= last; i++) {
6027 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6028
6029 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006030 mapping = pci_map_page(tp->pdev,
6031 frag->page,
6032 frag->page_offset,
6033 len, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006035 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006036 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006037 mapping);
6038 if (pci_dma_mapping_error(tp->pdev, mapping))
6039 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006041 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) &&
6042 len <= 8)
6043 would_hit_hwbug = 1;
6044
Matt Carlson0e1406d2009-11-02 12:33:33 +00006045 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
6046 tg3_4g_overflow_test(mapping, len))
Michael Chanc58ec932005-09-17 00:46:27 -07006047 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048
Matt Carlson0e1406d2009-11-02 12:33:33 +00006049 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
6050 tg3_40bit_overflow_test(tp, mapping, len))
Michael Chan72f2afb2006-03-06 19:28:35 -08006051 would_hit_hwbug = 1;
6052
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006054 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055 base_flags, (i == last)|(mss << 1));
6056 else
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006057 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058 base_flags, (i == last));
6059
6060 entry = NEXT_TX(entry);
6061 }
6062 }
6063
6064 if (would_hit_hwbug) {
6065 u32 last_plus_one = entry;
6066 u32 start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067
Michael Chanc58ec932005-09-17 00:46:27 -07006068 start = entry - 1 - skb_shinfo(skb)->nr_frags;
6069 start &= (TG3_TX_RING_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070
6071 /* If the workaround fails due to memory/mapping
6072 * failure, silently drop this packet.
6073 */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006074 if (tigon3_dma_hwbug_workaround(tnapi, skb, last_plus_one,
Michael Chanc58ec932005-09-17 00:46:27 -07006075 &start, base_flags, mss))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076 goto out_unlock;
6077
6078 entry = start;
6079 }
6080
6081 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006082 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006084 tnapi->tx_prod = entry;
6085 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006086 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006087
6088 /* netif_tx_stop_queue() must be done before checking
6089 * checking tx index in tg3_tx_avail() below, because in
6090 * tg3_tx(), we update tx index before checking for
6091 * netif_tx_queue_stopped().
6092 */
6093 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006094 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006095 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097
6098out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00006099 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100
6101 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006102
6103dma_error:
6104 last = i;
6105 entry = tnapi->tx_prod;
6106 tnapi->tx_buffers[entry].skb = NULL;
6107 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006108 dma_unmap_addr(&tnapi->tx_buffers[entry], mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006109 skb_headlen(skb),
6110 PCI_DMA_TODEVICE);
6111 for (i = 0; i <= last; i++) {
6112 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6113 entry = NEXT_TX(entry);
6114
6115 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006116 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00006117 mapping),
6118 frag->size, PCI_DMA_TODEVICE);
6119 }
6120
6121 dev_kfree_skb(skb);
6122 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123}
6124
6125static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
6126 int new_mtu)
6127{
6128 dev->mtu = new_mtu;
6129
Michael Chanef7f5ec2005-07-25 12:32:25 -07006130 if (new_mtu > ETH_DATA_LEN) {
Michael Chana4e2b342005-10-26 15:46:52 -07006131 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanef7f5ec2005-07-25 12:32:25 -07006132 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
6133 ethtool_op_set_tso(dev, 0);
Matt Carlson859a588792010-04-05 10:19:28 +00006134 } else {
Michael Chanef7f5ec2005-07-25 12:32:25 -07006135 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Matt Carlson859a588792010-04-05 10:19:28 +00006136 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07006137 } else {
Michael Chana4e2b342005-10-26 15:46:52 -07006138 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chanef7f5ec2005-07-25 12:32:25 -07006139 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -07006140 tp->tg3_flags &= ~TG3_FLAG_JUMBO_RING_ENABLE;
Michael Chanef7f5ec2005-07-25 12:32:25 -07006141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142}
6143
6144static int tg3_change_mtu(struct net_device *dev, int new_mtu)
6145{
6146 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07006147 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148
6149 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
6150 return -EINVAL;
6151
6152 if (!netif_running(dev)) {
6153 /* We'll just catch it later when the
6154 * device is up'd.
6155 */
6156 tg3_set_mtu(dev, tp, new_mtu);
6157 return 0;
6158 }
6159
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006160 tg3_phy_stop(tp);
6161
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006163
6164 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165
Michael Chan944d9802005-05-29 14:57:48 -07006166 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167
6168 tg3_set_mtu(dev, tp, new_mtu);
6169
Michael Chanb9ec6c12006-07-25 16:37:27 -07006170 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171
Michael Chanb9ec6c12006-07-25 16:37:27 -07006172 if (!err)
6173 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174
David S. Millerf47c11e2005-06-24 20:18:35 -07006175 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006177 if (!err)
6178 tg3_phy_start(tp);
6179
Michael Chanb9ec6c12006-07-25 16:37:27 -07006180 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181}
6182
Matt Carlson21f581a2009-08-28 14:00:25 +00006183static void tg3_rx_prodring_free(struct tg3 *tp,
6184 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186 int i;
6187
Matt Carlson8fea32b2010-09-15 08:59:58 +00006188 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006189 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006190 i = (i + 1) & tp->rx_std_ring_mask)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006191 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6192 tp->rx_pkt_map_sz);
6193
6194 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
6195 for (i = tpr->rx_jmb_cons_idx;
6196 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006197 i = (i + 1) & tp->rx_jmb_ring_mask) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006198 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6199 TG3_RX_JMB_MAP_SZ);
6200 }
6201 }
6202
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006203 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205
Matt Carlson2c49a442010-09-30 10:34:35 +00006206 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006207 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6208 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209
Matt Carlson48035722010-10-14 10:37:43 +00006210 if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
6211 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006212 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006213 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6214 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215 }
6216}
6217
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006218/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219 *
6220 * The chip has been shut down and the driver detached from
6221 * the networking, so no interrupts or new tx packets will
6222 * end up in the driver. tp->{tx,}lock are held and thus
6223 * we may not sleep.
6224 */
Matt Carlson21f581a2009-08-28 14:00:25 +00006225static int tg3_rx_prodring_alloc(struct tg3 *tp,
6226 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227{
Matt Carlson287be122009-08-28 13:58:46 +00006228 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006230 tpr->rx_std_cons_idx = 0;
6231 tpr->rx_std_prod_idx = 0;
6232 tpr->rx_jmb_cons_idx = 0;
6233 tpr->rx_jmb_prod_idx = 0;
6234
Matt Carlson8fea32b2010-09-15 08:59:58 +00006235 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006236 memset(&tpr->rx_std_buffers[0], 0,
6237 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00006238 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006239 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00006240 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006241 goto done;
6242 }
6243
Linus Torvalds1da177e2005-04-16 15:20:36 -07006244 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00006245 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246
Matt Carlson287be122009-08-28 13:58:46 +00006247 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Michael Chana4e2b342005-10-26 15:46:52 -07006248 if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00006249 tp->dev->mtu > ETH_DATA_LEN)
6250 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
6251 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07006252
Linus Torvalds1da177e2005-04-16 15:20:36 -07006253 /* Initialize invariants of the rings, we only set this
6254 * stuff once. This works because the card does not
6255 * write into the rx buffer posting rings.
6256 */
Matt Carlson2c49a442010-09-30 10:34:35 +00006257 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258 struct tg3_rx_buffer_desc *rxd;
6259
Matt Carlson21f581a2009-08-28 14:00:25 +00006260 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00006261 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
6263 rxd->opaque = (RXD_OPAQUE_RING_STD |
6264 (i << RXD_OPAQUE_INDEX_SHIFT));
6265 }
6266
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006267 /* Now allocate fresh SKBs for each rx ring. */
6268 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson86b21e52009-11-13 13:03:45 +00006269 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006270 netdev_warn(tp->dev,
6271 "Using a smaller RX standard ring. Only "
6272 "%d out of %d buffers were allocated "
6273 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006274 if (i == 0)
6275 goto initfail;
6276 tp->rx_pending = i;
6277 break;
6278 }
6279 }
6280
Matt Carlson48035722010-10-14 10:37:43 +00006281 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) ||
6282 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006283 goto done;
6284
Matt Carlson2c49a442010-09-30 10:34:35 +00006285 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006286
Matt Carlson0d86df82010-02-17 15:17:00 +00006287 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE))
6288 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289
Matt Carlson2c49a442010-09-30 10:34:35 +00006290 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00006291 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006292
Matt Carlson0d86df82010-02-17 15:17:00 +00006293 rxd = &tpr->rx_jmb[i].std;
6294 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
6295 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
6296 RXD_FLAG_JUMBO;
6297 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
6298 (i << RXD_OPAQUE_INDEX_SHIFT));
6299 }
6300
6301 for (i = 0; i < tp->rx_jumbo_pending; i++) {
6302 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006303 netdev_warn(tp->dev,
6304 "Using a smaller RX jumbo ring. Only %d "
6305 "out of %d buffers were allocated "
6306 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00006307 if (i == 0)
6308 goto initfail;
6309 tp->rx_jumbo_pending = i;
6310 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311 }
6312 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006313
6314done:
Michael Chan32d8c572006-07-25 16:38:29 -07006315 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006316
6317initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00006318 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006319 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006320}
6321
Matt Carlson21f581a2009-08-28 14:00:25 +00006322static void tg3_rx_prodring_fini(struct tg3 *tp,
6323 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324{
Matt Carlson21f581a2009-08-28 14:00:25 +00006325 kfree(tpr->rx_std_buffers);
6326 tpr->rx_std_buffers = NULL;
6327 kfree(tpr->rx_jmb_buffers);
6328 tpr->rx_jmb_buffers = NULL;
6329 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006330 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
6331 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006332 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333 }
Matt Carlson21f581a2009-08-28 14:00:25 +00006334 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006335 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
6336 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006337 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006339}
6340
Matt Carlson21f581a2009-08-28 14:00:25 +00006341static int tg3_rx_prodring_init(struct tg3 *tp,
6342 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006343{
Matt Carlson2c49a442010-09-30 10:34:35 +00006344 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
6345 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006346 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006347 return -ENOMEM;
6348
Matt Carlson4bae65c2010-11-24 08:31:52 +00006349 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
6350 TG3_RX_STD_RING_BYTES(tp),
6351 &tpr->rx_std_mapping,
6352 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006353 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006354 goto err_out;
6355
Matt Carlson48035722010-10-14 10:37:43 +00006356 if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
6357 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006358 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00006359 GFP_KERNEL);
6360 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006361 goto err_out;
6362
Matt Carlson4bae65c2010-11-24 08:31:52 +00006363 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
6364 TG3_RX_JMB_RING_BYTES(tp),
6365 &tpr->rx_jmb_mapping,
6366 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006367 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006368 goto err_out;
6369 }
6370
6371 return 0;
6372
6373err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00006374 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006375 return -ENOMEM;
6376}
6377
6378/* Free up pending packets in all rx/tx rings.
6379 *
6380 * The chip has been shut down and the driver detached from
6381 * the networking, so no interrupts or new tx packets will
6382 * end up in the driver. tp->{tx,}lock is not held and we are not
6383 * in an interrupt context and thus may sleep.
6384 */
6385static void tg3_free_rings(struct tg3 *tp)
6386{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006387 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006388
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006389 for (j = 0; j < tp->irq_cnt; j++) {
6390 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006391
Matt Carlson8fea32b2010-09-15 08:59:58 +00006392 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00006393
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006394 if (!tnapi->tx_buffers)
6395 continue;
6396
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006397 for (i = 0; i < TG3_TX_RING_SIZE; ) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006398 struct ring_info *txp;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006399 struct sk_buff *skb;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006400 unsigned int k;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006401
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006402 txp = &tnapi->tx_buffers[i];
6403 skb = txp->skb;
6404
6405 if (skb == NULL) {
6406 i++;
6407 continue;
6408 }
6409
Alexander Duyckf4188d82009-12-02 16:48:38 +00006410 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006411 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006412 skb_headlen(skb),
6413 PCI_DMA_TODEVICE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006414 txp->skb = NULL;
6415
Alexander Duyckf4188d82009-12-02 16:48:38 +00006416 i++;
6417
6418 for (k = 0; k < skb_shinfo(skb)->nr_frags; k++) {
6419 txp = &tnapi->tx_buffers[i & (TG3_TX_RING_SIZE - 1)];
6420 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006421 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006422 skb_shinfo(skb)->frags[k].size,
6423 PCI_DMA_TODEVICE);
6424 i++;
6425 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006426
6427 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006428 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006429 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006430}
6431
6432/* Initialize tx/rx rings for packet processing.
6433 *
6434 * The chip has been shut down and the driver detached from
6435 * the networking, so no interrupts or new tx packets will
6436 * end up in the driver. tp->{tx,}lock are held and thus
6437 * we may not sleep.
6438 */
6439static int tg3_init_rings(struct tg3 *tp)
6440{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006441 int i;
Matt Carlson72334482009-08-28 14:03:01 +00006442
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006443 /* Free up all the SKBs. */
6444 tg3_free_rings(tp);
6445
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006446 for (i = 0; i < tp->irq_cnt; i++) {
6447 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006448
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006449 tnapi->last_tag = 0;
6450 tnapi->last_irq_tag = 0;
6451 tnapi->hw_status->status = 0;
6452 tnapi->hw_status->status_tag = 0;
6453 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6454
6455 tnapi->tx_prod = 0;
6456 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006457 if (tnapi->tx_ring)
6458 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006459
6460 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006461 if (tnapi->rx_rcb)
6462 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006463
Matt Carlson8fea32b2010-09-15 08:59:58 +00006464 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00006465 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006466 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006467 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006468 }
Matt Carlson72334482009-08-28 14:03:01 +00006469
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006470 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006471}
6472
6473/*
6474 * Must not be invoked with interrupt sources disabled and
6475 * the hardware shutdown down.
6476 */
6477static void tg3_free_consistent(struct tg3 *tp)
6478{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006479 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006480
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006481 for (i = 0; i < tp->irq_cnt; i++) {
6482 struct tg3_napi *tnapi = &tp->napi[i];
6483
6484 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006485 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006486 tnapi->tx_ring, tnapi->tx_desc_mapping);
6487 tnapi->tx_ring = NULL;
6488 }
6489
6490 kfree(tnapi->tx_buffers);
6491 tnapi->tx_buffers = NULL;
6492
6493 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006494 dma_free_coherent(&tp->pdev->dev,
6495 TG3_RX_RCB_RING_BYTES(tp),
6496 tnapi->rx_rcb,
6497 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006498 tnapi->rx_rcb = NULL;
6499 }
6500
Matt Carlson8fea32b2010-09-15 08:59:58 +00006501 tg3_rx_prodring_fini(tp, &tnapi->prodring);
6502
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006503 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006504 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
6505 tnapi->hw_status,
6506 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006507 tnapi->hw_status = NULL;
6508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006509 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006510
Linus Torvalds1da177e2005-04-16 15:20:36 -07006511 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006512 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
6513 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006514 tp->hw_stats = NULL;
6515 }
6516}
6517
6518/*
6519 * Must not be invoked with interrupt sources disabled and
6520 * the hardware shutdown down. Can sleep.
6521 */
6522static int tg3_alloc_consistent(struct tg3 *tp)
6523{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006524 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006525
Matt Carlson4bae65c2010-11-24 08:31:52 +00006526 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
6527 sizeof(struct tg3_hw_stats),
6528 &tp->stats_mapping,
6529 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006530 if (!tp->hw_stats)
6531 goto err_out;
6532
Linus Torvalds1da177e2005-04-16 15:20:36 -07006533 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6534
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006535 for (i = 0; i < tp->irq_cnt; i++) {
6536 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006537 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006538
Matt Carlson4bae65c2010-11-24 08:31:52 +00006539 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
6540 TG3_HW_STATUS_SIZE,
6541 &tnapi->status_mapping,
6542 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006543 if (!tnapi->hw_status)
6544 goto err_out;
6545
6546 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006547 sblk = tnapi->hw_status;
6548
Matt Carlson8fea32b2010-09-15 08:59:58 +00006549 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
6550 goto err_out;
6551
Matt Carlson19cfaec2009-12-03 08:36:20 +00006552 /* If multivector TSS is enabled, vector 0 does not handle
6553 * tx interrupts. Don't allocate any resources for it.
6554 */
6555 if ((!i && !(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) ||
6556 (i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))) {
6557 tnapi->tx_buffers = kzalloc(sizeof(struct ring_info) *
6558 TG3_TX_RING_SIZE,
6559 GFP_KERNEL);
6560 if (!tnapi->tx_buffers)
6561 goto err_out;
6562
Matt Carlson4bae65c2010-11-24 08:31:52 +00006563 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
6564 TG3_TX_RING_BYTES,
6565 &tnapi->tx_desc_mapping,
6566 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00006567 if (!tnapi->tx_ring)
6568 goto err_out;
6569 }
6570
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006571 /*
6572 * When RSS is enabled, the status block format changes
6573 * slightly. The "rx_jumbo_consumer", "reserved",
6574 * and "rx_mini_consumer" members get mapped to the
6575 * other three rx return ring producer indexes.
6576 */
6577 switch (i) {
6578 default:
6579 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
6580 break;
6581 case 2:
6582 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
6583 break;
6584 case 3:
6585 tnapi->rx_rcb_prod_idx = &sblk->reserved;
6586 break;
6587 case 4:
6588 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
6589 break;
6590 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006591
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006592 /*
6593 * If multivector RSS is enabled, vector 0 does not handle
6594 * rx or tx interrupts. Don't allocate any resources for it.
6595 */
6596 if (!i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS))
6597 continue;
6598
Matt Carlson4bae65c2010-11-24 08:31:52 +00006599 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
6600 TG3_RX_RCB_RING_BYTES(tp),
6601 &tnapi->rx_rcb_mapping,
6602 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006603 if (!tnapi->rx_rcb)
6604 goto err_out;
6605
6606 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006607 }
6608
Linus Torvalds1da177e2005-04-16 15:20:36 -07006609 return 0;
6610
6611err_out:
6612 tg3_free_consistent(tp);
6613 return -ENOMEM;
6614}
6615
6616#define MAX_WAIT_CNT 1000
6617
6618/* To stop a block, clear the enable bit and poll till it
6619 * clears. tp->lock is held.
6620 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006621static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006622{
6623 unsigned int i;
6624 u32 val;
6625
6626 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
6627 switch (ofs) {
6628 case RCVLSC_MODE:
6629 case DMAC_MODE:
6630 case MBFREE_MODE:
6631 case BUFMGR_MODE:
6632 case MEMARB_MODE:
6633 /* We can't enable/disable these bits of the
6634 * 5705/5750, just say success.
6635 */
6636 return 0;
6637
6638 default:
6639 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006641 }
6642
6643 val = tr32(ofs);
6644 val &= ~enable_bit;
6645 tw32_f(ofs, val);
6646
6647 for (i = 0; i < MAX_WAIT_CNT; i++) {
6648 udelay(100);
6649 val = tr32(ofs);
6650 if ((val & enable_bit) == 0)
6651 break;
6652 }
6653
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006654 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00006655 dev_err(&tp->pdev->dev,
6656 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
6657 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006658 return -ENODEV;
6659 }
6660
6661 return 0;
6662}
6663
6664/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006665static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006666{
6667 int i, err;
6668
6669 tg3_disable_ints(tp);
6670
6671 tp->rx_mode &= ~RX_MODE_ENABLE;
6672 tw32_f(MAC_RX_MODE, tp->rx_mode);
6673 udelay(10);
6674
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006675 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
6676 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
6677 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
6678 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
6679 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
6680 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006681
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006682 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
6683 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
6684 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
6685 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
6686 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
6687 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
6688 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006689
6690 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
6691 tw32_f(MAC_MODE, tp->mac_mode);
6692 udelay(40);
6693
6694 tp->tx_mode &= ~TX_MODE_ENABLE;
6695 tw32_f(MAC_TX_MODE, tp->tx_mode);
6696
6697 for (i = 0; i < MAX_WAIT_CNT; i++) {
6698 udelay(100);
6699 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
6700 break;
6701 }
6702 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00006703 dev_err(&tp->pdev->dev,
6704 "%s timed out, TX_MODE_ENABLE will not clear "
6705 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07006706 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006707 }
6708
Michael Chane6de8ad2005-05-05 14:42:41 -07006709 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006710 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
6711 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006712
6713 tw32(FTQ_RESET, 0xffffffff);
6714 tw32(FTQ_RESET, 0x00000000);
6715
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006716 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
6717 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006718
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006719 for (i = 0; i < tp->irq_cnt; i++) {
6720 struct tg3_napi *tnapi = &tp->napi[i];
6721 if (tnapi->hw_status)
6722 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006724 if (tp->hw_stats)
6725 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6726
Linus Torvalds1da177e2005-04-16 15:20:36 -07006727 return err;
6728}
6729
Matt Carlson0d3031d2007-10-10 18:02:43 -07006730static void tg3_ape_send_event(struct tg3 *tp, u32 event)
6731{
6732 int i;
6733 u32 apedata;
6734
Matt Carlsondc6d0742010-09-15 08:59:55 +00006735 /* NCSI does not support APE events */
6736 if (tp->tg3_flags3 & TG3_FLG3_APE_HAS_NCSI)
6737 return;
6738
Matt Carlson0d3031d2007-10-10 18:02:43 -07006739 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
6740 if (apedata != APE_SEG_SIG_MAGIC)
6741 return;
6742
6743 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07006744 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006745 return;
6746
6747 /* Wait for up to 1 millisecond for APE to service previous event. */
6748 for (i = 0; i < 10; i++) {
6749 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
6750 return;
6751
6752 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
6753
6754 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6755 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
6756 event | APE_EVENT_STATUS_EVENT_PENDING);
6757
6758 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
6759
6760 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6761 break;
6762
6763 udelay(100);
6764 }
6765
6766 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6767 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
6768}
6769
6770static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
6771{
6772 u32 event;
6773 u32 apedata;
6774
6775 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
6776 return;
6777
6778 switch (kind) {
Matt Carlson33f401a2010-04-05 10:19:27 +00006779 case RESET_KIND_INIT:
6780 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
6781 APE_HOST_SEG_SIG_MAGIC);
6782 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
6783 APE_HOST_SEG_LEN_MAGIC);
6784 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
6785 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
6786 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
Matt Carlson6867c842010-07-11 09:31:44 +00006787 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
Matt Carlson33f401a2010-04-05 10:19:27 +00006788 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
6789 APE_HOST_BEHAV_NO_PHYLOCK);
Matt Carlsondc6d0742010-09-15 08:59:55 +00006790 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
6791 TG3_APE_HOST_DRVR_STATE_START);
Matt Carlson0d3031d2007-10-10 18:02:43 -07006792
Matt Carlson33f401a2010-04-05 10:19:27 +00006793 event = APE_EVENT_STATUS_STATE_START;
6794 break;
6795 case RESET_KIND_SHUTDOWN:
6796 /* With the interface we are currently using,
6797 * APE does not track driver state. Wiping
6798 * out the HOST SEGMENT SIGNATURE forces
6799 * the APE to assume OS absent status.
6800 */
6801 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
Matt Carlsonb2aee152008-11-03 16:51:11 -08006802
Matt Carlsondc6d0742010-09-15 08:59:55 +00006803 if (device_may_wakeup(&tp->pdev->dev) &&
6804 (tp->tg3_flags & TG3_FLAG_WOL_ENABLE)) {
6805 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
6806 TG3_APE_HOST_WOL_SPEED_AUTO);
6807 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
6808 } else
6809 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
6810
6811 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
6812
Matt Carlson33f401a2010-04-05 10:19:27 +00006813 event = APE_EVENT_STATUS_STATE_UNLOAD;
6814 break;
6815 case RESET_KIND_SUSPEND:
6816 event = APE_EVENT_STATUS_STATE_SUSPEND;
6817 break;
6818 default:
6819 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006820 }
6821
6822 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
6823
6824 tg3_ape_send_event(tp, event);
6825}
6826
Michael Chane6af3012005-04-21 17:12:05 -07006827/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
6829{
David S. Millerf49639e2006-06-09 11:58:36 -07006830 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
6831 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006832
6833 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6834 switch (kind) {
6835 case RESET_KIND_INIT:
6836 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6837 DRV_STATE_START);
6838 break;
6839
6840 case RESET_KIND_SHUTDOWN:
6841 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6842 DRV_STATE_UNLOAD);
6843 break;
6844
6845 case RESET_KIND_SUSPEND:
6846 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6847 DRV_STATE_SUSPEND);
6848 break;
6849
6850 default:
6851 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006853 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006854
6855 if (kind == RESET_KIND_INIT ||
6856 kind == RESET_KIND_SUSPEND)
6857 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006858}
6859
6860/* tp->lock is held. */
6861static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
6862{
6863 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6864 switch (kind) {
6865 case RESET_KIND_INIT:
6866 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6867 DRV_STATE_START_DONE);
6868 break;
6869
6870 case RESET_KIND_SHUTDOWN:
6871 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6872 DRV_STATE_UNLOAD_DONE);
6873 break;
6874
6875 default:
6876 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006879
6880 if (kind == RESET_KIND_SHUTDOWN)
6881 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006882}
6883
6884/* tp->lock is held. */
6885static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
6886{
6887 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
6888 switch (kind) {
6889 case RESET_KIND_INIT:
6890 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6891 DRV_STATE_START);
6892 break;
6893
6894 case RESET_KIND_SHUTDOWN:
6895 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6896 DRV_STATE_UNLOAD);
6897 break;
6898
6899 case RESET_KIND_SUSPEND:
6900 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6901 DRV_STATE_SUSPEND);
6902 break;
6903
6904 default:
6905 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006907 }
6908}
6909
Michael Chan7a6f4362006-09-27 16:03:31 -07006910static int tg3_poll_fw(struct tg3 *tp)
6911{
6912 int i;
6913 u32 val;
6914
Michael Chanb5d37722006-09-27 16:06:21 -07006915 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08006916 /* Wait up to 20ms for init done. */
6917 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07006918 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
6919 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08006920 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07006921 }
6922 return -ENODEV;
6923 }
6924
Michael Chan7a6f4362006-09-27 16:03:31 -07006925 /* Wait for firmware initialization to complete. */
6926 for (i = 0; i < 100000; i++) {
6927 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
6928 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
6929 break;
6930 udelay(10);
6931 }
6932
6933 /* Chip might not be fitted with firmware. Some Sun onboard
6934 * parts are configured like that. So don't signal the timeout
6935 * of the above loop as an error, but do report the lack of
6936 * running firmware once.
6937 */
6938 if (i >= 100000 &&
6939 !(tp->tg3_flags2 & TG3_FLG2_NO_FWARE_REPORTED)) {
6940 tp->tg3_flags2 |= TG3_FLG2_NO_FWARE_REPORTED;
6941
Joe Perches05dbe002010-02-17 19:44:19 +00006942 netdev_info(tp->dev, "No firmware running\n");
Michael Chan7a6f4362006-09-27 16:03:31 -07006943 }
6944
Matt Carlson6b10c162010-02-12 14:47:08 +00006945 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
6946 /* The 57765 A0 needs a little more
6947 * time to do some important work.
6948 */
6949 mdelay(10);
6950 }
6951
Michael Chan7a6f4362006-09-27 16:03:31 -07006952 return 0;
6953}
6954
Michael Chanee6a99b2007-07-18 21:49:10 -07006955/* Save PCI command register before chip reset */
6956static void tg3_save_pci_state(struct tg3 *tp)
6957{
Matt Carlson8a6eac92007-10-21 16:17:55 -07006958 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07006959}
6960
6961/* Restore PCI state after chip reset */
6962static void tg3_restore_pci_state(struct tg3 *tp)
6963{
6964 u32 val;
6965
6966 /* Re-enable indirect register accesses. */
6967 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
6968 tp->misc_host_ctrl);
6969
6970 /* Set MAX PCI retry to zero. */
6971 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
6972 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
6973 (tp->tg3_flags & TG3_FLAG_PCIX_MODE))
6974 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006975 /* Allow reads and writes to the APE register and memory space. */
6976 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
6977 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00006978 PCISTATE_ALLOW_APE_SHMEM_WR |
6979 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07006980 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
6981
Matt Carlson8a6eac92007-10-21 16:17:55 -07006982 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07006983
Matt Carlsonfcb389d2008-11-03 16:55:44 -08006984 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
6985 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
Matt Carlsoncf790032010-11-24 08:31:48 +00006986 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlsonfcb389d2008-11-03 16:55:44 -08006987 else {
6988 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
6989 tp->pci_cacheline_sz);
6990 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
6991 tp->pci_lat_timer);
6992 }
Michael Chan114342f2007-10-15 02:12:26 -07006993 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08006994
Michael Chanee6a99b2007-07-18 21:49:10 -07006995 /* Make sure PCI-X relaxed ordering bit is clear. */
Matt Carlson52f44902008-11-21 17:17:04 -08006996 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Matt Carlson9974a352007-10-07 23:27:28 -07006997 u16 pcix_cmd;
6998
6999 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7000 &pcix_cmd);
7001 pcix_cmd &= ~PCI_X_CMD_ERO;
7002 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7003 pcix_cmd);
7004 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007005
7006 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007007
7008 /* Chip reset on 5780 will reset MSI enable bit,
7009 * so need to restore it.
7010 */
7011 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
7012 u16 ctrl;
7013
7014 pci_read_config_word(tp->pdev,
7015 tp->msi_cap + PCI_MSI_FLAGS,
7016 &ctrl);
7017 pci_write_config_word(tp->pdev,
7018 tp->msi_cap + PCI_MSI_FLAGS,
7019 ctrl | PCI_MSI_FLAGS_ENABLE);
7020 val = tr32(MSGINT_MODE);
7021 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7022 }
7023 }
7024}
7025
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026static void tg3_stop_fw(struct tg3 *);
7027
7028/* tp->lock is held. */
7029static int tg3_chip_reset(struct tg3 *tp)
7030{
7031 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007032 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007033 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007034
David S. Millerf49639e2006-06-09 11:58:36 -07007035 tg3_nvram_lock(tp);
7036
Matt Carlson77b483f2008-08-15 14:07:24 -07007037 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7038
David S. Millerf49639e2006-06-09 11:58:36 -07007039 /* No matching tg3_nvram_unlock() after this because
7040 * chip reset below will undo the nvram lock.
7041 */
7042 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007043
Michael Chanee6a99b2007-07-18 21:49:10 -07007044 /* GRC_MISC_CFG core clock reset will clear the memory
7045 * enable bit in PCI register 4 and the MSI enable bit
7046 * on some chips, so we save relevant registers here.
7047 */
7048 tg3_save_pci_state(tp);
7049
Michael Chand9ab5ad12006-03-20 22:27:35 -08007050 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08007051 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08007052 tw32(GRC_FASTBOOT_PC, 0);
7053
Linus Torvalds1da177e2005-04-16 15:20:36 -07007054 /*
7055 * We must avoid the readl() that normally takes place.
7056 * It locks machines, causes machine checks, and other
7057 * fun things. So, temporarily disable the 5701
7058 * hardware workaround, while we do the reset.
7059 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007060 write_op = tp->write32;
7061 if (write_op == tg3_write_flush_reg32)
7062 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063
Michael Chand18edcb2007-03-24 20:57:11 -07007064 /* Prevent the irq handler from reading or writing PCI registers
7065 * during chip reset when the memory enable bit in the PCI command
7066 * register may be cleared. The chip does not generate interrupt
7067 * at this time, but the irq handler may still be called due to irq
7068 * sharing or irqpoll.
7069 */
7070 tp->tg3_flags |= TG3_FLAG_CHIP_RESETTING;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007071 for (i = 0; i < tp->irq_cnt; i++) {
7072 struct tg3_napi *tnapi = &tp->napi[i];
7073 if (tnapi->hw_status) {
7074 tnapi->hw_status->status = 0;
7075 tnapi->hw_status->status_tag = 0;
7076 }
7077 tnapi->last_tag = 0;
7078 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007079 }
Michael Chand18edcb2007-03-24 20:57:11 -07007080 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007081
7082 for (i = 0; i < tp->irq_cnt; i++)
7083 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007084
Matt Carlson255ca312009-08-25 10:07:27 +00007085 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7086 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7087 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7088 }
7089
Linus Torvalds1da177e2005-04-16 15:20:36 -07007090 /* do the reset */
7091 val = GRC_MISC_CFG_CORECLK_RESET;
7092
7093 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
Matt Carlson88075d92010-08-02 11:25:58 +00007094 /* Force PCIe 1.0a mode */
7095 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlson1407deb2011-04-05 14:22:44 +00007096 !(tp->tg3_flags3 & TG3_FLG3_57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00007097 tr32(TG3_PCIE_PHY_TSTCTL) ==
7098 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7099 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7100
Linus Torvalds1da177e2005-04-16 15:20:36 -07007101 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7102 tw32(GRC_MISC_CFG, (1 << 29));
7103 val |= (1 << 29);
7104 }
7105 }
7106
Michael Chanb5d37722006-09-27 16:06:21 -07007107 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7108 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7109 tw32(GRC_VCPU_EXT_CTRL,
7110 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7111 }
7112
Matt Carlsonf37500d2010-08-02 11:25:59 +00007113 /* Manage gphy power for all CPMU absent PCIe devices. */
7114 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
7115 !(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007116 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007117
Linus Torvalds1da177e2005-04-16 15:20:36 -07007118 tw32(GRC_MISC_CFG, val);
7119
Michael Chan1ee582d2005-08-09 20:16:46 -07007120 /* restore 5701 hardware bug workaround write method */
7121 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007122
7123 /* Unfortunately, we have to delay before the PCI read back.
7124 * Some 575X chips even will not respond to a PCI cfg access
7125 * when the reset command is given to the chip.
7126 *
7127 * How do these hardware designers expect things to work
7128 * properly if the PCI write is posted for a long period
7129 * of time? It is always necessary to have some method by
7130 * which a register read back can occur to push the write
7131 * out which does the reset.
7132 *
7133 * For most tg3 variants the trick below was working.
7134 * Ho hum...
7135 */
7136 udelay(120);
7137
7138 /* Flush PCI posted writes. The normal MMIO registers
7139 * are inaccessible at this time so this is the only
7140 * way to make this reliably (actually, this is no longer
7141 * the case, see above). I tried to use indirect
7142 * register read/write but this upset some 5701 variants.
7143 */
7144 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7145
7146 udelay(120);
7147
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007148 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) && tp->pcie_cap) {
Matt Carlsone7126992009-08-25 10:08:16 +00007149 u16 val16;
7150
Linus Torvalds1da177e2005-04-16 15:20:36 -07007151 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7152 int i;
7153 u32 cfg_val;
7154
7155 /* Wait for link training to complete. */
7156 for (i = 0; i < 5000; i++)
7157 udelay(100);
7158
7159 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7160 pci_write_config_dword(tp->pdev, 0xc4,
7161 cfg_val | (1 << 15));
7162 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007163
Matt Carlsone7126992009-08-25 10:08:16 +00007164 /* Clear the "no snoop" and "relaxed ordering" bits. */
7165 pci_read_config_word(tp->pdev,
7166 tp->pcie_cap + PCI_EXP_DEVCTL,
7167 &val16);
7168 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7169 PCI_EXP_DEVCTL_NOSNOOP_EN);
7170 /*
7171 * Older PCIe devices only support the 128 byte
7172 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007173 */
Matt Carlson6de34cb2010-08-02 11:25:55 +00007174 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007175 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007176 pci_write_config_word(tp->pdev,
7177 tp->pcie_cap + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007178 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007179
Matt Carlsoncf790032010-11-24 08:31:48 +00007180 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007181
7182 /* Clear error status */
7183 pci_write_config_word(tp->pdev,
7184 tp->pcie_cap + PCI_EXP_DEVSTA,
7185 PCI_EXP_DEVSTA_CED |
7186 PCI_EXP_DEVSTA_NFED |
7187 PCI_EXP_DEVSTA_FED |
7188 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007189 }
7190
Michael Chanee6a99b2007-07-18 21:49:10 -07007191 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007192
Michael Chand18edcb2007-03-24 20:57:11 -07007193 tp->tg3_flags &= ~TG3_FLAG_CHIP_RESETTING;
7194
Michael Chanee6a99b2007-07-18 21:49:10 -07007195 val = 0;
7196 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chan4cf78e42005-07-25 12:29:19 -07007197 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007198 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007199
7200 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7201 tg3_stop_fw(tp);
7202 tw32(0x5000, 0x400);
7203 }
7204
7205 tw32(GRC_MODE, tp->grc_mode);
7206
7207 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007208 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007209
7210 tw32(0xc4, val | (1 << 15));
7211 }
7212
7213 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7214 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7215 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7216 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7217 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7218 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7219 }
7220
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007221 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
7222 tp->mac_mode = MAC_MODE_APE_TX_EN |
7223 MAC_MODE_APE_RX_EN |
7224 MAC_MODE_TDE_ENABLE;
7225
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007226 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007227 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
7228 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007229 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007230 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7231 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007232 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007233 val = 0;
7234
7235 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007236 udelay(40);
7237
Matt Carlson77b483f2008-08-15 14:07:24 -07007238 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7239
Michael Chan7a6f4362006-09-27 16:03:31 -07007240 err = tg3_poll_fw(tp);
7241 if (err)
7242 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007243
Matt Carlson0a9140c2009-08-28 12:27:50 +00007244 tg3_mdio_start(tp);
7245
Linus Torvalds1da177e2005-04-16 15:20:36 -07007246 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007247 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7248 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlson1407deb2011-04-05 14:22:44 +00007249 !(tp->tg3_flags3 & TG3_FLG3_57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007250 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007251
7252 tw32(0x7c00, val | (1 << 25));
7253 }
7254
Matt Carlsond78b59f2011-04-05 14:22:46 +00007255 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
7256 val = tr32(TG3_CPMU_CLCK_ORIDE);
7257 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
7258 }
7259
Linus Torvalds1da177e2005-04-16 15:20:36 -07007260 /* Reprobe ASF enable state. */
7261 tp->tg3_flags &= ~TG3_FLAG_ENABLE_ASF;
7262 tp->tg3_flags2 &= ~TG3_FLG2_ASF_NEW_HANDSHAKE;
7263 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7264 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7265 u32 nic_cfg;
7266
7267 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7268 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
7269 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
Matt Carlson4ba526c2008-08-15 14:10:04 -07007270 tp->last_event_jiffies = jiffies;
John W. Linvillecbf46852005-04-21 17:01:29 -07007271 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007272 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
7273 }
7274 }
7275
7276 return 0;
7277}
7278
7279/* tp->lock is held. */
7280static void tg3_stop_fw(struct tg3 *tp)
7281{
Matt Carlson0d3031d2007-10-10 18:02:43 -07007282 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
7283 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07007284 /* Wait for RX cpu to ACK the previous event. */
7285 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007286
7287 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007288
7289 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007290
Matt Carlson7c5026a2008-05-02 16:49:29 -07007291 /* Wait for RX cpu to ACK this event. */
7292 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007293 }
7294}
7295
7296/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007297static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007298{
7299 int err;
7300
7301 tg3_stop_fw(tp);
7302
Michael Chan944d9802005-05-29 14:57:48 -07007303 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007304
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007305 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007306 err = tg3_chip_reset(tp);
7307
Matt Carlsondaba2a62009-04-20 06:58:52 +00007308 __tg3_set_mac_addr(tp, 0);
7309
Michael Chan944d9802005-05-29 14:57:48 -07007310 tg3_write_sig_legacy(tp, kind);
7311 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007312
7313 if (err)
7314 return err;
7315
7316 return 0;
7317}
7318
Linus Torvalds1da177e2005-04-16 15:20:36 -07007319#define RX_CPU_SCRATCH_BASE 0x30000
7320#define RX_CPU_SCRATCH_SIZE 0x04000
7321#define TX_CPU_SCRATCH_BASE 0x34000
7322#define TX_CPU_SCRATCH_SIZE 0x04000
7323
7324/* tp->lock is held. */
7325static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
7326{
7327 int i;
7328
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +02007329 BUG_ON(offset == TX_CPU_BASE &&
7330 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007331
Michael Chanb5d37722006-09-27 16:06:21 -07007332 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7333 u32 val = tr32(GRC_VCPU_EXT_CTRL);
7334
7335 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
7336 return 0;
7337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007338 if (offset == RX_CPU_BASE) {
7339 for (i = 0; i < 10000; i++) {
7340 tw32(offset + CPU_STATE, 0xffffffff);
7341 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7342 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7343 break;
7344 }
7345
7346 tw32(offset + CPU_STATE, 0xffffffff);
7347 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
7348 udelay(10);
7349 } else {
7350 for (i = 0; i < 10000; i++) {
7351 tw32(offset + CPU_STATE, 0xffffffff);
7352 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7353 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7354 break;
7355 }
7356 }
7357
7358 if (i >= 10000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007359 netdev_err(tp->dev, "%s timed out, %s CPU\n",
7360 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007361 return -ENODEV;
7362 }
Michael Chanec41c7d2006-01-17 02:40:55 -08007363
7364 /* Clear firmware's nvram arbitration. */
7365 if (tp->tg3_flags & TG3_FLAG_NVRAM)
7366 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007367 return 0;
7368}
7369
7370struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007371 unsigned int fw_base;
7372 unsigned int fw_len;
7373 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007374};
7375
7376/* tp->lock is held. */
7377static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
7378 int cpu_scratch_size, struct fw_info *info)
7379{
Michael Chanec41c7d2006-01-17 02:40:55 -08007380 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007381 void (*write_op)(struct tg3 *, u32, u32);
7382
7383 if (cpu_base == TX_CPU_BASE &&
7384 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007385 netdev_err(tp->dev,
7386 "%s: Trying to load TX cpu firmware which is 5705\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007387 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007388 return -EINVAL;
7389 }
7390
7391 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
7392 write_op = tg3_write_mem;
7393 else
7394 write_op = tg3_write_indirect_reg32;
7395
Michael Chan1b628152005-05-29 14:59:49 -07007396 /* It is possible that bootcode is still loading at this point.
7397 * Get the nvram lock first before halting the cpu.
7398 */
Michael Chanec41c7d2006-01-17 02:40:55 -08007399 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007400 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08007401 if (!lock_err)
7402 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007403 if (err)
7404 goto out;
7405
7406 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
7407 write_op(tp, cpu_scratch_base + i, 0);
7408 tw32(cpu_base + CPU_STATE, 0xffffffff);
7409 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007410 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007411 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007412 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07007413 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007414 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007415
7416 err = 0;
7417
7418out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007419 return err;
7420}
7421
7422/* tp->lock is held. */
7423static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
7424{
7425 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007426 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007427 int err, i;
7428
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007429 fw_data = (void *)tp->fw->data;
7430
7431 /* Firmware blob starts with version numbers, followed by
7432 start address and length. We are setting complete length.
7433 length = end_address_of_bss - start_address_of_text.
7434 Remainder is the blob to be loaded contiguously
7435 from start address. */
7436
7437 info.fw_base = be32_to_cpu(fw_data[1]);
7438 info.fw_len = tp->fw->size - 12;
7439 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007440
7441 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
7442 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
7443 &info);
7444 if (err)
7445 return err;
7446
7447 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
7448 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
7449 &info);
7450 if (err)
7451 return err;
7452
7453 /* Now startup only the RX cpu. */
7454 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007455 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007456
7457 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007458 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007459 break;
7460 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7461 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007462 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007463 udelay(1000);
7464 }
7465 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007466 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
7467 "should be %08x\n", __func__,
Joe Perches05dbe002010-02-17 19:44:19 +00007468 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007469 return -ENODEV;
7470 }
7471 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7472 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
7473
7474 return 0;
7475}
7476
Linus Torvalds1da177e2005-04-16 15:20:36 -07007477/* 5705 needs a special version of the TSO firmware. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007478
7479/* tp->lock is held. */
7480static int tg3_load_tso_firmware(struct tg3 *tp)
7481{
7482 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007483 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007484 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
7485 int err, i;
7486
7487 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
7488 return 0;
7489
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007490 fw_data = (void *)tp->fw->data;
7491
7492 /* Firmware blob starts with version numbers, followed by
7493 start address and length. We are setting complete length.
7494 length = end_address_of_bss - start_address_of_text.
7495 Remainder is the blob to be loaded contiguously
7496 from start address. */
7497
7498 info.fw_base = be32_to_cpu(fw_data[1]);
7499 cpu_scratch_size = tp->fw_len;
7500 info.fw_len = tp->fw->size - 12;
7501 info.fw_data = &fw_data[3];
7502
Linus Torvalds1da177e2005-04-16 15:20:36 -07007503 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007504 cpu_base = RX_CPU_BASE;
7505 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007506 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007507 cpu_base = TX_CPU_BASE;
7508 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
7509 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
7510 }
7511
7512 err = tg3_load_firmware_cpu(tp, cpu_base,
7513 cpu_scratch_base, cpu_scratch_size,
7514 &info);
7515 if (err)
7516 return err;
7517
7518 /* Now startup the cpu. */
7519 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007520 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007521
7522 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007523 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007524 break;
7525 tw32(cpu_base + CPU_STATE, 0xffffffff);
7526 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007527 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007528 udelay(1000);
7529 }
7530 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007531 netdev_err(tp->dev,
7532 "%s fails to set CPU PC, is %08x should be %08x\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007533 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007534 return -ENODEV;
7535 }
7536 tw32(cpu_base + CPU_STATE, 0xffffffff);
7537 tw32_f(cpu_base + CPU_MODE, 0x00000000);
7538 return 0;
7539}
7540
Linus Torvalds1da177e2005-04-16 15:20:36 -07007541
Linus Torvalds1da177e2005-04-16 15:20:36 -07007542static int tg3_set_mac_addr(struct net_device *dev, void *p)
7543{
7544 struct tg3 *tp = netdev_priv(dev);
7545 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007546 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007547
Michael Chanf9804dd2005-09-27 12:13:10 -07007548 if (!is_valid_ether_addr(addr->sa_data))
7549 return -EINVAL;
7550
Linus Torvalds1da177e2005-04-16 15:20:36 -07007551 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7552
Michael Chane75f7c92006-03-20 21:33:26 -08007553 if (!netif_running(dev))
7554 return 0;
7555
Michael Chan58712ef2006-04-29 18:58:01 -07007556 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007557 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007558
Michael Chan986e0ae2007-05-05 12:10:20 -07007559 addr0_high = tr32(MAC_ADDR_0_HIGH);
7560 addr0_low = tr32(MAC_ADDR_0_LOW);
7561 addr1_high = tr32(MAC_ADDR_1_HIGH);
7562 addr1_low = tr32(MAC_ADDR_1_LOW);
7563
7564 /* Skip MAC addr 1 if ASF is using it. */
7565 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7566 !(addr1_high == 0 && addr1_low == 0))
7567 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007568 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007569 spin_lock_bh(&tp->lock);
7570 __tg3_set_mac_addr(tp, skip_mac_1);
7571 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007572
Michael Chanb9ec6c12006-07-25 16:37:27 -07007573 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007574}
7575
7576/* tp->lock is held. */
7577static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7578 dma_addr_t mapping, u32 maxlen_flags,
7579 u32 nic_addr)
7580{
7581 tg3_write_mem(tp,
7582 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7583 ((u64) mapping >> 32));
7584 tg3_write_mem(tp,
7585 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7586 ((u64) mapping & 0xffffffff));
7587 tg3_write_mem(tp,
7588 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7589 maxlen_flags);
7590
7591 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7592 tg3_write_mem(tp,
7593 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7594 nic_addr);
7595}
7596
7597static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007598static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007599{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007600 int i;
7601
Matt Carlson19cfaec2009-12-03 08:36:20 +00007602 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007603 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7604 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7605 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007606 } else {
7607 tw32(HOSTCC_TXCOL_TICKS, 0);
7608 tw32(HOSTCC_TXMAX_FRAMES, 0);
7609 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007610 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007611
Matt Carlson20d73752010-07-11 09:31:41 +00007612 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007613 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
7614 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
7615 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
7616 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007617 tw32(HOSTCC_RXCOL_TICKS, 0);
7618 tw32(HOSTCC_RXMAX_FRAMES, 0);
7619 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07007620 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007621
David S. Miller15f98502005-05-18 22:49:26 -07007622 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7623 u32 val = ec->stats_block_coalesce_usecs;
7624
Matt Carlsonb6080e12009-09-01 13:12:00 +00007625 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
7626 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
7627
David S. Miller15f98502005-05-18 22:49:26 -07007628 if (!netif_carrier_ok(tp->dev))
7629 val = 0;
7630
7631 tw32(HOSTCC_STAT_COAL_TICKS, val);
7632 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007633
7634 for (i = 0; i < tp->irq_cnt - 1; i++) {
7635 u32 reg;
7636
7637 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
7638 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007639 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
7640 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007641 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
7642 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007643
7644 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7645 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
7646 tw32(reg, ec->tx_coalesce_usecs);
7647 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
7648 tw32(reg, ec->tx_max_coalesced_frames);
7649 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
7650 tw32(reg, ec->tx_max_coalesced_frames_irq);
7651 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007652 }
7653
7654 for (; i < tp->irq_max - 1; i++) {
7655 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007656 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007657 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007658
7659 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7660 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
7661 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
7662 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7663 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007664 }
David S. Miller15f98502005-05-18 22:49:26 -07007665}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007666
7667/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00007668static void tg3_rings_reset(struct tg3 *tp)
7669{
7670 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007671 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007672 struct tg3_napi *tnapi = &tp->napi[0];
7673
7674 /* Disable all transmit rings but the first. */
7675 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7676 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlson0a58d662011-04-05 14:22:45 +00007677 else if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlson3d377282010-10-14 10:37:39 +00007678 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlsonb703df62009-12-03 08:36:21 +00007679 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
7680 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007681 else
7682 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7683
7684 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7685 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
7686 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
7687 BDINFO_FLAGS_DISABLED);
7688
7689
7690 /* Disable all receive return rings but the first. */
Matt Carlson0a58d662011-04-05 14:22:45 +00007691 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007692 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
7693 else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007694 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007695 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7696 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00007697 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
7698 else
7699 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7700
7701 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7702 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
7703 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
7704 BDINFO_FLAGS_DISABLED);
7705
7706 /* Disable interrupts */
7707 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
7708
7709 /* Zero mailbox registers. */
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007710 if (tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00007711 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007712 tp->napi[i].tx_prod = 0;
7713 tp->napi[i].tx_cons = 0;
Matt Carlsonc2353a32010-01-20 16:58:08 +00007714 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
7715 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007716 tw32_rx_mbox(tp->napi[i].consmbox, 0);
7717 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
7718 }
Matt Carlsonc2353a32010-01-20 16:58:08 +00007719 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))
7720 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007721 } else {
7722 tp->napi[0].tx_prod = 0;
7723 tp->napi[0].tx_cons = 0;
7724 tw32_mailbox(tp->napi[0].prodmbox, 0);
7725 tw32_rx_mbox(tp->napi[0].consmbox, 0);
7726 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007727
7728 /* Make sure the NIC-based send BD rings are disabled. */
7729 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7730 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
7731 for (i = 0; i < 16; i++)
7732 tw32_tx_mbox(mbox + i * 8, 0);
7733 }
7734
7735 txrcb = NIC_SRAM_SEND_RCB;
7736 rxrcb = NIC_SRAM_RCV_RET_RCB;
7737
7738 /* Clear status block in ram. */
7739 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7740
7741 /* Set status block DMA address */
7742 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7743 ((u64) tnapi->status_mapping >> 32));
7744 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7745 ((u64) tnapi->status_mapping & 0xffffffff));
7746
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007747 if (tnapi->tx_ring) {
7748 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7749 (TG3_TX_RING_SIZE <<
7750 BDINFO_FLAGS_MAXLEN_SHIFT),
7751 NIC_SRAM_TX_BUFFER_DESC);
7752 txrcb += TG3_BDINFO_SIZE;
7753 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007754
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007755 if (tnapi->rx_rcb) {
7756 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007757 (tp->rx_ret_ring_mask + 1) <<
7758 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007759 rxrcb += TG3_BDINFO_SIZE;
7760 }
7761
7762 stblk = HOSTCC_STATBLCK_RING1;
7763
7764 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
7765 u64 mapping = (u64)tnapi->status_mapping;
7766 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
7767 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
7768
7769 /* Clear status block in ram. */
7770 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7771
Matt Carlson19cfaec2009-12-03 08:36:20 +00007772 if (tnapi->tx_ring) {
7773 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7774 (TG3_TX_RING_SIZE <<
7775 BDINFO_FLAGS_MAXLEN_SHIFT),
7776 NIC_SRAM_TX_BUFFER_DESC);
7777 txrcb += TG3_BDINFO_SIZE;
7778 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007779
7780 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007781 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007782 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7783
7784 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007785 rxrcb += TG3_BDINFO_SIZE;
7786 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007787}
7788
7789/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007790static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007791{
7792 u32 val, rdmac_mode;
7793 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00007794 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007795
7796 tg3_disable_ints(tp);
7797
7798 tg3_stop_fw(tp);
7799
7800 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
7801
Matt Carlson859a588792010-04-05 10:19:28 +00007802 if (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)
Michael Chane6de8ad2005-05-05 14:42:41 -07007803 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007804
Matt Carlson699c0192010-12-06 08:28:51 +00007805 /* Enable MAC control of LPI */
7806 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
7807 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
7808 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
7809 TG3_CPMU_EEE_LNKIDL_UART_IDL);
7810
7811 tw32_f(TG3_CPMU_EEE_CTRL,
7812 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
7813
Matt Carlsona386b902010-12-06 08:28:53 +00007814 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
7815 TG3_CPMU_EEEMD_LPI_IN_TX |
7816 TG3_CPMU_EEEMD_LPI_IN_RX |
7817 TG3_CPMU_EEEMD_EEE_ENABLE;
7818
7819 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
7820 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
7821
7822 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
7823 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
7824
7825 tw32_f(TG3_CPMU_EEE_MODE, val);
7826
7827 tw32_f(TG3_CPMU_EEE_DBTMR1,
7828 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
7829 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
7830
7831 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00007832 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00007833 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00007834 }
7835
Matt Carlson603f1172010-02-12 14:47:10 +00007836 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08007837 tg3_phy_reset(tp);
7838
Linus Torvalds1da177e2005-04-16 15:20:36 -07007839 err = tg3_chip_reset(tp);
7840 if (err)
7841 return err;
7842
7843 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
7844
Matt Carlsonbcb37f62008-11-03 16:52:09 -08007845 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007846 val = tr32(TG3_CPMU_CTRL);
7847 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
7848 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08007849
7850 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7851 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7852 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7853 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
7854
7855 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
7856 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
7857 val |= CPMU_LNK_AWARE_MACCLK_6_25;
7858 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
7859
7860 val = tr32(TG3_CPMU_HST_ACC);
7861 val &= ~CPMU_HST_ACC_MACCLK_MASK;
7862 val |= CPMU_HST_ACC_MACCLK_6_25;
7863 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07007864 }
7865
Matt Carlson33466d92009-04-20 06:57:41 +00007866 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7867 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
7868 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
7869 PCIE_PWR_MGMT_L1_THRESH_4MS;
7870 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00007871
7872 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
7873 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
7874
7875 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00007876
Matt Carlsonf40386c2009-11-02 14:24:02 +00007877 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7878 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00007879 }
7880
Matt Carlson614b0592010-01-20 16:58:02 +00007881 if (tp->tg3_flags3 & TG3_FLG3_L1PLLPD_EN) {
7882 u32 grc_mode = tr32(GRC_MODE);
7883
7884 /* Access the lower 1K of PL PCIE block registers. */
7885 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7886 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
7887
7888 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
7889 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
7890 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
7891
7892 tw32(GRC_MODE, grc_mode);
7893 }
7894
Matt Carlson5093eed2010-11-24 08:31:45 +00007895 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
7896 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
7897 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00007898
Matt Carlson5093eed2010-11-24 08:31:45 +00007899 /* Access the lower 1K of PL PCIE block registers. */
7900 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7901 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00007902
Matt Carlson5093eed2010-11-24 08:31:45 +00007903 val = tr32(TG3_PCIE_TLDLPL_PORT +
7904 TG3_PCIE_PL_LO_PHYCTL5);
7905 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
7906 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00007907
Matt Carlson5093eed2010-11-24 08:31:45 +00007908 tw32(GRC_MODE, grc_mode);
7909 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00007910
7911 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7912 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7913 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7914 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00007915 }
7916
Linus Torvalds1da177e2005-04-16 15:20:36 -07007917 /* This works around an issue with Athlon chipsets on
7918 * B3 tigon3 silicon. This bit has no effect on any
7919 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07007920 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007921 */
Matt Carlson795d01c2007-10-07 23:28:17 -07007922 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)) {
7923 if (!(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
7924 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
7925 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007927
7928 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
7929 (tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
7930 val = tr32(TG3PCI_PCISTATE);
7931 val |= PCISTATE_RETRY_SAME_DMA;
7932 tw32(TG3PCI_PCISTATE, val);
7933 }
7934
Matt Carlson0d3031d2007-10-10 18:02:43 -07007935 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
7936 /* Allow reads and writes to the
7937 * APE register and memory space.
7938 */
7939 val = tr32(TG3PCI_PCISTATE);
7940 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00007941 PCISTATE_ALLOW_APE_SHMEM_WR |
7942 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007943 tw32(TG3PCI_PCISTATE, val);
7944 }
7945
Linus Torvalds1da177e2005-04-16 15:20:36 -07007946 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
7947 /* Enable some hw fixes. */
7948 val = tr32(TG3PCI_MSI_DATA);
7949 val |= (1 << 26) | (1 << 28) | (1 << 29);
7950 tw32(TG3PCI_MSI_DATA, val);
7951 }
7952
7953 /* Descriptor ring init may make accesses to the
7954 * NIC SRAM area to setup the TX descriptors, so we
7955 * can only do this after the hardware has been
7956 * successfully reset.
7957 */
Michael Chan32d8c572006-07-25 16:38:29 -07007958 err = tg3_init_rings(tp);
7959 if (err)
7960 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007961
Matt Carlson1407deb2011-04-05 14:22:44 +00007962 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00007963 val = tr32(TG3PCI_DMA_RW_CTRL) &
7964 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00007965 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
7966 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00007967 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
7968 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
7969 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007970 /* This value is determined during the probe time DMA
7971 * engine test, tg3_test_dma.
7972 */
7973 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
7974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007975
7976 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
7977 GRC_MODE_4X_NIC_SEND_RINGS |
7978 GRC_MODE_NO_TX_PHDR_CSUM |
7979 GRC_MODE_NO_RX_PHDR_CSUM);
7980 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07007981
7982 /* Pseudo-header checksum is done by hardware logic and not
7983 * the offload processers, so make the chip do the pseudo-
7984 * header checksums on receive. For transmit it is more
7985 * convenient to do the pseudo-header checksum in software
7986 * as Linux does that on transmit for us in all cases.
7987 */
7988 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007989
7990 tw32(GRC_MODE,
7991 tp->grc_mode |
7992 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
7993
7994 /* Setup the timer prescalar register. Clock is always 66Mhz. */
7995 val = tr32(GRC_MISC_CFG);
7996 val &= ~0xff;
7997 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
7998 tw32(GRC_MISC_CFG, val);
7999
8000 /* Initialize MBUF/DESC pool. */
John W. Linvillecbf46852005-04-21 17:01:29 -07008001 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008002 /* Do nothing. */
8003 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8004 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8005 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8006 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8007 else
8008 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8009 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8010 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Matt Carlson859a588792010-04-05 10:19:28 +00008011 } else if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008012 int fw_len;
8013
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008014 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008015 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8016 tw32(BUFMGR_MB_POOL_ADDR,
8017 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8018 tw32(BUFMGR_MB_POOL_SIZE,
8019 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008021
Michael Chan0f893dc2005-07-25 12:30:38 -07008022 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008023 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8024 tp->bufmgr_config.mbuf_read_dma_low_water);
8025 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8026 tp->bufmgr_config.mbuf_mac_rx_low_water);
8027 tw32(BUFMGR_MB_HIGH_WATER,
8028 tp->bufmgr_config.mbuf_high_water);
8029 } else {
8030 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8031 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8032 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8033 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8034 tw32(BUFMGR_MB_HIGH_WATER,
8035 tp->bufmgr_config.mbuf_high_water_jumbo);
8036 }
8037 tw32(BUFMGR_DMA_LOW_WATER,
8038 tp->bufmgr_config.dma_low_water);
8039 tw32(BUFMGR_DMA_HIGH_WATER,
8040 tp->bufmgr_config.dma_high_water);
8041
Matt Carlsond309a462010-09-30 10:34:31 +00008042 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8043 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8044 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
8045 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008046 for (i = 0; i < 2000; i++) {
8047 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8048 break;
8049 udelay(10);
8050 }
8051 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008052 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008053 return -ENODEV;
8054 }
8055
8056 /* Setup replenish threshold. */
Michael Chanf92905d2006-06-29 20:14:29 -07008057 val = tp->rx_pending / 8;
8058 if (val == 0)
8059 val = 1;
8060 else if (val > tp->rx_std_max_post)
8061 val = tp->rx_std_max_post;
Michael Chanb5d37722006-09-27 16:06:21 -07008062 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
8063 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8064 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
8065
8066 if (val > (TG3_RX_INTERNAL_RING_SZ_5906 / 2))
8067 val = TG3_RX_INTERNAL_RING_SZ_5906 / 2;
8068 }
Michael Chanf92905d2006-06-29 20:14:29 -07008069
8070 tw32(RCVBDI_STD_THRESH, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008071
8072 /* Initialize TG3_BDINFO's at:
8073 * RCVDBDI_STD_BD: standard eth size rx ring
8074 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8075 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8076 *
8077 * like so:
8078 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8079 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8080 * ring attribute flags
8081 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8082 *
8083 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8084 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8085 *
8086 * The size of each ring is fixed in the firmware, but the location is
8087 * configurable.
8088 */
8089 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008090 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008091 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008092 ((u64) tpr->rx_std_mapping & 0xffffffff));
Matt Carlson0a58d662011-04-05 14:22:45 +00008093 if (!(tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00008094 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8095 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008096
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008097 /* Disable the mini ring */
8098 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008099 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8100 BDINFO_FLAGS_DISABLED);
8101
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008102 /* Program the jumbo buffer descriptor ring control
8103 * blocks on those devices that have them.
8104 */
Matt Carlsonbb18bb92011-03-09 16:58:19 +00008105 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson4d163b72011-01-25 15:58:48 +00008106 ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
8107 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008108 /* Setup replenish threshold. */
8109 tw32(RCVBDI_JUMBO_THRESH, tp->rx_jumbo_pending / 8);
8110
Michael Chan0f893dc2005-07-25 12:30:38 -07008111 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008112 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008113 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008114 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008115 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008116 val = TG3_RX_JMB_RING_SIZE(tp) <<
8117 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008118 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008119 val | BDINFO_FLAGS_USE_EXT_RECV);
Matt Carlsona50d0792010-06-05 17:24:37 +00008120 if (!(tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) ||
8121 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson87668d32009-11-13 13:03:34 +00008122 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8123 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008124 } else {
8125 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8126 BDINFO_FLAGS_DISABLED);
8127 }
8128
Matt Carlson1407deb2011-04-05 14:22:44 +00008129 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008130 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsonde9f5232011-04-05 14:22:43 +00008131 val = TG3_RX_STD_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008132 else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008133 val = TG3_RX_STD_MAX_SIZE_5717;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008134 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8135 val |= (TG3_RX_STD_DMA_SZ << 2);
8136 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008137 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008138 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008139 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008140
8141 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008142
Matt Carlson411da642009-11-13 13:03:46 +00008143 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00008144 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008145
Matt Carlson411da642009-11-13 13:03:46 +00008146 tpr->rx_jmb_prod_idx = (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) ?
Matt Carlson21f581a2009-08-28 14:00:25 +00008147 tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00008148 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008149
Matt Carlson1407deb2011-04-05 14:22:44 +00008150 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008151 tw32(STD_REPLENISH_LWM, 32);
8152 tw32(JMB_REPLENISH_LWM, 16);
8153 }
8154
Matt Carlson2d31eca2009-09-01 12:53:31 +00008155 tg3_rings_reset(tp);
8156
Linus Torvalds1da177e2005-04-16 15:20:36 -07008157 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008158 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008159
8160 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008161 tw32(MAC_RX_MTU_SIZE,
8162 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008163
8164 /* The slot time is changed by tg3_setup_phy if we
8165 * run at gigabit with half duplex.
8166 */
8167 tw32(MAC_TX_LENGTHS,
8168 (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8169 (6 << TX_LENGTHS_IPG_SHIFT) |
8170 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
8171
8172 /* Receive rules. */
8173 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8174 tw32(RCVLPC_CONFIG, 0x0181);
8175
8176 /* Calculate RDMAC_MODE setting early, we need it to determine
8177 * the RCVLPC_STATE_ENABLE mask.
8178 */
8179 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8180 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8181 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8182 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8183 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008184
Matt Carlsondeabaac2010-11-24 08:31:50 +00008185 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008186 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8187
Matt Carlson57e69832008-05-25 23:48:31 -07008188 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008189 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8190 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008191 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8192 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8193 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8194
Matt Carlsonc5908932011-03-09 16:58:25 +00008195 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8196 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008197 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008198 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008199 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8200 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
8201 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
8202 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8203 }
8204 }
8205
Michael Chan85e94ce2005-04-21 17:05:28 -07008206 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
8207 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8208
Linus Torvalds1da177e2005-04-16 15:20:36 -07008209 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlson027455a2008-12-21 20:19:30 -08008210 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8211
Matt Carlsone849cdc2009-11-13 13:03:38 +00008212 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
8213 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008214 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8215 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008216
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008217 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8218 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8219 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8220 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson1407deb2011-04-05 14:22:44 +00008221 (tp->tg3_flags3 & TG3_FLG3_57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008222 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00008223 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8224 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008225 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8226 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8227 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8228 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8229 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8230 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008231 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008232 tw32(TG3_RDMA_RSRVCTRL_REG,
8233 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8234 }
8235
Matt Carlsond78b59f2011-04-05 14:22:46 +00008236 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8237 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00008238 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8239 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8240 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8241 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8242 }
8243
Linus Torvalds1da177e2005-04-16 15:20:36 -07008244 /* Receive/send statistics. */
Michael Chan16613942006-06-29 20:15:13 -07008245 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
8246 val = tr32(RCVLPC_STATS_ENABLE);
8247 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8248 tw32(RCVLPC_STATS_ENABLE, val);
8249 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
8250 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008251 val = tr32(RCVLPC_STATS_ENABLE);
8252 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8253 tw32(RCVLPC_STATS_ENABLE, val);
8254 } else {
8255 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8256 }
8257 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8258 tw32(SNDDATAI_STATSENAB, 0xffffff);
8259 tw32(SNDDATAI_STATSCTRL,
8260 (SNDDATAI_SCTRL_ENABLE |
8261 SNDDATAI_SCTRL_FASTUPD));
8262
8263 /* Setup host coalescing engine. */
8264 tw32(HOSTCC_MODE, 0);
8265 for (i = 0; i < 2000; i++) {
8266 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8267 break;
8268 udelay(10);
8269 }
8270
Michael Chand244c892005-07-05 14:42:33 -07008271 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008272
Linus Torvalds1da177e2005-04-16 15:20:36 -07008273 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
8274 /* Status/statistics block address. See tg3_timer,
8275 * the tg3_periodic_fetch_stats call there, and
8276 * tg3_get_stats to see how this works for 5705/5750 chips.
8277 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008278 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8279 ((u64) tp->stats_mapping >> 32));
8280 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8281 ((u64) tp->stats_mapping & 0xffffffff));
8282 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008283
Linus Torvalds1da177e2005-04-16 15:20:36 -07008284 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008285
8286 /* Clear statistics and status block memory areas */
8287 for (i = NIC_SRAM_STATS_BLK;
8288 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8289 i += sizeof(u32)) {
8290 tg3_write_mem(tp, i, 0);
8291 udelay(40);
8292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008293 }
8294
8295 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8296
8297 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8298 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
8299 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8300 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8301
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008302 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8303 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07008304 /* reset to prevent losing 1st rx packet intermittently */
8305 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8306 udelay(10);
8307 }
8308
Matt Carlson3bda1252008-08-15 14:08:22 -07008309 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008310 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -07008311 else
8312 tp->mac_mode = 0;
8313 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07008314 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008315 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008316 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008317 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8318 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008319 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8320 udelay(40);
8321
Michael Chan314fba32005-04-21 17:07:04 -07008322 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Michael Chan9d26e212006-12-07 00:21:14 -08008323 * If TG3_FLG2_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008324 * register to preserve the GPIO settings for LOMs. The GPIOs,
8325 * whether used as inputs or outputs, are set by boot code after
8326 * reset.
8327 */
Michael Chan9d26e212006-12-07 00:21:14 -08008328 if (!(tp->tg3_flags2 & TG3_FLG2_IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008329 u32 gpio_mask;
8330
Michael Chan9d26e212006-12-07 00:21:14 -08008331 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8332 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8333 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008334
8335 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8336 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8337 GRC_LCLCTRL_GPIO_OUTPUT3;
8338
Michael Chanaf36e6b2006-03-23 01:28:06 -08008339 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8340 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8341
Gary Zambranoaaf84462007-05-05 11:51:45 -07008342 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008343 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8344
8345 /* GPIO1 must be driven high for eeprom write protect */
Michael Chan9d26e212006-12-07 00:21:14 -08008346 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT)
8347 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8348 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008350 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8351 udelay(100);
8352
Matt Carlson0583d522011-01-25 15:58:50 +00008353 if ((tp->tg3_flags2 & TG3_FLG2_USING_MSIX) &&
8354 tp->irq_cnt > 1) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008355 val = tr32(MSGINT_MODE);
8356 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
8357 tw32(MSGINT_MODE, val);
8358 }
8359
Linus Torvalds1da177e2005-04-16 15:20:36 -07008360 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
8361 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8362 udelay(40);
8363 }
8364
8365 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8366 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8367 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8368 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8369 WDMAC_MODE_LNGREAD_ENAB);
8370
Matt Carlsonc5908932011-03-09 16:58:25 +00008371 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8372 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Matt Carlson29ea0952009-08-25 10:07:54 +00008373 if ((tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008374 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8375 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8376 /* nothing */
8377 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Matt Carlsonc5908932011-03-09 16:58:25 +00008378 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008379 val |= WDMAC_MODE_RX_ACCEL;
8380 }
8381 }
8382
Michael Chand9ab5ad12006-03-20 22:27:35 -08008383 /* Enable host coalescing bug fix */
Matt Carlson321d32a2008-11-21 17:22:19 -08008384 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlsonf51f3562008-05-25 23:45:08 -07008385 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -08008386
Matt Carlson788a0352009-11-02 14:26:03 +00008387 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8388 val |= WDMAC_MODE_BURST_ALL_DATA;
8389
Linus Torvalds1da177e2005-04-16 15:20:36 -07008390 tw32_f(WDMAC_MODE, val);
8391 udelay(40);
8392
Matt Carlson9974a352007-10-07 23:27:28 -07008393 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
8394 u16 pcix_cmd;
8395
8396 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8397 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008398 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008399 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8400 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008401 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008402 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8403 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008404 }
Matt Carlson9974a352007-10-07 23:27:28 -07008405 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8406 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008407 }
8408
8409 tw32_f(RDMAC_MODE, rdmac_mode);
8410 udelay(40);
8411
8412 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
8413 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8414 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008415
8416 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8417 tw32(SNDDATAC_MODE,
8418 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8419 else
8420 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8421
Linus Torvalds1da177e2005-04-16 15:20:36 -07008422 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8423 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008424 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Matt Carlsonde9f5232011-04-05 14:22:43 +00008425 if (tp->tg3_flags3 & TG3_FLG3_LRG_PROD_RING_CAP)
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008426 val |= RCVDBDI_MODE_LRG_RING_SZ;
8427 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008428 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008429 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
8430 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008431 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Matt Carlson19cfaec2009-12-03 08:36:20 +00008432 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008433 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8434 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008435 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8436
8437 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8438 err = tg3_load_5701_a0_firmware_fix(tp);
8439 if (err)
8440 return err;
8441 }
8442
Linus Torvalds1da177e2005-04-16 15:20:36 -07008443 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
8444 err = tg3_load_tso_firmware(tp);
8445 if (err)
8446 return err;
8447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008448
8449 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonb1d05212010-06-05 17:24:31 +00008450 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
8451 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8452 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008453 tw32_f(MAC_TX_MODE, tp->tx_mode);
8454 udelay(100);
8455
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008456 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) {
8457 u32 reg = MAC_RSS_INDIR_TBL_0;
8458 u8 *ent = (u8 *)&val;
8459
8460 /* Setup the indirection table */
8461 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8462 int idx = i % sizeof(val);
8463
Matt Carlson5efeeea2010-07-11 09:31:40 +00008464 ent[idx] = i % (tp->irq_cnt - 1);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008465 if (idx == sizeof(val) - 1) {
8466 tw32(reg, val);
8467 reg += 4;
8468 }
8469 }
8470
8471 /* Setup the "secret" hash key. */
8472 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8473 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8474 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8475 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8476 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8477 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8478 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8479 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8480 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8481 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8482 }
8483
Linus Torvalds1da177e2005-04-16 15:20:36 -07008484 tp->rx_mode = RX_MODE_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08008485 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chanaf36e6b2006-03-23 01:28:06 -08008486 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8487
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008488 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)
8489 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8490 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8491 RX_MODE_RSS_IPV6_HASH_EN |
8492 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8493 RX_MODE_RSS_IPV4_HASH_EN |
8494 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8495
Linus Torvalds1da177e2005-04-16 15:20:36 -07008496 tw32_f(MAC_RX_MODE, tp->rx_mode);
8497 udelay(10);
8498
Linus Torvalds1da177e2005-04-16 15:20:36 -07008499 tw32(MAC_LED_CTRL, tp->led_ctrl);
8500
8501 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008502 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008503 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8504 udelay(10);
8505 }
8506 tw32_f(MAC_RX_MODE, tp->rx_mode);
8507 udelay(10);
8508
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008509 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008510 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008511 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008512 /* Set drive transmission level to 1.2V */
8513 /* only if the signal pre-emphasis bit is not set */
8514 val = tr32(MAC_SERDES_CFG);
8515 val &= 0xfffff000;
8516 val |= 0x880;
8517 tw32(MAC_SERDES_CFG, val);
8518 }
8519 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8520 tw32(MAC_SERDES_CFG, 0x616000);
8521 }
8522
8523 /* Prevent chip from dropping frames when flow control
8524 * is enabled.
8525 */
Matt Carlson666bc832010-01-20 16:58:03 +00008526 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8527 val = 1;
8528 else
8529 val = 2;
8530 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008531
8532 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008533 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008534 /* Use hardware link auto-negotiation */
8535 tp->tg3_flags2 |= TG3_FLG2_HW_AUTONEG;
8536 }
8537
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008538 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Michael Chand4d2c552006-03-20 17:47:20 -08008539 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)) {
8540 u32 tmp;
8541
8542 tmp = tr32(SERDES_RX_CTRL);
8543 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
8544 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
8545 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
8546 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8547 }
8548
Matt Carlsondd477002008-05-25 23:45:58 -07008549 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
Matt Carlson80096062010-08-02 11:26:06 +00008550 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
8551 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07008552 tp->link_config.speed = tp->link_config.orig_speed;
8553 tp->link_config.duplex = tp->link_config.orig_duplex;
8554 tp->link_config.autoneg = tp->link_config.orig_autoneg;
8555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008556
Matt Carlsondd477002008-05-25 23:45:58 -07008557 err = tg3_setup_phy(tp, 0);
8558 if (err)
8559 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008560
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008561 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
8562 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07008563 u32 tmp;
8564
8565 /* Clear CRC stats. */
8566 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
8567 tg3_writephy(tp, MII_TG3_TEST1,
8568 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00008569 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07008570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008571 }
8572 }
8573
8574 __tg3_set_rx_mode(tp->dev);
8575
8576 /* Initialize receive rules. */
8577 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
8578 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
8579 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
8580 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
8581
Michael Chan4cf78e42005-07-25 12:29:19 -07008582 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Michael Chana4e2b342005-10-26 15:46:52 -07008583 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008584 limit = 8;
8585 else
8586 limit = 16;
8587 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
8588 limit -= 4;
8589 switch (limit) {
8590 case 16:
8591 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
8592 case 15:
8593 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
8594 case 14:
8595 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
8596 case 13:
8597 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
8598 case 12:
8599 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
8600 case 11:
8601 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
8602 case 10:
8603 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
8604 case 9:
8605 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
8606 case 8:
8607 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
8608 case 7:
8609 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
8610 case 6:
8611 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
8612 case 5:
8613 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
8614 case 4:
8615 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
8616 case 3:
8617 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
8618 case 2:
8619 case 1:
8620
8621 default:
8622 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008624
Matt Carlson9ce768e2007-10-11 19:49:11 -07008625 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
8626 /* Write our heartbeat update interval to APE. */
8627 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
8628 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07008629
Linus Torvalds1da177e2005-04-16 15:20:36 -07008630 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
8631
Linus Torvalds1da177e2005-04-16 15:20:36 -07008632 return 0;
8633}
8634
8635/* Called at device open time to get the chip ready for
8636 * packet processing. Invoked with tp->lock held.
8637 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008638static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008639{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008640 tg3_switch_clocks(tp);
8641
8642 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
8643
Matt Carlson2f751b62008-08-04 23:17:34 -07008644 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008645}
8646
8647#define TG3_STAT_ADD32(PSTAT, REG) \
8648do { u32 __val = tr32(REG); \
8649 (PSTAT)->low += __val; \
8650 if ((PSTAT)->low < __val) \
8651 (PSTAT)->high += 1; \
8652} while (0)
8653
8654static void tg3_periodic_fetch_stats(struct tg3 *tp)
8655{
8656 struct tg3_hw_stats *sp = tp->hw_stats;
8657
8658 if (!netif_carrier_ok(tp->dev))
8659 return;
8660
8661 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
8662 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
8663 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
8664 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
8665 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
8666 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
8667 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
8668 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
8669 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
8670 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
8671 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
8672 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
8673 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
8674
8675 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
8676 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
8677 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
8678 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
8679 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
8680 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
8681 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
8682 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
8683 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
8684 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
8685 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
8686 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
8687 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
8688 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07008689
8690 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
8691 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
8692 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008693}
8694
8695static void tg3_timer(unsigned long __opaque)
8696{
8697 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008698
Michael Chanf475f162006-03-27 23:20:14 -08008699 if (tp->irq_sync)
8700 goto restart_timer;
8701
David S. Millerf47c11e2005-06-24 20:18:35 -07008702 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008703
David S. Millerfac9b832005-05-18 22:46:34 -07008704 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
8705 /* All of this garbage is because when using non-tagged
8706 * IRQ status the mailbox/status_block protocol the chip
8707 * uses with the cpu is race prone.
8708 */
Matt Carlson898a56f2009-08-28 14:02:40 +00008709 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07008710 tw32(GRC_LOCAL_CTRL,
8711 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
8712 } else {
8713 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008714 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07008715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008716
David S. Millerfac9b832005-05-18 22:46:34 -07008717 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
8718 tp->tg3_flags2 |= TG3_FLG2_RESTART_TIMER;
David S. Millerf47c11e2005-06-24 20:18:35 -07008719 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07008720 schedule_work(&tp->reset_task);
8721 return;
8722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008723 }
8724
Linus Torvalds1da177e2005-04-16 15:20:36 -07008725 /* This part only runs once per second. */
8726 if (!--tp->timer_counter) {
David S. Millerfac9b832005-05-18 22:46:34 -07008727 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
8728 tg3_periodic_fetch_stats(tp);
8729
Matt Carlson52b02d02010-10-14 10:37:41 +00008730 if (tp->setlpicnt && !--tp->setlpicnt) {
8731 u32 val = tr32(TG3_CPMU_EEE_MODE);
8732 tw32(TG3_CPMU_EEE_MODE,
8733 val | TG3_CPMU_EEEMD_LPI_ENABLE);
8734 }
8735
Linus Torvalds1da177e2005-04-16 15:20:36 -07008736 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
8737 u32 mac_stat;
8738 int phy_event;
8739
8740 mac_stat = tr32(MAC_STATUS);
8741
8742 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008743 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008744 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
8745 phy_event = 1;
8746 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
8747 phy_event = 1;
8748
8749 if (phy_event)
8750 tg3_setup_phy(tp, 0);
8751 } else if (tp->tg3_flags & TG3_FLAG_POLL_SERDES) {
8752 u32 mac_stat = tr32(MAC_STATUS);
8753 int need_setup = 0;
8754
8755 if (netif_carrier_ok(tp->dev) &&
8756 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
8757 need_setup = 1;
8758 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00008759 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008760 (mac_stat & (MAC_STATUS_PCS_SYNCED |
8761 MAC_STATUS_SIGNAL_DET))) {
8762 need_setup = 1;
8763 }
8764 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07008765 if (!tp->serdes_counter) {
8766 tw32_f(MAC_MODE,
8767 (tp->mac_mode &
8768 ~MAC_MODE_PORT_MODE_MASK));
8769 udelay(40);
8770 tw32_f(MAC_MODE, tp->mac_mode);
8771 udelay(40);
8772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008773 tg3_setup_phy(tp, 0);
8774 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008775 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson2138c002010-07-11 09:31:43 +00008776 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07008777 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00008778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008779
8780 tp->timer_counter = tp->timer_multiplier;
8781 }
8782
Michael Chan130b8e42006-09-27 16:00:40 -07008783 /* Heartbeat is only sent once every 2 seconds.
8784 *
8785 * The heartbeat is to tell the ASF firmware that the host
8786 * driver is still alive. In the event that the OS crashes,
8787 * ASF needs to reset the hardware to free up the FIFO space
8788 * that may be filled with rx packets destined for the host.
8789 * If the FIFO is full, ASF will no longer function properly.
8790 *
8791 * Unintended resets have been reported on real time kernels
8792 * where the timer doesn't run on time. Netpoll will also have
8793 * same problem.
8794 *
8795 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
8796 * to check the ring condition when the heartbeat is expiring
8797 * before doing the reset. This will prevent most unintended
8798 * resets.
8799 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008800 if (!--tp->asf_counter) {
Matt Carlsonbc7959b2008-08-15 14:08:55 -07008801 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
8802 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07008803 tg3_wait_for_event_ack(tp);
8804
Michael Chanbbadf502006-04-06 21:46:34 -07008805 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07008806 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07008807 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00008808 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
8809 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008810
8811 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008812 }
8813 tp->asf_counter = tp->asf_multiplier;
8814 }
8815
David S. Millerf47c11e2005-06-24 20:18:35 -07008816 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008817
Michael Chanf475f162006-03-27 23:20:14 -08008818restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07008819 tp->timer.expires = jiffies + tp->timer_offset;
8820 add_timer(&tp->timer);
8821}
8822
Matt Carlson4f125f42009-09-01 12:55:02 +00008823static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08008824{
David Howells7d12e782006-10-05 14:55:46 +01008825 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008826 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00008827 char *name;
8828 struct tg3_napi *tnapi = &tp->napi[irq_num];
8829
8830 if (tp->irq_cnt == 1)
8831 name = tp->dev->name;
8832 else {
8833 name = &tnapi->irq_lbl[0];
8834 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
8835 name[IFNAMSIZ-1] = 0;
8836 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008837
Matt Carlson679563f2009-09-01 12:55:46 +00008838 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08008839 fn = tg3_msi;
8840 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
8841 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00008842 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008843 } else {
8844 fn = tg3_interrupt;
8845 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
8846 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00008847 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008848 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008849
8850 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008851}
8852
Michael Chan79381092005-04-21 17:13:59 -07008853static int tg3_test_interrupt(struct tg3 *tp)
8854{
Matt Carlson09943a12009-08-28 14:01:57 +00008855 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07008856 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07008857 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008858 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07008859
Michael Chand4bc3922005-05-29 14:59:20 -07008860 if (!netif_running(dev))
8861 return -ENODEV;
8862
Michael Chan79381092005-04-21 17:13:59 -07008863 tg3_disable_ints(tp);
8864
Matt Carlson4f125f42009-09-01 12:55:02 +00008865 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008866
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008867 /*
8868 * Turn off MSI one shot mode. Otherwise this test has no
8869 * observable way to know whether the interrupt was delivered.
8870 */
Matt Carlson1407deb2011-04-05 14:22:44 +00008871 if ((tp->tg3_flags3 & TG3_FLG3_57765_PLUS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008872 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8873 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
8874 tw32(MSGINT_MODE, val);
8875 }
8876
Matt Carlson4f125f42009-09-01 12:55:02 +00008877 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00008878 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008879 if (err)
8880 return err;
8881
Matt Carlson898a56f2009-08-28 14:02:40 +00008882 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07008883 tg3_enable_ints(tp);
8884
8885 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008886 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07008887
8888 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07008889 u32 int_mbox, misc_host_ctrl;
8890
Matt Carlson898a56f2009-08-28 14:02:40 +00008891 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07008892 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
8893
8894 if ((int_mbox != 0) ||
8895 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
8896 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07008897 break;
Michael Chanb16250e2006-09-27 16:10:14 -07008898 }
8899
Michael Chan79381092005-04-21 17:13:59 -07008900 msleep(10);
8901 }
8902
8903 tg3_disable_ints(tp);
8904
Matt Carlson4f125f42009-09-01 12:55:02 +00008905 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008906
Matt Carlson4f125f42009-09-01 12:55:02 +00008907 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07008908
8909 if (err)
8910 return err;
8911
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008912 if (intr_ok) {
8913 /* Reenable MSI one shot mode. */
Matt Carlson1407deb2011-04-05 14:22:44 +00008914 if ((tp->tg3_flags3 & TG3_FLG3_57765_PLUS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008915 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8916 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
8917 tw32(MSGINT_MODE, val);
8918 }
Michael Chan79381092005-04-21 17:13:59 -07008919 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008920 }
Michael Chan79381092005-04-21 17:13:59 -07008921
8922 return -EIO;
8923}
8924
8925/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
8926 * successfully restored
8927 */
8928static int tg3_test_msi(struct tg3 *tp)
8929{
Michael Chan79381092005-04-21 17:13:59 -07008930 int err;
8931 u16 pci_cmd;
8932
8933 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSI))
8934 return 0;
8935
8936 /* Turn off SERR reporting in case MSI terminates with Master
8937 * Abort.
8938 */
8939 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
8940 pci_write_config_word(tp->pdev, PCI_COMMAND,
8941 pci_cmd & ~PCI_COMMAND_SERR);
8942
8943 err = tg3_test_interrupt(tp);
8944
8945 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
8946
8947 if (!err)
8948 return 0;
8949
8950 /* other failures */
8951 if (err != -EIO)
8952 return err;
8953
8954 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00008955 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
8956 "to INTx mode. Please report this failure to the PCI "
8957 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07008958
Matt Carlson4f125f42009-09-01 12:55:02 +00008959 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00008960
Michael Chan79381092005-04-21 17:13:59 -07008961 pci_disable_msi(tp->pdev);
8962
8963 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI;
Andre Detschdc8bf1b2010-04-26 07:27:07 +00008964 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07008965
Matt Carlson4f125f42009-09-01 12:55:02 +00008966 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07008967 if (err)
8968 return err;
8969
8970 /* Need to reset the chip because the MSI cycle may have terminated
8971 * with Master Abort.
8972 */
David S. Millerf47c11e2005-06-24 20:18:35 -07008973 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07008974
Michael Chan944d9802005-05-29 14:57:48 -07008975 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008976 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07008977
David S. Millerf47c11e2005-06-24 20:18:35 -07008978 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07008979
8980 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00008981 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07008982
8983 return err;
8984}
8985
Matt Carlson9e9fd122009-01-19 16:57:45 -08008986static int tg3_request_firmware(struct tg3 *tp)
8987{
8988 const __be32 *fw_data;
8989
8990 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00008991 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
8992 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08008993 return -ENOENT;
8994 }
8995
8996 fw_data = (void *)tp->fw->data;
8997
8998 /* Firmware blob starts with version numbers, followed by
8999 * start address and _full_ length including BSS sections
9000 * (which must be longer than the actual data, of course
9001 */
9002
9003 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9004 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009005 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9006 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009007 release_firmware(tp->fw);
9008 tp->fw = NULL;
9009 return -EINVAL;
9010 }
9011
9012 /* We no longer need firmware; we have it. */
9013 tp->fw_needed = NULL;
9014 return 0;
9015}
9016
Matt Carlson679563f2009-09-01 12:55:46 +00009017static bool tg3_enable_msix(struct tg3 *tp)
9018{
9019 int i, rc, cpus = num_online_cpus();
9020 struct msix_entry msix_ent[tp->irq_max];
9021
9022 if (cpus == 1)
9023 /* Just fallback to the simpler MSI mode. */
9024 return false;
9025
9026 /*
9027 * We want as many rx rings enabled as there are cpus.
9028 * The first MSIX vector only deals with link interrupts, etc,
9029 * so we add one to the number of vectors we are requesting.
9030 */
9031 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
9032
9033 for (i = 0; i < tp->irq_max; i++) {
9034 msix_ent[i].entry = i;
9035 msix_ent[i].vector = 0;
9036 }
9037
9038 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009039 if (rc < 0) {
9040 return false;
9041 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009042 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9043 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009044 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9045 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009046 tp->irq_cnt = rc;
9047 }
9048
9049 for (i = 0; i < tp->irq_max; i++)
9050 tp->napi[i].irq_vec = msix_ent[i].vector;
9051
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009052 netif_set_real_num_tx_queues(tp->dev, 1);
9053 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9054 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9055 pci_disable_msix(tp->pdev);
9056 return false;
9057 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009058
9059 if (tp->irq_cnt > 1) {
Matt Carlson2430b032010-06-05 17:24:34 +00009060 tp->tg3_flags3 |= TG3_FLG3_ENABLE_RSS;
Matt Carlsond78b59f2011-04-05 14:22:46 +00009061
9062 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9063 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb92b9042010-11-24 08:31:51 +00009064 tp->tg3_flags3 |= TG3_FLG3_ENABLE_TSS;
9065 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9066 }
9067 }
Matt Carlson2430b032010-06-05 17:24:34 +00009068
Matt Carlson679563f2009-09-01 12:55:46 +00009069 return true;
9070}
9071
Matt Carlson07b01732009-08-28 14:01:15 +00009072static void tg3_ints_init(struct tg3 *tp)
9073{
Matt Carlson679563f2009-09-01 12:55:46 +00009074 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI_OR_MSIX) &&
9075 !(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009076 /* All MSI supporting chips should support tagged
9077 * status. Assert that this is the case.
9078 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009079 netdev_warn(tp->dev,
9080 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009081 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009082 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009083
Matt Carlson679563f2009-09-01 12:55:46 +00009084 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) && tg3_enable_msix(tp))
9085 tp->tg3_flags2 |= TG3_FLG2_USING_MSIX;
9086 else if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI) &&
9087 pci_enable_msi(tp->pdev) == 0)
9088 tp->tg3_flags2 |= TG3_FLG2_USING_MSI;
9089
9090 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
9091 u32 msi_mode = tr32(MSGINT_MODE);
Matt Carlson0583d522011-01-25 15:58:50 +00009092 if ((tp->tg3_flags2 & TG3_FLG2_USING_MSIX) &&
9093 tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009094 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson679563f2009-09-01 12:55:46 +00009095 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9096 }
9097defcfg:
9098 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSIX)) {
9099 tp->irq_cnt = 1;
9100 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009101 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009102 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009103 }
Matt Carlson07b01732009-08-28 14:01:15 +00009104}
9105
9106static void tg3_ints_fini(struct tg3 *tp)
9107{
Matt Carlson679563f2009-09-01 12:55:46 +00009108 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
9109 pci_disable_msix(tp->pdev);
9110 else if (tp->tg3_flags2 & TG3_FLG2_USING_MSI)
9111 pci_disable_msi(tp->pdev);
9112 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI_OR_MSIX;
Matt Carlson774ee752010-08-02 11:25:56 +00009113 tp->tg3_flags3 &= ~(TG3_FLG3_ENABLE_RSS | TG3_FLG3_ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009114}
9115
Linus Torvalds1da177e2005-04-16 15:20:36 -07009116static int tg3_open(struct net_device *dev)
9117{
9118 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009119 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009120
Matt Carlson9e9fd122009-01-19 16:57:45 -08009121 if (tp->fw_needed) {
9122 err = tg3_request_firmware(tp);
9123 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9124 if (err)
9125 return err;
9126 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009127 netdev_warn(tp->dev, "TSO capability disabled\n");
Matt Carlson9e9fd122009-01-19 16:57:45 -08009128 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
9129 } else if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009130 netdev_notice(tp->dev, "TSO capability restored\n");
Matt Carlson9e9fd122009-01-19 16:57:45 -08009131 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
9132 }
9133 }
9134
Michael Chanc49a1562006-12-17 17:07:29 -08009135 netif_carrier_off(tp->dev);
9136
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009137 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -07009138 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009139 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009140
9141 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009142
Linus Torvalds1da177e2005-04-16 15:20:36 -07009143 tg3_disable_ints(tp);
9144 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
9145
David S. Millerf47c11e2005-06-24 20:18:35 -07009146 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009147
Matt Carlson679563f2009-09-01 12:55:46 +00009148 /*
9149 * Setup interrupts first so we know how
9150 * many NAPI resources to allocate
9151 */
9152 tg3_ints_init(tp);
9153
Linus Torvalds1da177e2005-04-16 15:20:36 -07009154 /* The placement of this call is tied
9155 * to the setup and use of Host TX descriptors.
9156 */
9157 err = tg3_alloc_consistent(tp);
9158 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009159 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009160
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009161 tg3_napi_init(tp);
9162
Matt Carlsonfed97812009-09-01 13:10:19 +00009163 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07009164
Matt Carlson4f125f42009-09-01 12:55:02 +00009165 for (i = 0; i < tp->irq_cnt; i++) {
9166 struct tg3_napi *tnapi = &tp->napi[i];
9167 err = tg3_request_irq(tp, i);
9168 if (err) {
9169 for (i--; i >= 0; i--)
9170 free_irq(tnapi->irq_vec, tnapi);
9171 break;
9172 }
9173 }
Matt Carlson07b01732009-08-28 14:01:15 +00009174
9175 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009176 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00009177
David S. Millerf47c11e2005-06-24 20:18:35 -07009178 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009179
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009180 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009181 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07009182 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009183 tg3_free_rings(tp);
9184 } else {
David S. Millerfac9b832005-05-18 22:46:34 -07009185 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
9186 tp->timer_offset = HZ;
9187 else
9188 tp->timer_offset = HZ / 10;
9189
9190 BUG_ON(tp->timer_offset > HZ);
9191 tp->timer_counter = tp->timer_multiplier =
9192 (HZ / tp->timer_offset);
9193 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07009194 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009195
9196 init_timer(&tp->timer);
9197 tp->timer.expires = jiffies + tp->timer_offset;
9198 tp->timer.data = (unsigned long) tp;
9199 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009200 }
9201
David S. Millerf47c11e2005-06-24 20:18:35 -07009202 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009203
Matt Carlson07b01732009-08-28 14:01:15 +00009204 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009205 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009206
Michael Chan79381092005-04-21 17:13:59 -07009207 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
9208 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07009209
Michael Chan79381092005-04-21 17:13:59 -07009210 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009211 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07009212 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07009213 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07009214 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009215
Matt Carlson679563f2009-09-01 12:55:46 +00009216 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07009217 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009218
Matt Carlson1407deb2011-04-05 14:22:44 +00009219 if (!(tp->tg3_flags3 & TG3_FLG3_57765_PLUS) &&
Matt Carlsonc885e822010-08-02 11:25:57 +00009220 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009221 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009222
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009223 tw32(PCIE_TRANSACTION_CFG,
9224 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009225 }
Michael Chan79381092005-04-21 17:13:59 -07009226 }
9227
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009228 tg3_phy_start(tp);
9229
David S. Millerf47c11e2005-06-24 20:18:35 -07009230 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009231
Michael Chan79381092005-04-21 17:13:59 -07009232 add_timer(&tp->timer);
9233 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009234 tg3_enable_ints(tp);
9235
David S. Millerf47c11e2005-06-24 20:18:35 -07009236 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009237
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009238 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009239
9240 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00009241
Matt Carlson679563f2009-09-01 12:55:46 +00009242err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00009243 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9244 struct tg3_napi *tnapi = &tp->napi[i];
9245 free_irq(tnapi->irq_vec, tnapi);
9246 }
Matt Carlson07b01732009-08-28 14:01:15 +00009247
Matt Carlson679563f2009-09-01 12:55:46 +00009248err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00009249 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009250 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009251 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00009252
9253err_out1:
9254 tg3_ints_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009255 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009256}
9257
Eric Dumazet511d2222010-07-07 20:44:24 +00009258static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
9259 struct rtnl_link_stats64 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009260static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
9261
9262static int tg3_close(struct net_device *dev)
9263{
Matt Carlson4f125f42009-09-01 12:55:02 +00009264 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009265 struct tg3 *tp = netdev_priv(dev);
9266
Matt Carlsonfed97812009-09-01 13:10:19 +00009267 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07009268 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08009269
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009270 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009271
9272 del_timer_sync(&tp->timer);
9273
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009274 tg3_phy_stop(tp);
9275
David S. Millerf47c11e2005-06-24 20:18:35 -07009276 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009277
9278 tg3_disable_ints(tp);
9279
Michael Chan944d9802005-05-29 14:57:48 -07009280 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009281 tg3_free_rings(tp);
Michael Chan5cf64b8a2007-05-05 12:11:21 -07009282 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009283
David S. Millerf47c11e2005-06-24 20:18:35 -07009284 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009285
Matt Carlson4f125f42009-09-01 12:55:02 +00009286 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9287 struct tg3_napi *tnapi = &tp->napi[i];
9288 free_irq(tnapi->irq_vec, tnapi);
9289 }
Matt Carlson07b01732009-08-28 14:01:15 +00009290
9291 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009292
Eric Dumazet511d2222010-07-07 20:44:24 +00009293 tg3_get_stats64(tp->dev, &tp->net_stats_prev);
9294
Linus Torvalds1da177e2005-04-16 15:20:36 -07009295 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9296 sizeof(tp->estats_prev));
9297
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009298 tg3_napi_fini(tp);
9299
Linus Torvalds1da177e2005-04-16 15:20:36 -07009300 tg3_free_consistent(tp);
9301
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009302 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -08009303
9304 netif_carrier_off(tp->dev);
9305
Linus Torvalds1da177e2005-04-16 15:20:36 -07009306 return 0;
9307}
9308
Eric Dumazet511d2222010-07-07 20:44:24 +00009309static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -07009310{
9311 return ((u64)val->high << 32) | ((u64)val->low);
9312}
9313
Eric Dumazet511d2222010-07-07 20:44:24 +00009314static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009315{
9316 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9317
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009318 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009319 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9320 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009321 u32 val;
9322
David S. Millerf47c11e2005-06-24 20:18:35 -07009323 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009324 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9325 tg3_writephy(tp, MII_TG3_TEST1,
9326 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009327 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009328 } else
9329 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009330 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009331
9332 tp->phy_crc_errors += val;
9333
9334 return tp->phy_crc_errors;
9335 }
9336
9337 return get_stat64(&hw_stats->rx_fcs_errors);
9338}
9339
9340#define ESTAT_ADD(member) \
9341 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +00009342 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009343
9344static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9345{
9346 struct tg3_ethtool_stats *estats = &tp->estats;
9347 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9348 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9349
9350 if (!hw_stats)
9351 return old_estats;
9352
9353 ESTAT_ADD(rx_octets);
9354 ESTAT_ADD(rx_fragments);
9355 ESTAT_ADD(rx_ucast_packets);
9356 ESTAT_ADD(rx_mcast_packets);
9357 ESTAT_ADD(rx_bcast_packets);
9358 ESTAT_ADD(rx_fcs_errors);
9359 ESTAT_ADD(rx_align_errors);
9360 ESTAT_ADD(rx_xon_pause_rcvd);
9361 ESTAT_ADD(rx_xoff_pause_rcvd);
9362 ESTAT_ADD(rx_mac_ctrl_rcvd);
9363 ESTAT_ADD(rx_xoff_entered);
9364 ESTAT_ADD(rx_frame_too_long_errors);
9365 ESTAT_ADD(rx_jabbers);
9366 ESTAT_ADD(rx_undersize_packets);
9367 ESTAT_ADD(rx_in_length_errors);
9368 ESTAT_ADD(rx_out_length_errors);
9369 ESTAT_ADD(rx_64_or_less_octet_packets);
9370 ESTAT_ADD(rx_65_to_127_octet_packets);
9371 ESTAT_ADD(rx_128_to_255_octet_packets);
9372 ESTAT_ADD(rx_256_to_511_octet_packets);
9373 ESTAT_ADD(rx_512_to_1023_octet_packets);
9374 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9375 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9376 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9377 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9378 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9379
9380 ESTAT_ADD(tx_octets);
9381 ESTAT_ADD(tx_collisions);
9382 ESTAT_ADD(tx_xon_sent);
9383 ESTAT_ADD(tx_xoff_sent);
9384 ESTAT_ADD(tx_flow_control);
9385 ESTAT_ADD(tx_mac_errors);
9386 ESTAT_ADD(tx_single_collisions);
9387 ESTAT_ADD(tx_mult_collisions);
9388 ESTAT_ADD(tx_deferred);
9389 ESTAT_ADD(tx_excessive_collisions);
9390 ESTAT_ADD(tx_late_collisions);
9391 ESTAT_ADD(tx_collide_2times);
9392 ESTAT_ADD(tx_collide_3times);
9393 ESTAT_ADD(tx_collide_4times);
9394 ESTAT_ADD(tx_collide_5times);
9395 ESTAT_ADD(tx_collide_6times);
9396 ESTAT_ADD(tx_collide_7times);
9397 ESTAT_ADD(tx_collide_8times);
9398 ESTAT_ADD(tx_collide_9times);
9399 ESTAT_ADD(tx_collide_10times);
9400 ESTAT_ADD(tx_collide_11times);
9401 ESTAT_ADD(tx_collide_12times);
9402 ESTAT_ADD(tx_collide_13times);
9403 ESTAT_ADD(tx_collide_14times);
9404 ESTAT_ADD(tx_collide_15times);
9405 ESTAT_ADD(tx_ucast_packets);
9406 ESTAT_ADD(tx_mcast_packets);
9407 ESTAT_ADD(tx_bcast_packets);
9408 ESTAT_ADD(tx_carrier_sense_errors);
9409 ESTAT_ADD(tx_discards);
9410 ESTAT_ADD(tx_errors);
9411
9412 ESTAT_ADD(dma_writeq_full);
9413 ESTAT_ADD(dma_write_prioq_full);
9414 ESTAT_ADD(rxbds_empty);
9415 ESTAT_ADD(rx_discards);
9416 ESTAT_ADD(rx_errors);
9417 ESTAT_ADD(rx_threshold_hit);
9418
9419 ESTAT_ADD(dma_readq_full);
9420 ESTAT_ADD(dma_read_prioq_full);
9421 ESTAT_ADD(tx_comp_queue_full);
9422
9423 ESTAT_ADD(ring_set_send_prod_index);
9424 ESTAT_ADD(ring_status_update);
9425 ESTAT_ADD(nic_irqs);
9426 ESTAT_ADD(nic_avoided_irqs);
9427 ESTAT_ADD(nic_tx_threshold_hit);
9428
9429 return estats;
9430}
9431
Eric Dumazet511d2222010-07-07 20:44:24 +00009432static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9433 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009434{
9435 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +00009436 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009437 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9438
9439 if (!hw_stats)
9440 return old_stats;
9441
9442 stats->rx_packets = old_stats->rx_packets +
9443 get_stat64(&hw_stats->rx_ucast_packets) +
9444 get_stat64(&hw_stats->rx_mcast_packets) +
9445 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009446
Linus Torvalds1da177e2005-04-16 15:20:36 -07009447 stats->tx_packets = old_stats->tx_packets +
9448 get_stat64(&hw_stats->tx_ucast_packets) +
9449 get_stat64(&hw_stats->tx_mcast_packets) +
9450 get_stat64(&hw_stats->tx_bcast_packets);
9451
9452 stats->rx_bytes = old_stats->rx_bytes +
9453 get_stat64(&hw_stats->rx_octets);
9454 stats->tx_bytes = old_stats->tx_bytes +
9455 get_stat64(&hw_stats->tx_octets);
9456
9457 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009458 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009459 stats->tx_errors = old_stats->tx_errors +
9460 get_stat64(&hw_stats->tx_errors) +
9461 get_stat64(&hw_stats->tx_mac_errors) +
9462 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9463 get_stat64(&hw_stats->tx_discards);
9464
9465 stats->multicast = old_stats->multicast +
9466 get_stat64(&hw_stats->rx_mcast_packets);
9467 stats->collisions = old_stats->collisions +
9468 get_stat64(&hw_stats->tx_collisions);
9469
9470 stats->rx_length_errors = old_stats->rx_length_errors +
9471 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9472 get_stat64(&hw_stats->rx_undersize_packets);
9473
9474 stats->rx_over_errors = old_stats->rx_over_errors +
9475 get_stat64(&hw_stats->rxbds_empty);
9476 stats->rx_frame_errors = old_stats->rx_frame_errors +
9477 get_stat64(&hw_stats->rx_align_errors);
9478 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9479 get_stat64(&hw_stats->tx_discards);
9480 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
9481 get_stat64(&hw_stats->tx_carrier_sense_errors);
9482
9483 stats->rx_crc_errors = old_stats->rx_crc_errors +
9484 calc_crc_errors(tp);
9485
John W. Linville4f63b872005-09-12 14:43:18 -07009486 stats->rx_missed_errors = old_stats->rx_missed_errors +
9487 get_stat64(&hw_stats->rx_discards);
9488
Eric Dumazetb0057c52010-10-10 19:55:52 +00009489 stats->rx_dropped = tp->rx_dropped;
9490
Linus Torvalds1da177e2005-04-16 15:20:36 -07009491 return stats;
9492}
9493
9494static inline u32 calc_crc(unsigned char *buf, int len)
9495{
9496 u32 reg;
9497 u32 tmp;
9498 int j, k;
9499
9500 reg = 0xffffffff;
9501
9502 for (j = 0; j < len; j++) {
9503 reg ^= buf[j];
9504
9505 for (k = 0; k < 8; k++) {
9506 tmp = reg & 0x01;
9507
9508 reg >>= 1;
9509
Matt Carlson859a588792010-04-05 10:19:28 +00009510 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009511 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009512 }
9513 }
9514
9515 return ~reg;
9516}
9517
9518static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9519{
9520 /* accept or reject all multicast frames */
9521 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9522 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9523 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9524 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9525}
9526
9527static void __tg3_set_rx_mode(struct net_device *dev)
9528{
9529 struct tg3 *tp = netdev_priv(dev);
9530 u32 rx_mode;
9531
9532 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9533 RX_MODE_KEEP_VLAN_TAG);
9534
Matt Carlsonbf933c82011-01-25 15:58:49 +00009535#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009536 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9537 * flag clear.
9538 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009539 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
9540 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9541#endif
9542
9543 if (dev->flags & IFF_PROMISC) {
9544 /* Promiscuous mode. */
9545 rx_mode |= RX_MODE_PROMISC;
9546 } else if (dev->flags & IFF_ALLMULTI) {
9547 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009548 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00009549 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009550 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009551 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009552 } else {
9553 /* Accept one or more multicast(s). */
Jiri Pirko22bedad32010-04-01 21:22:57 +00009554 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009555 u32 mc_filter[4] = { 0, };
9556 u32 regidx;
9557 u32 bit;
9558 u32 crc;
9559
Jiri Pirko22bedad32010-04-01 21:22:57 +00009560 netdev_for_each_mc_addr(ha, dev) {
9561 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009562 bit = ~crc & 0x7f;
9563 regidx = (bit & 0x60) >> 5;
9564 bit &= 0x1f;
9565 mc_filter[regidx] |= (1 << bit);
9566 }
9567
9568 tw32(MAC_HASH_REG_0, mc_filter[0]);
9569 tw32(MAC_HASH_REG_1, mc_filter[1]);
9570 tw32(MAC_HASH_REG_2, mc_filter[2]);
9571 tw32(MAC_HASH_REG_3, mc_filter[3]);
9572 }
9573
9574 if (rx_mode != tp->rx_mode) {
9575 tp->rx_mode = rx_mode;
9576 tw32_f(MAC_RX_MODE, rx_mode);
9577 udelay(10);
9578 }
9579}
9580
9581static void tg3_set_rx_mode(struct net_device *dev)
9582{
9583 struct tg3 *tp = netdev_priv(dev);
9584
Michael Chane75f7c92006-03-20 21:33:26 -08009585 if (!netif_running(dev))
9586 return;
9587
David S. Millerf47c11e2005-06-24 20:18:35 -07009588 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009589 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -07009590 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009591}
9592
9593#define TG3_REGDUMP_LEN (32 * 1024)
9594
9595static int tg3_get_regs_len(struct net_device *dev)
9596{
9597 return TG3_REGDUMP_LEN;
9598}
9599
9600static void tg3_get_regs(struct net_device *dev,
9601 struct ethtool_regs *regs, void *_p)
9602{
9603 u32 *p = _p;
9604 struct tg3 *tp = netdev_priv(dev);
9605 u8 *orig_p = _p;
9606 int i;
9607
9608 regs->version = 0;
9609
9610 memset(p, 0, TG3_REGDUMP_LEN);
9611
Matt Carlson80096062010-08-02 11:26:06 +00009612 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009613 return;
9614
David S. Millerf47c11e2005-06-24 20:18:35 -07009615 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009616
9617#define __GET_REG32(reg) (*(p)++ = tr32(reg))
Matt Carlsonbe98da62010-07-11 09:31:46 +00009618#define GET_REG32_LOOP(base, len) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07009619do { p = (u32 *)(orig_p + (base)); \
9620 for (i = 0; i < len; i += 4) \
9621 __GET_REG32((base) + i); \
9622} while (0)
9623#define GET_REG32_1(reg) \
9624do { p = (u32 *)(orig_p + (reg)); \
9625 __GET_REG32((reg)); \
9626} while (0)
9627
9628 GET_REG32_LOOP(TG3PCI_VENDOR, 0xb0);
9629 GET_REG32_LOOP(MAILBOX_INTERRUPT_0, 0x200);
9630 GET_REG32_LOOP(MAC_MODE, 0x4f0);
9631 GET_REG32_LOOP(SNDDATAI_MODE, 0xe0);
9632 GET_REG32_1(SNDDATAC_MODE);
9633 GET_REG32_LOOP(SNDBDS_MODE, 0x80);
9634 GET_REG32_LOOP(SNDBDI_MODE, 0x48);
9635 GET_REG32_1(SNDBDC_MODE);
9636 GET_REG32_LOOP(RCVLPC_MODE, 0x20);
9637 GET_REG32_LOOP(RCVLPC_SELLST_BASE, 0x15c);
9638 GET_REG32_LOOP(RCVDBDI_MODE, 0x0c);
9639 GET_REG32_LOOP(RCVDBDI_JUMBO_BD, 0x3c);
9640 GET_REG32_LOOP(RCVDBDI_BD_PROD_IDX_0, 0x44);
9641 GET_REG32_1(RCVDCC_MODE);
9642 GET_REG32_LOOP(RCVBDI_MODE, 0x20);
9643 GET_REG32_LOOP(RCVCC_MODE, 0x14);
9644 GET_REG32_LOOP(RCVLSC_MODE, 0x08);
9645 GET_REG32_1(MBFREE_MODE);
9646 GET_REG32_LOOP(HOSTCC_MODE, 0x100);
9647 GET_REG32_LOOP(MEMARB_MODE, 0x10);
9648 GET_REG32_LOOP(BUFMGR_MODE, 0x58);
9649 GET_REG32_LOOP(RDMAC_MODE, 0x08);
9650 GET_REG32_LOOP(WDMAC_MODE, 0x08);
Chris Elmquist091465d2005-12-20 13:25:19 -08009651 GET_REG32_1(RX_CPU_MODE);
9652 GET_REG32_1(RX_CPU_STATE);
9653 GET_REG32_1(RX_CPU_PGMCTR);
9654 GET_REG32_1(RX_CPU_HWBKPT);
9655 GET_REG32_1(TX_CPU_MODE);
9656 GET_REG32_1(TX_CPU_STATE);
9657 GET_REG32_1(TX_CPU_PGMCTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009658 GET_REG32_LOOP(GRCMBOX_INTERRUPT_0, 0x110);
9659 GET_REG32_LOOP(FTQ_RESET, 0x120);
9660 GET_REG32_LOOP(MSGINT_MODE, 0x0c);
9661 GET_REG32_1(DMAC_MODE);
9662 GET_REG32_LOOP(GRC_MODE, 0x4c);
9663 if (tp->tg3_flags & TG3_FLAG_NVRAM)
9664 GET_REG32_LOOP(NVRAM_CMD, 0x24);
9665
9666#undef __GET_REG32
9667#undef GET_REG32_LOOP
9668#undef GET_REG32_1
9669
David S. Millerf47c11e2005-06-24 20:18:35 -07009670 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009671}
9672
9673static int tg3_get_eeprom_len(struct net_device *dev)
9674{
9675 struct tg3 *tp = netdev_priv(dev);
9676
9677 return tp->nvram_size;
9678}
9679
Linus Torvalds1da177e2005-04-16 15:20:36 -07009680static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9681{
9682 struct tg3 *tp = netdev_priv(dev);
9683 int ret;
9684 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -08009685 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009686 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009687
Matt Carlsondf259d82009-04-20 06:57:14 +00009688 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
9689 return -EINVAL;
9690
Matt Carlson80096062010-08-02 11:26:06 +00009691 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009692 return -EAGAIN;
9693
Linus Torvalds1da177e2005-04-16 15:20:36 -07009694 offset = eeprom->offset;
9695 len = eeprom->len;
9696 eeprom->len = 0;
9697
9698 eeprom->magic = TG3_EEPROM_MAGIC;
9699
9700 if (offset & 3) {
9701 /* adjustments to start on required 4 byte boundary */
9702 b_offset = offset & 3;
9703 b_count = 4 - b_offset;
9704 if (b_count > len) {
9705 /* i.e. offset=1 len=2 */
9706 b_count = len;
9707 }
Matt Carlsona9dc5292009-02-25 14:25:30 +00009708 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009709 if (ret)
9710 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +00009711 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009712 len -= b_count;
9713 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009714 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009715 }
9716
9717 /* read bytes upto the last 4 byte boundary */
9718 pd = &data[eeprom->len];
9719 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00009720 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009721 if (ret) {
9722 eeprom->len += i;
9723 return ret;
9724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009725 memcpy(pd + i, &val, 4);
9726 }
9727 eeprom->len += i;
9728
9729 if (len & 3) {
9730 /* read last bytes not ending on 4 byte boundary */
9731 pd = &data[eeprom->len];
9732 b_count = len & 3;
9733 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009734 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009735 if (ret)
9736 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009737 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009738 eeprom->len += b_count;
9739 }
9740 return 0;
9741}
9742
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009743static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009744
9745static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9746{
9747 struct tg3 *tp = netdev_priv(dev);
9748 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009749 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009750 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009751 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009752
Matt Carlson80096062010-08-02 11:26:06 +00009753 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009754 return -EAGAIN;
9755
Matt Carlsondf259d82009-04-20 06:57:14 +00009756 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
9757 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009758 return -EINVAL;
9759
9760 offset = eeprom->offset;
9761 len = eeprom->len;
9762
9763 if ((b_offset = (offset & 3))) {
9764 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +00009765 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009766 if (ret)
9767 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009768 len += b_offset;
9769 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -07009770 if (len < 4)
9771 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009772 }
9773
9774 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -07009775 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009776 /* adjustments to end on required 4 byte boundary */
9777 odd_len = 1;
9778 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009779 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009780 if (ret)
9781 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009782 }
9783
9784 buf = data;
9785 if (b_offset || odd_len) {
9786 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009787 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009788 return -ENOMEM;
9789 if (b_offset)
9790 memcpy(buf, &start, 4);
9791 if (odd_len)
9792 memcpy(buf+len-4, &end, 4);
9793 memcpy(buf + b_offset, data, eeprom->len);
9794 }
9795
9796 ret = tg3_nvram_write_block(tp, offset, len, buf);
9797
9798 if (buf != data)
9799 kfree(buf);
9800
9801 return ret;
9802}
9803
9804static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9805{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009806 struct tg3 *tp = netdev_priv(dev);
9807
9808 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009809 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009810 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009811 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009812 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9813 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009814 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009815
Linus Torvalds1da177e2005-04-16 15:20:36 -07009816 cmd->supported = (SUPPORTED_Autoneg);
9817
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009818 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009819 cmd->supported |= (SUPPORTED_1000baseT_Half |
9820 SUPPORTED_1000baseT_Full);
9821
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009822 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009823 cmd->supported |= (SUPPORTED_100baseT_Half |
9824 SUPPORTED_100baseT_Full |
9825 SUPPORTED_10baseT_Half |
9826 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -08009827 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -07009828 cmd->port = PORT_TP;
9829 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009830 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -07009831 cmd->port = PORT_FIBRE;
9832 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009833
Linus Torvalds1da177e2005-04-16 15:20:36 -07009834 cmd->advertising = tp->link_config.advertising;
9835 if (netif_running(dev)) {
9836 cmd->speed = tp->link_config.active_speed;
9837 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson64c22182010-10-14 10:37:44 +00009838 } else {
9839 cmd->speed = SPEED_INVALID;
9840 cmd->duplex = DUPLEX_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009841 }
Matt Carlson882e9792009-09-01 13:21:36 +00009842 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009843 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009844 cmd->autoneg = tp->link_config.autoneg;
9845 cmd->maxtxpkt = 0;
9846 cmd->maxrxpkt = 0;
9847 return 0;
9848}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009849
Linus Torvalds1da177e2005-04-16 15:20:36 -07009850static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9851{
9852 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009853
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009854 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009855 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009856 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009857 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009858 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9859 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009860 }
9861
Matt Carlson7e5856b2009-02-25 14:23:01 +00009862 if (cmd->autoneg != AUTONEG_ENABLE &&
9863 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -07009864 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009865
9866 if (cmd->autoneg == AUTONEG_DISABLE &&
9867 cmd->duplex != DUPLEX_FULL &&
9868 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -07009869 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009870
Matt Carlson7e5856b2009-02-25 14:23:01 +00009871 if (cmd->autoneg == AUTONEG_ENABLE) {
9872 u32 mask = ADVERTISED_Autoneg |
9873 ADVERTISED_Pause |
9874 ADVERTISED_Asym_Pause;
9875
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009876 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +00009877 mask |= ADVERTISED_1000baseT_Half |
9878 ADVERTISED_1000baseT_Full;
9879
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009880 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +00009881 mask |= ADVERTISED_100baseT_Half |
9882 ADVERTISED_100baseT_Full |
9883 ADVERTISED_10baseT_Half |
9884 ADVERTISED_10baseT_Full |
9885 ADVERTISED_TP;
9886 else
9887 mask |= ADVERTISED_FIBRE;
9888
9889 if (cmd->advertising & ~mask)
9890 return -EINVAL;
9891
9892 mask &= (ADVERTISED_1000baseT_Half |
9893 ADVERTISED_1000baseT_Full |
9894 ADVERTISED_100baseT_Half |
9895 ADVERTISED_100baseT_Full |
9896 ADVERTISED_10baseT_Half |
9897 ADVERTISED_10baseT_Full);
9898
9899 cmd->advertising &= mask;
9900 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009901 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
Matt Carlson7e5856b2009-02-25 14:23:01 +00009902 if (cmd->speed != SPEED_1000)
9903 return -EINVAL;
9904
9905 if (cmd->duplex != DUPLEX_FULL)
9906 return -EINVAL;
9907 } else {
9908 if (cmd->speed != SPEED_100 &&
9909 cmd->speed != SPEED_10)
9910 return -EINVAL;
9911 }
9912 }
9913
David S. Millerf47c11e2005-06-24 20:18:35 -07009914 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009915
9916 tp->link_config.autoneg = cmd->autoneg;
9917 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -07009918 tp->link_config.advertising = (cmd->advertising |
9919 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009920 tp->link_config.speed = SPEED_INVALID;
9921 tp->link_config.duplex = DUPLEX_INVALID;
9922 } else {
9923 tp->link_config.advertising = 0;
9924 tp->link_config.speed = cmd->speed;
9925 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009926 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009927
Michael Chan24fcad62006-12-17 17:06:46 -08009928 tp->link_config.orig_speed = tp->link_config.speed;
9929 tp->link_config.orig_duplex = tp->link_config.duplex;
9930 tp->link_config.orig_autoneg = tp->link_config.autoneg;
9931
Linus Torvalds1da177e2005-04-16 15:20:36 -07009932 if (netif_running(dev))
9933 tg3_setup_phy(tp, 1);
9934
David S. Millerf47c11e2005-06-24 20:18:35 -07009935 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009936
Linus Torvalds1da177e2005-04-16 15:20:36 -07009937 return 0;
9938}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009939
Linus Torvalds1da177e2005-04-16 15:20:36 -07009940static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
9941{
9942 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009943
Linus Torvalds1da177e2005-04-16 15:20:36 -07009944 strcpy(info->driver, DRV_MODULE_NAME);
9945 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -08009946 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009947 strcpy(info->bus_info, pci_name(tp->pdev));
9948}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009949
Linus Torvalds1da177e2005-04-16 15:20:36 -07009950static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9951{
9952 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009953
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009954 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
9955 device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -07009956 wol->supported = WAKE_MAGIC;
9957 else
9958 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009959 wol->wolopts = 0;
Matt Carlson05ac4cb2008-11-03 16:53:46 -08009960 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) &&
9961 device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009962 wol->wolopts = WAKE_MAGIC;
9963 memset(&wol->sopass, 0, sizeof(wol->sopass));
9964}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009965
Linus Torvalds1da177e2005-04-16 15:20:36 -07009966static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9967{
9968 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009969 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009970
Linus Torvalds1da177e2005-04-16 15:20:36 -07009971 if (wol->wolopts & ~WAKE_MAGIC)
9972 return -EINVAL;
9973 if ((wol->wolopts & WAKE_MAGIC) &&
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009974 !((tp->tg3_flags & TG3_FLAG_WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009975 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009976
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +00009977 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
9978
David S. Millerf47c11e2005-06-24 20:18:35 -07009979 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +00009980 if (device_may_wakeup(dp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009981 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +00009982 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07009983 tp->tg3_flags &= ~TG3_FLAG_WOL_ENABLE;
David S. Millerf47c11e2005-06-24 20:18:35 -07009984 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009985
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +00009986
Linus Torvalds1da177e2005-04-16 15:20:36 -07009987 return 0;
9988}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009989
Linus Torvalds1da177e2005-04-16 15:20:36 -07009990static u32 tg3_get_msglevel(struct net_device *dev)
9991{
9992 struct tg3 *tp = netdev_priv(dev);
9993 return tp->msg_enable;
9994}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009995
Linus Torvalds1da177e2005-04-16 15:20:36 -07009996static void tg3_set_msglevel(struct net_device *dev, u32 value)
9997{
9998 struct tg3 *tp = netdev_priv(dev);
9999 tp->msg_enable = value;
10000}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010001
Linus Torvalds1da177e2005-04-16 15:20:36 -070010002static int tg3_set_tso(struct net_device *dev, u32 value)
10003{
10004 struct tg3 *tp = netdev_priv(dev);
10005
10006 if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
10007 if (value)
10008 return -EINVAL;
10009 return 0;
10010 }
Matt Carlson027455a2008-12-21 20:19:30 -080010011 if ((dev->features & NETIF_F_IPV6_CSUM) &&
Matt Carlsone849cdc2009-11-13 13:03:38 +000010012 ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
10013 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3))) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070010014 if (value) {
Michael Chanb0026622006-07-03 19:42:14 -070010015 dev->features |= NETIF_F_TSO6;
Matt Carlsone849cdc2009-11-13 13:03:38 +000010016 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
10017 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070010018 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
10019 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080010020 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000010021 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson9936bcf2007-10-10 18:03:07 -070010022 dev->features |= NETIF_F_TSO_ECN;
10023 } else
10024 dev->features &= ~(NETIF_F_TSO6 | NETIF_F_TSO_ECN);
Michael Chanb0026622006-07-03 19:42:14 -070010025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010026 return ethtool_op_set_tso(dev, value);
10027}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010028
Linus Torvalds1da177e2005-04-16 15:20:36 -070010029static int tg3_nway_reset(struct net_device *dev)
10030{
10031 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010032 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010033
Linus Torvalds1da177e2005-04-16 15:20:36 -070010034 if (!netif_running(dev))
10035 return -EAGAIN;
10036
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010037 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010038 return -EINVAL;
10039
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010040 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010041 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010042 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010043 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010044 } else {
10045 u32 bmcr;
10046
10047 spin_lock_bh(&tp->lock);
10048 r = -EINVAL;
10049 tg3_readphy(tp, MII_BMCR, &bmcr);
10050 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10051 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010052 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010053 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10054 BMCR_ANENABLE);
10055 r = 0;
10056 }
10057 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010058 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010059
Linus Torvalds1da177e2005-04-16 15:20:36 -070010060 return r;
10061}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010062
Linus Torvalds1da177e2005-04-16 15:20:36 -070010063static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10064{
10065 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010066
Matt Carlson2c49a442010-09-30 10:34:35 +000010067 ering->rx_max_pending = tp->rx_std_ring_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010068 ering->rx_mini_max_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -080010069 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
Matt Carlson2c49a442010-09-30 10:34:35 +000010070 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010071 else
10072 ering->rx_jumbo_max_pending = 0;
10073
10074 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010075
10076 ering->rx_pending = tp->rx_pending;
10077 ering->rx_mini_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -080010078 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
10079 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10080 else
10081 ering->rx_jumbo_pending = 0;
10082
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010083 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010084}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010085
Linus Torvalds1da177e2005-04-16 15:20:36 -070010086static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10087{
10088 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010089 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010090
Matt Carlson2c49a442010-09-30 10:34:35 +000010091 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10092 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010093 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10094 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Michael Chan7f62ad52007-02-20 23:25:40 -080010095 ((tp->tg3_flags2 & TG3_FLG2_TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010096 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010097 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010098
Michael Chanbbe832c2005-06-24 20:20:04 -070010099 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010100 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010101 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010102 irq_sync = 1;
10103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010104
Michael Chanbbe832c2005-06-24 20:20:04 -070010105 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010106
Linus Torvalds1da177e2005-04-16 15:20:36 -070010107 tp->rx_pending = ering->rx_pending;
10108
10109 if ((tp->tg3_flags2 & TG3_FLG2_MAX_RXPEND_64) &&
10110 tp->rx_pending > 63)
10111 tp->rx_pending = 63;
10112 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010113
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010114 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010115 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010116
10117 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010118 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010119 err = tg3_restart_hw(tp, 1);
10120 if (!err)
10121 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010122 }
10123
David S. Millerf47c11e2005-06-24 20:18:35 -070010124 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010125
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010126 if (irq_sync && !err)
10127 tg3_phy_start(tp);
10128
Michael Chanb9ec6c12006-07-25 16:37:27 -070010129 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010130}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010131
Linus Torvalds1da177e2005-04-16 15:20:36 -070010132static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10133{
10134 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010135
Linus Torvalds1da177e2005-04-16 15:20:36 -070010136 epause->autoneg = (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG) != 0;
Matt Carlson8d018622007-12-20 20:05:44 -080010137
Steve Glendinninge18ce342008-12-16 02:00:00 -080010138 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010139 epause->rx_pause = 1;
10140 else
10141 epause->rx_pause = 0;
10142
Steve Glendinninge18ce342008-12-16 02:00:00 -080010143 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010144 epause->tx_pause = 1;
10145 else
10146 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010147}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010148
Linus Torvalds1da177e2005-04-16 15:20:36 -070010149static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10150{
10151 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010152 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010153
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010154 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson27121682010-02-17 15:16:57 +000010155 u32 newadv;
10156 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010157
Matt Carlson27121682010-02-17 15:16:57 +000010158 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010159
Matt Carlson27121682010-02-17 15:16:57 +000010160 if (!(phydev->supported & SUPPORTED_Pause) ||
10161 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010162 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010163 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010164
Matt Carlson27121682010-02-17 15:16:57 +000010165 tp->link_config.flowctrl = 0;
10166 if (epause->rx_pause) {
10167 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010168
Matt Carlson27121682010-02-17 15:16:57 +000010169 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010170 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010171 newadv = ADVERTISED_Pause;
10172 } else
10173 newadv = ADVERTISED_Pause |
10174 ADVERTISED_Asym_Pause;
10175 } else if (epause->tx_pause) {
10176 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10177 newadv = ADVERTISED_Asym_Pause;
10178 } else
10179 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010180
Matt Carlson27121682010-02-17 15:16:57 +000010181 if (epause->autoneg)
10182 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
10183 else
10184 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
10185
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010186 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010187 u32 oldadv = phydev->advertising &
10188 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10189 if (oldadv != newadv) {
10190 phydev->advertising &=
10191 ~(ADVERTISED_Pause |
10192 ADVERTISED_Asym_Pause);
10193 phydev->advertising |= newadv;
10194 if (phydev->autoneg) {
10195 /*
10196 * Always renegotiate the link to
10197 * inform our link partner of our
10198 * flow control settings, even if the
10199 * flow control is forced. Let
10200 * tg3_adjust_link() do the final
10201 * flow control setup.
10202 */
10203 return phy_start_aneg(phydev);
10204 }
10205 }
10206
10207 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010208 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010209 } else {
10210 tp->link_config.orig_advertising &=
10211 ~(ADVERTISED_Pause |
10212 ADVERTISED_Asym_Pause);
10213 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010214 }
10215 } else {
10216 int irq_sync = 0;
10217
10218 if (netif_running(dev)) {
10219 tg3_netif_stop(tp);
10220 irq_sync = 1;
10221 }
10222
10223 tg3_full_lock(tp, irq_sync);
10224
10225 if (epause->autoneg)
10226 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
10227 else
10228 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
10229 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010230 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010231 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010232 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010233 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010234 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010235 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010236 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010237
10238 if (netif_running(dev)) {
10239 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10240 err = tg3_restart_hw(tp, 1);
10241 if (!err)
10242 tg3_netif_start(tp);
10243 }
10244
10245 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010247
Michael Chanb9ec6c12006-07-25 16:37:27 -070010248 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010249}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010250
Linus Torvalds1da177e2005-04-16 15:20:36 -070010251static u32 tg3_get_rx_csum(struct net_device *dev)
10252{
10253 struct tg3 *tp = netdev_priv(dev);
10254 return (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0;
10255}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010256
Linus Torvalds1da177e2005-04-16 15:20:36 -070010257static int tg3_set_rx_csum(struct net_device *dev, u32 data)
10258{
10259 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010260
Linus Torvalds1da177e2005-04-16 15:20:36 -070010261 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
10262 if (data != 0)
10263 return -EINVAL;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010264 return 0;
10265 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010266
David S. Millerf47c11e2005-06-24 20:18:35 -070010267 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010268 if (data)
10269 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
10270 else
10271 tp->tg3_flags &= ~TG3_FLAG_RX_CHECKSUMS;
David S. Millerf47c11e2005-06-24 20:18:35 -070010272 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010273
Linus Torvalds1da177e2005-04-16 15:20:36 -070010274 return 0;
10275}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010276
Linus Torvalds1da177e2005-04-16 15:20:36 -070010277static int tg3_set_tx_csum(struct net_device *dev, u32 data)
10278{
10279 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010280
Linus Torvalds1da177e2005-04-16 15:20:36 -070010281 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
10282 if (data != 0)
10283 return -EINVAL;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010284 return 0;
10285 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010286
Matt Carlson321d32a2008-11-21 17:22:19 -080010287 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chan6460d942007-07-14 19:07:52 -070010288 ethtool_op_set_tx_ipv6_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010289 else
Michael Chan9c27dbd2006-03-20 22:28:27 -080010290 ethtool_op_set_tx_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010291
10292 return 0;
10293}
10294
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010295static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010296{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010297 switch (sset) {
10298 case ETH_SS_TEST:
10299 return TG3_NUM_TEST;
10300 case ETH_SS_STATS:
10301 return TG3_NUM_STATS;
10302 default:
10303 return -EOPNOTSUPP;
10304 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010305}
10306
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010307static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010308{
10309 switch (stringset) {
10310 case ETH_SS_STATS:
10311 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10312 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010313 case ETH_SS_TEST:
10314 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10315 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010316 default:
10317 WARN_ON(1); /* we need a WARN() */
10318 break;
10319 }
10320}
10321
Michael Chan4009a932005-09-05 17:52:54 -070010322static int tg3_phys_id(struct net_device *dev, u32 data)
10323{
10324 struct tg3 *tp = netdev_priv(dev);
10325 int i;
10326
10327 if (!netif_running(tp->dev))
10328 return -EAGAIN;
10329
10330 if (data == 0)
Stephen Hemminger759afc32008-02-23 19:51:59 -080010331 data = UINT_MAX / 2;
Michael Chan4009a932005-09-05 17:52:54 -070010332
10333 for (i = 0; i < (data * 2); i++) {
10334 if ((i % 2) == 0)
10335 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10336 LED_CTRL_1000MBPS_ON |
10337 LED_CTRL_100MBPS_ON |
10338 LED_CTRL_10MBPS_ON |
10339 LED_CTRL_TRAFFIC_OVERRIDE |
10340 LED_CTRL_TRAFFIC_BLINK |
10341 LED_CTRL_TRAFFIC_LED);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010342
Michael Chan4009a932005-09-05 17:52:54 -070010343 else
10344 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10345 LED_CTRL_TRAFFIC_OVERRIDE);
10346
10347 if (msleep_interruptible(500))
10348 break;
10349 }
10350 tw32(MAC_LED_CTRL, tp->led_ctrl);
10351 return 0;
10352}
10353
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010354static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010355 struct ethtool_stats *estats, u64 *tmp_stats)
10356{
10357 struct tg3 *tp = netdev_priv(dev);
10358 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10359}
10360
Michael Chan566f86a2005-05-29 14:56:58 -070010361#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010362#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10363#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10364#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Michael Chanb16250e2006-09-27 16:10:14 -070010365#define NVRAM_SELFBOOT_HW_SIZE 0x20
10366#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010367
10368static int tg3_test_nvram(struct tg3 *tp)
10369{
Al Virob9fc7dc2007-12-17 22:59:57 -080010370 u32 csum, magic;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010371 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010372 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010373
Matt Carlsondf259d82009-04-20 06:57:14 +000010374 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
10375 return 0;
10376
Matt Carlsone4f34112009-02-25 14:25:00 +000010377 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010378 return -EIO;
10379
Michael Chan1b277772006-03-20 22:27:48 -080010380 if (magic == TG3_EEPROM_MAGIC)
10381 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010382 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010383 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10384 TG3_EEPROM_SB_FORMAT_1) {
10385 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10386 case TG3_EEPROM_SB_REVISION_0:
10387 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10388 break;
10389 case TG3_EEPROM_SB_REVISION_2:
10390 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10391 break;
10392 case TG3_EEPROM_SB_REVISION_3:
10393 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10394 break;
10395 default:
10396 return 0;
10397 }
10398 } else
Michael Chan1b277772006-03-20 22:27:48 -080010399 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010400 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10401 size = NVRAM_SELFBOOT_HW_SIZE;
10402 else
Michael Chan1b277772006-03-20 22:27:48 -080010403 return -EIO;
10404
10405 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010406 if (buf == NULL)
10407 return -ENOMEM;
10408
Michael Chan1b277772006-03-20 22:27:48 -080010409 err = -EIO;
10410 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010411 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10412 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010413 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010414 }
Michael Chan1b277772006-03-20 22:27:48 -080010415 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010416 goto out;
10417
Michael Chan1b277772006-03-20 22:27:48 -080010418 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010419 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010420 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010421 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010422 u8 *buf8 = (u8 *) buf, csum8 = 0;
10423
Al Virob9fc7dc2007-12-17 22:59:57 -080010424 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010425 TG3_EEPROM_SB_REVISION_2) {
10426 /* For rev 2, the csum doesn't include the MBA. */
10427 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10428 csum8 += buf8[i];
10429 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10430 csum8 += buf8[i];
10431 } else {
10432 for (i = 0; i < size; i++)
10433 csum8 += buf8[i];
10434 }
Michael Chan1b277772006-03-20 22:27:48 -080010435
Adrian Bunkad96b482006-04-05 22:21:04 -070010436 if (csum8 == 0) {
10437 err = 0;
10438 goto out;
10439 }
10440
10441 err = -EIO;
10442 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010443 }
Michael Chan566f86a2005-05-29 14:56:58 -070010444
Al Virob9fc7dc2007-12-17 22:59:57 -080010445 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010446 TG3_EEPROM_MAGIC_HW) {
10447 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010448 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010449 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010450
10451 /* Separate the parity bits and the data bytes. */
10452 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10453 if ((i == 0) || (i == 8)) {
10454 int l;
10455 u8 msk;
10456
10457 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10458 parity[k++] = buf8[i] & msk;
10459 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000010460 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010461 int l;
10462 u8 msk;
10463
10464 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10465 parity[k++] = buf8[i] & msk;
10466 i++;
10467
10468 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10469 parity[k++] = buf8[i] & msk;
10470 i++;
10471 }
10472 data[j++] = buf8[i];
10473 }
10474
10475 err = -EIO;
10476 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10477 u8 hw8 = hweight8(data[i]);
10478
10479 if ((hw8 & 0x1) && parity[i])
10480 goto out;
10481 else if (!(hw8 & 0x1) && !parity[i])
10482 goto out;
10483 }
10484 err = 0;
10485 goto out;
10486 }
10487
Matt Carlson01c3a392011-03-09 16:58:20 +000010488 err = -EIO;
10489
Michael Chan566f86a2005-05-29 14:56:58 -070010490 /* Bootstrap checksum at offset 0x10 */
10491 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000010492 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010493 goto out;
10494
10495 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10496 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000010497 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000010498 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010499
Matt Carlsond4894f32011-03-09 16:58:21 +000010500 for (i = 0; i < TG3_NVM_VPD_LEN; i += 4) {
10501 /* The data is in little-endian format in NVRAM.
10502 * Use the big-endian read routines to preserve
10503 * the byte order as it exists in NVRAM.
10504 */
10505 if (tg3_nvram_read_be32(tp, TG3_NVM_VPD_OFF + i, &buf[i/4]))
10506 goto out;
10507 }
10508
10509 i = pci_vpd_find_tag((u8 *)buf, 0, TG3_NVM_VPD_LEN,
10510 PCI_VPD_LRDT_RO_DATA);
10511 if (i > 0) {
10512 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
10513 if (j < 0)
10514 goto out;
10515
10516 if (i + PCI_VPD_LRDT_TAG_SIZE + j > TG3_NVM_VPD_LEN)
10517 goto out;
10518
10519 i += PCI_VPD_LRDT_TAG_SIZE;
10520 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
10521 PCI_VPD_RO_KEYWORD_CHKSUM);
10522 if (j > 0) {
10523 u8 csum8 = 0;
10524
10525 j += PCI_VPD_INFO_FLD_HDR_SIZE;
10526
10527 for (i = 0; i <= j; i++)
10528 csum8 += ((u8 *)buf)[i];
10529
10530 if (csum8)
10531 goto out;
10532 }
10533 }
10534
Michael Chan566f86a2005-05-29 14:56:58 -070010535 err = 0;
10536
10537out:
10538 kfree(buf);
10539 return err;
10540}
10541
Michael Chanca430072005-05-29 14:57:23 -070010542#define TG3_SERDES_TIMEOUT_SEC 2
10543#define TG3_COPPER_TIMEOUT_SEC 6
10544
10545static int tg3_test_link(struct tg3 *tp)
10546{
10547 int i, max;
10548
10549 if (!netif_running(tp->dev))
10550 return -ENODEV;
10551
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010552 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070010553 max = TG3_SERDES_TIMEOUT_SEC;
10554 else
10555 max = TG3_COPPER_TIMEOUT_SEC;
10556
10557 for (i = 0; i < max; i++) {
10558 if (netif_carrier_ok(tp->dev))
10559 return 0;
10560
10561 if (msleep_interruptible(1000))
10562 break;
10563 }
10564
10565 return -EIO;
10566}
10567
Michael Chana71116d2005-05-29 14:58:11 -070010568/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080010569static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070010570{
Michael Chanb16250e2006-09-27 16:10:14 -070010571 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070010572 u32 offset, read_mask, write_mask, val, save_val, read_val;
10573 static struct {
10574 u16 offset;
10575 u16 flags;
10576#define TG3_FL_5705 0x1
10577#define TG3_FL_NOT_5705 0x2
10578#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070010579#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070010580 u32 read_mask;
10581 u32 write_mask;
10582 } reg_tbl[] = {
10583 /* MAC Control Registers */
10584 { MAC_MODE, TG3_FL_NOT_5705,
10585 0x00000000, 0x00ef6f8c },
10586 { MAC_MODE, TG3_FL_5705,
10587 0x00000000, 0x01ef6b8c },
10588 { MAC_STATUS, TG3_FL_NOT_5705,
10589 0x03800107, 0x00000000 },
10590 { MAC_STATUS, TG3_FL_5705,
10591 0x03800100, 0x00000000 },
10592 { MAC_ADDR_0_HIGH, 0x0000,
10593 0x00000000, 0x0000ffff },
10594 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010595 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070010596 { MAC_RX_MTU_SIZE, 0x0000,
10597 0x00000000, 0x0000ffff },
10598 { MAC_TX_MODE, 0x0000,
10599 0x00000000, 0x00000070 },
10600 { MAC_TX_LENGTHS, 0x0000,
10601 0x00000000, 0x00003fff },
10602 { MAC_RX_MODE, TG3_FL_NOT_5705,
10603 0x00000000, 0x000007fc },
10604 { MAC_RX_MODE, TG3_FL_5705,
10605 0x00000000, 0x000007dc },
10606 { MAC_HASH_REG_0, 0x0000,
10607 0x00000000, 0xffffffff },
10608 { MAC_HASH_REG_1, 0x0000,
10609 0x00000000, 0xffffffff },
10610 { MAC_HASH_REG_2, 0x0000,
10611 0x00000000, 0xffffffff },
10612 { MAC_HASH_REG_3, 0x0000,
10613 0x00000000, 0xffffffff },
10614
10615 /* Receive Data and Receive BD Initiator Control Registers. */
10616 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
10617 0x00000000, 0xffffffff },
10618 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
10619 0x00000000, 0xffffffff },
10620 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
10621 0x00000000, 0x00000003 },
10622 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
10623 0x00000000, 0xffffffff },
10624 { RCVDBDI_STD_BD+0, 0x0000,
10625 0x00000000, 0xffffffff },
10626 { RCVDBDI_STD_BD+4, 0x0000,
10627 0x00000000, 0xffffffff },
10628 { RCVDBDI_STD_BD+8, 0x0000,
10629 0x00000000, 0xffff0002 },
10630 { RCVDBDI_STD_BD+0xc, 0x0000,
10631 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010632
Michael Chana71116d2005-05-29 14:58:11 -070010633 /* Receive BD Initiator Control Registers. */
10634 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
10635 0x00000000, 0xffffffff },
10636 { RCVBDI_STD_THRESH, TG3_FL_5705,
10637 0x00000000, 0x000003ff },
10638 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
10639 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010640
Michael Chana71116d2005-05-29 14:58:11 -070010641 /* Host Coalescing Control Registers. */
10642 { HOSTCC_MODE, TG3_FL_NOT_5705,
10643 0x00000000, 0x00000004 },
10644 { HOSTCC_MODE, TG3_FL_5705,
10645 0x00000000, 0x000000f6 },
10646 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
10647 0x00000000, 0xffffffff },
10648 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
10649 0x00000000, 0x000003ff },
10650 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
10651 0x00000000, 0xffffffff },
10652 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
10653 0x00000000, 0x000003ff },
10654 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
10655 0x00000000, 0xffffffff },
10656 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10657 0x00000000, 0x000000ff },
10658 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
10659 0x00000000, 0xffffffff },
10660 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10661 0x00000000, 0x000000ff },
10662 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
10663 0x00000000, 0xffffffff },
10664 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
10665 0x00000000, 0xffffffff },
10666 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10667 0x00000000, 0xffffffff },
10668 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10669 0x00000000, 0x000000ff },
10670 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10671 0x00000000, 0xffffffff },
10672 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10673 0x00000000, 0x000000ff },
10674 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
10675 0x00000000, 0xffffffff },
10676 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
10677 0x00000000, 0xffffffff },
10678 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
10679 0x00000000, 0xffffffff },
10680 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
10681 0x00000000, 0xffffffff },
10682 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
10683 0x00000000, 0xffffffff },
10684 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
10685 0xffffffff, 0x00000000 },
10686 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
10687 0xffffffff, 0x00000000 },
10688
10689 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070010690 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010691 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070010692 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010693 0x00000000, 0x007fffff },
10694 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
10695 0x00000000, 0x0000003f },
10696 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
10697 0x00000000, 0x000001ff },
10698 { BUFMGR_MB_HIGH_WATER, 0x0000,
10699 0x00000000, 0x000001ff },
10700 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
10701 0xffffffff, 0x00000000 },
10702 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
10703 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010704
Michael Chana71116d2005-05-29 14:58:11 -070010705 /* Mailbox Registers */
10706 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
10707 0x00000000, 0x000001ff },
10708 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
10709 0x00000000, 0x000001ff },
10710 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
10711 0x00000000, 0x000007ff },
10712 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
10713 0x00000000, 0x000001ff },
10714
10715 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
10716 };
10717
Michael Chanb16250e2006-09-27 16:10:14 -070010718 is_5705 = is_5750 = 0;
10719 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chana71116d2005-05-29 14:58:11 -070010720 is_5705 = 1;
Michael Chanb16250e2006-09-27 16:10:14 -070010721 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
10722 is_5750 = 1;
10723 }
Michael Chana71116d2005-05-29 14:58:11 -070010724
10725 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
10726 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
10727 continue;
10728
10729 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
10730 continue;
10731
10732 if ((tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
10733 (reg_tbl[i].flags & TG3_FL_NOT_5788))
10734 continue;
10735
Michael Chanb16250e2006-09-27 16:10:14 -070010736 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
10737 continue;
10738
Michael Chana71116d2005-05-29 14:58:11 -070010739 offset = (u32) reg_tbl[i].offset;
10740 read_mask = reg_tbl[i].read_mask;
10741 write_mask = reg_tbl[i].write_mask;
10742
10743 /* Save the original register content */
10744 save_val = tr32(offset);
10745
10746 /* Determine the read-only value. */
10747 read_val = save_val & read_mask;
10748
10749 /* Write zero to the register, then make sure the read-only bits
10750 * are not changed and the read/write bits are all zeros.
10751 */
10752 tw32(offset, 0);
10753
10754 val = tr32(offset);
10755
10756 /* Test the read-only and read/write bits. */
10757 if (((val & read_mask) != read_val) || (val & write_mask))
10758 goto out;
10759
10760 /* Write ones to all the bits defined by RdMask and WrMask, then
10761 * make sure the read-only bits are not changed and the
10762 * read/write bits are all ones.
10763 */
10764 tw32(offset, read_mask | write_mask);
10765
10766 val = tr32(offset);
10767
10768 /* Test the read-only bits. */
10769 if ((val & read_mask) != read_val)
10770 goto out;
10771
10772 /* Test the read/write bits. */
10773 if ((val & write_mask) != write_mask)
10774 goto out;
10775
10776 tw32(offset, save_val);
10777 }
10778
10779 return 0;
10780
10781out:
Michael Chan9f88f292006-12-07 00:22:54 -080010782 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000010783 netdev_err(tp->dev,
10784 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070010785 tw32(offset, save_val);
10786 return -EIO;
10787}
10788
Michael Chan7942e1d2005-05-29 14:58:36 -070010789static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
10790{
Arjan van de Venf71e1302006-03-03 21:33:57 -050010791 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070010792 int i;
10793 u32 j;
10794
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020010795 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070010796 for (j = 0; j < len; j += 4) {
10797 u32 val;
10798
10799 tg3_write_mem(tp, offset + j, test_pattern[i]);
10800 tg3_read_mem(tp, offset + j, &val);
10801 if (val != test_pattern[i])
10802 return -EIO;
10803 }
10804 }
10805 return 0;
10806}
10807
10808static int tg3_test_memory(struct tg3 *tp)
10809{
10810 static struct mem_entry {
10811 u32 offset;
10812 u32 len;
10813 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080010814 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070010815 { 0x00002000, 0x1c000},
10816 { 0xffffffff, 0x00000}
10817 }, mem_tbl_5705[] = {
10818 { 0x00000100, 0x0000c},
10819 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070010820 { 0x00004000, 0x00800},
10821 { 0x00006000, 0x01000},
10822 { 0x00008000, 0x02000},
10823 { 0x00010000, 0x0e000},
10824 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080010825 }, mem_tbl_5755[] = {
10826 { 0x00000200, 0x00008},
10827 { 0x00004000, 0x00800},
10828 { 0x00006000, 0x00800},
10829 { 0x00008000, 0x02000},
10830 { 0x00010000, 0x0c000},
10831 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070010832 }, mem_tbl_5906[] = {
10833 { 0x00000200, 0x00008},
10834 { 0x00004000, 0x00400},
10835 { 0x00006000, 0x00400},
10836 { 0x00008000, 0x01000},
10837 { 0x00010000, 0x01000},
10838 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010839 }, mem_tbl_5717[] = {
10840 { 0x00000200, 0x00008},
10841 { 0x00010000, 0x0a000},
10842 { 0x00020000, 0x13c00},
10843 { 0xffffffff, 0x00000}
10844 }, mem_tbl_57765[] = {
10845 { 0x00000200, 0x00008},
10846 { 0x00004000, 0x00800},
10847 { 0x00006000, 0x09800},
10848 { 0x00010000, 0x0a000},
10849 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070010850 };
10851 struct mem_entry *mem_tbl;
10852 int err = 0;
10853 int i;
10854
Matt Carlson0a58d662011-04-05 14:22:45 +000010855 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010856 mem_tbl = mem_tbl_5717;
10857 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
10858 mem_tbl = mem_tbl_57765;
10859 else if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlson321d32a2008-11-21 17:22:19 -080010860 mem_tbl = mem_tbl_5755;
10861 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
10862 mem_tbl = mem_tbl_5906;
10863 else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
10864 mem_tbl = mem_tbl_5705;
10865 else
Michael Chan7942e1d2005-05-29 14:58:36 -070010866 mem_tbl = mem_tbl_570x;
10867
10868 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000010869 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
10870 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070010871 break;
10872 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010873
Michael Chan7942e1d2005-05-29 14:58:36 -070010874 return err;
10875}
10876
Michael Chan9f40dea2005-09-05 17:53:06 -070010877#define TG3_MAC_LOOPBACK 0
10878#define TG3_PHY_LOOPBACK 1
10879
10880static int tg3_run_loopback(struct tg3 *tp, int loopback_mode)
Michael Chanc76949a2005-05-29 14:58:59 -070010881{
Michael Chan9f40dea2005-09-05 17:53:06 -070010882 u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010883 u32 desc_idx, coal_now;
Michael Chanc76949a2005-05-29 14:58:59 -070010884 struct sk_buff *skb, *rx_skb;
10885 u8 *tx_data;
10886 dma_addr_t map;
10887 int num_pkts, tx_len, rx_len, i, err;
10888 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000010889 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000010890 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070010891
Matt Carlsonc8873402010-02-12 14:47:11 +000010892 tnapi = &tp->napi[0];
10893 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010894 if (tp->irq_cnt > 1) {
Matt Carlson1da85aa2010-09-30 10:34:34 +000010895 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)
10896 rnapi = &tp->napi[1];
Matt Carlsonc8873402010-02-12 14:47:11 +000010897 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
10898 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010899 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010900 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000010901
Michael Chan9f40dea2005-09-05 17:53:06 -070010902 if (loopback_mode == TG3_MAC_LOOPBACK) {
Michael Chanc94e3942005-09-27 12:12:42 -070010903 /* HW errata - mac loopback fails in some cases on 5780.
10904 * Normal traffic and PHY loopback are not affected by
Matt Carlsonaba49f22011-01-25 15:58:53 +000010905 * errata. Also, the MAC loopback test is deprecated for
10906 * all newer ASIC revisions.
Michael Chanc94e3942005-09-27 12:12:42 -070010907 */
Matt Carlsonaba49f22011-01-25 15:58:53 +000010908 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
10909 (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT))
Michael Chanc94e3942005-09-27 12:12:42 -070010910 return 0;
10911
Matt Carlson49692ca2011-01-25 15:58:52 +000010912 mac_mode = tp->mac_mode &
10913 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
10914 mac_mode |= MAC_MODE_PORT_INT_LPBACK;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010915 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
10916 mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010917 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
Michael Chan3f7045c2006-09-27 16:02:29 -070010918 mac_mode |= MAC_MODE_PORT_MODE_MII;
10919 else
10920 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chan9f40dea2005-09-05 17:53:06 -070010921 tw32(MAC_MODE, mac_mode);
10922 } else if (loopback_mode == TG3_PHY_LOOPBACK) {
Michael Chan3f7045c2006-09-27 16:02:29 -070010923 u32 val;
10924
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010925 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +000010926 tg3_phy_fet_toggle_apd(tp, false);
Michael Chan5d64ad32006-12-07 00:19:40 -080010927 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
10928 } else
10929 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
Michael Chan3f7045c2006-09-27 16:02:29 -070010930
Matt Carlson9ef8ca92007-07-11 19:48:29 -070010931 tg3_phy_toggle_automdix(tp, 0);
10932
Michael Chan3f7045c2006-09-27 16:02:29 -070010933 tg3_writephy(tp, MII_BMCR, val);
Michael Chanc94e3942005-09-27 12:12:42 -070010934 udelay(40);
Michael Chan5d64ad32006-12-07 00:19:40 -080010935
Matt Carlson49692ca2011-01-25 15:58:52 +000010936 mac_mode = tp->mac_mode &
10937 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010938 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson1061b7c2010-02-12 14:47:12 +000010939 tg3_writephy(tp, MII_TG3_FET_PTEST,
10940 MII_TG3_FET_PTEST_FRC_TX_LINK |
10941 MII_TG3_FET_PTEST_FRC_TX_LOCK);
10942 /* The write needs to be flushed for the AC131 */
10943 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
10944 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
Michael Chan5d64ad32006-12-07 00:19:40 -080010945 mac_mode |= MAC_MODE_PORT_MODE_MII;
10946 } else
10947 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chanb16250e2006-09-27 16:10:14 -070010948
Michael Chanc94e3942005-09-27 12:12:42 -070010949 /* reset to prevent losing 1st rx packet intermittently */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010950 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Michael Chanc94e3942005-09-27 12:12:42 -070010951 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10952 udelay(10);
10953 tw32_f(MAC_RX_MODE, tp->rx_mode);
10954 }
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010955 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlson79eb6902010-02-17 15:17:03 +000010956 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
10957 if (masked_phy_id == TG3_PHY_ID_BCM5401)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010958 mac_mode &= ~MAC_MODE_LINK_POLARITY;
Matt Carlson79eb6902010-02-17 15:17:03 +000010959 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010960 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chanff18ff02006-03-27 23:17:27 -080010961 tg3_writephy(tp, MII_TG3_EXT_CTRL,
10962 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
10963 }
Michael Chan9f40dea2005-09-05 17:53:06 -070010964 tw32(MAC_MODE, mac_mode);
Matt Carlson49692ca2011-01-25 15:58:52 +000010965
10966 /* Wait for link */
10967 for (i = 0; i < 100; i++) {
10968 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
10969 break;
10970 mdelay(1);
10971 }
Matt Carlson859a588792010-04-05 10:19:28 +000010972 } else {
Michael Chan9f40dea2005-09-05 17:53:06 -070010973 return -EINVAL;
Matt Carlson859a588792010-04-05 10:19:28 +000010974 }
Michael Chanc76949a2005-05-29 14:58:59 -070010975
10976 err = -EIO;
10977
Michael Chanc76949a2005-05-29 14:58:59 -070010978 tx_len = 1514;
David S. Millera20e9c62006-07-31 22:38:16 -070010979 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070010980 if (!skb)
10981 return -ENOMEM;
10982
Michael Chanc76949a2005-05-29 14:58:59 -070010983 tx_data = skb_put(skb, tx_len);
10984 memcpy(tx_data, tp->dev->dev_addr, 6);
10985 memset(tx_data + 6, 0x0, 8);
10986
10987 tw32(MAC_RX_MTU_SIZE, tx_len + 4);
10988
10989 for (i = 14; i < tx_len; i++)
10990 tx_data[i] = (u8) (i & 0xff);
10991
Alexander Duyckf4188d82009-12-02 16:48:38 +000010992 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
10993 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000010994 dev_kfree_skb(skb);
10995 return -EIO;
10996 }
Michael Chanc76949a2005-05-29 14:58:59 -070010997
10998 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010999 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011000
11001 udelay(10);
11002
Matt Carlson898a56f2009-08-28 14:02:40 +000011003 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011004
Michael Chanc76949a2005-05-29 14:58:59 -070011005 num_pkts = 0;
11006
Alexander Duyckf4188d82009-12-02 16:48:38 +000011007 tg3_set_txd(tnapi, tnapi->tx_prod, map, tx_len, 0, 1);
Michael Chanc76949a2005-05-29 14:58:59 -070011008
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011009 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011010 num_pkts++;
11011
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011012 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11013 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011014
11015 udelay(10);
11016
Matt Carlson303fc922009-11-02 14:27:34 +000011017 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11018 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011019 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011020 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011021
11022 udelay(10);
11023
Matt Carlson898a56f2009-08-28 14:02:40 +000011024 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11025 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011026 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011027 (rx_idx == (rx_start_idx + num_pkts)))
11028 break;
11029 }
11030
Alexander Duyckf4188d82009-12-02 16:48:38 +000011031 pci_unmap_single(tp->pdev, map, tx_len, PCI_DMA_TODEVICE);
Michael Chanc76949a2005-05-29 14:58:59 -070011032 dev_kfree_skb(skb);
11033
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011034 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011035 goto out;
11036
11037 if (rx_idx != rx_start_idx + num_pkts)
11038 goto out;
11039
Matt Carlson72334482009-08-28 14:03:01 +000011040 desc = &rnapi->rx_rcb[rx_start_idx];
Michael Chanc76949a2005-05-29 14:58:59 -070011041 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11042 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
11043 if (opaque_key != RXD_OPAQUE_RING_STD)
11044 goto out;
11045
11046 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11047 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
11048 goto out;
11049
11050 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) - 4;
11051 if (rx_len != tx_len)
11052 goto out;
11053
Matt Carlson21f581a2009-08-28 14:00:25 +000011054 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
Michael Chanc76949a2005-05-29 14:58:59 -070011055
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +000011056 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx], mapping);
Michael Chanc76949a2005-05-29 14:58:59 -070011057 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len, PCI_DMA_FROMDEVICE);
11058
11059 for (i = 14; i < tx_len; i++) {
11060 if (*(rx_skb->data + i) != (u8) (i & 0xff))
11061 goto out;
11062 }
11063 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011064
Michael Chanc76949a2005-05-29 14:58:59 -070011065 /* tg3_free_rings will unmap and free the rx_skb */
11066out:
11067 return err;
11068}
11069
Michael Chan9f40dea2005-09-05 17:53:06 -070011070#define TG3_MAC_LOOPBACK_FAILED 1
11071#define TG3_PHY_LOOPBACK_FAILED 2
11072#define TG3_LOOPBACK_FAILED (TG3_MAC_LOOPBACK_FAILED | \
11073 TG3_PHY_LOOPBACK_FAILED)
11074
11075static int tg3_test_loopback(struct tg3 *tp)
11076{
11077 int err = 0;
Matt Carlsonab789042011-01-25 15:58:54 +000011078 u32 eee_cap, cpmuctrl = 0;
Michael Chan9f40dea2005-09-05 17:53:06 -070011079
11080 if (!netif_running(tp->dev))
11081 return TG3_LOOPBACK_FAILED;
11082
Matt Carlsonab789042011-01-25 15:58:54 +000011083 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11084 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11085
Michael Chanb9ec6c12006-07-25 16:37:27 -070011086 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011087 if (err) {
11088 err = TG3_LOOPBACK_FAILED;
11089 goto done;
11090 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011091
Matt Carlson6833c042008-11-21 17:18:59 -080011092 /* Turn off gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011093 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080011094 tg3_phy_toggle_apd(tp, false);
11095
Matt Carlson321d32a2008-11-21 17:22:19 -080011096 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070011097 int i;
11098 u32 status;
11099
11100 tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
11101
11102 /* Wait for up to 40 microseconds to acquire lock. */
11103 for (i = 0; i < 4; i++) {
11104 status = tr32(TG3_CPMU_MUTEX_GNT);
11105 if (status == CPMU_MUTEX_GNT_DRIVER)
11106 break;
11107 udelay(10);
11108 }
11109
Matt Carlsonab789042011-01-25 15:58:54 +000011110 if (status != CPMU_MUTEX_GNT_DRIVER) {
11111 err = TG3_LOOPBACK_FAILED;
11112 goto done;
11113 }
Matt Carlson9936bcf2007-10-10 18:03:07 -070011114
Matt Carlsonb2a5c192008-04-03 21:44:44 -070011115 /* Turn off link-based power management. */
Matt Carlsone8750932007-11-12 21:11:51 -080011116 cpmuctrl = tr32(TG3_CPMU_CTRL);
Matt Carlson109115e2008-05-02 16:48:59 -070011117 tw32(TG3_CPMU_CTRL,
11118 cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
11119 CPMU_CTRL_LINK_AWARE_MODE));
Matt Carlson9936bcf2007-10-10 18:03:07 -070011120 }
11121
Michael Chan9f40dea2005-09-05 17:53:06 -070011122 if (tg3_run_loopback(tp, TG3_MAC_LOOPBACK))
11123 err |= TG3_MAC_LOOPBACK_FAILED;
Matt Carlson9936bcf2007-10-10 18:03:07 -070011124
Matt Carlson321d32a2008-11-21 17:22:19 -080011125 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070011126 tw32(TG3_CPMU_CTRL, cpmuctrl);
11127
11128 /* Release the mutex */
11129 tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
11130 }
11131
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011132 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsondd477002008-05-25 23:45:58 -070011133 !(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
Michael Chan9f40dea2005-09-05 17:53:06 -070011134 if (tg3_run_loopback(tp, TG3_PHY_LOOPBACK))
11135 err |= TG3_PHY_LOOPBACK_FAILED;
11136 }
11137
Matt Carlson6833c042008-11-21 17:18:59 -080011138 /* Re-enable gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011139 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080011140 tg3_phy_toggle_apd(tp, true);
11141
Matt Carlsonab789042011-01-25 15:58:54 +000011142done:
11143 tp->phy_flags |= eee_cap;
11144
Michael Chan9f40dea2005-09-05 17:53:06 -070011145 return err;
11146}
11147
Michael Chan4cafd3f2005-05-29 14:56:34 -070011148static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11149 u64 *data)
11150{
Michael Chan566f86a2005-05-29 14:56:58 -070011151 struct tg3 *tp = netdev_priv(dev);
11152
Matt Carlson80096062010-08-02 11:26:06 +000011153 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011154 tg3_power_up(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011155
Michael Chan566f86a2005-05-29 14:56:58 -070011156 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
11157
11158 if (tg3_test_nvram(tp) != 0) {
11159 etest->flags |= ETH_TEST_FL_FAILED;
11160 data[0] = 1;
11161 }
Michael Chanca430072005-05-29 14:57:23 -070011162 if (tg3_test_link(tp) != 0) {
11163 etest->flags |= ETH_TEST_FL_FAILED;
11164 data[1] = 1;
11165 }
Michael Chana71116d2005-05-29 14:58:11 -070011166 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011167 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070011168
Michael Chanbbe832c2005-06-24 20:20:04 -070011169 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011170 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011171 tg3_netif_stop(tp);
11172 irq_sync = 1;
11173 }
11174
11175 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070011176
11177 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080011178 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011179 tg3_halt_cpu(tp, RX_CPU_BASE);
11180 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
11181 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080011182 if (!err)
11183 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011184
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011185 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad12006-03-20 22:27:35 -080011186 tg3_phy_reset(tp);
11187
Michael Chana71116d2005-05-29 14:58:11 -070011188 if (tg3_test_registers(tp) != 0) {
11189 etest->flags |= ETH_TEST_FL_FAILED;
11190 data[2] = 1;
11191 }
Michael Chan7942e1d2005-05-29 14:58:36 -070011192 if (tg3_test_memory(tp) != 0) {
11193 etest->flags |= ETH_TEST_FL_FAILED;
11194 data[3] = 1;
11195 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011196 if ((data[4] = tg3_test_loopback(tp)) != 0)
Michael Chanc76949a2005-05-29 14:58:59 -070011197 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070011198
David S. Millerf47c11e2005-06-24 20:18:35 -070011199 tg3_full_unlock(tp);
11200
Michael Chand4bc3922005-05-29 14:59:20 -070011201 if (tg3_test_interrupt(tp) != 0) {
11202 etest->flags |= ETH_TEST_FL_FAILED;
11203 data[5] = 1;
11204 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011205
11206 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070011207
Michael Chana71116d2005-05-29 14:58:11 -070011208 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11209 if (netif_running(dev)) {
11210 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011211 err2 = tg3_restart_hw(tp, 1);
11212 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070011213 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011214 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011215
11216 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011217
11218 if (irq_sync && !err2)
11219 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011220 }
Matt Carlson80096062010-08-02 11:26:06 +000011221 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011222 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011223
Michael Chan4cafd3f2005-05-29 14:56:34 -070011224}
11225
Linus Torvalds1da177e2005-04-16 15:20:36 -070011226static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
11227{
11228 struct mii_ioctl_data *data = if_mii(ifr);
11229 struct tg3 *tp = netdev_priv(dev);
11230 int err;
11231
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011232 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011233 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011234 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011235 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011236 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000011237 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011238 }
11239
Matt Carlson33f401a2010-04-05 10:19:27 +000011240 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011241 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000011242 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011243
11244 /* fallthru */
11245 case SIOCGMIIREG: {
11246 u32 mii_regval;
11247
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011248 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011249 break; /* We have no PHY */
11250
Matt Carlsonf746a312011-01-25 15:58:51 +000011251 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) ||
11252 ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
11253 !netif_running(dev)))
Michael Chanbc1c7562006-03-20 17:48:03 -080011254 return -EAGAIN;
11255
David S. Millerf47c11e2005-06-24 20:18:35 -070011256 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011257 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070011258 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011259
11260 data->val_out = mii_regval;
11261
11262 return err;
11263 }
11264
11265 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011266 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011267 break; /* We have no PHY */
11268
Matt Carlsonf746a312011-01-25 15:58:51 +000011269 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) ||
11270 ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
11271 !netif_running(dev)))
Michael Chanbc1c7562006-03-20 17:48:03 -080011272 return -EAGAIN;
11273
David S. Millerf47c11e2005-06-24 20:18:35 -070011274 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011275 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070011276 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011277
11278 return err;
11279
11280 default:
11281 /* do nothing */
11282 break;
11283 }
11284 return -EOPNOTSUPP;
11285}
11286
David S. Miller15f98502005-05-18 22:49:26 -070011287static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11288{
11289 struct tg3 *tp = netdev_priv(dev);
11290
11291 memcpy(ec, &tp->coal, sizeof(*ec));
11292 return 0;
11293}
11294
Michael Chand244c892005-07-05 14:42:33 -070011295static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11296{
11297 struct tg3 *tp = netdev_priv(dev);
11298 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11299 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11300
11301 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
11302 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11303 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11304 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11305 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11306 }
11307
11308 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11309 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11310 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11311 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11312 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11313 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11314 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11315 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11316 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11317 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11318 return -EINVAL;
11319
11320 /* No rx interrupts will be generated if both are zero */
11321 if ((ec->rx_coalesce_usecs == 0) &&
11322 (ec->rx_max_coalesced_frames == 0))
11323 return -EINVAL;
11324
11325 /* No tx interrupts will be generated if both are zero */
11326 if ((ec->tx_coalesce_usecs == 0) &&
11327 (ec->tx_max_coalesced_frames == 0))
11328 return -EINVAL;
11329
11330 /* Only copy relevant parameters, ignore all others. */
11331 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11332 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11333 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11334 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11335 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11336 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11337 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11338 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11339 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11340
11341 if (netif_running(dev)) {
11342 tg3_full_lock(tp, 0);
11343 __tg3_set_coalesce(tp, &tp->coal);
11344 tg3_full_unlock(tp);
11345 }
11346 return 0;
11347}
11348
Jeff Garzik7282d492006-09-13 14:30:00 -040011349static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011350 .get_settings = tg3_get_settings,
11351 .set_settings = tg3_set_settings,
11352 .get_drvinfo = tg3_get_drvinfo,
11353 .get_regs_len = tg3_get_regs_len,
11354 .get_regs = tg3_get_regs,
11355 .get_wol = tg3_get_wol,
11356 .set_wol = tg3_set_wol,
11357 .get_msglevel = tg3_get_msglevel,
11358 .set_msglevel = tg3_set_msglevel,
11359 .nway_reset = tg3_nway_reset,
11360 .get_link = ethtool_op_get_link,
11361 .get_eeprom_len = tg3_get_eeprom_len,
11362 .get_eeprom = tg3_get_eeprom,
11363 .set_eeprom = tg3_set_eeprom,
11364 .get_ringparam = tg3_get_ringparam,
11365 .set_ringparam = tg3_set_ringparam,
11366 .get_pauseparam = tg3_get_pauseparam,
11367 .set_pauseparam = tg3_set_pauseparam,
11368 .get_rx_csum = tg3_get_rx_csum,
11369 .set_rx_csum = tg3_set_rx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011370 .set_tx_csum = tg3_set_tx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011371 .set_sg = ethtool_op_set_sg,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011372 .set_tso = tg3_set_tso,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011373 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011374 .get_strings = tg3_get_strings,
Michael Chan4009a932005-09-05 17:52:54 -070011375 .phys_id = tg3_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011376 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011377 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011378 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011379 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011380};
11381
11382static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11383{
Michael Chan1b277772006-03-20 22:27:48 -080011384 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011385
11386 tp->nvram_size = EEPROM_CHIP_SIZE;
11387
Matt Carlsone4f34112009-02-25 14:25:00 +000011388 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011389 return;
11390
Michael Chanb16250e2006-09-27 16:10:14 -070011391 if ((magic != TG3_EEPROM_MAGIC) &&
11392 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11393 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011394 return;
11395
11396 /*
11397 * Size the chip by reading offsets at increasing powers of two.
11398 * When we encounter our validation signature, we know the addressing
11399 * has wrapped around, and thus have our chip size.
11400 */
Michael Chan1b277772006-03-20 22:27:48 -080011401 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011402
11403 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011404 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011405 return;
11406
Michael Chan18201802006-03-20 22:29:15 -080011407 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011408 break;
11409
11410 cursize <<= 1;
11411 }
11412
11413 tp->nvram_size = cursize;
11414}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011415
Linus Torvalds1da177e2005-04-16 15:20:36 -070011416static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11417{
11418 u32 val;
11419
Matt Carlsondf259d82009-04-20 06:57:14 +000011420 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
11421 tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011422 return;
11423
11424 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011425 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011426 tg3_get_eeprom_size(tp);
11427 return;
11428 }
11429
Matt Carlson6d348f22009-02-25 14:25:52 +000011430 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011431 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011432 /* This is confusing. We want to operate on the
11433 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11434 * call will read from NVRAM and byteswap the data
11435 * according to the byteswapping settings for all
11436 * other register accesses. This ensures the data we
11437 * want will always reside in the lower 16-bits.
11438 * However, the data in NVRAM is in LE format, which
11439 * means the data from the NVRAM read will always be
11440 * opposite the endianness of the CPU. The 16-bit
11441 * byteswap then brings the data to CPU endianness.
11442 */
11443 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011444 return;
11445 }
11446 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070011447 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011448}
11449
11450static void __devinit tg3_get_nvram_info(struct tg3 *tp)
11451{
11452 u32 nvcfg1;
11453
11454 nvcfg1 = tr32(NVRAM_CFG1);
11455 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
11456 tp->tg3_flags2 |= TG3_FLG2_FLASH;
Matt Carlson8590a602009-08-28 12:29:16 +000011457 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011458 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11459 tw32(NVRAM_CFG1, nvcfg1);
11460 }
11461
Michael Chan4c987482005-09-05 17:52:38 -070011462 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) ||
Michael Chana4e2b342005-10-26 15:46:52 -070011463 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011464 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011465 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
11466 tp->nvram_jedecnum = JEDEC_ATMEL;
11467 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11468 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11469 break;
11470 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
11471 tp->nvram_jedecnum = JEDEC_ATMEL;
11472 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
11473 break;
11474 case FLASH_VENDOR_ATMEL_EEPROM:
11475 tp->nvram_jedecnum = JEDEC_ATMEL;
11476 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11477 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11478 break;
11479 case FLASH_VENDOR_ST:
11480 tp->nvram_jedecnum = JEDEC_ST;
11481 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
11482 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11483 break;
11484 case FLASH_VENDOR_SAIFUN:
11485 tp->nvram_jedecnum = JEDEC_SAIFUN;
11486 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
11487 break;
11488 case FLASH_VENDOR_SST_SMALL:
11489 case FLASH_VENDOR_SST_LARGE:
11490 tp->nvram_jedecnum = JEDEC_SST;
11491 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
11492 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011493 }
Matt Carlson8590a602009-08-28 12:29:16 +000011494 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011495 tp->nvram_jedecnum = JEDEC_ATMEL;
11496 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11497 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11498 }
11499}
11500
Matt Carlsona1b950d2009-09-01 13:20:17 +000011501static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
11502{
11503 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
11504 case FLASH_5752PAGE_SIZE_256:
11505 tp->nvram_pagesize = 256;
11506 break;
11507 case FLASH_5752PAGE_SIZE_512:
11508 tp->nvram_pagesize = 512;
11509 break;
11510 case FLASH_5752PAGE_SIZE_1K:
11511 tp->nvram_pagesize = 1024;
11512 break;
11513 case FLASH_5752PAGE_SIZE_2K:
11514 tp->nvram_pagesize = 2048;
11515 break;
11516 case FLASH_5752PAGE_SIZE_4K:
11517 tp->nvram_pagesize = 4096;
11518 break;
11519 case FLASH_5752PAGE_SIZE_264:
11520 tp->nvram_pagesize = 264;
11521 break;
11522 case FLASH_5752PAGE_SIZE_528:
11523 tp->nvram_pagesize = 528;
11524 break;
11525 }
11526}
11527
Michael Chan361b4ac2005-04-21 17:11:21 -070011528static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
11529{
11530 u32 nvcfg1;
11531
11532 nvcfg1 = tr32(NVRAM_CFG1);
11533
Michael Chane6af3012005-04-21 17:12:05 -070011534 /* NVRAM protection for TPM */
11535 if (nvcfg1 & (1 << 27))
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011536 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Michael Chane6af3012005-04-21 17:12:05 -070011537
Michael Chan361b4ac2005-04-21 17:11:21 -070011538 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011539 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
11540 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
11541 tp->nvram_jedecnum = JEDEC_ATMEL;
11542 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11543 break;
11544 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11545 tp->nvram_jedecnum = JEDEC_ATMEL;
11546 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11547 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11548 break;
11549 case FLASH_5752VENDOR_ST_M45PE10:
11550 case FLASH_5752VENDOR_ST_M45PE20:
11551 case FLASH_5752VENDOR_ST_M45PE40:
11552 tp->nvram_jedecnum = JEDEC_ST;
11553 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11554 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11555 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070011556 }
11557
11558 if (tp->tg3_flags2 & TG3_FLG2_FLASH) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000011559 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000011560 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070011561 /* For eeprom, set pagesize to maximum eeprom size */
11562 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11563
11564 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11565 tw32(NVRAM_CFG1, nvcfg1);
11566 }
11567}
11568
Michael Chand3c7b882006-03-23 01:28:25 -080011569static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
11570{
Matt Carlson989a9d22007-05-05 11:51:05 -070011571 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080011572
11573 nvcfg1 = tr32(NVRAM_CFG1);
11574
11575 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070011576 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011577 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson989a9d22007-05-05 11:51:05 -070011578 protect = 1;
11579 }
Michael Chand3c7b882006-03-23 01:28:25 -080011580
Matt Carlson989a9d22007-05-05 11:51:05 -070011581 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11582 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011583 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11584 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11585 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11586 case FLASH_5755VENDOR_ATMEL_FLASH_5:
11587 tp->nvram_jedecnum = JEDEC_ATMEL;
11588 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11589 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11590 tp->nvram_pagesize = 264;
11591 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
11592 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
11593 tp->nvram_size = (protect ? 0x3e200 :
11594 TG3_NVRAM_SIZE_512KB);
11595 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
11596 tp->nvram_size = (protect ? 0x1f200 :
11597 TG3_NVRAM_SIZE_256KB);
11598 else
11599 tp->nvram_size = (protect ? 0x1f200 :
11600 TG3_NVRAM_SIZE_128KB);
11601 break;
11602 case FLASH_5752VENDOR_ST_M45PE10:
11603 case FLASH_5752VENDOR_ST_M45PE20:
11604 case FLASH_5752VENDOR_ST_M45PE40:
11605 tp->nvram_jedecnum = JEDEC_ST;
11606 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11607 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11608 tp->nvram_pagesize = 256;
11609 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
11610 tp->nvram_size = (protect ?
11611 TG3_NVRAM_SIZE_64KB :
11612 TG3_NVRAM_SIZE_128KB);
11613 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
11614 tp->nvram_size = (protect ?
11615 TG3_NVRAM_SIZE_64KB :
11616 TG3_NVRAM_SIZE_256KB);
11617 else
11618 tp->nvram_size = (protect ?
11619 TG3_NVRAM_SIZE_128KB :
11620 TG3_NVRAM_SIZE_512KB);
11621 break;
Michael Chand3c7b882006-03-23 01:28:25 -080011622 }
11623}
11624
Michael Chan1b277772006-03-20 22:27:48 -080011625static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
11626{
11627 u32 nvcfg1;
11628
11629 nvcfg1 = tr32(NVRAM_CFG1);
11630
11631 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011632 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
11633 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11634 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
11635 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11636 tp->nvram_jedecnum = JEDEC_ATMEL;
11637 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11638 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080011639
Matt Carlson8590a602009-08-28 12:29:16 +000011640 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11641 tw32(NVRAM_CFG1, nvcfg1);
11642 break;
11643 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11644 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11645 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11646 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11647 tp->nvram_jedecnum = JEDEC_ATMEL;
11648 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11649 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11650 tp->nvram_pagesize = 264;
11651 break;
11652 case FLASH_5752VENDOR_ST_M45PE10:
11653 case FLASH_5752VENDOR_ST_M45PE20:
11654 case FLASH_5752VENDOR_ST_M45PE40:
11655 tp->nvram_jedecnum = JEDEC_ST;
11656 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11657 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11658 tp->nvram_pagesize = 256;
11659 break;
Michael Chan1b277772006-03-20 22:27:48 -080011660 }
11661}
11662
Matt Carlson6b91fa02007-10-10 18:01:09 -070011663static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
11664{
11665 u32 nvcfg1, protect = 0;
11666
11667 nvcfg1 = tr32(NVRAM_CFG1);
11668
11669 /* NVRAM protection for TPM */
11670 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011671 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011672 protect = 1;
11673 }
11674
11675 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11676 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011677 case FLASH_5761VENDOR_ATMEL_ADB021D:
11678 case FLASH_5761VENDOR_ATMEL_ADB041D:
11679 case FLASH_5761VENDOR_ATMEL_ADB081D:
11680 case FLASH_5761VENDOR_ATMEL_ADB161D:
11681 case FLASH_5761VENDOR_ATMEL_MDB021D:
11682 case FLASH_5761VENDOR_ATMEL_MDB041D:
11683 case FLASH_5761VENDOR_ATMEL_MDB081D:
11684 case FLASH_5761VENDOR_ATMEL_MDB161D:
11685 tp->nvram_jedecnum = JEDEC_ATMEL;
11686 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11687 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11688 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
11689 tp->nvram_pagesize = 256;
11690 break;
11691 case FLASH_5761VENDOR_ST_A_M45PE20:
11692 case FLASH_5761VENDOR_ST_A_M45PE40:
11693 case FLASH_5761VENDOR_ST_A_M45PE80:
11694 case FLASH_5761VENDOR_ST_A_M45PE16:
11695 case FLASH_5761VENDOR_ST_M_M45PE20:
11696 case FLASH_5761VENDOR_ST_M_M45PE40:
11697 case FLASH_5761VENDOR_ST_M_M45PE80:
11698 case FLASH_5761VENDOR_ST_M_M45PE16:
11699 tp->nvram_jedecnum = JEDEC_ST;
11700 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11701 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11702 tp->nvram_pagesize = 256;
11703 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011704 }
11705
11706 if (protect) {
11707 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
11708 } else {
11709 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011710 case FLASH_5761VENDOR_ATMEL_ADB161D:
11711 case FLASH_5761VENDOR_ATMEL_MDB161D:
11712 case FLASH_5761VENDOR_ST_A_M45PE16:
11713 case FLASH_5761VENDOR_ST_M_M45PE16:
11714 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
11715 break;
11716 case FLASH_5761VENDOR_ATMEL_ADB081D:
11717 case FLASH_5761VENDOR_ATMEL_MDB081D:
11718 case FLASH_5761VENDOR_ST_A_M45PE80:
11719 case FLASH_5761VENDOR_ST_M_M45PE80:
11720 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
11721 break;
11722 case FLASH_5761VENDOR_ATMEL_ADB041D:
11723 case FLASH_5761VENDOR_ATMEL_MDB041D:
11724 case FLASH_5761VENDOR_ST_A_M45PE40:
11725 case FLASH_5761VENDOR_ST_M_M45PE40:
11726 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11727 break;
11728 case FLASH_5761VENDOR_ATMEL_ADB021D:
11729 case FLASH_5761VENDOR_ATMEL_MDB021D:
11730 case FLASH_5761VENDOR_ST_A_M45PE20:
11731 case FLASH_5761VENDOR_ST_M_M45PE20:
11732 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11733 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011734 }
11735 }
11736}
11737
Michael Chanb5d37722006-09-27 16:06:21 -070011738static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
11739{
11740 tp->nvram_jedecnum = JEDEC_ATMEL;
11741 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11742 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11743}
11744
Matt Carlson321d32a2008-11-21 17:22:19 -080011745static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
11746{
11747 u32 nvcfg1;
11748
11749 nvcfg1 = tr32(NVRAM_CFG1);
11750
11751 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11752 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11753 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11754 tp->nvram_jedecnum = JEDEC_ATMEL;
11755 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11756 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11757
11758 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11759 tw32(NVRAM_CFG1, nvcfg1);
11760 return;
11761 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11762 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11763 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11764 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11765 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11766 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11767 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11768 tp->nvram_jedecnum = JEDEC_ATMEL;
11769 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11770 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11771
11772 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11773 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11774 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11775 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11776 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11777 break;
11778 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11779 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11780 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11781 break;
11782 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11783 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11784 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11785 break;
11786 }
11787 break;
11788 case FLASH_5752VENDOR_ST_M45PE10:
11789 case FLASH_5752VENDOR_ST_M45PE20:
11790 case FLASH_5752VENDOR_ST_M45PE40:
11791 tp->nvram_jedecnum = JEDEC_ST;
11792 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11793 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11794
11795 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11796 case FLASH_5752VENDOR_ST_M45PE10:
11797 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11798 break;
11799 case FLASH_5752VENDOR_ST_M45PE20:
11800 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11801 break;
11802 case FLASH_5752VENDOR_ST_M45PE40:
11803 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11804 break;
11805 }
11806 break;
11807 default:
Matt Carlsondf259d82009-04-20 06:57:14 +000011808 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
Matt Carlson321d32a2008-11-21 17:22:19 -080011809 return;
11810 }
11811
Matt Carlsona1b950d2009-09-01 13:20:17 +000011812 tg3_nvram_get_pagesize(tp, nvcfg1);
11813 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Matt Carlson321d32a2008-11-21 17:22:19 -080011814 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011815}
11816
11817
11818static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
11819{
11820 u32 nvcfg1;
11821
11822 nvcfg1 = tr32(NVRAM_CFG1);
11823
11824 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11825 case FLASH_5717VENDOR_ATMEL_EEPROM:
11826 case FLASH_5717VENDOR_MICRO_EEPROM:
11827 tp->nvram_jedecnum = JEDEC_ATMEL;
11828 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11829 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11830
11831 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11832 tw32(NVRAM_CFG1, nvcfg1);
11833 return;
11834 case FLASH_5717VENDOR_ATMEL_MDB011D:
11835 case FLASH_5717VENDOR_ATMEL_ADB011B:
11836 case FLASH_5717VENDOR_ATMEL_ADB011D:
11837 case FLASH_5717VENDOR_ATMEL_MDB021D:
11838 case FLASH_5717VENDOR_ATMEL_ADB021B:
11839 case FLASH_5717VENDOR_ATMEL_ADB021D:
11840 case FLASH_5717VENDOR_ATMEL_45USPT:
11841 tp->nvram_jedecnum = JEDEC_ATMEL;
11842 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11843 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11844
11845 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11846 case FLASH_5717VENDOR_ATMEL_MDB021D:
11847 case FLASH_5717VENDOR_ATMEL_ADB021B:
11848 case FLASH_5717VENDOR_ATMEL_ADB021D:
11849 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11850 break;
11851 default:
11852 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11853 break;
11854 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011855 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011856 case FLASH_5717VENDOR_ST_M_M25PE10:
11857 case FLASH_5717VENDOR_ST_A_M25PE10:
11858 case FLASH_5717VENDOR_ST_M_M45PE10:
11859 case FLASH_5717VENDOR_ST_A_M45PE10:
11860 case FLASH_5717VENDOR_ST_M_M25PE20:
11861 case FLASH_5717VENDOR_ST_A_M25PE20:
11862 case FLASH_5717VENDOR_ST_M_M45PE20:
11863 case FLASH_5717VENDOR_ST_A_M45PE20:
11864 case FLASH_5717VENDOR_ST_25USPT:
11865 case FLASH_5717VENDOR_ST_45USPT:
11866 tp->nvram_jedecnum = JEDEC_ST;
11867 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11868 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11869
11870 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11871 case FLASH_5717VENDOR_ST_M_M25PE20:
11872 case FLASH_5717VENDOR_ST_A_M25PE20:
11873 case FLASH_5717VENDOR_ST_M_M45PE20:
11874 case FLASH_5717VENDOR_ST_A_M45PE20:
11875 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11876 break;
11877 default:
11878 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11879 break;
11880 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011881 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011882 default:
11883 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
11884 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080011885 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000011886
11887 tg3_nvram_get_pagesize(tp, nvcfg1);
11888 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
11889 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlson321d32a2008-11-21 17:22:19 -080011890}
11891
Matt Carlson9b91b5f2011-04-05 14:22:47 +000011892static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
11893{
11894 u32 nvcfg1, nvmpinstrp;
11895
11896 nvcfg1 = tr32(NVRAM_CFG1);
11897 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
11898
11899 switch (nvmpinstrp) {
11900 case FLASH_5720_EEPROM_HD:
11901 case FLASH_5720_EEPROM_LD:
11902 tp->nvram_jedecnum = JEDEC_ATMEL;
11903 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11904
11905 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11906 tw32(NVRAM_CFG1, nvcfg1);
11907 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
11908 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11909 else
11910 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
11911 return;
11912 case FLASH_5720VENDOR_M_ATMEL_DB011D:
11913 case FLASH_5720VENDOR_A_ATMEL_DB011B:
11914 case FLASH_5720VENDOR_A_ATMEL_DB011D:
11915 case FLASH_5720VENDOR_M_ATMEL_DB021D:
11916 case FLASH_5720VENDOR_A_ATMEL_DB021B:
11917 case FLASH_5720VENDOR_A_ATMEL_DB021D:
11918 case FLASH_5720VENDOR_M_ATMEL_DB041D:
11919 case FLASH_5720VENDOR_A_ATMEL_DB041B:
11920 case FLASH_5720VENDOR_A_ATMEL_DB041D:
11921 case FLASH_5720VENDOR_M_ATMEL_DB081D:
11922 case FLASH_5720VENDOR_A_ATMEL_DB081D:
11923 case FLASH_5720VENDOR_ATMEL_45USPT:
11924 tp->nvram_jedecnum = JEDEC_ATMEL;
11925 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11926 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11927
11928 switch (nvmpinstrp) {
11929 case FLASH_5720VENDOR_M_ATMEL_DB021D:
11930 case FLASH_5720VENDOR_A_ATMEL_DB021B:
11931 case FLASH_5720VENDOR_A_ATMEL_DB021D:
11932 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11933 break;
11934 case FLASH_5720VENDOR_M_ATMEL_DB041D:
11935 case FLASH_5720VENDOR_A_ATMEL_DB041B:
11936 case FLASH_5720VENDOR_A_ATMEL_DB041D:
11937 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11938 break;
11939 case FLASH_5720VENDOR_M_ATMEL_DB081D:
11940 case FLASH_5720VENDOR_A_ATMEL_DB081D:
11941 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
11942 break;
11943 default:
11944 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11945 break;
11946 }
11947 break;
11948 case FLASH_5720VENDOR_M_ST_M25PE10:
11949 case FLASH_5720VENDOR_M_ST_M45PE10:
11950 case FLASH_5720VENDOR_A_ST_M25PE10:
11951 case FLASH_5720VENDOR_A_ST_M45PE10:
11952 case FLASH_5720VENDOR_M_ST_M25PE20:
11953 case FLASH_5720VENDOR_M_ST_M45PE20:
11954 case FLASH_5720VENDOR_A_ST_M25PE20:
11955 case FLASH_5720VENDOR_A_ST_M45PE20:
11956 case FLASH_5720VENDOR_M_ST_M25PE40:
11957 case FLASH_5720VENDOR_M_ST_M45PE40:
11958 case FLASH_5720VENDOR_A_ST_M25PE40:
11959 case FLASH_5720VENDOR_A_ST_M45PE40:
11960 case FLASH_5720VENDOR_M_ST_M25PE80:
11961 case FLASH_5720VENDOR_M_ST_M45PE80:
11962 case FLASH_5720VENDOR_A_ST_M25PE80:
11963 case FLASH_5720VENDOR_A_ST_M45PE80:
11964 case FLASH_5720VENDOR_ST_25USPT:
11965 case FLASH_5720VENDOR_ST_45USPT:
11966 tp->nvram_jedecnum = JEDEC_ST;
11967 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11968 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11969
11970 switch (nvmpinstrp) {
11971 case FLASH_5720VENDOR_M_ST_M25PE20:
11972 case FLASH_5720VENDOR_M_ST_M45PE20:
11973 case FLASH_5720VENDOR_A_ST_M25PE20:
11974 case FLASH_5720VENDOR_A_ST_M45PE20:
11975 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11976 break;
11977 case FLASH_5720VENDOR_M_ST_M25PE40:
11978 case FLASH_5720VENDOR_M_ST_M45PE40:
11979 case FLASH_5720VENDOR_A_ST_M25PE40:
11980 case FLASH_5720VENDOR_A_ST_M45PE40:
11981 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11982 break;
11983 case FLASH_5720VENDOR_M_ST_M25PE80:
11984 case FLASH_5720VENDOR_M_ST_M45PE80:
11985 case FLASH_5720VENDOR_A_ST_M25PE80:
11986 case FLASH_5720VENDOR_A_ST_M45PE80:
11987 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
11988 break;
11989 default:
11990 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11991 break;
11992 }
11993 break;
11994 default:
11995 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
11996 return;
11997 }
11998
11999 tg3_nvram_get_pagesize(tp, nvcfg1);
12000 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
12001 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
12002}
12003
Linus Torvalds1da177e2005-04-16 15:20:36 -070012004/* Chips other than 5700/5701 use the NVRAM for fetching info. */
12005static void __devinit tg3_nvram_init(struct tg3 *tp)
12006{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012007 tw32_f(GRC_EEPROM_ADDR,
12008 (EEPROM_ADDR_FSM_RESET |
12009 (EEPROM_DEFAULT_CLOCK_PERIOD <<
12010 EEPROM_ADDR_CLKPERD_SHIFT)));
12011
Michael Chan9d57f012006-12-07 00:23:25 -080012012 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012013
12014 /* Enable seeprom accesses. */
12015 tw32_f(GRC_LOCAL_CTRL,
12016 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
12017 udelay(100);
12018
12019 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12020 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
12021 tp->tg3_flags |= TG3_FLAG_NVRAM;
12022
Michael Chanec41c7d2006-01-17 02:40:55 -080012023 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000012024 netdev_warn(tp->dev,
12025 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000012026 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080012027 return;
12028 }
Michael Chane6af3012005-04-21 17:12:05 -070012029 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012030
Matt Carlson989a9d22007-05-05 11:51:05 -070012031 tp->nvram_size = 0;
12032
Michael Chan361b4ac2005-04-21 17:11:21 -070012033 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12034 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080012035 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
12036 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070012037 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012038 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12039 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080012040 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012041 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
12042 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070012043 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12044 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000012045 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
12046 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080012047 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012048 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12049 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000012050 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012051 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
12052 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070012053 else
12054 tg3_get_nvram_info(tp);
12055
Matt Carlson989a9d22007-05-05 11:51:05 -070012056 if (tp->nvram_size == 0)
12057 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012058
Michael Chane6af3012005-04-21 17:12:05 -070012059 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080012060 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012061
12062 } else {
12063 tp->tg3_flags &= ~(TG3_FLAG_NVRAM | TG3_FLAG_NVRAM_BUFFERED);
12064
12065 tg3_get_eeprom_size(tp);
12066 }
12067}
12068
Linus Torvalds1da177e2005-04-16 15:20:36 -070012069static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
12070 u32 offset, u32 len, u8 *buf)
12071{
12072 int i, j, rc = 0;
12073 u32 val;
12074
12075 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012076 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012077 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012078
12079 addr = offset + i;
12080
12081 memcpy(&data, buf + i, 4);
12082
Matt Carlson62cedd12009-04-20 14:52:29 -070012083 /*
12084 * The SEEPROM interface expects the data to always be opposite
12085 * the native endian format. We accomplish this by reversing
12086 * all the operations that would have been performed on the
12087 * data from a call to tg3_nvram_read_be32().
12088 */
12089 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012090
12091 val = tr32(GRC_EEPROM_ADDR);
12092 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
12093
12094 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
12095 EEPROM_ADDR_READ);
12096 tw32(GRC_EEPROM_ADDR, val |
12097 (0 << EEPROM_ADDR_DEVID_SHIFT) |
12098 (addr & EEPROM_ADDR_ADDR_MASK) |
12099 EEPROM_ADDR_START |
12100 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012101
Michael Chan9d57f012006-12-07 00:23:25 -080012102 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012103 val = tr32(GRC_EEPROM_ADDR);
12104
12105 if (val & EEPROM_ADDR_COMPLETE)
12106 break;
Michael Chan9d57f012006-12-07 00:23:25 -080012107 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012108 }
12109 if (!(val & EEPROM_ADDR_COMPLETE)) {
12110 rc = -EBUSY;
12111 break;
12112 }
12113 }
12114
12115 return rc;
12116}
12117
12118/* offset and length are dword aligned */
12119static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
12120 u8 *buf)
12121{
12122 int ret = 0;
12123 u32 pagesize = tp->nvram_pagesize;
12124 u32 pagemask = pagesize - 1;
12125 u32 nvram_cmd;
12126 u8 *tmp;
12127
12128 tmp = kmalloc(pagesize, GFP_KERNEL);
12129 if (tmp == NULL)
12130 return -ENOMEM;
12131
12132 while (len) {
12133 int j;
Michael Chane6af3012005-04-21 17:12:05 -070012134 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012135
12136 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012137
Linus Torvalds1da177e2005-04-16 15:20:36 -070012138 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012139 ret = tg3_nvram_read_be32(tp, phy_addr + j,
12140 (__be32 *) (tmp + j));
12141 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012142 break;
12143 }
12144 if (ret)
12145 break;
12146
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012147 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012148 size = pagesize;
12149 if (len < size)
12150 size = len;
12151
12152 len -= size;
12153
12154 memcpy(tmp + page_off, buf, size);
12155
12156 offset = offset + (pagesize - page_off);
12157
Michael Chane6af3012005-04-21 17:12:05 -070012158 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012159
12160 /*
12161 * Before we can erase the flash page, we need
12162 * to issue a special "write enable" command.
12163 */
12164 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12165
12166 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12167 break;
12168
12169 /* Erase the target page */
12170 tw32(NVRAM_ADDR, phy_addr);
12171
12172 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
12173 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
12174
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012175 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012176 break;
12177
12178 /* Issue another write enable to start the write. */
12179 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12180
12181 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12182 break;
12183
12184 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012185 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012186
Al Virob9fc7dc2007-12-17 22:59:57 -080012187 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000012188
Al Virob9fc7dc2007-12-17 22:59:57 -080012189 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012190
12191 tw32(NVRAM_ADDR, phy_addr + j);
12192
12193 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
12194 NVRAM_CMD_WR;
12195
12196 if (j == 0)
12197 nvram_cmd |= NVRAM_CMD_FIRST;
12198 else if (j == (pagesize - 4))
12199 nvram_cmd |= NVRAM_CMD_LAST;
12200
12201 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12202 break;
12203 }
12204 if (ret)
12205 break;
12206 }
12207
12208 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12209 tg3_nvram_exec_cmd(tp, nvram_cmd);
12210
12211 kfree(tmp);
12212
12213 return ret;
12214}
12215
12216/* offset and length are dword aligned */
12217static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
12218 u8 *buf)
12219{
12220 int i, ret = 0;
12221
12222 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012223 u32 page_off, phy_addr, nvram_cmd;
12224 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012225
12226 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080012227 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012228
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012229 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012230
Michael Chan18201802006-03-20 22:29:15 -080012231 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012232
12233 tw32(NVRAM_ADDR, phy_addr);
12234
12235 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
12236
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012237 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012238 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070012239 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012240 nvram_cmd |= NVRAM_CMD_LAST;
12241
12242 if (i == (len - 4))
12243 nvram_cmd |= NVRAM_CMD_LAST;
12244
Matt Carlson321d32a2008-11-21 17:22:19 -080012245 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
12246 !(tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070012247 (tp->nvram_jedecnum == JEDEC_ST) &&
12248 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012249
12250 if ((ret = tg3_nvram_exec_cmd(tp,
12251 NVRAM_CMD_WREN | NVRAM_CMD_GO |
12252 NVRAM_CMD_DONE)))
12253
12254 break;
12255 }
12256 if (!(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
12257 /* We always do complete word writes to eeprom. */
12258 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
12259 }
12260
12261 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12262 break;
12263 }
12264 return ret;
12265}
12266
12267/* offset and length are dword aligned */
12268static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
12269{
12270 int ret;
12271
Linus Torvalds1da177e2005-04-16 15:20:36 -070012272 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070012273 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
12274 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012275 udelay(40);
12276 }
12277
12278 if (!(tp->tg3_flags & TG3_FLAG_NVRAM)) {
12279 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a588792010-04-05 10:19:28 +000012280 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012281 u32 grc_mode;
12282
Michael Chanec41c7d2006-01-17 02:40:55 -080012283 ret = tg3_nvram_lock(tp);
12284 if (ret)
12285 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012286
Michael Chane6af3012005-04-21 17:12:05 -070012287 tg3_enable_nvram_access(tp);
12288 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +000012289 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012290 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012291
12292 grc_mode = tr32(GRC_MODE);
12293 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
12294
12295 if ((tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) ||
12296 !(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
12297
12298 ret = tg3_nvram_write_block_buffered(tp, offset, len,
12299 buf);
Matt Carlson859a588792010-04-05 10:19:28 +000012300 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012301 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
12302 buf);
12303 }
12304
12305 grc_mode = tr32(GRC_MODE);
12306 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
12307
Michael Chane6af3012005-04-21 17:12:05 -070012308 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012309 tg3_nvram_unlock(tp);
12310 }
12311
12312 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070012313 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012314 udelay(40);
12315 }
12316
12317 return ret;
12318}
12319
12320struct subsys_tbl_ent {
12321 u16 subsys_vendor, subsys_devid;
12322 u32 phy_id;
12323};
12324
Matt Carlson24daf2b2010-02-17 15:17:02 +000012325static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012326 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012327 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012328 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012329 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012330 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012331 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012332 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012333 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12334 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
12335 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012336 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012337 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012338 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012339 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12340 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
12341 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012342 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012343 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012344 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012345 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012346 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012347 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012348 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012349
12350 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012351 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012352 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012353 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012354 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012355 { TG3PCI_SUBVENDOR_ID_3COM,
12356 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
12357 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012358 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012359 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012360 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012361
12362 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012363 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012364 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012365 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012366 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012367 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012368 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012369 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012370 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012371
12372 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012373 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012374 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012375 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012376 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012377 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12378 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
12379 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012380 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012381 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012382 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012383
12384 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012385 { TG3PCI_SUBVENDOR_ID_IBM,
12386 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012387};
12388
Matt Carlson24daf2b2010-02-17 15:17:02 +000012389static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012390{
12391 int i;
12392
12393 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
12394 if ((subsys_id_to_phy_id[i].subsys_vendor ==
12395 tp->pdev->subsystem_vendor) &&
12396 (subsys_id_to_phy_id[i].subsys_devid ==
12397 tp->pdev->subsystem_device))
12398 return &subsys_id_to_phy_id[i];
12399 }
12400 return NULL;
12401}
12402
Michael Chan7d0c41e2005-04-21 17:06:20 -070012403static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012404{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012405 u32 val;
Michael Chancaf636c72006-03-22 01:05:31 -080012406 u16 pmcsr;
12407
12408 /* On some early chips the SRAM cannot be accessed in D3hot state,
12409 * so need make sure we're in D0.
12410 */
12411 pci_read_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, &pmcsr);
12412 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
12413 pci_write_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, pmcsr);
12414 msleep(1);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012415
12416 /* Make sure register accesses (indirect or otherwise)
12417 * will function correctly.
12418 */
12419 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12420 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012421
David S. Millerf49639e2006-06-09 11:58:36 -070012422 /* The memory arbiter has to be enabled in order for SRAM accesses
12423 * to succeed. Normally on powerup the tg3 chip firmware will make
12424 * sure it is enabled, but other entities such as system netboot
12425 * code might disable it.
12426 */
12427 val = tr32(MEMARB_MODE);
12428 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
12429
Matt Carlson79eb6902010-02-17 15:17:03 +000012430 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012431 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12432
Gary Zambranoa85feb82007-05-05 11:52:19 -070012433 /* Assume an onboard device and WOL capable by default. */
12434 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT | TG3_FLAG_WOL_CAP;
David S. Miller72b845e2006-03-14 14:11:48 -080012435
Michael Chanb5d37722006-09-27 16:06:21 -070012436 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012437 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Michael Chanb5d37722006-09-27 16:06:21 -070012438 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012439 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12440 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012441 val = tr32(VCPU_CFGSHDW);
12442 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Matt Carlson8ed5d972007-05-07 00:25:49 -070012443 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
Matt Carlson0527ba32007-10-10 18:03:30 -070012444 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Matt Carlson20232762008-12-21 20:18:56 -080012445 (val & VCPU_CFGSHDW_WOL_MAGPKT))
Matt Carlson0527ba32007-10-10 18:03:30 -070012446 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012447 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070012448 }
12449
Linus Torvalds1da177e2005-04-16 15:20:36 -070012450 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12451 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12452 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012453 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012454 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012455
12456 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12457 tp->nic_sram_data_cfg = nic_cfg;
12458
12459 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
12460 ver >>= NIC_SRAM_DATA_VER_SHIFT;
12461 if ((GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700) &&
12462 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) &&
12463 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703) &&
12464 (ver > 0) && (ver < 0x100))
12465 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
12466
Matt Carlsona9daf362008-05-25 23:49:44 -070012467 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
12468 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
12469
Linus Torvalds1da177e2005-04-16 15:20:36 -070012470 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
12471 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
12472 eeprom_phy_serdes = 1;
12473
12474 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
12475 if (nic_phy_id != 0) {
12476 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
12477 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
12478
12479 eeprom_phy_id = (id1 >> 16) << 10;
12480 eeprom_phy_id |= (id2 & 0xfc00) << 16;
12481 eeprom_phy_id |= (id2 & 0x03ff) << 0;
12482 } else
12483 eeprom_phy_id = 0;
12484
Michael Chan7d0c41e2005-04-21 17:06:20 -070012485 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070012486 if (eeprom_phy_serdes) {
Matt Carlsona50d0792010-06-05 17:24:37 +000012487 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012488 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000012489 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012490 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070012491 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070012492
John W. Linvillecbf46852005-04-21 17:01:29 -070012493 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012494 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
12495 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070012496 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070012497 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
12498
12499 switch (led_cfg) {
12500 default:
12501 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
12502 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12503 break;
12504
12505 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
12506 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12507 break;
12508
12509 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
12510 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070012511
12512 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
12513 * read on some older 5700/5701 bootcode.
12514 */
12515 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
12516 ASIC_REV_5700 ||
12517 GET_ASIC_REV(tp->pci_chip_rev_id) ==
12518 ASIC_REV_5701)
12519 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12520
Linus Torvalds1da177e2005-04-16 15:20:36 -070012521 break;
12522
12523 case SHASTA_EXT_LED_SHARED:
12524 tp->led_ctrl = LED_CTRL_MODE_SHARED;
12525 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
12526 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
12527 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12528 LED_CTRL_MODE_PHY_2);
12529 break;
12530
12531 case SHASTA_EXT_LED_MAC:
12532 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
12533 break;
12534
12535 case SHASTA_EXT_LED_COMBO:
12536 tp->led_ctrl = LED_CTRL_MODE_COMBO;
12537 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
12538 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12539 LED_CTRL_MODE_PHY_2);
12540 break;
12541
Stephen Hemminger855e1112008-04-16 16:37:28 -070012542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012543
12544 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12545 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
12546 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
12547 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12548
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012549 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
12550 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080012551
Michael Chan9d26e212006-12-07 00:21:14 -080012552 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012553 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012554 if ((tp->pdev->subsystem_vendor ==
12555 PCI_VENDOR_ID_ARIMA) &&
12556 (tp->pdev->subsystem_device == 0x205a ||
12557 tp->pdev->subsystem_device == 0x2063))
12558 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
12559 } else {
David S. Millerf49639e2006-06-09 11:58:36 -070012560 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012561 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012563
12564 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
12565 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
John W. Linvillecbf46852005-04-21 17:01:29 -070012566 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012567 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
12568 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012569
12570 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
12571 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Matt Carlson0d3031d2007-10-10 18:02:43 -070012572 tp->tg3_flags3 |= TG3_FLG3_ENABLE_APE;
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012573
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012574 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070012575 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
12576 tp->tg3_flags &= ~TG3_FLAG_WOL_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012577
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070012578 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012579 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE))
Matt Carlson0527ba32007-10-10 18:03:30 -070012580 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
12581
Linus Torvalds1da177e2005-04-16 15:20:36 -070012582 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012583 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012584
12585 /* serdes signal pre-emphasis in register 0x590 set by */
12586 /* bootcode if bit 18 is set */
12587 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012588 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070012589
Matt Carlson1407deb2011-04-05 14:22:44 +000012590 if (((tp->tg3_flags3 & TG3_FLG3_57765_PLUS) ||
Matt Carlson2e1e3292010-11-24 08:31:53 +000012591 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
12592 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX))) &&
Matt Carlson6833c042008-11-21 17:18:59 -080012593 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012594 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080012595
Matt Carlson8c69b1e2010-08-02 11:26:00 +000012596 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
12597 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlson1407deb2011-04-05 14:22:44 +000012598 !(tp->tg3_flags3 & TG3_FLG3_57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070012599 u32 cfg3;
12600
12601 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
12602 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
12603 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
12604 }
Matt Carlsona9daf362008-05-25 23:49:44 -070012605
Matt Carlson14417062010-02-17 15:16:59 +000012606 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
12607 tp->tg3_flags3 |= TG3_FLG3_RGMII_INBAND_DISABLE;
Matt Carlsona9daf362008-05-25 23:49:44 -070012608 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
12609 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_RX_EN;
12610 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
12611 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_TX_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012612 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012613done:
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000012614 if (tp->tg3_flags & TG3_FLAG_WOL_CAP)
12615 device_set_wakeup_enable(&tp->pdev->dev,
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012616 tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000012617 else
12618 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012619}
12620
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012621static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
12622{
12623 int i;
12624 u32 val;
12625
12626 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
12627 tw32(OTP_CTRL, cmd);
12628
12629 /* Wait for up to 1 ms for command to execute. */
12630 for (i = 0; i < 100; i++) {
12631 val = tr32(OTP_STATUS);
12632 if (val & OTP_STATUS_CMD_DONE)
12633 break;
12634 udelay(10);
12635 }
12636
12637 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
12638}
12639
12640/* Read the gphy configuration from the OTP region of the chip. The gphy
12641 * configuration is a 32-bit value that straddles the alignment boundary.
12642 * We do two 32-bit reads and then shift and merge the results.
12643 */
12644static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
12645{
12646 u32 bhalf_otp, thalf_otp;
12647
12648 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
12649
12650 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
12651 return 0;
12652
12653 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
12654
12655 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12656 return 0;
12657
12658 thalf_otp = tr32(OTP_READ_DATA);
12659
12660 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
12661
12662 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12663 return 0;
12664
12665 bhalf_otp = tr32(OTP_READ_DATA);
12666
12667 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
12668}
12669
Matt Carlsone256f8a2011-03-09 16:58:24 +000012670static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
12671{
12672 u32 adv = ADVERTISED_Autoneg |
12673 ADVERTISED_Pause;
12674
12675 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
12676 adv |= ADVERTISED_1000baseT_Half |
12677 ADVERTISED_1000baseT_Full;
12678
12679 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
12680 adv |= ADVERTISED_100baseT_Half |
12681 ADVERTISED_100baseT_Full |
12682 ADVERTISED_10baseT_Half |
12683 ADVERTISED_10baseT_Full |
12684 ADVERTISED_TP;
12685 else
12686 adv |= ADVERTISED_FIBRE;
12687
12688 tp->link_config.advertising = adv;
12689 tp->link_config.speed = SPEED_INVALID;
12690 tp->link_config.duplex = DUPLEX_INVALID;
12691 tp->link_config.autoneg = AUTONEG_ENABLE;
12692 tp->link_config.active_speed = SPEED_INVALID;
12693 tp->link_config.active_duplex = DUPLEX_INVALID;
12694 tp->link_config.orig_speed = SPEED_INVALID;
12695 tp->link_config.orig_duplex = DUPLEX_INVALID;
12696 tp->link_config.orig_autoneg = AUTONEG_INVALID;
12697}
12698
Michael Chan7d0c41e2005-04-21 17:06:20 -070012699static int __devinit tg3_phy_probe(struct tg3 *tp)
12700{
12701 u32 hw_phy_id_1, hw_phy_id_2;
12702 u32 hw_phy_id, hw_phy_id_masked;
12703 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012704
Matt Carlsone256f8a2011-03-09 16:58:24 +000012705 /* flow control autonegotiation is default behavior */
12706 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
12707 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
12708
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012709 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
12710 return tg3_phy_init(tp);
12711
Linus Torvalds1da177e2005-04-16 15:20:36 -070012712 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010012713 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012714 */
12715 err = 0;
Matt Carlson0d3031d2007-10-10 18:02:43 -070012716 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
12717 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000012718 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012719 } else {
12720 /* Now read the physical PHY_ID from the chip and verify
12721 * that it is sane. If it doesn't look good, we fall back
12722 * to either the hard-coded table based PHY_ID and failing
12723 * that the value found in the eeprom area.
12724 */
12725 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
12726 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
12727
12728 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
12729 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
12730 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
12731
Matt Carlson79eb6902010-02-17 15:17:03 +000012732 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012733 }
12734
Matt Carlson79eb6902010-02-17 15:17:03 +000012735 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012736 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000012737 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012738 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070012739 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012740 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012741 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000012742 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070012743 /* Do nothing, phy ID already set up in
12744 * tg3_get_eeprom_hw_cfg().
12745 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012746 } else {
12747 struct subsys_tbl_ent *p;
12748
12749 /* No eeprom signature? Try the hardcoded
12750 * subsys device table.
12751 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012752 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012753 if (!p)
12754 return -ENODEV;
12755
12756 tp->phy_id = p->phy_id;
12757 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000012758 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012759 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012760 }
12761 }
12762
Matt Carlsona6b68da2010-12-06 08:28:52 +000012763 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
12764 ((tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
12765 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
12766 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12767 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000012768 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
12769
Matt Carlsone256f8a2011-03-09 16:58:24 +000012770 tg3_phy_init_link_config(tp);
12771
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012772 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlson0d3031d2007-10-10 18:02:43 -070012773 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012774 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan3600d912006-12-07 00:21:48 -080012775 u32 bmsr, adv_reg, tg3_ctrl, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012776
12777 tg3_readphy(tp, MII_BMSR, &bmsr);
12778 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
12779 (bmsr & BMSR_LSTATUS))
12780 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012781
Linus Torvalds1da177e2005-04-16 15:20:36 -070012782 err = tg3_phy_reset(tp);
12783 if (err)
12784 return err;
12785
12786 adv_reg = (ADVERTISE_10HALF | ADVERTISE_10FULL |
12787 ADVERTISE_100HALF | ADVERTISE_100FULL |
12788 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
12789 tg3_ctrl = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012790 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012791 tg3_ctrl = (MII_TG3_CTRL_ADV_1000_HALF |
12792 MII_TG3_CTRL_ADV_1000_FULL);
12793 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
12794 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
12795 tg3_ctrl |= (MII_TG3_CTRL_AS_MASTER |
12796 MII_TG3_CTRL_ENABLE_AS_MASTER);
12797 }
12798
Michael Chan3600d912006-12-07 00:21:48 -080012799 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
12800 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
12801 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
12802 if (!tg3_copper_is_advertising_all(tp, mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012803 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
12804
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012805 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012806 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12807
12808 tg3_writephy(tp, MII_BMCR,
12809 BMCR_ANENABLE | BMCR_ANRESTART);
12810 }
12811 tg3_phy_set_wirespeed(tp);
12812
12813 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012814 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012815 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12816 }
12817
12818skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000012819 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012820 err = tg3_init_5401phy_dsp(tp);
12821 if (err)
12822 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012823
Linus Torvalds1da177e2005-04-16 15:20:36 -070012824 err = tg3_init_5401phy_dsp(tp);
12825 }
12826
Linus Torvalds1da177e2005-04-16 15:20:36 -070012827 return err;
12828}
12829
Matt Carlson184b8902010-04-05 10:19:25 +000012830static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012831{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000012832 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012833 unsigned int block_end, rosize, len;
Matt Carlson184b8902010-04-05 10:19:25 +000012834 int j, i = 0;
Michael Chan1b277772006-03-20 22:27:48 -080012835 u32 magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012836
Matt Carlsondf259d82009-04-20 06:57:14 +000012837 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
12838 tg3_nvram_read(tp, 0x0, &magic))
Matt Carlsona4a8bb12010-09-15 09:00:00 +000012839 goto out_no_vpd;
12840
12841 vpd_data = kmalloc(TG3_NVM_VPD_LEN, GFP_KERNEL);
12842 if (!vpd_data)
12843 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012844
Michael Chan18201802006-03-20 22:29:15 -080012845 if (magic == TG3_EEPROM_MAGIC) {
Matt Carlson141518c2009-12-03 08:36:22 +000012846 for (i = 0; i < TG3_NVM_VPD_LEN; i += 4) {
Michael Chan1b277772006-03-20 22:27:48 -080012847 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012848
Matt Carlson6d348f22009-02-25 14:25:52 +000012849 /* The data is in little-endian format in NVRAM.
12850 * Use the big-endian read routines to preserve
12851 * the byte order as it exists in NVRAM.
12852 */
Matt Carlson141518c2009-12-03 08:36:22 +000012853 if (tg3_nvram_read_be32(tp, TG3_NVM_VPD_OFF + i, &tmp))
Michael Chan1b277772006-03-20 22:27:48 -080012854 goto out_not_found;
12855
Matt Carlson6d348f22009-02-25 14:25:52 +000012856 memcpy(&vpd_data[i], &tmp, sizeof(tmp));
Michael Chan1b277772006-03-20 22:27:48 -080012857 }
12858 } else {
Matt Carlson94c982b2009-12-03 08:36:23 +000012859 ssize_t cnt;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012860 unsigned int pos = 0;
Michael Chan1b277772006-03-20 22:27:48 -080012861
Matt Carlson94c982b2009-12-03 08:36:23 +000012862 for (; pos < TG3_NVM_VPD_LEN && i < 3; i++, pos += cnt) {
12863 cnt = pci_read_vpd(tp->pdev, pos,
12864 TG3_NVM_VPD_LEN - pos,
12865 &vpd_data[pos]);
David Sterba824f5f32010-12-29 03:40:31 +000012866 if (cnt == -ETIMEDOUT || cnt == -EINTR)
Matt Carlson94c982b2009-12-03 08:36:23 +000012867 cnt = 0;
12868 else if (cnt < 0)
David S. Millerf49639e2006-06-09 11:58:36 -070012869 goto out_not_found;
Michael Chan1b277772006-03-20 22:27:48 -080012870 }
Matt Carlson94c982b2009-12-03 08:36:23 +000012871 if (pos != TG3_NVM_VPD_LEN)
12872 goto out_not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012873 }
12874
Matt Carlson4181b2c2010-02-26 14:04:45 +000012875 i = pci_vpd_find_tag(vpd_data, 0, TG3_NVM_VPD_LEN,
12876 PCI_VPD_LRDT_RO_DATA);
12877 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012878 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012879
12880 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
12881 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
12882 i += PCI_VPD_LRDT_TAG_SIZE;
12883
12884 if (block_end > TG3_NVM_VPD_LEN)
12885 goto out_not_found;
12886
Matt Carlson184b8902010-04-05 10:19:25 +000012887 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12888 PCI_VPD_RO_KEYWORD_MFR_ID);
12889 if (j > 0) {
12890 len = pci_vpd_info_field_size(&vpd_data[j]);
12891
12892 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12893 if (j + len > block_end || len != 4 ||
12894 memcmp(&vpd_data[j], "1028", 4))
12895 goto partno;
12896
12897 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12898 PCI_VPD_RO_KEYWORD_VENDOR0);
12899 if (j < 0)
12900 goto partno;
12901
12902 len = pci_vpd_info_field_size(&vpd_data[j]);
12903
12904 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12905 if (j + len > block_end)
12906 goto partno;
12907
12908 memcpy(tp->fw_ver, &vpd_data[j], len);
12909 strncat(tp->fw_ver, " bc ", TG3_NVM_VPD_LEN - len - 1);
12910 }
12911
12912partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000012913 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12914 PCI_VPD_RO_KEYWORD_PARTNO);
12915 if (i < 0)
12916 goto out_not_found;
12917
12918 len = pci_vpd_info_field_size(&vpd_data[i]);
12919
12920 i += PCI_VPD_INFO_FLD_HDR_SIZE;
12921 if (len > TG3_BPN_SIZE ||
12922 (len + i) > TG3_NVM_VPD_LEN)
12923 goto out_not_found;
12924
12925 memcpy(tp->board_part_number, &vpd_data[i], len);
12926
Linus Torvalds1da177e2005-04-16 15:20:36 -070012927out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000012928 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000012929 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000012930 return;
12931
12932out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000012933 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
12934 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
12935 strcpy(tp->board_part_number, "BCM5717");
12936 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
12937 strcpy(tp->board_part_number, "BCM5718");
12938 else
12939 goto nomatch;
12940 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
12941 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
12942 strcpy(tp->board_part_number, "BCM57780");
12943 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
12944 strcpy(tp->board_part_number, "BCM57760");
12945 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
12946 strcpy(tp->board_part_number, "BCM57790");
12947 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
12948 strcpy(tp->board_part_number, "BCM57788");
12949 else
12950 goto nomatch;
12951 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
12952 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
12953 strcpy(tp->board_part_number, "BCM57761");
12954 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
12955 strcpy(tp->board_part_number, "BCM57765");
12956 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
12957 strcpy(tp->board_part_number, "BCM57781");
12958 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
12959 strcpy(tp->board_part_number, "BCM57785");
12960 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
12961 strcpy(tp->board_part_number, "BCM57791");
12962 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
12963 strcpy(tp->board_part_number, "BCM57795");
12964 else
12965 goto nomatch;
12966 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070012967 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000012968 } else {
12969nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070012970 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000012971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012972}
12973
Matt Carlson9c8a6202007-10-21 16:16:08 -070012974static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
12975{
12976 u32 val;
12977
Matt Carlsone4f34112009-02-25 14:25:00 +000012978 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012979 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000012980 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012981 val != 0)
12982 return 0;
12983
12984 return 1;
12985}
12986
Matt Carlsonacd9c112009-02-25 14:26:33 +000012987static void __devinit tg3_read_bc_ver(struct tg3 *tp)
12988{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012989 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000012990 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012991 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012992
12993 if (tg3_nvram_read(tp, 0xc, &offset) ||
12994 tg3_nvram_read(tp, 0x4, &start))
12995 return;
12996
12997 offset = tg3_nvram_logical_addr(tp, offset);
12998
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012999 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013000 return;
13001
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013002 if ((val & 0xfc000000) == 0x0c000000) {
13003 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013004 return;
13005
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013006 if (val == 0)
13007 newver = true;
13008 }
13009
Matt Carlson75f99362010-04-05 10:19:24 +000013010 dst_off = strlen(tp->fw_ver);
13011
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013012 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013013 if (TG3_VER_SIZE - dst_off < 16 ||
13014 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013015 return;
13016
13017 offset = offset + ver_offset - start;
13018 for (i = 0; i < 16; i += 4) {
13019 __be32 v;
13020 if (tg3_nvram_read_be32(tp, offset + i, &v))
13021 return;
13022
Matt Carlson75f99362010-04-05 10:19:24 +000013023 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013024 }
13025 } else {
13026 u32 major, minor;
13027
13028 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13029 return;
13030
13031 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13032 TG3_NVM_BCVER_MAJSFT;
13033 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013034 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13035 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013036 }
13037}
13038
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013039static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13040{
13041 u32 val, major, minor;
13042
13043 /* Use native endian representation */
13044 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13045 return;
13046
13047 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13048 TG3_NVM_HWSB_CFG1_MAJSFT;
13049 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13050 TG3_NVM_HWSB_CFG1_MINSFT;
13051
13052 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13053}
13054
Matt Carlsondfe00d72008-11-21 17:19:41 -080013055static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13056{
13057 u32 offset, major, minor, build;
13058
Matt Carlson75f99362010-04-05 10:19:24 +000013059 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013060
13061 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13062 return;
13063
13064 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13065 case TG3_EEPROM_SB_REVISION_0:
13066 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13067 break;
13068 case TG3_EEPROM_SB_REVISION_2:
13069 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13070 break;
13071 case TG3_EEPROM_SB_REVISION_3:
13072 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13073 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013074 case TG3_EEPROM_SB_REVISION_4:
13075 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13076 break;
13077 case TG3_EEPROM_SB_REVISION_5:
13078 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13079 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013080 case TG3_EEPROM_SB_REVISION_6:
13081 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13082 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013083 default:
13084 return;
13085 }
13086
Matt Carlsone4f34112009-02-25 14:25:00 +000013087 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013088 return;
13089
13090 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13091 TG3_EEPROM_SB_EDH_BLD_SHFT;
13092 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13093 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13094 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13095
13096 if (minor > 99 || build > 26)
13097 return;
13098
Matt Carlson75f99362010-04-05 10:19:24 +000013099 offset = strlen(tp->fw_ver);
13100 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13101 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013102
13103 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013104 offset = strlen(tp->fw_ver);
13105 if (offset < TG3_VER_SIZE - 1)
13106 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013107 }
13108}
13109
Matt Carlsonacd9c112009-02-25 14:26:33 +000013110static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013111{
13112 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013113 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013114
13115 for (offset = TG3_NVM_DIR_START;
13116 offset < TG3_NVM_DIR_END;
13117 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013118 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013119 return;
13120
13121 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13122 break;
13123 }
13124
13125 if (offset == TG3_NVM_DIR_END)
13126 return;
13127
13128 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
13129 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013130 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013131 return;
13132
Matt Carlsone4f34112009-02-25 14:25:00 +000013133 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013134 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013135 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013136 return;
13137
13138 offset += val - start;
13139
Matt Carlsonacd9c112009-02-25 14:26:33 +000013140 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013141
Matt Carlsonacd9c112009-02-25 14:26:33 +000013142 tp->fw_ver[vlen++] = ',';
13143 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013144
13145 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013146 __be32 v;
13147 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013148 return;
13149
Al Virob9fc7dc2007-12-17 22:59:57 -080013150 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013151
Matt Carlsonacd9c112009-02-25 14:26:33 +000013152 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13153 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013154 break;
13155 }
13156
Matt Carlsonacd9c112009-02-25 14:26:33 +000013157 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13158 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013159 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013160}
13161
Matt Carlson7fd76442009-02-25 14:27:20 +000013162static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13163{
13164 int vlen;
13165 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013166 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013167
13168 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) ||
13169 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
13170 return;
13171
13172 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13173 if (apedata != APE_SEG_SIG_MAGIC)
13174 return;
13175
13176 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13177 if (!(apedata & APE_FW_STATUS_READY))
13178 return;
13179
13180 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13181
Matt Carlsondc6d0742010-09-15 08:59:55 +000013182 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
13183 tp->tg3_flags3 |= TG3_FLG3_APE_HAS_NCSI;
Matt Carlsonecc79642010-08-02 11:26:01 +000013184 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013185 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013186 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013187 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013188
Matt Carlson7fd76442009-02-25 14:27:20 +000013189 vlen = strlen(tp->fw_ver);
13190
Matt Carlsonecc79642010-08-02 11:26:01 +000013191 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13192 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013193 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13194 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13195 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13196 (apedata & APE_FW_VERSION_BLDMSK));
13197}
13198
Matt Carlsonacd9c112009-02-25 14:26:33 +000013199static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13200{
13201 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013202 bool vpd_vers = false;
13203
13204 if (tp->fw_ver[0] != 0)
13205 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013206
Matt Carlsondf259d82009-04-20 06:57:14 +000013207 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) {
Matt Carlson75f99362010-04-05 10:19:24 +000013208 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013209 return;
13210 }
13211
Matt Carlsonacd9c112009-02-25 14:26:33 +000013212 if (tg3_nvram_read(tp, 0, &val))
13213 return;
13214
13215 if (val == TG3_EEPROM_MAGIC)
13216 tg3_read_bc_ver(tp);
13217 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13218 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013219 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13220 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013221 else
13222 return;
13223
13224 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson75f99362010-04-05 10:19:24 +000013225 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) || vpd_vers)
13226 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013227
13228 tg3_read_mgmtfw_ver(tp);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013229
Matt Carlson75f99362010-04-05 10:19:24 +000013230done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013231 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013232}
13233
Michael Chan7544b092007-05-05 13:08:32 -070013234static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
13235
Javier Martinez Canillas6303e6e2011-03-26 16:42:33 +000013236static inline void vlan_features_add(struct net_device *dev, unsigned long flags)
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013237{
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013238 dev->vlan_features |= flags;
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013239}
13240
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013241static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13242{
Matt Carlsonde9f5232011-04-05 14:22:43 +000013243 if (tp->tg3_flags3 & TG3_FLG3_LRG_PROD_RING_CAP)
13244 return TG3_RX_RET_MAX_SIZE_5717;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013245 else if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
13246 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013247 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013248 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013249 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013250}
13251
Matt Carlson41434702011-03-09 16:58:22 +000013252static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013253 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13254 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13255 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13256 { },
13257};
13258
Linus Torvalds1da177e2005-04-16 15:20:36 -070013259static int __devinit tg3_get_invariants(struct tg3 *tp)
13260{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013261 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013262 u32 pci_state_reg, grc_misc_cfg;
13263 u32 val;
13264 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013265 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013266
Linus Torvalds1da177e2005-04-16 15:20:36 -070013267 /* Force memory write invalidate off. If we leave it on,
13268 * then on 5700_BX chips we have to enable a workaround.
13269 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13270 * to match the cacheline size. The Broadcom driver have this
13271 * workaround but turns MWI off all the times so never uses
13272 * it. This seems to suggest that the workaround is insufficient.
13273 */
13274 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13275 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13276 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13277
13278 /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
13279 * has the register indirect write enable bit set before
13280 * we try to access any of the MMIO registers. It is also
13281 * critical that the PCI-X hw workaround situation is decided
13282 * before that as well.
13283 */
13284 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13285 &misc_ctrl_reg);
13286
13287 tp->pci_chip_rev_id = (misc_ctrl_reg >>
13288 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070013289 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13290 u32 prod_id_asic_rev;
13291
Matt Carlson5001e2f2009-11-13 13:03:51 +000013292 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13293 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013294 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
13295 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013296 pci_read_config_dword(tp->pdev,
13297 TG3PCI_GEN2_PRODID_ASICREV,
13298 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000013299 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13300 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13301 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13302 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13303 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13304 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13305 pci_read_config_dword(tp->pdev,
13306 TG3PCI_GEN15_PRODID_ASICREV,
13307 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013308 else
13309 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
13310 &prod_id_asic_rev);
13311
Matt Carlson321d32a2008-11-21 17:22:19 -080013312 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070013313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013314
Michael Chanff645be2005-04-21 17:09:53 -070013315 /* Wrong chip ID in 5752 A0. This code can be removed later
13316 * as A0 is not in production.
13317 */
13318 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13319 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13320
Michael Chan68929142005-08-09 20:17:14 -070013321 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
13322 * we need to disable memory and use config. cycles
13323 * only to access all registers. The 5702/03 chips
13324 * can mistakenly decode the special cycles from the
13325 * ICH chipsets as memory write cycles, causing corruption
13326 * of register and memory space. Only certain ICH bridges
13327 * will drive special cycles with non-zero data during the
13328 * address phase which can fall within the 5703's address
13329 * range. This is not an ICH bug as the PCI spec allows
13330 * non-zero address during special cycles. However, only
13331 * these ICH bridges are known to drive non-zero addresses
13332 * during special cycles.
13333 *
13334 * Since special cycles do not cross PCI bridges, we only
13335 * enable this workaround if the 5703 is on the secondary
13336 * bus of these ICH bridges.
13337 */
13338 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
13339 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
13340 static struct tg3_dev_id {
13341 u32 vendor;
13342 u32 device;
13343 u32 rev;
13344 } ich_chipsets[] = {
13345 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
13346 PCI_ANY_ID },
13347 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
13348 PCI_ANY_ID },
13349 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
13350 0xa },
13351 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
13352 PCI_ANY_ID },
13353 { },
13354 };
13355 struct tg3_dev_id *pci_id = &ich_chipsets[0];
13356 struct pci_dev *bridge = NULL;
13357
13358 while (pci_id->vendor != 0) {
13359 bridge = pci_get_device(pci_id->vendor, pci_id->device,
13360 bridge);
13361 if (!bridge) {
13362 pci_id++;
13363 continue;
13364 }
13365 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070013366 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070013367 continue;
13368 }
13369 if (bridge->subordinate &&
13370 (bridge->subordinate->number ==
13371 tp->pdev->bus->number)) {
13372
13373 tp->tg3_flags2 |= TG3_FLG2_ICH_WORKAROUND;
13374 pci_dev_put(bridge);
13375 break;
13376 }
13377 }
13378 }
13379
Matt Carlson41588ba2008-04-19 18:12:33 -070013380 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
13381 static struct tg3_dev_id {
13382 u32 vendor;
13383 u32 device;
13384 } bridge_chipsets[] = {
13385 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
13386 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
13387 { },
13388 };
13389 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
13390 struct pci_dev *bridge = NULL;
13391
13392 while (pci_id->vendor != 0) {
13393 bridge = pci_get_device(pci_id->vendor,
13394 pci_id->device,
13395 bridge);
13396 if (!bridge) {
13397 pci_id++;
13398 continue;
13399 }
13400 if (bridge->subordinate &&
13401 (bridge->subordinate->number <=
13402 tp->pdev->bus->number) &&
13403 (bridge->subordinate->subordinate >=
13404 tp->pdev->bus->number)) {
13405 tp->tg3_flags3 |= TG3_FLG3_5701_DMA_BUG;
13406 pci_dev_put(bridge);
13407 break;
13408 }
13409 }
13410 }
13411
Michael Chan4a29cc22006-03-19 13:21:12 -080013412 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
13413 * DMA addresses > 40-bit. This bridge may have other additional
13414 * 57xx devices behind it in some 4-port NIC designs for example.
13415 * Any tg3 device found behind the bridge will also need the 40-bit
13416 * DMA workaround.
13417 */
Michael Chana4e2b342005-10-26 15:46:52 -070013418 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
13419 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
13420 tp->tg3_flags2 |= TG3_FLG2_5780_CLASS;
Michael Chan4a29cc22006-03-19 13:21:12 -080013421 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
Michael Chan4cf78e42005-07-25 12:29:19 -070013422 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000013423 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080013424 struct pci_dev *bridge = NULL;
13425
13426 do {
13427 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
13428 PCI_DEVICE_ID_SERVERWORKS_EPB,
13429 bridge);
13430 if (bridge && bridge->subordinate &&
13431 (bridge->subordinate->number <=
13432 tp->pdev->bus->number) &&
13433 (bridge->subordinate->subordinate >=
13434 tp->pdev->bus->number)) {
13435 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
13436 pci_dev_put(bridge);
13437 break;
13438 }
13439 } while (bridge);
13440 }
Michael Chan4cf78e42005-07-25 12:29:19 -070013441
Linus Torvalds1da177e2005-04-16 15:20:36 -070013442 /* Initialize misc host control in PCI block. */
13443 tp->misc_host_ctrl |= (misc_ctrl_reg &
13444 MISC_HOST_CTRL_CHIPREV);
13445 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13446 tp->misc_host_ctrl);
13447
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013448 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
13449 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013450 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13451 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Michael Chan7544b092007-05-05 13:08:32 -070013452 tp->pdev_peer = tg3_find_peer(tp);
13453
Matt Carlsonc885e822010-08-02 11:25:57 +000013454 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013455 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13456 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Matt Carlson0a58d662011-04-05 14:22:45 +000013457 tp->tg3_flags3 |= TG3_FLG3_5717_PLUS;
13458
13459 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
13460 (tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
Matt Carlson1407deb2011-04-05 14:22:44 +000013461 tp->tg3_flags3 |= TG3_FLG3_57765_PLUS;
Matt Carlsonc885e822010-08-02 11:25:57 +000013462
Matt Carlson321d32a2008-11-21 17:22:19 -080013463 /* Intentionally exclude ASIC_REV_5906 */
13464 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad12006-03-20 22:27:35 -080013465 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013466 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013467 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013468 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013469 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson1407deb2011-04-05 14:22:44 +000013470 (tp->tg3_flags3 & TG3_FLG3_57765_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013471 tp->tg3_flags3 |= TG3_FLG3_5755_PLUS;
13472
13473 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13474 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070013475 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013476 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chana4e2b342005-10-26 15:46:52 -070013477 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
John W. Linville6708e5c2005-04-21 17:00:52 -070013478 tp->tg3_flags2 |= TG3_FLG2_5750_PLUS;
13479
John W. Linville1b440c562005-04-21 17:03:18 -070013480 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) ||
13481 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
13482 tp->tg3_flags2 |= TG3_FLG2_5705_PLUS;
13483
Matt Carlson027455a2008-12-21 20:19:30 -080013484 /* 5700 B0 chips do not support checksumming correctly due
13485 * to hardware bugs.
13486 */
13487 if (tp->pci_chip_rev_id == CHIPREV_ID_5700_B0)
13488 tp->tg3_flags |= TG3_FLAG_BROKEN_CHECKSUMS;
13489 else {
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013490 unsigned long features = NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_GRO;
13491
Matt Carlson027455a2008-12-21 20:19:30 -080013492 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
Matt Carlson027455a2008-12-21 20:19:30 -080013493 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013494 features |= NETIF_F_IPV6_CSUM;
13495 tp->dev->features |= features;
13496 vlan_features_add(tp->dev, features);
Matt Carlson027455a2008-12-21 20:19:30 -080013497 }
13498
Matt Carlson507399f2009-11-13 13:03:37 +000013499 /* Determine TSO capabilities */
Matt Carlson2866d952011-02-10 20:06:46 -080013500 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlson4d163b72011-01-25 15:58:48 +000013501 ; /* Do nothing. HW bug. */
Matt Carlson1407deb2011-04-05 14:22:44 +000013502 else if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS)
Matt Carlsone849cdc2009-11-13 13:03:38 +000013503 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_3;
13504 else if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
13505 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlson507399f2009-11-13 13:03:37 +000013506 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_2;
13507 else if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
13508 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_1 | TG3_FLG2_TSO_BUG;
13509 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13510 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
13511 tp->tg3_flags2 &= ~TG3_FLG2_TSO_BUG;
13512 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13513 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13514 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
13515 tp->tg3_flags2 |= TG3_FLG2_TSO_BUG;
13516 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13517 tp->fw_needed = FIRMWARE_TG3TSO5;
13518 else
13519 tp->fw_needed = FIRMWARE_TG3TSO;
13520 }
13521
13522 tp->irq_max = 1;
13523
Michael Chan5a6f3072006-03-20 22:28:05 -080013524 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Michael Chan7544b092007-05-05 13:08:32 -070013525 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSI;
13526 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
13527 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
13528 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
13529 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
13530 tp->pdev_peer == tp->pdev))
13531 tp->tg3_flags &= ~TG3_FLAG_SUPPORT_MSI;
13532
Matt Carlson321d32a2008-11-21 17:22:19 -080013533 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070013534 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080013535 tp->tg3_flags2 |= TG3_FLG2_1SHOT_MSI;
Michael Chan52c0fd82006-06-29 20:15:54 -070013536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013537
Matt Carlson1407deb2011-04-05 14:22:44 +000013538 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
Matt Carlson507399f2009-11-13 13:03:37 +000013539 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSIX;
13540 tp->irq_max = TG3_IRQ_MAX_VECS;
13541 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013542 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000013543
Matt Carlson615774f2009-11-13 13:03:39 +000013544 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsona50d0792010-06-05 17:24:37 +000013545 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson615774f2009-11-13 13:03:39 +000013546 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13547 tp->tg3_flags3 |= TG3_FLG3_SHORT_DMA_BUG;
13548 else if (!(tp->tg3_flags3 & TG3_FLG3_5755_PLUS)) {
13549 tp->tg3_flags3 |= TG3_FLG3_4G_DMA_BNDRY_BUG;
13550 tp->tg3_flags3 |= TG3_FLG3_40BIT_DMA_LIMIT_BUG;
Matt Carlson0e1406d2009-11-02 12:33:33 +000013551 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013552
Matt Carlson0a58d662011-04-05 14:22:45 +000013553 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlsonde9f5232011-04-05 14:22:43 +000013554 tp->tg3_flags3 |= TG3_FLG3_LRG_PROD_RING_CAP;
13555
Matt Carlson1407deb2011-04-05 14:22:44 +000013556 if ((tp->tg3_flags3 & TG3_FLG3_57765_PLUS) &&
Matt Carlson2866d952011-02-10 20:06:46 -080013557 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
Matt Carlsonb703df62009-12-03 08:36:21 +000013558 tp->tg3_flags3 |= TG3_FLG3_USE_JUMBO_BDFLAG;
13559
Matt Carlsonf51f3562008-05-25 23:45:08 -070013560 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsonc6cdf432010-04-05 10:19:26 +000013561 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
13562 (tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG))
Matt Carlson8f666b02009-08-28 13:58:24 +000013563 tp->tg3_flags |= TG3_FLAG_JUMBO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -070013564
Matt Carlson52f44902008-11-21 17:17:04 -080013565 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13566 &pci_state_reg);
13567
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013568 tp->pcie_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_EXP);
13569 if (tp->pcie_cap != 0) {
13570 u16 lnkctl;
13571
Linus Torvalds1da177e2005-04-16 15:20:36 -070013572 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013573
Matt Carlsoncf790032010-11-24 08:31:48 +000013574 tp->pcie_readrq = 4096;
Matt Carlsond78b59f2011-04-05 14:22:46 +000013575 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13576 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Matt Carlsonb4495ed2011-01-25 15:58:47 +000013577 tp->pcie_readrq = 2048;
Matt Carlsoncf790032010-11-24 08:31:48 +000013578
13579 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013580
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013581 pci_read_config_word(tp->pdev,
13582 tp->pcie_cap + PCI_EXP_LNKCTL,
13583 &lnkctl);
13584 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
13585 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanc7835a72006-11-15 21:14:42 -080013586 tp->tg3_flags2 &= ~TG3_FLG2_HW_TSO_2;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013587 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013588 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000013589 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
13590 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013591 tp->tg3_flags3 |= TG3_FLG3_CLKREQ_BUG;
Matt Carlson614b0592010-01-20 16:58:02 +000013592 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
13593 tp->tg3_flags3 |= TG3_FLG3_L1PLLPD_EN;
Michael Chanc7835a72006-11-15 21:14:42 -080013594 }
Matt Carlson52f44902008-11-21 17:17:04 -080013595 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -080013596 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson52f44902008-11-21 17:17:04 -080013597 } else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
13598 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
13599 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
13600 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000013601 dev_err(&tp->pdev->dev,
13602 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080013603 return -EIO;
13604 }
13605
13606 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
13607 tp->tg3_flags |= TG3_FLAG_PCIX_MODE;
13608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013609
Michael Chan399de502005-10-03 14:02:39 -070013610 /* If we have an AMD 762 or VIA K8T800 chipset, write
13611 * reordering to the mailbox registers done by the host
13612 * controller can cause major troubles. We read back from
13613 * every mailbox register write to force the writes to be
13614 * posted to the chip in order.
13615 */
Matt Carlson41434702011-03-09 16:58:22 +000013616 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Michael Chan399de502005-10-03 14:02:39 -070013617 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13618 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
13619
Matt Carlson69fc4052008-12-21 20:19:57 -080013620 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
13621 &tp->pci_cacheline_sz);
13622 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13623 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013624 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13625 tp->pci_lat_timer < 64) {
13626 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080013627 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13628 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013629 }
13630
Matt Carlson52f44902008-11-21 17:17:04 -080013631 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
13632 /* 5700 BX chips need to have their TX producer index
13633 * mailboxes written twice to workaround a bug.
13634 */
13635 tp->tg3_flags |= TG3_FLAG_TXD_MBOX_HWBUG;
Matt Carlson9974a352007-10-07 23:27:28 -070013636
Matt Carlson52f44902008-11-21 17:17:04 -080013637 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013638 *
13639 * The workaround is to use indirect register accesses
13640 * for all chip writes not to mailbox registers.
13641 */
Matt Carlson52f44902008-11-21 17:17:04 -080013642 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013643 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013644
13645 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13646
13647 /* The chip can have it's power management PCI config
13648 * space registers clobbered due to this bug.
13649 * So explicitly force the chip into D0 here.
13650 */
Matt Carlson9974a352007-10-07 23:27:28 -070013651 pci_read_config_dword(tp->pdev,
13652 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013653 &pm_reg);
13654 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
13655 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070013656 pci_write_config_dword(tp->pdev,
13657 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013658 pm_reg);
13659
13660 /* Also, force SERR#/PERR# in PCI command. */
13661 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13662 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
13663 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13664 }
13665 }
13666
Linus Torvalds1da177e2005-04-16 15:20:36 -070013667 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
13668 tp->tg3_flags |= TG3_FLAG_PCI_HIGH_SPEED;
13669 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
13670 tp->tg3_flags |= TG3_FLAG_PCI_32BIT;
13671
13672 /* Chip-specific fixup from Broadcom driver */
13673 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
13674 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
13675 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
13676 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
13677 }
13678
Michael Chan1ee582d2005-08-09 20:16:46 -070013679 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070013680 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013681 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070013682 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070013683 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013684 tp->write32_tx_mbox = tg3_write32;
13685 tp->write32_rx_mbox = tg3_write32;
13686
13687 /* Various workaround register access methods */
13688 if (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG)
13689 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013690 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
13691 ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
13692 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
13693 /*
13694 * Back to back register writes can cause problems on these
13695 * chips, the workaround is to read back all reg writes
13696 * except those to mailbox regs.
13697 *
13698 * See tg3_write_indirect_reg32().
13699 */
Michael Chan1ee582d2005-08-09 20:16:46 -070013700 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013701 }
13702
Michael Chan1ee582d2005-08-09 20:16:46 -070013703 if ((tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG) ||
13704 (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)) {
13705 tp->write32_tx_mbox = tg3_write32_tx_mbox;
13706 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
13707 tp->write32_rx_mbox = tg3_write_flush_reg32;
13708 }
Michael Chan20094932005-08-09 20:16:32 -070013709
Michael Chan68929142005-08-09 20:17:14 -070013710 if (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND) {
13711 tp->read32 = tg3_read_indirect_reg32;
13712 tp->write32 = tg3_write_indirect_reg32;
13713 tp->read32_mbox = tg3_read_indirect_mbox;
13714 tp->write32_mbox = tg3_write_indirect_mbox;
13715 tp->write32_tx_mbox = tg3_write_indirect_mbox;
13716 tp->write32_rx_mbox = tg3_write_indirect_mbox;
13717
13718 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070013719 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070013720
13721 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13722 pci_cmd &= ~PCI_COMMAND_MEMORY;
13723 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13724 }
Michael Chanb5d37722006-09-27 16:06:21 -070013725 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13726 tp->read32_mbox = tg3_read32_mbox_5906;
13727 tp->write32_mbox = tg3_write32_mbox_5906;
13728 tp->write32_tx_mbox = tg3_write32_mbox_5906;
13729 tp->write32_rx_mbox = tg3_write32_mbox_5906;
13730 }
Michael Chan68929142005-08-09 20:17:14 -070013731
Michael Chanbbadf502006-04-06 21:46:34 -070013732 if (tp->write32 == tg3_write_indirect_reg32 ||
13733 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13734 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070013735 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Michael Chanbbadf502006-04-06 21:46:34 -070013736 tp->tg3_flags |= TG3_FLAG_SRAM_USE_CONFIG;
13737
Michael Chan7d0c41e2005-04-21 17:06:20 -070013738 /* Get eeprom hw config before calling tg3_set_power_state().
Michael Chan9d26e212006-12-07 00:21:14 -080013739 * In particular, the TG3_FLG2_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070013740 * determined before calling tg3_set_power_state() so that
13741 * we know whether or not to switch out of Vaux power.
13742 * When the flag is set, it means that GPIO1 is used for eeprom
13743 * write protect and also implies that it is a LOM where GPIOs
13744 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013745 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070013746 tg3_get_eeprom_hw_cfg(tp);
13747
Matt Carlson0d3031d2007-10-10 18:02:43 -070013748 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
13749 /* Allow reads and writes to the
13750 * APE register and memory space.
13751 */
13752 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000013753 PCISTATE_ALLOW_APE_SHMEM_WR |
13754 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070013755 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
13756 pci_state_reg);
13757 }
13758
Matt Carlson9936bcf2007-10-10 18:03:07 -070013759 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013760 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013761 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013762 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson1407deb2011-04-05 14:22:44 +000013763 (tp->tg3_flags3 & TG3_FLG3_57765_PLUS))
Matt Carlsond30cdd22007-10-07 23:28:35 -070013764 tp->tg3_flags |= TG3_FLAG_CPMU_PRESENT;
13765
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000013766 /* Set up tp->grc_local_ctrl before calling tg_power_up().
Michael Chan314fba32005-04-21 17:07:04 -070013767 * GPIO1 driven high will bring 5700's external PHY out of reset.
13768 * It is also used as eeprom write protect on LOMs.
13769 */
13770 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
13771 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13772 (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT))
13773 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
13774 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070013775 /* Unused GPIO3 must be driven as output on 5752 because there
13776 * are no pull-up resistors on unused GPIO pins.
13777 */
13778 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
13779 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070013780
Matt Carlson321d32a2008-11-21 17:22:19 -080013781 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000013782 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
13783 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Michael Chanaf36e6b2006-03-23 01:28:06 -080013784 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13785
Matt Carlson8d519ab2009-04-20 06:58:01 +000013786 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
13787 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070013788 /* Turn off the debug UART. */
13789 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13790 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
13791 /* Keep VMain power. */
13792 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
13793 GRC_LCLCTRL_GPIO_OUTPUT0;
13794 }
13795
Linus Torvalds1da177e2005-04-16 15:20:36 -070013796 /* Force the chip into D0. */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000013797 err = tg3_power_up(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013798 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000013799 dev_err(&tp->pdev->dev, "Transition to D0 failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070013800 return err;
13801 }
13802
Linus Torvalds1da177e2005-04-16 15:20:36 -070013803 /* Derive initial jumbo mode from MTU assigned in
13804 * ether_setup() via the alloc_etherdev() call
13805 */
Michael Chan0f893dc2005-07-25 12:30:38 -070013806 if (tp->dev->mtu > ETH_DATA_LEN &&
Michael Chana4e2b342005-10-26 15:46:52 -070013807 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan0f893dc2005-07-25 12:30:38 -070013808 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013809
13810 /* Determine WakeOnLan speed to use. */
13811 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13812 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
13813 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
13814 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
13815 tp->tg3_flags &= ~(TG3_FLAG_WOL_SPEED_100MB);
13816 } else {
13817 tp->tg3_flags |= TG3_FLAG_WOL_SPEED_100MB;
13818 }
13819
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013820 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013821 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013822
Linus Torvalds1da177e2005-04-16 15:20:36 -070013823 /* A few boards don't want Ethernet@WireSpeed phy feature */
13824 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13825 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
13826 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070013827 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013828 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
13829 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13830 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013831
13832 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
13833 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013834 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013835 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013836 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013837
Matt Carlson321d32a2008-11-21 17:22:19 -080013838 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013839 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080013840 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013841 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Matt Carlson1407deb2011-04-05 14:22:44 +000013842 !(tp->tg3_flags3 & TG3_FLG3_57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070013843 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013844 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013845 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13846 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080013847 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
13848 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013849 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080013850 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013851 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080013852 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013853 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070013854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013855
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013856 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13857 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
13858 tp->phy_otp = tg3_read_otp_phycfg(tp);
13859 if (tp->phy_otp == 0)
13860 tp->phy_otp = TG3_OTP_DEFAULT;
13861 }
13862
Matt Carlsonf51f3562008-05-25 23:45:08 -070013863 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)
Matt Carlson8ef21422008-05-02 16:47:53 -070013864 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
13865 else
13866 tp->mi_mode = MAC_MI_MODE_BASE;
13867
Linus Torvalds1da177e2005-04-16 15:20:36 -070013868 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013869 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
13870 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
13871 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
13872
Matt Carlson321d32a2008-11-21 17:22:19 -080013873 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
13874 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson57e69832008-05-25 23:48:31 -070013875 tp->tg3_flags3 |= TG3_FLG3_USE_PHYLIB;
13876
Matt Carlson158d7ab2008-05-29 01:37:54 -070013877 err = tg3_mdio_init(tp);
13878 if (err)
13879 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013880
13881 /* Initialize data/descriptor byte/word swapping. */
13882 val = tr32(GRC_MODE);
13883 val &= GRC_MODE_HOST_STACKUP;
13884 tw32(GRC_MODE, val | tp->grc_mode);
13885
13886 tg3_switch_clocks(tp);
13887
13888 /* Clear this out for sanity. */
13889 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
13890
13891 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13892 &pci_state_reg);
13893 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
13894 (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) == 0) {
13895 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
13896
13897 if (chiprevid == CHIPREV_ID_5701_A0 ||
13898 chiprevid == CHIPREV_ID_5701_B0 ||
13899 chiprevid == CHIPREV_ID_5701_B2 ||
13900 chiprevid == CHIPREV_ID_5701_B5) {
13901 void __iomem *sram_base;
13902
13903 /* Write some dummy words into the SRAM status block
13904 * area, see if it reads back correctly. If the return
13905 * value is bad, force enable the PCIX workaround.
13906 */
13907 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
13908
13909 writel(0x00000000, sram_base);
13910 writel(0x00000000, sram_base + 4);
13911 writel(0xffffffff, sram_base + 4);
13912 if (readl(sram_base) != 0x00000000)
13913 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13914 }
13915 }
13916
13917 udelay(50);
13918 tg3_nvram_init(tp);
13919
13920 grc_misc_cfg = tr32(GRC_MISC_CFG);
13921 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
13922
Linus Torvalds1da177e2005-04-16 15:20:36 -070013923 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13924 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
13925 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
13926 tp->tg3_flags2 |= TG3_FLG2_IS_5788;
13927
David S. Millerfac9b832005-05-18 22:46:34 -070013928 if (!(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
13929 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700))
13930 tp->tg3_flags |= TG3_FLAG_TAGGED_STATUS;
13931 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
13932 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
13933 HOSTCC_MODE_CLRTICK_TXBD);
13934
13935 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
13936 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13937 tp->misc_host_ctrl);
13938 }
13939
Matt Carlson3bda1252008-08-15 14:08:22 -070013940 /* Preserve the APE MAC_MODE bits */
13941 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
Matt Carlsond2394e6b2010-11-24 08:31:47 +000013942 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070013943 else
13944 tp->mac_mode = TG3_DEF_MAC_MODE;
13945
Linus Torvalds1da177e2005-04-16 15:20:36 -070013946 /* these are limited to 10/100 only */
13947 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13948 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
13949 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13950 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13951 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
13952 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
13953 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
13954 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13955 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080013956 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
13957 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013958 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000013959 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13960 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013961 (tp->phy_flags & TG3_PHYFLG_IS_FET))
13962 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013963
13964 err = tg3_phy_probe(tp);
13965 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000013966 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013967 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013968 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013969 }
13970
Matt Carlson184b8902010-04-05 10:19:25 +000013971 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080013972 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013973
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013974 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
13975 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013976 } else {
13977 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013978 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013979 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013980 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013981 }
13982
13983 /* 5700 {AX,BX} chips have a broken status block link
13984 * change bit implementation, so we must use the
13985 * status register in those cases.
13986 */
13987 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
13988 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
13989 else
13990 tp->tg3_flags &= ~TG3_FLAG_USE_LINKCHG_REG;
13991
13992 /* The led_ctrl is set during tg3_phy_probe, here we might
13993 * have to force the link status polling mechanism based
13994 * upon subsystem IDs.
13995 */
13996 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070013997 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013998 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
13999 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
14000 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014001 }
14002
14003 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014004 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014005 tp->tg3_flags |= TG3_FLAG_POLL_SERDES;
14006 else
14007 tp->tg3_flags &= ~TG3_FLAG_POLL_SERDES;
14008
Matt Carlsonbf933c82011-01-25 15:58:49 +000014009 tp->rx_offset = NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014010 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014011 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsond2757fc2010-04-12 06:58:27 +000014012 (tp->tg3_flags & TG3_FLAG_PCIX_MODE) != 0) {
Matt Carlsonbf933c82011-01-25 15:58:49 +000014013 tp->rx_offset = 0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014014#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000014015 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014016#endif
14017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014018
Matt Carlson2c49a442010-09-30 10:34:35 +000014019 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14020 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014021 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14022
Matt Carlson2c49a442010-09-30 10:34:35 +000014023 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070014024
14025 /* Increment the rx prod index on the rx std ring by at most
14026 * 8 for these chips to workaround hw errata.
14027 */
14028 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14029 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14030 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14031 tp->rx_std_max_post = 8;
14032
Matt Carlson8ed5d972007-05-07 00:25:49 -070014033 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND)
14034 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14035 PCIE_PWR_MGMT_L1_THRESH_MSK;
14036
Linus Torvalds1da177e2005-04-16 15:20:36 -070014037 return err;
14038}
14039
David S. Miller49b6e95f2007-03-29 01:38:42 -070014040#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014041static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14042{
14043 struct net_device *dev = tp->dev;
14044 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014045 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070014046 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014047 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014048
David S. Miller49b6e95f2007-03-29 01:38:42 -070014049 addr = of_get_property(dp, "local-mac-address", &len);
14050 if (addr && len == 6) {
14051 memcpy(dev->dev_addr, addr, 6);
14052 memcpy(dev->perm_addr, dev->dev_addr, 6);
14053 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014054 }
14055 return -ENODEV;
14056}
14057
14058static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14059{
14060 struct net_device *dev = tp->dev;
14061
14062 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070014063 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014064 return 0;
14065}
14066#endif
14067
14068static int __devinit tg3_get_device_address(struct tg3 *tp)
14069{
14070 struct net_device *dev = tp->dev;
14071 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080014072 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014073
David S. Miller49b6e95f2007-03-29 01:38:42 -070014074#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014075 if (!tg3_get_macaddr_sparc(tp))
14076 return 0;
14077#endif
14078
14079 mac_offset = 0x7c;
David S. Millerf49639e2006-06-09 11:58:36 -070014080 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
Michael Chana4e2b342005-10-26 15:46:52 -070014081 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014082 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14083 mac_offset = 0xcc;
14084 if (tg3_nvram_lock(tp))
14085 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14086 else
14087 tg3_nvram_unlock(tp);
Matt Carlson0a58d662011-04-05 14:22:45 +000014088 } else if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlsona50d0792010-06-05 17:24:37 +000014089 if (PCI_FUNC(tp->pdev->devfn) & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014090 mac_offset = 0xcc;
Matt Carlsona50d0792010-06-05 17:24:37 +000014091 if (PCI_FUNC(tp->pdev->devfn) > 1)
14092 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014093 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014094 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014095
14096 /* First try to get it from MAC address mailbox. */
14097 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14098 if ((hi >> 16) == 0x484b) {
14099 dev->dev_addr[0] = (hi >> 8) & 0xff;
14100 dev->dev_addr[1] = (hi >> 0) & 0xff;
14101
14102 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14103 dev->dev_addr[2] = (lo >> 24) & 0xff;
14104 dev->dev_addr[3] = (lo >> 16) & 0xff;
14105 dev->dev_addr[4] = (lo >> 8) & 0xff;
14106 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014107
Michael Chan008652b2006-03-27 23:14:53 -080014108 /* Some old bootcode may report a 0 MAC address in SRAM */
14109 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14110 }
14111 if (!addr_ok) {
14112 /* Next, try NVRAM. */
Matt Carlsondf259d82009-04-20 06:57:14 +000014113 if (!(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) &&
14114 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000014115 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070014116 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14117 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080014118 }
14119 /* Finally just fetch it out of the MAC control regs. */
14120 else {
14121 hi = tr32(MAC_ADDR_0_HIGH);
14122 lo = tr32(MAC_ADDR_0_LOW);
14123
14124 dev->dev_addr[5] = lo & 0xff;
14125 dev->dev_addr[4] = (lo >> 8) & 0xff;
14126 dev->dev_addr[3] = (lo >> 16) & 0xff;
14127 dev->dev_addr[2] = (lo >> 24) & 0xff;
14128 dev->dev_addr[1] = hi & 0xff;
14129 dev->dev_addr[0] = (hi >> 8) & 0xff;
14130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014131 }
14132
14133 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014134#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014135 if (!tg3_get_default_macaddr_sparc(tp))
14136 return 0;
14137#endif
14138 return -EINVAL;
14139 }
John W. Linville2ff43692005-09-12 14:44:20 -070014140 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014141 return 0;
14142}
14143
David S. Miller59e6b432005-05-18 22:50:10 -070014144#define BOUNDARY_SINGLE_CACHELINE 1
14145#define BOUNDARY_MULTI_CACHELINE 2
14146
14147static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14148{
14149 int cacheline_size;
14150 u8 byte;
14151 int goal;
14152
14153 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14154 if (byte == 0)
14155 cacheline_size = 1024;
14156 else
14157 cacheline_size = (int) byte * 4;
14158
14159 /* On 5703 and later chips, the boundary bits have no
14160 * effect.
14161 */
14162 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14163 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
14164 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
14165 goto out;
14166
14167#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14168 goal = BOUNDARY_MULTI_CACHELINE;
14169#else
14170#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14171 goal = BOUNDARY_SINGLE_CACHELINE;
14172#else
14173 goal = 0;
14174#endif
14175#endif
14176
Matt Carlson1407deb2011-04-05 14:22:44 +000014177 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014178 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14179 goto out;
14180 }
14181
David S. Miller59e6b432005-05-18 22:50:10 -070014182 if (!goal)
14183 goto out;
14184
14185 /* PCI controllers on most RISC systems tend to disconnect
14186 * when a device tries to burst across a cache-line boundary.
14187 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14188 *
14189 * Unfortunately, for PCI-E there are only limited
14190 * write-side controls for this, and thus for reads
14191 * we will still get the disconnects. We'll also waste
14192 * these PCI cycles for both read and write for chips
14193 * other than 5700 and 5701 which do not implement the
14194 * boundary bits.
14195 */
14196 if ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
14197 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
14198 switch (cacheline_size) {
14199 case 16:
14200 case 32:
14201 case 64:
14202 case 128:
14203 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14204 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14205 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14206 } else {
14207 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14208 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14209 }
14210 break;
14211
14212 case 256:
14213 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14214 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14215 break;
14216
14217 default:
14218 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14219 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14220 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014221 }
David S. Miller59e6b432005-05-18 22:50:10 -070014222 } else if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14223 switch (cacheline_size) {
14224 case 16:
14225 case 32:
14226 case 64:
14227 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14228 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14229 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14230 break;
14231 }
14232 /* fallthrough */
14233 case 128:
14234 default:
14235 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14236 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14237 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014238 }
David S. Miller59e6b432005-05-18 22:50:10 -070014239 } else {
14240 switch (cacheline_size) {
14241 case 16:
14242 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14243 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14244 DMA_RWCTRL_WRITE_BNDRY_16);
14245 break;
14246 }
14247 /* fallthrough */
14248 case 32:
14249 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14250 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14251 DMA_RWCTRL_WRITE_BNDRY_32);
14252 break;
14253 }
14254 /* fallthrough */
14255 case 64:
14256 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14257 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14258 DMA_RWCTRL_WRITE_BNDRY_64);
14259 break;
14260 }
14261 /* fallthrough */
14262 case 128:
14263 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14264 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14265 DMA_RWCTRL_WRITE_BNDRY_128);
14266 break;
14267 }
14268 /* fallthrough */
14269 case 256:
14270 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14271 DMA_RWCTRL_WRITE_BNDRY_256);
14272 break;
14273 case 512:
14274 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14275 DMA_RWCTRL_WRITE_BNDRY_512);
14276 break;
14277 case 1024:
14278 default:
14279 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14280 DMA_RWCTRL_WRITE_BNDRY_1024);
14281 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014282 }
David S. Miller59e6b432005-05-18 22:50:10 -070014283 }
14284
14285out:
14286 return val;
14287}
14288
Linus Torvalds1da177e2005-04-16 15:20:36 -070014289static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
14290{
14291 struct tg3_internal_buffer_desc test_desc;
14292 u32 sram_dma_descs;
14293 int i, ret;
14294
14295 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
14296
14297 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
14298 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
14299 tw32(RDMAC_STATUS, 0);
14300 tw32(WDMAC_STATUS, 0);
14301
14302 tw32(BUFMGR_MODE, 0);
14303 tw32(FTQ_RESET, 0);
14304
14305 test_desc.addr_hi = ((u64) buf_dma) >> 32;
14306 test_desc.addr_lo = buf_dma & 0xffffffff;
14307 test_desc.nic_mbuf = 0x00002100;
14308 test_desc.len = size;
14309
14310 /*
14311 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
14312 * the *second* time the tg3 driver was getting loaded after an
14313 * initial scan.
14314 *
14315 * Broadcom tells me:
14316 * ...the DMA engine is connected to the GRC block and a DMA
14317 * reset may affect the GRC block in some unpredictable way...
14318 * The behavior of resets to individual blocks has not been tested.
14319 *
14320 * Broadcom noted the GRC reset will also reset all sub-components.
14321 */
14322 if (to_device) {
14323 test_desc.cqid_sqid = (13 << 8) | 2;
14324
14325 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
14326 udelay(40);
14327 } else {
14328 test_desc.cqid_sqid = (16 << 8) | 7;
14329
14330 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
14331 udelay(40);
14332 }
14333 test_desc.flags = 0x00000005;
14334
14335 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
14336 u32 val;
14337
14338 val = *(((u32 *)&test_desc) + i);
14339 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
14340 sram_dma_descs + (i * sizeof(u32)));
14341 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
14342 }
14343 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
14344
Matt Carlson859a588792010-04-05 10:19:28 +000014345 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014346 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000014347 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014348 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014349
14350 ret = -ENODEV;
14351 for (i = 0; i < 40; i++) {
14352 u32 val;
14353
14354 if (to_device)
14355 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
14356 else
14357 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
14358 if ((val & 0xffff) == sram_dma_descs) {
14359 ret = 0;
14360 break;
14361 }
14362
14363 udelay(100);
14364 }
14365
14366 return ret;
14367}
14368
David S. Millerded73402005-05-23 13:59:47 -070014369#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070014370
Matt Carlson41434702011-03-09 16:58:22 +000014371static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080014372 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14373 { },
14374};
14375
Linus Torvalds1da177e2005-04-16 15:20:36 -070014376static int __devinit tg3_test_dma(struct tg3 *tp)
14377{
14378 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070014379 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014380 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014381
Matt Carlson4bae65c2010-11-24 08:31:52 +000014382 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
14383 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014384 if (!buf) {
14385 ret = -ENOMEM;
14386 goto out_nofree;
14387 }
14388
14389 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
14390 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
14391
David S. Miller59e6b432005-05-18 22:50:10 -070014392 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014393
Matt Carlson1407deb2011-04-05 14:22:44 +000014394 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS)
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014395 goto out;
14396
Linus Torvalds1da177e2005-04-16 15:20:36 -070014397 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14398 /* DMA read watermark not used on PCIE */
14399 tp->dma_rwctrl |= 0x00180000;
14400 } else if (!(tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070014401 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14402 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014403 tp->dma_rwctrl |= 0x003f0000;
14404 else
14405 tp->dma_rwctrl |= 0x003f000f;
14406 } else {
14407 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14408 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
14409 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080014410 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014411
Michael Chan4a29cc22006-03-19 13:21:12 -080014412 /* If the 5704 is behind the EPB bridge, we can
14413 * do the less restrictive ONE_DMA workaround for
14414 * better performance.
14415 */
14416 if ((tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) &&
14417 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14418 tp->dma_rwctrl |= 0x8000;
14419 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014420 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
14421
Michael Chan49afdeb2007-02-13 12:17:03 -080014422 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
14423 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070014424 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080014425 tp->dma_rwctrl |=
14426 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
14427 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
14428 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070014429 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
14430 /* 5780 always in PCIX mode */
14431 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070014432 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
14433 /* 5714 always in PCIX mode */
14434 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014435 } else {
14436 tp->dma_rwctrl |= 0x001b000f;
14437 }
14438 }
14439
14440 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14441 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14442 tp->dma_rwctrl &= 0xfffffff0;
14443
14444 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14445 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
14446 /* Remove this if it causes problems for some boards. */
14447 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
14448
14449 /* On 5700/5701 chips, we need to set this bit.
14450 * Otherwise the chip will issue cacheline transactions
14451 * to streamable DMA memory with not all the byte
14452 * enables turned on. This is an error on several
14453 * RISC PCI controllers, in particular sparc64.
14454 *
14455 * On 5703/5704 chips, this bit has been reassigned
14456 * a different meaning. In particular, it is used
14457 * on those chips to enable a PCI-X workaround.
14458 */
14459 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
14460 }
14461
14462 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14463
14464#if 0
14465 /* Unneeded, already done by tg3_get_invariants. */
14466 tg3_switch_clocks(tp);
14467#endif
14468
Linus Torvalds1da177e2005-04-16 15:20:36 -070014469 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14470 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
14471 goto out;
14472
David S. Miller59e6b432005-05-18 22:50:10 -070014473 /* It is best to perform DMA test with maximum write burst size
14474 * to expose the 5700/5701 write DMA bug.
14475 */
14476 saved_dma_rwctrl = tp->dma_rwctrl;
14477 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14478 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14479
Linus Torvalds1da177e2005-04-16 15:20:36 -070014480 while (1) {
14481 u32 *p = buf, i;
14482
14483 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
14484 p[i] = i;
14485
14486 /* Send the buffer to the chip. */
14487 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
14488 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000014489 dev_err(&tp->pdev->dev,
14490 "%s: Buffer write failed. err = %d\n",
14491 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014492 break;
14493 }
14494
14495#if 0
14496 /* validate data reached card RAM correctly. */
14497 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14498 u32 val;
14499 tg3_read_mem(tp, 0x2100 + (i*4), &val);
14500 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000014501 dev_err(&tp->pdev->dev,
14502 "%s: Buffer corrupted on device! "
14503 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014504 /* ret = -ENODEV here? */
14505 }
14506 p[i] = 0;
14507 }
14508#endif
14509 /* Now read it back. */
14510 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
14511 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014512 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
14513 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014514 break;
14515 }
14516
14517 /* Verify it. */
14518 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14519 if (p[i] == i)
14520 continue;
14521
David S. Miller59e6b432005-05-18 22:50:10 -070014522 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14523 DMA_RWCTRL_WRITE_BNDRY_16) {
14524 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014525 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
14526 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14527 break;
14528 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000014529 dev_err(&tp->pdev->dev,
14530 "%s: Buffer corrupted on read back! "
14531 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014532 ret = -ENODEV;
14533 goto out;
14534 }
14535 }
14536
14537 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
14538 /* Success. */
14539 ret = 0;
14540 break;
14541 }
14542 }
David S. Miller59e6b432005-05-18 22:50:10 -070014543 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14544 DMA_RWCTRL_WRITE_BNDRY_16) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014545
David S. Miller59e6b432005-05-18 22:50:10 -070014546 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070014547 * now look for chipsets that are known to expose the
14548 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070014549 */
Matt Carlson41434702011-03-09 16:58:22 +000014550 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014551 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14552 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000014553 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014554 /* Safe to use the calculated DMA boundary. */
14555 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000014556 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070014557
David S. Miller59e6b432005-05-18 22:50:10 -070014558 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014560
14561out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000014562 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014563out_nofree:
14564 return ret;
14565}
14566
Linus Torvalds1da177e2005-04-16 15:20:36 -070014567static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
14568{
Matt Carlson1407deb2011-04-05 14:22:44 +000014569 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
Matt Carlson666bc832010-01-20 16:58:03 +000014570 tp->bufmgr_config.mbuf_read_dma_low_water =
14571 DEFAULT_MB_RDMA_LOW_WATER_5705;
14572 tp->bufmgr_config.mbuf_mac_rx_low_water =
14573 DEFAULT_MB_MACRX_LOW_WATER_57765;
14574 tp->bufmgr_config.mbuf_high_water =
14575 DEFAULT_MB_HIGH_WATER_57765;
14576
14577 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14578 DEFAULT_MB_RDMA_LOW_WATER_5705;
14579 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14580 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
14581 tp->bufmgr_config.mbuf_high_water_jumbo =
14582 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
14583 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chanfdfec1722005-07-25 12:31:48 -070014584 tp->bufmgr_config.mbuf_read_dma_low_water =
14585 DEFAULT_MB_RDMA_LOW_WATER_5705;
14586 tp->bufmgr_config.mbuf_mac_rx_low_water =
14587 DEFAULT_MB_MACRX_LOW_WATER_5705;
14588 tp->bufmgr_config.mbuf_high_water =
14589 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070014590 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14591 tp->bufmgr_config.mbuf_mac_rx_low_water =
14592 DEFAULT_MB_MACRX_LOW_WATER_5906;
14593 tp->bufmgr_config.mbuf_high_water =
14594 DEFAULT_MB_HIGH_WATER_5906;
14595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014596
Michael Chanfdfec1722005-07-25 12:31:48 -070014597 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14598 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
14599 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14600 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
14601 tp->bufmgr_config.mbuf_high_water_jumbo =
14602 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
14603 } else {
14604 tp->bufmgr_config.mbuf_read_dma_low_water =
14605 DEFAULT_MB_RDMA_LOW_WATER;
14606 tp->bufmgr_config.mbuf_mac_rx_low_water =
14607 DEFAULT_MB_MACRX_LOW_WATER;
14608 tp->bufmgr_config.mbuf_high_water =
14609 DEFAULT_MB_HIGH_WATER;
14610
14611 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14612 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
14613 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14614 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
14615 tp->bufmgr_config.mbuf_high_water_jumbo =
14616 DEFAULT_MB_HIGH_WATER_JUMBO;
14617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014618
14619 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
14620 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
14621}
14622
14623static char * __devinit tg3_phy_string(struct tg3 *tp)
14624{
Matt Carlson79eb6902010-02-17 15:17:03 +000014625 switch (tp->phy_id & TG3_PHY_ID_MASK) {
14626 case TG3_PHY_ID_BCM5400: return "5400";
14627 case TG3_PHY_ID_BCM5401: return "5401";
14628 case TG3_PHY_ID_BCM5411: return "5411";
14629 case TG3_PHY_ID_BCM5701: return "5701";
14630 case TG3_PHY_ID_BCM5703: return "5703";
14631 case TG3_PHY_ID_BCM5704: return "5704";
14632 case TG3_PHY_ID_BCM5705: return "5705";
14633 case TG3_PHY_ID_BCM5750: return "5750";
14634 case TG3_PHY_ID_BCM5752: return "5752";
14635 case TG3_PHY_ID_BCM5714: return "5714";
14636 case TG3_PHY_ID_BCM5780: return "5780";
14637 case TG3_PHY_ID_BCM5755: return "5755";
14638 case TG3_PHY_ID_BCM5787: return "5787";
14639 case TG3_PHY_ID_BCM5784: return "5784";
14640 case TG3_PHY_ID_BCM5756: return "5722/5756";
14641 case TG3_PHY_ID_BCM5906: return "5906";
14642 case TG3_PHY_ID_BCM5761: return "5761";
14643 case TG3_PHY_ID_BCM5718C: return "5718C";
14644 case TG3_PHY_ID_BCM5718S: return "5718S";
14645 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000014646 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson79eb6902010-02-17 15:17:03 +000014647 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070014648 case 0: return "serdes";
14649 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070014650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014651}
14652
Michael Chanf9804dd2005-09-27 12:13:10 -070014653static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
14654{
14655 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14656 strcpy(str, "PCI Express");
14657 return str;
14658 } else if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
14659 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
14660
14661 strcpy(str, "PCIX:");
14662
14663 if ((clock_ctrl == 7) ||
14664 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
14665 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
14666 strcat(str, "133MHz");
14667 else if (clock_ctrl == 0)
14668 strcat(str, "33MHz");
14669 else if (clock_ctrl == 2)
14670 strcat(str, "50MHz");
14671 else if (clock_ctrl == 4)
14672 strcat(str, "66MHz");
14673 else if (clock_ctrl == 6)
14674 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070014675 } else {
14676 strcpy(str, "PCI:");
14677 if (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED)
14678 strcat(str, "66MHz");
14679 else
14680 strcat(str, "33MHz");
14681 }
14682 if (tp->tg3_flags & TG3_FLAG_PCI_32BIT)
14683 strcat(str, ":32-bit");
14684 else
14685 strcat(str, ":64-bit");
14686 return str;
14687}
14688
Michael Chan8c2dc7e2005-12-19 16:26:02 -080014689static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014690{
14691 struct pci_dev *peer;
14692 unsigned int func, devnr = tp->pdev->devfn & ~7;
14693
14694 for (func = 0; func < 8; func++) {
14695 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14696 if (peer && peer != tp->pdev)
14697 break;
14698 pci_dev_put(peer);
14699 }
Michael Chan16fe9d72005-12-13 21:09:54 -080014700 /* 5704 can be configured in single-port mode, set peer to
14701 * tp->pdev in that case.
14702 */
14703 if (!peer) {
14704 peer = tp->pdev;
14705 return peer;
14706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014707
14708 /*
14709 * We don't need to keep the refcount elevated; there's no way
14710 * to remove one half of this device without removing the other
14711 */
14712 pci_dev_put(peer);
14713
14714 return peer;
14715}
14716
David S. Miller15f98502005-05-18 22:49:26 -070014717static void __devinit tg3_init_coal(struct tg3 *tp)
14718{
14719 struct ethtool_coalesce *ec = &tp->coal;
14720
14721 memset(ec, 0, sizeof(*ec));
14722 ec->cmd = ETHTOOL_GCOALESCE;
14723 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
14724 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
14725 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
14726 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
14727 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
14728 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
14729 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
14730 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
14731 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
14732
14733 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
14734 HOSTCC_MODE_CLRTICK_TXBD)) {
14735 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
14736 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
14737 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
14738 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
14739 }
Michael Chand244c892005-07-05 14:42:33 -070014740
14741 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
14742 ec->rx_coalesce_usecs_irq = 0;
14743 ec->tx_coalesce_usecs_irq = 0;
14744 ec->stats_block_coalesce_usecs = 0;
14745 }
David S. Miller15f98502005-05-18 22:49:26 -070014746}
14747
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014748static const struct net_device_ops tg3_netdev_ops = {
14749 .ndo_open = tg3_open,
14750 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080014751 .ndo_start_xmit = tg3_start_xmit,
Eric Dumazet511d2222010-07-07 20:44:24 +000014752 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -080014753 .ndo_validate_addr = eth_validate_addr,
14754 .ndo_set_multicast_list = tg3_set_rx_mode,
14755 .ndo_set_mac_address = tg3_set_mac_addr,
14756 .ndo_do_ioctl = tg3_ioctl,
14757 .ndo_tx_timeout = tg3_tx_timeout,
14758 .ndo_change_mtu = tg3_change_mtu,
Stephen Hemminger00829822008-11-20 20:14:53 -080014759#ifdef CONFIG_NET_POLL_CONTROLLER
14760 .ndo_poll_controller = tg3_poll_controller,
14761#endif
14762};
14763
14764static const struct net_device_ops tg3_netdev_ops_dma_bug = {
14765 .ndo_open = tg3_open,
14766 .ndo_stop = tg3_close,
14767 .ndo_start_xmit = tg3_start_xmit_dma_bug,
Eric Dumazet511d2222010-07-07 20:44:24 +000014768 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014769 .ndo_validate_addr = eth_validate_addr,
14770 .ndo_set_multicast_list = tg3_set_rx_mode,
14771 .ndo_set_mac_address = tg3_set_mac_addr,
14772 .ndo_do_ioctl = tg3_ioctl,
14773 .ndo_tx_timeout = tg3_tx_timeout,
14774 .ndo_change_mtu = tg3_change_mtu,
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014775#ifdef CONFIG_NET_POLL_CONTROLLER
14776 .ndo_poll_controller = tg3_poll_controller,
14777#endif
14778};
14779
Linus Torvalds1da177e2005-04-16 15:20:36 -070014780static int __devinit tg3_init_one(struct pci_dev *pdev,
14781 const struct pci_device_id *ent)
14782{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014783 struct net_device *dev;
14784 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000014785 int i, err, pm_cap;
14786 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070014787 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080014788 u64 dma_mask, persist_dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014789
Joe Perches05dbe002010-02-17 19:44:19 +000014790 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014791
14792 err = pci_enable_device(pdev);
14793 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014794 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014795 return err;
14796 }
14797
Linus Torvalds1da177e2005-04-16 15:20:36 -070014798 err = pci_request_regions(pdev, DRV_MODULE_NAME);
14799 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014800 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014801 goto err_out_disable_pdev;
14802 }
14803
14804 pci_set_master(pdev);
14805
14806 /* Find power-management capability. */
14807 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
14808 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000014809 dev_err(&pdev->dev,
14810 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014811 err = -EIO;
14812 goto err_out_free_res;
14813 }
14814
Matt Carlsonfe5f5782009-09-01 13:09:39 +000014815 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014816 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000014817 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014818 err = -ENOMEM;
14819 goto err_out_free_res;
14820 }
14821
Linus Torvalds1da177e2005-04-16 15:20:36 -070014822 SET_NETDEV_DEV(dev, &pdev->dev);
14823
Linus Torvalds1da177e2005-04-16 15:20:36 -070014824 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014825
14826 tp = netdev_priv(dev);
14827 tp->pdev = pdev;
14828 tp->dev = dev;
14829 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014830 tp->rx_mode = TG3_DEF_RX_MODE;
14831 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070014832
Linus Torvalds1da177e2005-04-16 15:20:36 -070014833 if (tg3_debug > 0)
14834 tp->msg_enable = tg3_debug;
14835 else
14836 tp->msg_enable = TG3_DEF_MSG_ENABLE;
14837
14838 /* The word/byte swap controls here control register access byte
14839 * swapping. DMA data byte swapping is controlled in the GRC_MODE
14840 * setting below.
14841 */
14842 tp->misc_host_ctrl =
14843 MISC_HOST_CTRL_MASK_PCI_INT |
14844 MISC_HOST_CTRL_WORD_SWAP |
14845 MISC_HOST_CTRL_INDIR_ACCESS |
14846 MISC_HOST_CTRL_PCISTATE_RW;
14847
14848 /* The NONFRM (non-frame) byte/word swap controls take effect
14849 * on descriptor entries, anything which isn't packet data.
14850 *
14851 * The StrongARM chips on the board (one for tx, one for rx)
14852 * are running in big-endian mode.
14853 */
14854 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
14855 GRC_MODE_WSWAP_NONFRM_DATA);
14856#ifdef __BIG_ENDIAN
14857 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
14858#endif
14859 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014860 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000014861 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014862
Matt Carlsond5fe4882008-11-21 17:20:32 -080014863 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010014864 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014865 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014866 err = -ENOMEM;
14867 goto err_out_free_dev;
14868 }
14869
Linus Torvalds1da177e2005-04-16 15:20:36 -070014870 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
14871 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014872
Linus Torvalds1da177e2005-04-16 15:20:36 -070014873 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014874 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014875 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014876
14877 err = tg3_get_invariants(tp);
14878 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014879 dev_err(&pdev->dev,
14880 "Problem fetching invariants of chip, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014881 goto err_out_iounmap;
14882 }
14883
Matt Carlson615774f2009-11-13 13:03:39 +000014884 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Matt Carlson0a58d662011-04-05 14:22:45 +000014885 !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
Stephen Hemminger00829822008-11-20 20:14:53 -080014886 dev->netdev_ops = &tg3_netdev_ops;
14887 else
14888 dev->netdev_ops = &tg3_netdev_ops_dma_bug;
14889
14890
Michael Chan4a29cc22006-03-19 13:21:12 -080014891 /* The EPB bridge inside 5714, 5715, and 5780 and any
14892 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080014893 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
14894 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
14895 * do DMA address check in tg3_start_xmit().
14896 */
Michael Chan4a29cc22006-03-19 13:21:12 -080014897 if (tp->tg3_flags2 & TG3_FLG2_IS_5788)
Yang Hongyang284901a2009-04-06 19:01:15 -070014898 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Michael Chan4a29cc22006-03-19 13:21:12 -080014899 else if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070014900 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080014901#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070014902 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014903#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080014904 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070014905 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014906
14907 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070014908 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080014909 err = pci_set_dma_mask(pdev, dma_mask);
14910 if (!err) {
14911 dev->features |= NETIF_F_HIGHDMA;
14912 err = pci_set_consistent_dma_mask(pdev,
14913 persist_dma_mask);
14914 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014915 dev_err(&pdev->dev, "Unable to obtain 64 bit "
14916 "DMA for consistent allocations\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080014917 goto err_out_iounmap;
14918 }
14919 }
14920 }
Yang Hongyang284901a2009-04-06 19:01:15 -070014921 if (err || dma_mask == DMA_BIT_MASK(32)) {
14922 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080014923 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014924 dev_err(&pdev->dev,
14925 "No usable DMA configuration, aborting\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080014926 goto err_out_iounmap;
14927 }
14928 }
14929
Michael Chanfdfec1722005-07-25 12:31:48 -070014930 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014931
Matt Carlson507399f2009-11-13 13:03:37 +000014932 /* Selectively allow TSO based on operating conditions */
14933 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) ||
14934 (tp->fw_needed && !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)))
14935 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
14936 else {
14937 tp->tg3_flags2 &= ~(TG3_FLG2_TSO_CAPABLE | TG3_FLG2_TSO_BUG);
14938 tp->fw_needed = NULL;
14939 }
14940
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014941 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
Matt Carlson9e9fd122009-01-19 16:57:45 -080014942 tp->fw_needed = FIRMWARE_TG3;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014943
Michael Chan4e3a7aa2006-03-20 17:47:44 -080014944 /* TSO is on by default on chips that support hardware TSO.
14945 * Firmware TSO on older chips gives lower performance, so it
14946 * is off by default, but can be enabled using ethtool.
14947 */
Matt Carlsone849cdc2009-11-13 13:03:38 +000014948 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) &&
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014949 (dev->features & NETIF_F_IP_CSUM)) {
Matt Carlsone849cdc2009-11-13 13:03:38 +000014950 dev->features |= NETIF_F_TSO;
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014951 vlan_features_add(dev, NETIF_F_TSO);
14952 }
Matt Carlsone849cdc2009-11-13 13:03:38 +000014953 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
14954 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3)) {
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014955 if (dev->features & NETIF_F_IPV6_CSUM) {
Michael Chanb0026622006-07-03 19:42:14 -070014956 dev->features |= NETIF_F_TSO6;
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014957 vlan_features_add(dev, NETIF_F_TSO6);
14958 }
Matt Carlsone849cdc2009-11-13 13:03:38 +000014959 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
14960 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070014961 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14962 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014963 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014964 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070014965 dev->features |= NETIF_F_TSO_ECN;
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014966 vlan_features_add(dev, NETIF_F_TSO_ECN);
14967 }
Michael Chanb0026622006-07-03 19:42:14 -070014968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014969
Linus Torvalds1da177e2005-04-16 15:20:36 -070014970 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
14971 !(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
14972 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
14973 tp->tg3_flags2 |= TG3_FLG2_MAX_RXPEND_64;
14974 tp->rx_pending = 63;
14975 }
14976
Linus Torvalds1da177e2005-04-16 15:20:36 -070014977 err = tg3_get_device_address(tp);
14978 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014979 dev_err(&pdev->dev,
14980 "Could not obtain valid ethernet address, aborting\n");
Matt Carlson026a6c22009-12-03 08:36:24 +000014981 goto err_out_iounmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014982 }
14983
Matt Carlson0d3031d2007-10-10 18:02:43 -070014984 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
Matt Carlson63532392008-11-03 16:49:57 -080014985 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
Al Viro79ea13c2008-01-24 02:06:46 -080014986 if (!tp->aperegs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014987 dev_err(&pdev->dev,
14988 "Cannot map APE registers, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070014989 err = -ENOMEM;
Matt Carlson026a6c22009-12-03 08:36:24 +000014990 goto err_out_iounmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014991 }
14992
14993 tg3_ape_lock_init(tp);
Matt Carlson7fd76442009-02-25 14:27:20 +000014994
14995 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
14996 tg3_read_dash_ver(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014997 }
14998
Matt Carlsonc88864d2007-11-12 21:07:01 -080014999 /*
15000 * Reset chip in case UNDI or EFI driver did not shutdown
15001 * DMA self test will enable WDMAC and we'll see (spurious)
15002 * pending DMA on the PCI bus at that point.
15003 */
15004 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15005 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15006 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15007 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15008 }
15009
15010 err = tg3_test_dma(tp);
15011 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015012 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080015013 goto err_out_apeunmap;
15014 }
15015
Matt Carlson78f90dc2009-11-13 13:03:42 +000015016 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15017 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15018 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000015019 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000015020 struct tg3_napi *tnapi = &tp->napi[i];
15021
15022 tnapi->tp = tp;
15023 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15024
15025 tnapi->int_mbox = intmbx;
15026 if (i < 4)
15027 intmbx += 0x8;
15028 else
15029 intmbx += 0x4;
15030
15031 tnapi->consmbox = rcvmbx;
15032 tnapi->prodmbox = sndmbx;
15033
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015034 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015035 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015036 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000015037 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000015038
15039 if (!(tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX))
15040 break;
15041
15042 /*
15043 * If we support MSIX, we'll be using RSS. If we're using
15044 * RSS, the first vector only handles link interrupts and the
15045 * remaining vectors handle rx and tx interrupts. Reuse the
15046 * mailbox values for the next iteration. The values we setup
15047 * above are still useful for the single vectored mode.
15048 */
15049 if (!i)
15050 continue;
15051
15052 rcvmbx += 0x8;
15053
15054 if (sndmbx & 0x4)
15055 sndmbx -= 0x4;
15056 else
15057 sndmbx += 0xc;
15058 }
15059
Matt Carlsonc88864d2007-11-12 21:07:01 -080015060 tg3_init_coal(tp);
15061
Michael Chanc49a1562006-12-17 17:07:29 -080015062 pci_set_drvdata(pdev, dev);
15063
Linus Torvalds1da177e2005-04-16 15:20:36 -070015064 err = register_netdev(dev);
15065 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015066 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015067 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015068 }
15069
Joe Perches05dbe002010-02-17 19:44:19 +000015070 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15071 tp->board_part_number,
15072 tp->pci_chip_rev_id,
15073 tg3_bus_string(tp, str),
15074 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015075
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015076 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000015077 struct phy_device *phydev;
15078 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000015079 netdev_info(dev,
15080 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015081 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015082 } else {
15083 char *ethtype;
15084
15085 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15086 ethtype = "10/100Base-TX";
15087 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15088 ethtype = "1000Base-SX";
15089 else
15090 ethtype = "10/100/1000Base-T";
15091
Matt Carlson5129c3a2010-04-05 10:19:23 +000015092 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015093 "(WireSpeed[%d])\n", tg3_phy_string(tp), ethtype,
15094 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0);
15095 }
Matt Carlsondf59c942008-11-03 16:52:56 -080015096
Joe Perches05dbe002010-02-17 19:44:19 +000015097 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
15098 (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0,
15099 (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015100 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches05dbe002010-02-17 19:44:19 +000015101 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0,
15102 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) != 0);
15103 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15104 tp->dma_rwctrl,
15105 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15106 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015107
15108 return 0;
15109
Matt Carlson0d3031d2007-10-10 18:02:43 -070015110err_out_apeunmap:
15111 if (tp->aperegs) {
15112 iounmap(tp->aperegs);
15113 tp->aperegs = NULL;
15114 }
15115
Linus Torvalds1da177e2005-04-16 15:20:36 -070015116err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070015117 if (tp->regs) {
15118 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015119 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015121
15122err_out_free_dev:
15123 free_netdev(dev);
15124
15125err_out_free_res:
15126 pci_release_regions(pdev);
15127
15128err_out_disable_pdev:
15129 pci_disable_device(pdev);
15130 pci_set_drvdata(pdev, NULL);
15131 return err;
15132}
15133
15134static void __devexit tg3_remove_one(struct pci_dev *pdev)
15135{
15136 struct net_device *dev = pci_get_drvdata(pdev);
15137
15138 if (dev) {
15139 struct tg3 *tp = netdev_priv(dev);
15140
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015141 if (tp->fw)
15142 release_firmware(tp->fw);
15143
Tejun Heo23f333a2010-12-12 16:45:14 +010015144 cancel_work_sync(&tp->reset_task);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015145
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015146 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
15147 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015148 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015149 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015150
Linus Torvalds1da177e2005-04-16 15:20:36 -070015151 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015152 if (tp->aperegs) {
15153 iounmap(tp->aperegs);
15154 tp->aperegs = NULL;
15155 }
Michael Chan68929142005-08-09 20:17:14 -070015156 if (tp->regs) {
15157 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015158 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015160 free_netdev(dev);
15161 pci_release_regions(pdev);
15162 pci_disable_device(pdev);
15163 pci_set_drvdata(pdev, NULL);
15164 }
15165}
15166
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015167#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015168static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015169{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015170 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015171 struct net_device *dev = pci_get_drvdata(pdev);
15172 struct tg3 *tp = netdev_priv(dev);
15173 int err;
15174
15175 if (!netif_running(dev))
15176 return 0;
15177
Tejun Heo23f333a2010-12-12 16:45:14 +010015178 flush_work_sync(&tp->reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015179 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015180 tg3_netif_stop(tp);
15181
15182 del_timer_sync(&tp->timer);
15183
David S. Millerf47c11e2005-06-24 20:18:35 -070015184 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015185 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015186 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015187
15188 netif_device_detach(dev);
15189
David S. Millerf47c11e2005-06-24 20:18:35 -070015190 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015191 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan6a9eba12005-12-13 21:08:58 -080015192 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
David S. Millerf47c11e2005-06-24 20:18:35 -070015193 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015194
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015195 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015196 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015197 int err2;
15198
David S. Millerf47c11e2005-06-24 20:18:35 -070015199 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015200
Michael Chan6a9eba12005-12-13 21:08:58 -080015201 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015202 err2 = tg3_restart_hw(tp, 1);
15203 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015204 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015205
15206 tp->timer.expires = jiffies + tp->timer_offset;
15207 add_timer(&tp->timer);
15208
15209 netif_device_attach(dev);
15210 tg3_netif_start(tp);
15211
Michael Chanb9ec6c12006-07-25 16:37:27 -070015212out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015213 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015214
15215 if (!err2)
15216 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015217 }
15218
15219 return err;
15220}
15221
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015222static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015223{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015224 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015225 struct net_device *dev = pci_get_drvdata(pdev);
15226 struct tg3 *tp = netdev_priv(dev);
15227 int err;
15228
15229 if (!netif_running(dev))
15230 return 0;
15231
Linus Torvalds1da177e2005-04-16 15:20:36 -070015232 netif_device_attach(dev);
15233
David S. Millerf47c11e2005-06-24 20:18:35 -070015234 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015235
Michael Chan6a9eba12005-12-13 21:08:58 -080015236 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Michael Chanb9ec6c12006-07-25 16:37:27 -070015237 err = tg3_restart_hw(tp, 1);
15238 if (err)
15239 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015240
15241 tp->timer.expires = jiffies + tp->timer_offset;
15242 add_timer(&tp->timer);
15243
Linus Torvalds1da177e2005-04-16 15:20:36 -070015244 tg3_netif_start(tp);
15245
Michael Chanb9ec6c12006-07-25 16:37:27 -070015246out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015247 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015248
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015249 if (!err)
15250 tg3_phy_start(tp);
15251
Michael Chanb9ec6c12006-07-25 16:37:27 -070015252 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015253}
15254
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015255static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015256#define TG3_PM_OPS (&tg3_pm_ops)
15257
15258#else
15259
15260#define TG3_PM_OPS NULL
15261
15262#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015263
Linus Torvalds1da177e2005-04-16 15:20:36 -070015264static struct pci_driver tg3_driver = {
15265 .name = DRV_MODULE_NAME,
15266 .id_table = tg3_pci_tbl,
15267 .probe = tg3_init_one,
15268 .remove = __devexit_p(tg3_remove_one),
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015269 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015270};
15271
15272static int __init tg3_init(void)
15273{
Jeff Garzik29917622006-08-19 17:48:59 -040015274 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015275}
15276
15277static void __exit tg3_cleanup(void)
15278{
15279 pci_unregister_driver(&tg3_driver);
15280}
15281
15282module_init(tg3_init);
15283module_exit(tg3_cleanup);