blob: 72744353b1cbee029e8a754342e32c663abef2df [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * tg3.c: Broadcom Tigon3 ethernet driver.
3 *
4 * Copyright (C) 2001, 2002, 2003, 2004 David S. Miller (davem@redhat.com)
5 * Copyright (C) 2001, 2002, 2003 Jeff Garzik (jgarzik@pobox.com)
6 * Copyright (C) 2004 Sun Microsystems Inc.
Matt Carlsonb86fb2c2011-01-25 15:58:57 +00007 * Copyright (C) 2005-2011 Broadcom Corporation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Firmware is:
Michael Chan49cabf42005-06-06 15:15:17 -070010 * Derived from proprietary unpublished source code,
11 * Copyright (C) 2000-2003 Broadcom Corporation.
12 *
13 * Permission is hereby granted for the distribution of this firmware
14 * data in hexadecimal or equivalent format, provided this copyright
15 * notice is accompanying it.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <linux/module.h>
20#include <linux/moduleparam.h>
Matt Carlson6867c842010-07-11 09:31:44 +000021#include <linux/stringify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/kernel.h>
23#include <linux/types.h>
24#include <linux/compiler.h>
25#include <linux/slab.h>
26#include <linux/delay.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020027#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/init.h>
29#include <linux/ioport.h>
30#include <linux/pci.h>
31#include <linux/netdevice.h>
32#include <linux/etherdevice.h>
33#include <linux/skbuff.h>
34#include <linux/ethtool.h>
Matt Carlson3110f5f52010-12-06 08:28:50 +000035#include <linux/mdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/mii.h>
Matt Carlson158d7ab2008-05-29 01:37:54 -070037#include <linux/phy.h>
Matt Carlsona9daf362008-05-25 23:49:44 -070038#include <linux/brcmphy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/if_vlan.h>
40#include <linux/ip.h>
41#include <linux/tcp.h>
42#include <linux/workqueue.h>
Michael Chan61487482005-09-05 17:53:19 -070043#include <linux/prefetch.h>
Tobias Klauserf9a5f7d2005-10-29 15:09:26 +020044#include <linux/dma-mapping.h>
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080045#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#include <net/checksum.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030048#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#include <asm/system.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000051#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <asm/byteorder.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000053#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
David S. Miller49b6e95f2007-03-29 01:38:42 -070055#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <asm/idprom.h>
David S. Miller49b6e95f2007-03-29 01:38:42 -070057#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#endif
59
Matt Carlson63532392008-11-03 16:49:57 -080060#define BAR_0 0
61#define BAR_2 2
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include "tg3.h"
64
65#define DRV_MODULE_NAME "tg3"
Matt Carlson6867c842010-07-11 09:31:44 +000066#define TG3_MAJ_NUM 3
Matt Carlsonb86fb2c2011-01-25 15:58:57 +000067#define TG3_MIN_NUM 117
Matt Carlson6867c842010-07-11 09:31:44 +000068#define DRV_MODULE_VERSION \
69 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Matt Carlsonb86fb2c2011-01-25 15:58:57 +000070#define DRV_MODULE_RELDATE "January 25, 2011"
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72#define TG3_DEF_MAC_MODE 0
73#define TG3_DEF_RX_MODE 0
74#define TG3_DEF_TX_MODE 0
75#define TG3_DEF_MSG_ENABLE \
76 (NETIF_MSG_DRV | \
77 NETIF_MSG_PROBE | \
78 NETIF_MSG_LINK | \
79 NETIF_MSG_TIMER | \
80 NETIF_MSG_IFDOWN | \
81 NETIF_MSG_IFUP | \
82 NETIF_MSG_RX_ERR | \
83 NETIF_MSG_TX_ERR)
84
85/* length of time before we decide the hardware is borked,
86 * and dev->tx_timeout() should be called to fix the problem
87 */
88#define TG3_TX_TIMEOUT (5 * HZ)
89
90/* hardware minimum and maximum for a single frame's data payload */
91#define TG3_MIN_MTU 60
92#define TG3_MAX_MTU(tp) \
Matt Carlson8f666b02009-08-28 13:58:24 +000093 ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95/* These numbers seem to be hard coded in the NIC firmware somehow.
96 * You can't change the ring sizes, but you can change where you place
97 * them in the NIC onboard memory.
98 */
Matt Carlson7cb32cf2010-09-30 10:34:36 +000099#define TG3_RX_STD_RING_SIZE(tp) \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000100 ((tp->tg3_flags3 & TG3_FLG3_LRG_PROD_RING_CAP) ? \
101 TG3_RX_STD_MAX_SIZE_5717 : TG3_RX_STD_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#define TG3_DEF_RX_RING_PENDING 200
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000103#define TG3_RX_JMB_RING_SIZE(tp) \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000104 ((tp->tg3_flags3 & TG3_FLG3_LRG_PROD_RING_CAP) ? \
105 TG3_RX_JMB_MAX_SIZE_5717 : TG3_RX_JMB_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#define TG3_DEF_RX_JUMBO_RING_PENDING 100
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000107#define TG3_RSS_INDIR_TBL_SIZE 128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109/* Do not place this n-ring entries value into the tp struct itself,
110 * we really want to expose these constants to GCC so that modulo et
111 * al. operations are done with shifts and masks instead of with
112 * hw multiply/modulo instructions. Another solution would be to
113 * replace things like '% foo' with '& (foo - 1)'.
114 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116#define TG3_TX_RING_SIZE 512
117#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
118
Matt Carlson2c49a442010-09-30 10:34:35 +0000119#define TG3_RX_STD_RING_BYTES(tp) \
120 (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
121#define TG3_RX_JMB_RING_BYTES(tp) \
122 (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
123#define TG3_RX_RCB_RING_BYTES(tp) \
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000124 (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
126 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
128
Matt Carlson287be122009-08-28 13:58:46 +0000129#define TG3_DMA_BYTE_ENAB 64
130
131#define TG3_RX_STD_DMA_SZ 1536
132#define TG3_RX_JMB_DMA_SZ 9046
133
134#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
135
136#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
137#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Matt Carlson2c49a442010-09-30 10:34:35 +0000139#define TG3_RX_STD_BUFF_RING_SIZE(tp) \
140 (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000141
Matt Carlson2c49a442010-09-30 10:34:35 +0000142#define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
143 (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000144
Matt Carlsond2757fc2010-04-12 06:58:27 +0000145/* Due to a hardware bug, the 5701 can only DMA to memory addresses
146 * that are at least dword aligned when used in PCIX mode. The driver
147 * works around this bug by double copying the packet. This workaround
148 * is built into the normal double copy length check for efficiency.
149 *
150 * However, the double copy is only necessary on those architectures
151 * where unaligned memory accesses are inefficient. For those architectures
152 * where unaligned memory accesses incur little penalty, we can reintegrate
153 * the 5701 in the normal rx path. Doing so saves a device structure
154 * dereference by hardcoding the double copy threshold in place.
155 */
156#define TG3_RX_COPY_THRESHOLD 256
157#if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
158 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
159#else
160 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
161#endif
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000164#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Matt Carlsonad829262008-11-21 17:16:16 -0800166#define TG3_RAW_IP_ALIGN 2
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168/* number of ETHTOOL_GSTATS u64's */
169#define TG3_NUM_STATS (sizeof(struct tg3_ethtool_stats)/sizeof(u64))
170
Michael Chan4cafd3f2005-05-29 14:56:34 -0700171#define TG3_NUM_TEST 6
172
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000173#define TG3_FW_UPDATE_TIMEOUT_SEC 5
174
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800175#define FIRMWARE_TG3 "tigon/tg3.bin"
176#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
177#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179static char version[] __devinitdata =
Joe Perches05dbe002010-02-17 19:44:19 +0000180 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
183MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
184MODULE_LICENSE("GPL");
185MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800186MODULE_FIRMWARE(FIRMWARE_TG3);
187MODULE_FIRMWARE(FIRMWARE_TG3TSO);
188MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
191module_param(tg3_debug, int, 0);
192MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
193
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000194static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700195 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
196 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
197 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
198 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
199 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
200 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
201 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
202 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
203 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
204 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
205 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
206 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
207 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
208 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
209 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
210 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
211 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
212 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
213 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901)},
214 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2)},
215 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
216 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700217 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700218 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700219 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700220 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
221 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F)},
222 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
223 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
224 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
225 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
226 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F)},
227 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
228 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
229 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
230 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700231 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700232 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
233 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
234 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Michael Chan676917d2006-12-07 00:20:22 -0800235 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700236 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
237 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
238 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
239 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
240 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Matt Carlson321d32a2008-11-21 17:22:19 -0800254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
255 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
256 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790)},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000257 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000258 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
259 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000260 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
261 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
262 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
263 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
264 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791)},
265 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795)},
Matt Carlson302b5002010-06-05 17:24:38 +0000266 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Matt Carlsonba1f3c72011-04-05 14:22:50 +0000267 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700268 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
269 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
270 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
271 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
272 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
273 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
274 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
275 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276};
277
278MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
279
Andreas Mohr50da8592006-08-14 23:54:30 -0700280static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 const char string[ETH_GSTRING_LEN];
282} ethtool_stats_keys[TG3_NUM_STATS] = {
283 { "rx_octets" },
284 { "rx_fragments" },
285 { "rx_ucast_packets" },
286 { "rx_mcast_packets" },
287 { "rx_bcast_packets" },
288 { "rx_fcs_errors" },
289 { "rx_align_errors" },
290 { "rx_xon_pause_rcvd" },
291 { "rx_xoff_pause_rcvd" },
292 { "rx_mac_ctrl_rcvd" },
293 { "rx_xoff_entered" },
294 { "rx_frame_too_long_errors" },
295 { "rx_jabbers" },
296 { "rx_undersize_packets" },
297 { "rx_in_length_errors" },
298 { "rx_out_length_errors" },
299 { "rx_64_or_less_octet_packets" },
300 { "rx_65_to_127_octet_packets" },
301 { "rx_128_to_255_octet_packets" },
302 { "rx_256_to_511_octet_packets" },
303 { "rx_512_to_1023_octet_packets" },
304 { "rx_1024_to_1522_octet_packets" },
305 { "rx_1523_to_2047_octet_packets" },
306 { "rx_2048_to_4095_octet_packets" },
307 { "rx_4096_to_8191_octet_packets" },
308 { "rx_8192_to_9022_octet_packets" },
309
310 { "tx_octets" },
311 { "tx_collisions" },
312
313 { "tx_xon_sent" },
314 { "tx_xoff_sent" },
315 { "tx_flow_control" },
316 { "tx_mac_errors" },
317 { "tx_single_collisions" },
318 { "tx_mult_collisions" },
319 { "tx_deferred" },
320 { "tx_excessive_collisions" },
321 { "tx_late_collisions" },
322 { "tx_collide_2times" },
323 { "tx_collide_3times" },
324 { "tx_collide_4times" },
325 { "tx_collide_5times" },
326 { "tx_collide_6times" },
327 { "tx_collide_7times" },
328 { "tx_collide_8times" },
329 { "tx_collide_9times" },
330 { "tx_collide_10times" },
331 { "tx_collide_11times" },
332 { "tx_collide_12times" },
333 { "tx_collide_13times" },
334 { "tx_collide_14times" },
335 { "tx_collide_15times" },
336 { "tx_ucast_packets" },
337 { "tx_mcast_packets" },
338 { "tx_bcast_packets" },
339 { "tx_carrier_sense_errors" },
340 { "tx_discards" },
341 { "tx_errors" },
342
343 { "dma_writeq_full" },
344 { "dma_write_prioq_full" },
345 { "rxbds_empty" },
346 { "rx_discards" },
347 { "rx_errors" },
348 { "rx_threshold_hit" },
349
350 { "dma_readq_full" },
351 { "dma_read_prioq_full" },
352 { "tx_comp_queue_full" },
353
354 { "ring_set_send_prod_index" },
355 { "ring_status_update" },
356 { "nic_irqs" },
357 { "nic_avoided_irqs" },
358 { "nic_tx_threshold_hit" }
359};
360
Andreas Mohr50da8592006-08-14 23:54:30 -0700361static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700362 const char string[ETH_GSTRING_LEN];
363} ethtool_test_keys[TG3_NUM_TEST] = {
364 { "nvram test (online) " },
365 { "link test (online) " },
366 { "register test (offline)" },
367 { "memory test (offline)" },
368 { "loopback test (offline)" },
369 { "interrupt test (offline)" },
370};
371
Michael Chanb401e9e2005-12-19 16:27:04 -0800372static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
373{
374 writel(val, tp->regs + off);
375}
376
377static u32 tg3_read32(struct tg3 *tp, u32 off)
378{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000379 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800380}
381
Matt Carlson0d3031d2007-10-10 18:02:43 -0700382static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
383{
384 writel(val, tp->aperegs + off);
385}
386
387static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
388{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000389 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700390}
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
393{
Michael Chan68929142005-08-09 20:17:14 -0700394 unsigned long flags;
395
396 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700397 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
398 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700399 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700400}
401
402static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
403{
404 writel(val, tp->regs + off);
405 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
Michael Chan68929142005-08-09 20:17:14 -0700408static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
409{
410 unsigned long flags;
411 u32 val;
412
413 spin_lock_irqsave(&tp->indirect_lock, flags);
414 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
415 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
416 spin_unlock_irqrestore(&tp->indirect_lock, flags);
417 return val;
418}
419
420static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
421{
422 unsigned long flags;
423
424 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
425 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
426 TG3_64BIT_REG_LOW, val);
427 return;
428 }
Matt Carlson66711e662009-11-13 13:03:49 +0000429 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700430 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
431 TG3_64BIT_REG_LOW, val);
432 return;
433 }
434
435 spin_lock_irqsave(&tp->indirect_lock, flags);
436 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
437 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
438 spin_unlock_irqrestore(&tp->indirect_lock, flags);
439
440 /* In indirect mode when disabling interrupts, we also need
441 * to clear the interrupt bit in the GRC local ctrl register.
442 */
443 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
444 (val == 0x1)) {
445 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
446 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
447 }
448}
449
450static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
451{
452 unsigned long flags;
453 u32 val;
454
455 spin_lock_irqsave(&tp->indirect_lock, flags);
456 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
457 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
458 spin_unlock_irqrestore(&tp->indirect_lock, flags);
459 return val;
460}
461
Michael Chanb401e9e2005-12-19 16:27:04 -0800462/* usec_wait specifies the wait time in usec when writing to certain registers
463 * where it is unsafe to read back the register without some delay.
464 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
465 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
466 */
467static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Michael Chanb401e9e2005-12-19 16:27:04 -0800469 if ((tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) ||
470 (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
471 /* Non-posted methods */
472 tp->write32(tp, off, val);
473 else {
474 /* Posted method */
475 tg3_write32(tp, off, val);
476 if (usec_wait)
477 udelay(usec_wait);
478 tp->read32(tp, off);
479 }
480 /* Wait again after the read for the posted method to guarantee that
481 * the wait time is met.
482 */
483 if (usec_wait)
484 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
Michael Chan09ee9292005-08-09 20:17:00 -0700487static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
488{
489 tp->write32_mbox(tp, off, val);
Michael Chan68929142005-08-09 20:17:14 -0700490 if (!(tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) &&
491 !(tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
492 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700493}
494
Michael Chan20094932005-08-09 20:16:32 -0700495static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
497 void __iomem *mbox = tp->regs + off;
498 writel(val, mbox);
499 if (tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG)
500 writel(val, mbox);
501 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
502 readl(mbox);
503}
504
Michael Chanb5d37722006-09-27 16:06:21 -0700505static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
506{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000507 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700508}
509
510static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
511{
512 writel(val, tp->regs + off + GRCMBOX_BASE);
513}
514
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000515#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700516#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000517#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
518#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
519#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700520
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000521#define tw32(reg, val) tp->write32(tp, reg, val)
522#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
523#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
524#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
527{
Michael Chan68929142005-08-09 20:17:14 -0700528 unsigned long flags;
529
Michael Chanb5d37722006-09-27 16:06:21 -0700530 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
531 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
532 return;
533
Michael Chan68929142005-08-09 20:17:14 -0700534 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chanbbadf502006-04-06 21:46:34 -0700535 if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
536 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
537 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Michael Chanbbadf502006-04-06 21:46:34 -0700539 /* Always leave this as zero. */
540 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
541 } else {
542 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
543 tw32_f(TG3PCI_MEM_WIN_DATA, val);
544
545 /* Always leave this as zero. */
546 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
547 }
Michael Chan68929142005-08-09 20:17:14 -0700548 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
551static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
552{
Michael Chan68929142005-08-09 20:17:14 -0700553 unsigned long flags;
554
Michael Chanb5d37722006-09-27 16:06:21 -0700555 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
556 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
557 *val = 0;
558 return;
559 }
560
Michael Chan68929142005-08-09 20:17:14 -0700561 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chanbbadf502006-04-06 21:46:34 -0700562 if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
563 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
564 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Michael Chanbbadf502006-04-06 21:46:34 -0700566 /* Always leave this as zero. */
567 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
568 } else {
569 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
570 *val = tr32(TG3PCI_MEM_WIN_DATA);
571
572 /* Always leave this as zero. */
573 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
574 }
Michael Chan68929142005-08-09 20:17:14 -0700575 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
Matt Carlson0d3031d2007-10-10 18:02:43 -0700578static void tg3_ape_lock_init(struct tg3 *tp)
579{
580 int i;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000581 u32 regbase;
582
583 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
584 regbase = TG3_APE_LOCK_GRANT;
585 else
586 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700587
588 /* Make sure the driver hasn't any stale locks. */
589 for (i = 0; i < 8; i++)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000590 tg3_ape_write32(tp, regbase + 4 * i, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700591}
592
593static int tg3_ape_lock(struct tg3 *tp, int locknum)
594{
595 int i, off;
596 int ret = 0;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000597 u32 status, req, gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700598
599 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
600 return 0;
601
602 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000603 case TG3_APE_LOCK_GRC:
604 case TG3_APE_LOCK_MEM:
605 break;
606 default:
607 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700608 }
609
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000610 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
611 req = TG3_APE_LOCK_REQ;
612 gnt = TG3_APE_LOCK_GRANT;
613 } else {
614 req = TG3_APE_PER_LOCK_REQ;
615 gnt = TG3_APE_PER_LOCK_GRANT;
616 }
617
Matt Carlson0d3031d2007-10-10 18:02:43 -0700618 off = 4 * locknum;
619
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000620 tg3_ape_write32(tp, req + off, APE_LOCK_REQ_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700621
622 /* Wait for up to 1 millisecond to acquire lock. */
623 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000624 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700625 if (status == APE_LOCK_GRANT_DRIVER)
626 break;
627 udelay(10);
628 }
629
630 if (status != APE_LOCK_GRANT_DRIVER) {
631 /* Revoke the lock request. */
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000632 tg3_ape_write32(tp, gnt + off,
Matt Carlson0d3031d2007-10-10 18:02:43 -0700633 APE_LOCK_GRANT_DRIVER);
634
635 ret = -EBUSY;
636 }
637
638 return ret;
639}
640
641static void tg3_ape_unlock(struct tg3 *tp, int locknum)
642{
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000643 u32 gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700644
645 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
646 return;
647
648 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000649 case TG3_APE_LOCK_GRC:
650 case TG3_APE_LOCK_MEM:
651 break;
652 default:
653 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700654 }
655
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000656 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
657 gnt = TG3_APE_LOCK_GRANT;
658 else
659 gnt = TG3_APE_PER_LOCK_GRANT;
660
661 tg3_ape_write32(tp, gnt + 4 * locknum, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700662}
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664static void tg3_disable_ints(struct tg3 *tp)
665{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000666 int i;
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 tw32(TG3PCI_MISC_HOST_CTRL,
669 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000670 for (i = 0; i < tp->irq_max; i++)
671 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674static void tg3_enable_ints(struct tg3 *tp)
675{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000676 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000677
Michael Chanbbe832c2005-06-24 20:20:04 -0700678 tp->irq_sync = 0;
679 wmb();
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 tw32(TG3PCI_MISC_HOST_CTRL,
682 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000683
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000684 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000685 for (i = 0; i < tp->irq_cnt; i++) {
686 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000687
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000688 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
689 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
690 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
691
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000692 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000693 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000694
695 /* Force an initial interrupt */
696 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
697 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
698 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
699 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000700 tw32(HOSTCC_MODE, tp->coal_now);
701
702 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
Matt Carlson17375d22009-08-28 14:02:18 +0000705static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700706{
Matt Carlson17375d22009-08-28 14:02:18 +0000707 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000708 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700709 unsigned int work_exists = 0;
710
711 /* check for phy events */
712 if (!(tp->tg3_flags &
713 (TG3_FLAG_USE_LINKCHG_REG |
714 TG3_FLAG_POLL_SERDES))) {
715 if (sblk->status & SD_STATUS_LINK_CHG)
716 work_exists = 1;
717 }
718 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000719 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000720 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700721 work_exists = 1;
722
723 return work_exists;
724}
725
Matt Carlson17375d22009-08-28 14:02:18 +0000726/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700727 * similar to tg3_enable_ints, but it accurately determines whether there
728 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400729 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 */
Matt Carlson17375d22009-08-28 14:02:18 +0000731static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Matt Carlson17375d22009-08-28 14:02:18 +0000733 struct tg3 *tp = tnapi->tp;
734
Matt Carlson898a56f2009-08-28 14:02:40 +0000735 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 mmiowb();
737
David S. Millerfac9b832005-05-18 22:46:34 -0700738 /* When doing tagged status, this work check is unnecessary.
739 * The last_tag we write above tells the chip which piece of
740 * work we've completed.
741 */
742 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
Matt Carlson17375d22009-08-28 14:02:18 +0000743 tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700744 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000745 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748static void tg3_switch_clocks(struct tg3 *tp)
749{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000750 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 u32 orig_clock_ctrl;
752
Matt Carlson795d01c2007-10-07 23:28:17 -0700753 if ((tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
754 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700755 return;
756
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000757 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 orig_clock_ctrl = clock_ctrl;
760 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
761 CLOCK_CTRL_CLKRUN_OENABLE |
762 0x1f);
763 tp->pci_clock_ctrl = clock_ctrl;
764
765 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
766 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800767 tw32_wait_f(TG3PCI_CLOCK_CTRL,
768 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
770 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800771 tw32_wait_f(TG3PCI_CLOCK_CTRL,
772 clock_ctrl |
773 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
774 40);
775 tw32_wait_f(TG3PCI_CLOCK_CTRL,
776 clock_ctrl | (CLOCK_CTRL_ALTCLK),
777 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800779 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
781
782#define PHY_BUSY_LOOPS 5000
783
784static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
785{
786 u32 frame_val;
787 unsigned int loops;
788 int ret;
789
790 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
791 tw32_f(MAC_MI_MODE,
792 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
793 udelay(80);
794 }
795
796 *val = 0x0;
797
Matt Carlson882e9792009-09-01 13:21:36 +0000798 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 MI_COM_PHY_ADDR_MASK);
800 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
801 MI_COM_REG_ADDR_MASK);
802 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 tw32_f(MAC_MI_COM, frame_val);
805
806 loops = PHY_BUSY_LOOPS;
807 while (loops != 0) {
808 udelay(10);
809 frame_val = tr32(MAC_MI_COM);
810
811 if ((frame_val & MI_COM_BUSY) == 0) {
812 udelay(5);
813 frame_val = tr32(MAC_MI_COM);
814 break;
815 }
816 loops -= 1;
817 }
818
819 ret = -EBUSY;
820 if (loops != 0) {
821 *val = frame_val & MI_COM_DATA_MASK;
822 ret = 0;
823 }
824
825 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
826 tw32_f(MAC_MI_MODE, tp->mi_mode);
827 udelay(80);
828 }
829
830 return ret;
831}
832
833static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
834{
835 u32 frame_val;
836 unsigned int loops;
837 int ret;
838
Matt Carlsonf07e9af2010-08-02 11:26:07 +0000839 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Michael Chanb5d37722006-09-27 16:06:21 -0700840 (reg == MII_TG3_CTRL || reg == MII_TG3_AUX_CTRL))
841 return 0;
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
844 tw32_f(MAC_MI_MODE,
845 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
846 udelay(80);
847 }
848
Matt Carlson882e9792009-09-01 13:21:36 +0000849 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 MI_COM_PHY_ADDR_MASK);
851 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
852 MI_COM_REG_ADDR_MASK);
853 frame_val |= (val & MI_COM_DATA_MASK);
854 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 tw32_f(MAC_MI_COM, frame_val);
857
858 loops = PHY_BUSY_LOOPS;
859 while (loops != 0) {
860 udelay(10);
861 frame_val = tr32(MAC_MI_COM);
862 if ((frame_val & MI_COM_BUSY) == 0) {
863 udelay(5);
864 frame_val = tr32(MAC_MI_COM);
865 break;
866 }
867 loops -= 1;
868 }
869
870 ret = -EBUSY;
871 if (loops != 0)
872 ret = 0;
873
874 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
875 tw32_f(MAC_MI_MODE, tp->mi_mode);
876 udelay(80);
877 }
878
879 return ret;
880}
881
Matt Carlson95e28692008-05-25 23:44:14 -0700882static int tg3_bmcr_reset(struct tg3 *tp)
883{
884 u32 phy_control;
885 int limit, err;
886
887 /* OK, reset it, and poll the BMCR_RESET bit until it
888 * clears or we time out.
889 */
890 phy_control = BMCR_RESET;
891 err = tg3_writephy(tp, MII_BMCR, phy_control);
892 if (err != 0)
893 return -EBUSY;
894
895 limit = 5000;
896 while (limit--) {
897 err = tg3_readphy(tp, MII_BMCR, &phy_control);
898 if (err != 0)
899 return -EBUSY;
900
901 if ((phy_control & BMCR_RESET) == 0) {
902 udelay(40);
903 break;
904 }
905 udelay(10);
906 }
Roel Kluind4675b52009-02-12 16:33:27 -0800907 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -0700908 return -EBUSY;
909
910 return 0;
911}
912
Matt Carlson158d7ab2008-05-29 01:37:54 -0700913static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
914{
Francois Romieu3d165432009-01-19 16:56:50 -0800915 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700916 u32 val;
917
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000918 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700919
920 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000921 val = -EIO;
922
923 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700924
925 return val;
926}
927
928static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
929{
Francois Romieu3d165432009-01-19 16:56:50 -0800930 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000931 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700932
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000933 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700934
935 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000936 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700937
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000938 spin_unlock_bh(&tp->lock);
939
940 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700941}
942
943static int tg3_mdio_reset(struct mii_bus *bp)
944{
945 return 0;
946}
947
Matt Carlson9c61d6b2008-11-03 16:54:56 -0800948static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -0700949{
950 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800951 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -0700952
Matt Carlson3f0e3ad2009-11-02 14:24:36 +0000953 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800954 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +0000955 case PHY_ID_BCM50610:
956 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800957 val = MAC_PHYCFG2_50610_LED_MODES;
958 break;
Matt Carlson6a443a02010-02-17 15:17:04 +0000959 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800960 val = MAC_PHYCFG2_AC131_LED_MODES;
961 break;
Matt Carlson6a443a02010-02-17 15:17:04 +0000962 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800963 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
964 break;
Matt Carlson6a443a02010-02-17 15:17:04 +0000965 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800966 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
967 break;
968 default:
Matt Carlsona9daf362008-05-25 23:49:44 -0700969 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800970 }
971
972 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
973 tw32(MAC_PHYCFG2, val);
974
975 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +0000976 val &= ~(MAC_PHYCFG1_RGMII_INT |
977 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
978 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800979 tw32(MAC_PHYCFG1, val);
980
981 return;
982 }
983
Matt Carlson14417062010-02-17 15:16:59 +0000984 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800985 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
986 MAC_PHYCFG2_FMODE_MASK_MASK |
987 MAC_PHYCFG2_GMODE_MASK_MASK |
988 MAC_PHYCFG2_ACT_MASK_MASK |
989 MAC_PHYCFG2_QUAL_MASK_MASK |
990 MAC_PHYCFG2_INBAND_ENABLE;
991
992 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -0700993
Matt Carlsonbb85fbb2009-08-25 10:09:07 +0000994 val = tr32(MAC_PHYCFG1);
995 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
996 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Matt Carlson14417062010-02-17 15:16:59 +0000997 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)) {
Matt Carlsona9daf362008-05-25 23:49:44 -0700998 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
999 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
1000 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1001 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1002 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001003 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1004 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1005 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001006
Matt Carlsona9daf362008-05-25 23:49:44 -07001007 val = tr32(MAC_EXT_RGMII_MODE);
1008 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1009 MAC_RGMII_MODE_RX_QUALITY |
1010 MAC_RGMII_MODE_RX_ACTIVITY |
1011 MAC_RGMII_MODE_RX_ENG_DET |
1012 MAC_RGMII_MODE_TX_ENABLE |
1013 MAC_RGMII_MODE_TX_LOWPWR |
1014 MAC_RGMII_MODE_TX_RESET);
Matt Carlson14417062010-02-17 15:16:59 +00001015 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)) {
Matt Carlsona9daf362008-05-25 23:49:44 -07001016 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1017 val |= MAC_RGMII_MODE_RX_INT_B |
1018 MAC_RGMII_MODE_RX_QUALITY |
1019 MAC_RGMII_MODE_RX_ACTIVITY |
1020 MAC_RGMII_MODE_RX_ENG_DET;
1021 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1022 val |= MAC_RGMII_MODE_TX_ENABLE |
1023 MAC_RGMII_MODE_TX_LOWPWR |
1024 MAC_RGMII_MODE_TX_RESET;
1025 }
1026 tw32(MAC_EXT_RGMII_MODE, val);
1027}
1028
Matt Carlson158d7ab2008-05-29 01:37:54 -07001029static void tg3_mdio_start(struct tg3 *tp)
1030{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001031 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1032 tw32_f(MAC_MI_MODE, tp->mi_mode);
1033 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001034
Matt Carlson9ea48182010-02-17 15:17:01 +00001035 if ((tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) &&
1036 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1037 tg3_mdio_config_5785(tp);
1038}
1039
1040static int tg3_mdio_init(struct tg3 *tp)
1041{
1042 int i;
1043 u32 reg;
1044 struct phy_device *phydev;
1045
Matt Carlson0a58d662011-04-05 14:22:45 +00001046 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001047 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001048
Matt Carlson9c7df912010-06-05 17:24:36 +00001049 tp->phy_addr = PCI_FUNC(tp->pdev->devfn) + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001050
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001051 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1052 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1053 else
1054 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1055 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001056 if (is_serdes)
1057 tp->phy_addr += 7;
1058 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001059 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001060
Matt Carlson158d7ab2008-05-29 01:37:54 -07001061 tg3_mdio_start(tp);
1062
1063 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) ||
1064 (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED))
1065 return 0;
1066
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001067 tp->mdio_bus = mdiobus_alloc();
1068 if (tp->mdio_bus == NULL)
1069 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001070
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001071 tp->mdio_bus->name = "tg3 mdio bus";
1072 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001073 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001074 tp->mdio_bus->priv = tp;
1075 tp->mdio_bus->parent = &tp->pdev->dev;
1076 tp->mdio_bus->read = &tg3_mdio_read;
1077 tp->mdio_bus->write = &tg3_mdio_write;
1078 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001079 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001080 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001081
1082 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001083 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001084
1085 /* The bus registration will look for all the PHYs on the mdio bus.
1086 * Unfortunately, it does not ensure the PHY is powered up before
1087 * accessing the PHY ID registers. A chip reset is the
1088 * quickest way to bring the device back to an operational state..
1089 */
1090 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1091 tg3_bmcr_reset(tp);
1092
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001093 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001094 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001095 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001096 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001097 return i;
1098 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001099
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001100 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001101
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001102 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001103 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001104 mdiobus_unregister(tp->mdio_bus);
1105 mdiobus_free(tp->mdio_bus);
1106 return -ENODEV;
1107 }
1108
1109 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001110 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001111 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001112 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001113 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001114 case PHY_ID_BCM50610:
1115 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001116 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001117 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001118 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001119 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson14417062010-02-17 15:16:59 +00001120 if (tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)
Matt Carlsona9daf362008-05-25 23:49:44 -07001121 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
1122 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1123 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
1124 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1125 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001126 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001127 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001128 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001129 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001130 case PHY_ID_RTL8201E:
1131 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001132 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001133 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001134 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001135 break;
1136 }
1137
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001138 tp->tg3_flags3 |= TG3_FLG3_MDIOBUS_INITED;
1139
1140 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1141 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001142
1143 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001144}
1145
1146static void tg3_mdio_fini(struct tg3 *tp)
1147{
1148 if (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) {
1149 tp->tg3_flags3 &= ~TG3_FLG3_MDIOBUS_INITED;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001150 mdiobus_unregister(tp->mdio_bus);
1151 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001152 }
1153}
1154
Matt Carlsonddfc87b2010-10-14 10:37:40 +00001155static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
1156{
1157 int err;
1158
1159 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1160 if (err)
1161 goto done;
1162
1163 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1164 if (err)
1165 goto done;
1166
1167 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1168 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1169 if (err)
1170 goto done;
1171
1172 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
1173
1174done:
1175 return err;
1176}
1177
1178static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
1179{
1180 int err;
1181
1182 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1183 if (err)
1184 goto done;
1185
1186 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1187 if (err)
1188 goto done;
1189
1190 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1191 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1192 if (err)
1193 goto done;
1194
1195 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
1196
1197done:
1198 return err;
1199}
1200
Matt Carlson95e28692008-05-25 23:44:14 -07001201/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001202static inline void tg3_generate_fw_event(struct tg3 *tp)
1203{
1204 u32 val;
1205
1206 val = tr32(GRC_RX_CPU_EVENT);
1207 val |= GRC_RX_CPU_DRIVER_EVENT;
1208 tw32_f(GRC_RX_CPU_EVENT, val);
1209
1210 tp->last_event_jiffies = jiffies;
1211}
1212
1213#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1214
1215/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001216static void tg3_wait_for_event_ack(struct tg3 *tp)
1217{
1218 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001219 unsigned int delay_cnt;
1220 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001221
Matt Carlson4ba526c2008-08-15 14:10:04 -07001222 /* If enough time has passed, no wait is necessary. */
1223 time_remain = (long)(tp->last_event_jiffies + 1 +
1224 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1225 (long)jiffies;
1226 if (time_remain < 0)
1227 return;
1228
1229 /* Check if we can shorten the wait time. */
1230 delay_cnt = jiffies_to_usecs(time_remain);
1231 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1232 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1233 delay_cnt = (delay_cnt >> 3) + 1;
1234
1235 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001236 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1237 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001238 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001239 }
1240}
1241
1242/* tp->lock is held. */
1243static void tg3_ump_link_report(struct tg3 *tp)
1244{
1245 u32 reg;
1246 u32 val;
1247
1248 if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
1249 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
1250 return;
1251
1252 tg3_wait_for_event_ack(tp);
1253
1254 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1255
1256 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1257
1258 val = 0;
1259 if (!tg3_readphy(tp, MII_BMCR, &reg))
1260 val = reg << 16;
1261 if (!tg3_readphy(tp, MII_BMSR, &reg))
1262 val |= (reg & 0xffff);
1263 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1264
1265 val = 0;
1266 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1267 val = reg << 16;
1268 if (!tg3_readphy(tp, MII_LPA, &reg))
1269 val |= (reg & 0xffff);
1270 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1271
1272 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001273 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001274 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1275 val = reg << 16;
1276 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1277 val |= (reg & 0xffff);
1278 }
1279 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1280
1281 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1282 val = reg << 16;
1283 else
1284 val = 0;
1285 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1286
Matt Carlson4ba526c2008-08-15 14:10:04 -07001287 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001288}
1289
1290static void tg3_link_report(struct tg3 *tp)
1291{
1292 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001293 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001294 tg3_ump_link_report(tp);
1295 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001296 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1297 (tp->link_config.active_speed == SPEED_1000 ?
1298 1000 :
1299 (tp->link_config.active_speed == SPEED_100 ?
1300 100 : 10)),
1301 (tp->link_config.active_duplex == DUPLEX_FULL ?
1302 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001303
Joe Perches05dbe002010-02-17 19:44:19 +00001304 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1305 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1306 "on" : "off",
1307 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1308 "on" : "off");
Matt Carlson95e28692008-05-25 23:44:14 -07001309 tg3_ump_link_report(tp);
1310 }
1311}
1312
1313static u16 tg3_advert_flowctrl_1000T(u8 flow_ctrl)
1314{
1315 u16 miireg;
1316
Steve Glendinninge18ce342008-12-16 02:00:00 -08001317 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001318 miireg = ADVERTISE_PAUSE_CAP;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001319 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001320 miireg = ADVERTISE_PAUSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001321 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001322 miireg = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1323 else
1324 miireg = 0;
1325
1326 return miireg;
1327}
1328
1329static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1330{
1331 u16 miireg;
1332
Steve Glendinninge18ce342008-12-16 02:00:00 -08001333 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001334 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001335 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001336 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001337 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001338 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1339 else
1340 miireg = 0;
1341
1342 return miireg;
1343}
1344
Matt Carlson95e28692008-05-25 23:44:14 -07001345static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1346{
1347 u8 cap = 0;
1348
1349 if (lcladv & ADVERTISE_1000XPAUSE) {
1350 if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1351 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001352 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001353 else if (rmtadv & LPA_1000XPAUSE_ASYM)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001354 cap = FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001355 } else {
1356 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001357 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001358 }
1359 } else if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1360 if ((rmtadv & LPA_1000XPAUSE) && (rmtadv & LPA_1000XPAUSE_ASYM))
Steve Glendinninge18ce342008-12-16 02:00:00 -08001361 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001362 }
1363
1364 return cap;
1365}
1366
Matt Carlsonf51f3562008-05-25 23:45:08 -07001367static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001368{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001369 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001370 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001371 u32 old_rx_mode = tp->rx_mode;
1372 u32 old_tx_mode = tp->tx_mode;
1373
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001374 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001375 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001376 else
1377 autoneg = tp->link_config.autoneg;
1378
1379 if (autoneg == AUTONEG_ENABLE &&
Matt Carlson95e28692008-05-25 23:44:14 -07001380 (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001381 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001382 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001383 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001384 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001385 } else
1386 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001387
Matt Carlsonf51f3562008-05-25 23:45:08 -07001388 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001389
Steve Glendinninge18ce342008-12-16 02:00:00 -08001390 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001391 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1392 else
1393 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1394
Matt Carlsonf51f3562008-05-25 23:45:08 -07001395 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001396 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001397
Steve Glendinninge18ce342008-12-16 02:00:00 -08001398 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001399 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1400 else
1401 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1402
Matt Carlsonf51f3562008-05-25 23:45:08 -07001403 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001404 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001405}
1406
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001407static void tg3_adjust_link(struct net_device *dev)
1408{
1409 u8 oldflowctrl, linkmesg = 0;
1410 u32 mac_mode, lcl_adv, rmt_adv;
1411 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001412 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001413
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001414 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001415
1416 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1417 MAC_MODE_HALF_DUPLEX);
1418
1419 oldflowctrl = tp->link_config.active_flowctrl;
1420
1421 if (phydev->link) {
1422 lcl_adv = 0;
1423 rmt_adv = 0;
1424
1425 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1426 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001427 else if (phydev->speed == SPEED_1000 ||
1428 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001429 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001430 else
1431 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001432
1433 if (phydev->duplex == DUPLEX_HALF)
1434 mac_mode |= MAC_MODE_HALF_DUPLEX;
1435 else {
1436 lcl_adv = tg3_advert_flowctrl_1000T(
1437 tp->link_config.flowctrl);
1438
1439 if (phydev->pause)
1440 rmt_adv = LPA_PAUSE_CAP;
1441 if (phydev->asym_pause)
1442 rmt_adv |= LPA_PAUSE_ASYM;
1443 }
1444
1445 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1446 } else
1447 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1448
1449 if (mac_mode != tp->mac_mode) {
1450 tp->mac_mode = mac_mode;
1451 tw32_f(MAC_MODE, tp->mac_mode);
1452 udelay(40);
1453 }
1454
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001455 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1456 if (phydev->speed == SPEED_10)
1457 tw32(MAC_MI_STAT,
1458 MAC_MI_STAT_10MBPS_MODE |
1459 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1460 else
1461 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1462 }
1463
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001464 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1465 tw32(MAC_TX_LENGTHS,
1466 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1467 (6 << TX_LENGTHS_IPG_SHIFT) |
1468 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1469 else
1470 tw32(MAC_TX_LENGTHS,
1471 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1472 (6 << TX_LENGTHS_IPG_SHIFT) |
1473 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1474
1475 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1476 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1477 phydev->speed != tp->link_config.active_speed ||
1478 phydev->duplex != tp->link_config.active_duplex ||
1479 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001480 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001481
1482 tp->link_config.active_speed = phydev->speed;
1483 tp->link_config.active_duplex = phydev->duplex;
1484
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001485 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001486
1487 if (linkmesg)
1488 tg3_link_report(tp);
1489}
1490
1491static int tg3_phy_init(struct tg3 *tp)
1492{
1493 struct phy_device *phydev;
1494
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001495 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001496 return 0;
1497
1498 /* Bring the PHY back to a known state. */
1499 tg3_bmcr_reset(tp);
1500
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001501 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001502
1503 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08001504 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001505 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001506 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001507 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001508 return PTR_ERR(phydev);
1509 }
1510
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001511 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001512 switch (phydev->interface) {
1513 case PHY_INTERFACE_MODE_GMII:
1514 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001515 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08001516 phydev->supported &= (PHY_GBIT_FEATURES |
1517 SUPPORTED_Pause |
1518 SUPPORTED_Asym_Pause);
1519 break;
1520 }
1521 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001522 case PHY_INTERFACE_MODE_MII:
1523 phydev->supported &= (PHY_BASIC_FEATURES |
1524 SUPPORTED_Pause |
1525 SUPPORTED_Asym_Pause);
1526 break;
1527 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001528 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001529 return -EINVAL;
1530 }
1531
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001532 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001533
1534 phydev->advertising = phydev->supported;
1535
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001536 return 0;
1537}
1538
1539static void tg3_phy_start(struct tg3 *tp)
1540{
1541 struct phy_device *phydev;
1542
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001543 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001544 return;
1545
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001546 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001547
Matt Carlson80096062010-08-02 11:26:06 +00001548 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
1549 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001550 phydev->speed = tp->link_config.orig_speed;
1551 phydev->duplex = tp->link_config.orig_duplex;
1552 phydev->autoneg = tp->link_config.orig_autoneg;
1553 phydev->advertising = tp->link_config.orig_advertising;
1554 }
1555
1556 phy_start(phydev);
1557
1558 phy_start_aneg(phydev);
1559}
1560
1561static void tg3_phy_stop(struct tg3 *tp)
1562{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001563 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001564 return;
1565
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001566 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001567}
1568
1569static void tg3_phy_fini(struct tg3 *tp)
1570{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001571 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001572 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001573 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001574 }
1575}
1576
Matt Carlson52b02d02010-10-14 10:37:41 +00001577static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
1578{
1579 int err;
1580
1581 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1582 if (!err)
1583 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
1584
1585 return err;
1586}
1587
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00001588static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001589{
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00001590 int err;
1591
1592 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1593 if (!err)
1594 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1595
1596 return err;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001597}
1598
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001599static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1600{
1601 u32 phytest;
1602
1603 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1604 u32 phy;
1605
1606 tg3_writephy(tp, MII_TG3_FET_TEST,
1607 phytest | MII_TG3_FET_SHADOW_EN);
1608 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1609 if (enable)
1610 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1611 else
1612 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1613 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1614 }
1615 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1616 }
1617}
1618
Matt Carlson6833c042008-11-21 17:18:59 -08001619static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1620{
1621 u32 reg;
1622
Matt Carlsonecf14102010-01-20 16:58:05 +00001623 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlson0a58d662011-04-05 14:22:45 +00001624 ((tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001625 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001626 return;
1627
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001628 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001629 tg3_phy_fet_toggle_apd(tp, enable);
1630 return;
1631 }
1632
Matt Carlson6833c042008-11-21 17:18:59 -08001633 reg = MII_TG3_MISC_SHDW_WREN |
1634 MII_TG3_MISC_SHDW_SCR5_SEL |
1635 MII_TG3_MISC_SHDW_SCR5_LPED |
1636 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1637 MII_TG3_MISC_SHDW_SCR5_SDTL |
1638 MII_TG3_MISC_SHDW_SCR5_C125OE;
1639 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1640 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1641
1642 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1643
1644
1645 reg = MII_TG3_MISC_SHDW_WREN |
1646 MII_TG3_MISC_SHDW_APD_SEL |
1647 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1648 if (enable)
1649 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1650
1651 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1652}
1653
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001654static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
1655{
1656 u32 phy;
1657
1658 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001659 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001660 return;
1661
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001662 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001663 u32 ephy;
1664
Matt Carlson535ef6e2009-08-25 10:09:36 +00001665 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
1666 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
1667
1668 tg3_writephy(tp, MII_TG3_FET_TEST,
1669 ephy | MII_TG3_FET_SHADOW_EN);
1670 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001671 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00001672 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001673 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00001674 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1675 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001676 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00001677 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001678 }
1679 } else {
1680 phy = MII_TG3_AUXCTL_MISC_RDSEL_MISC |
1681 MII_TG3_AUXCTL_SHDWSEL_MISC;
1682 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, phy) &&
1683 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &phy)) {
1684 if (enable)
1685 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1686 else
1687 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1688 phy |= MII_TG3_AUXCTL_MISC_WREN;
1689 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1690 }
1691 }
1692}
1693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694static void tg3_phy_set_wirespeed(struct tg3 *tp)
1695{
1696 u32 val;
1697
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001698 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 return;
1700
1701 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x7007) &&
1702 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &val))
1703 tg3_writephy(tp, MII_TG3_AUX_CTRL,
1704 (val | (1 << 15) | (1 << 4)));
1705}
1706
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001707static void tg3_phy_apply_otp(struct tg3 *tp)
1708{
1709 u32 otp, phy;
1710
1711 if (!tp->phy_otp)
1712 return;
1713
1714 otp = tp->phy_otp;
1715
1716 /* Enable SM_DSP clock and tx 6dB coding. */
1717 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1718 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
1719 MII_TG3_AUXCTL_ACTL_TX_6DB;
1720 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1721
1722 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
1723 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
1724 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
1725
1726 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
1727 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
1728 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
1729
1730 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
1731 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
1732 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
1733
1734 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
1735 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
1736
1737 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
1738 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
1739
1740 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
1741 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
1742 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
1743
1744 /* Turn off SM_DSP clock. */
1745 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1746 MII_TG3_AUXCTL_ACTL_TX_6DB;
1747 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1748}
1749
Matt Carlson52b02d02010-10-14 10:37:41 +00001750static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
1751{
1752 u32 val;
1753
1754 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
1755 return;
1756
1757 tp->setlpicnt = 0;
1758
1759 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
1760 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00001761 tp->link_config.active_duplex == DUPLEX_FULL &&
1762 (tp->link_config.active_speed == SPEED_100 ||
1763 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00001764 u32 eeectl;
1765
1766 if (tp->link_config.active_speed == SPEED_1000)
1767 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
1768 else
1769 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
1770
1771 tw32(TG3_CPMU_EEE_CTRL, eeectl);
1772
Matt Carlson3110f5f52010-12-06 08:28:50 +00001773 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
1774 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00001775
Matt Carlson21a00ab2011-01-25 15:58:55 +00001776 switch (val) {
1777 case TG3_CL45_D7_EEERES_STAT_LP_1000T:
1778 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
1779 case ASIC_REV_5717:
1780 case ASIC_REV_5719:
1781 case ASIC_REV_57765:
1782 /* Enable SM_DSP clock and tx 6dB coding. */
1783 val = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1784 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
1785 MII_TG3_AUXCTL_ACTL_TX_6DB;
1786 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
1787
1788 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
1789
1790 /* Turn off SM_DSP clock. */
1791 val = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1792 MII_TG3_AUXCTL_ACTL_TX_6DB;
1793 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
1794 }
1795 /* Fallthrough */
1796 case TG3_CL45_D7_EEERES_STAT_LP_100TX:
Matt Carlson52b02d02010-10-14 10:37:41 +00001797 tp->setlpicnt = 2;
Matt Carlson21a00ab2011-01-25 15:58:55 +00001798 }
Matt Carlson52b02d02010-10-14 10:37:41 +00001799 }
1800
1801 if (!tp->setlpicnt) {
1802 val = tr32(TG3_CPMU_EEE_MODE);
1803 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
1804 }
1805}
1806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807static int tg3_wait_macro_done(struct tg3 *tp)
1808{
1809 int limit = 100;
1810
1811 while (limit--) {
1812 u32 tmp32;
1813
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001814 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 if ((tmp32 & 0x1000) == 0)
1816 break;
1817 }
1818 }
Roel Kluind4675b52009-02-12 16:33:27 -08001819 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 return -EBUSY;
1821
1822 return 0;
1823}
1824
1825static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
1826{
1827 static const u32 test_pat[4][6] = {
1828 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
1829 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
1830 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
1831 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
1832 };
1833 int chan;
1834
1835 for (chan = 0; chan < 4; chan++) {
1836 int i;
1837
1838 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1839 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001840 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842 for (i = 0; i < 6; i++)
1843 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
1844 test_pat[chan][i]);
1845
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001846 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 if (tg3_wait_macro_done(tp)) {
1848 *resetp = 1;
1849 return -EBUSY;
1850 }
1851
1852 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1853 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001854 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 if (tg3_wait_macro_done(tp)) {
1856 *resetp = 1;
1857 return -EBUSY;
1858 }
1859
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001860 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 if (tg3_wait_macro_done(tp)) {
1862 *resetp = 1;
1863 return -EBUSY;
1864 }
1865
1866 for (i = 0; i < 6; i += 2) {
1867 u32 low, high;
1868
1869 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
1870 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
1871 tg3_wait_macro_done(tp)) {
1872 *resetp = 1;
1873 return -EBUSY;
1874 }
1875 low &= 0x7fff;
1876 high &= 0x000f;
1877 if (low != test_pat[chan][i] ||
1878 high != test_pat[chan][i+1]) {
1879 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
1880 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
1881 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
1882
1883 return -EBUSY;
1884 }
1885 }
1886 }
1887
1888 return 0;
1889}
1890
1891static int tg3_phy_reset_chanpat(struct tg3 *tp)
1892{
1893 int chan;
1894
1895 for (chan = 0; chan < 4; chan++) {
1896 int i;
1897
1898 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1899 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001900 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 for (i = 0; i < 6; i++)
1902 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001903 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 if (tg3_wait_macro_done(tp))
1905 return -EBUSY;
1906 }
1907
1908 return 0;
1909}
1910
1911static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
1912{
1913 u32 reg32, phy9_orig;
1914 int retries, do_phy_reset, err;
1915
1916 retries = 10;
1917 do_phy_reset = 1;
1918 do {
1919 if (do_phy_reset) {
1920 err = tg3_bmcr_reset(tp);
1921 if (err)
1922 return err;
1923 do_phy_reset = 0;
1924 }
1925
1926 /* Disable transmitter and interrupt. */
1927 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
1928 continue;
1929
1930 reg32 |= 0x3000;
1931 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1932
1933 /* Set full-duplex, 1000 mbps. */
1934 tg3_writephy(tp, MII_BMCR,
1935 BMCR_FULLDPLX | TG3_BMCR_SPEED1000);
1936
1937 /* Set to master mode. */
1938 if (tg3_readphy(tp, MII_TG3_CTRL, &phy9_orig))
1939 continue;
1940
1941 tg3_writephy(tp, MII_TG3_CTRL,
1942 (MII_TG3_CTRL_AS_MASTER |
1943 MII_TG3_CTRL_ENABLE_AS_MASTER));
1944
1945 /* Enable SM_DSP_CLOCK and 6dB. */
1946 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1947
1948 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00001949 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
1951 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
1952 if (!err)
1953 break;
1954 } while (--retries);
1955
1956 err = tg3_phy_reset_chanpat(tp);
1957 if (err)
1958 return err;
1959
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00001960 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
1962 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001963 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
1965 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
1966 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
1967 /* Set Extended packet length bit for jumbo frames */
1968 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4400);
Matt Carlson859a588792010-04-05 10:19:28 +00001969 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1971 }
1972
1973 tg3_writephy(tp, MII_TG3_CTRL, phy9_orig);
1974
1975 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
1976 reg32 &= ~0x3000;
1977 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1978 } else if (!err)
1979 err = -EBUSY;
1980
1981 return err;
1982}
1983
1984/* This will reset the tigon3 PHY if there is no valid
1985 * link unless the FORCE argument is non-zero.
1986 */
1987static int tg3_phy_reset(struct tg3 *tp)
1988{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00001989 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 int err;
1991
Michael Chan60189dd2006-12-17 17:08:07 -08001992 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08001993 val = tr32(GRC_MISC_CFG);
1994 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
1995 udelay(40);
1996 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00001997 err = tg3_readphy(tp, MII_BMSR, &val);
1998 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 if (err != 0)
2000 return -EBUSY;
2001
Michael Chanc8e1e822006-04-29 18:55:17 -07002002 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
2003 netif_carrier_off(tp->dev);
2004 tg3_link_report(tp);
2005 }
2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2008 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2009 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2010 err = tg3_phy_reset_5703_4_5(tp);
2011 if (err)
2012 return err;
2013 goto out;
2014 }
2015
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002016 cpmuctrl = 0;
2017 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2018 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2019 cpmuctrl = tr32(TG3_CPMU_CTRL);
2020 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2021 tw32(TG3_CPMU_CTRL,
2022 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2023 }
2024
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 err = tg3_bmcr_reset(tp);
2026 if (err)
2027 return err;
2028
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002029 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002030 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2031 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002032
2033 tw32(TG3_CPMU_CTRL, cpmuctrl);
2034 }
2035
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002036 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2037 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002038 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2039 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2040 CPMU_LSPD_1000MB_MACCLK_12_5) {
2041 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2042 udelay(40);
2043 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2044 }
2045 }
2046
Matt Carlson0a58d662011-04-05 14:22:45 +00002047 if ((tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002048 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002049 return 0;
2050
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002051 tg3_phy_apply_otp(tp);
2052
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002053 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002054 tg3_phy_toggle_apd(tp, true);
2055 else
2056 tg3_phy_toggle_apd(tp, false);
2057
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058out:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002059 if (tp->phy_flags & TG3_PHYFLG_ADC_BUG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002061 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2062 tg3_phydsp_write(tp, 0x000a, 0x0323);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
2064 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002065 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002066 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2067 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002069 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002071 tg3_phydsp_write(tp, 0x000a, 0x310b);
2072 tg3_phydsp_write(tp, 0x201f, 0x9506);
2073 tg3_phydsp_write(tp, 0x401f, 0x14e2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002075 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Michael Chanc424cb22006-04-29 18:56:34 -07002076 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
2077 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002078 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
Michael Chanc1d2a192007-01-08 19:57:20 -08002079 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2080 tg3_writephy(tp, MII_TG3_TEST1,
2081 MII_TG3_TEST1_TRIM_EN | 0x4);
2082 } else
2083 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
Michael Chanc424cb22006-04-29 18:56:34 -07002084 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
2085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 /* Set Extended packet length bit (bit 14) on all chips that */
2087 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002088 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 /* Cannot do read-modify-write on 5401 */
2090 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
Matt Carlson8f666b02009-08-28 13:58:24 +00002091 } else if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 /* Set bit 14 with read-modify-write to preserve other bits */
2093 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0007) &&
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002094 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &val))
2095 tg3_writephy(tp, MII_TG3_AUX_CTRL, val | 0x4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 }
2097
2098 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2099 * jumbo frames transmission.
2100 */
Matt Carlson8f666b02009-08-28 13:58:24 +00002101 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002102 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002103 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002104 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 }
2106
Michael Chan715116a2006-09-27 16:09:25 -07002107 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002108 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002109 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002110 }
2111
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002112 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 tg3_phy_set_wirespeed(tp);
2114 return 0;
2115}
2116
2117static void tg3_frob_aux_power(struct tg3 *tp)
2118{
Matt Carlson683644b2011-03-09 16:58:23 +00002119 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Matt Carlson334355a2010-01-20 16:58:10 +00002121 /* The GPIOs do something completely different on 57765. */
2122 if ((tp->tg3_flags2 & TG3_FLG2_IS_NIC) == 0 ||
Matt Carlsona50d0792010-06-05 17:24:37 +00002123 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson334355a2010-01-20 16:58:10 +00002124 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 return;
2126
Matt Carlson683644b2011-03-09 16:58:23 +00002127 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2128 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +00002129 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2130 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) &&
Matt Carlson683644b2011-03-09 16:58:23 +00002131 tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002132 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002134 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002135
Michael Chanbc1c7562006-03-20 17:48:03 -08002136 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002137 if (dev_peer) {
2138 struct tg3 *tp_peer = netdev_priv(dev_peer);
2139
2140 if (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE)
2141 return;
2142
2143 if ((tp_peer->tg3_flags & TG3_FLAG_WOL_ENABLE) ||
2144 (tp_peer->tg3_flags & TG3_FLAG_ENABLE_ASF))
2145 need_vaux = true;
2146 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
Matt Carlson683644b2011-03-09 16:58:23 +00002149 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) ||
2150 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
2151 need_vaux = true;
2152
2153 if (need_vaux) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2155 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002156 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2157 (GRC_LCLCTRL_GPIO_OE0 |
2158 GRC_LCLCTRL_GPIO_OE1 |
2159 GRC_LCLCTRL_GPIO_OE2 |
2160 GRC_LCLCTRL_GPIO_OUTPUT0 |
2161 GRC_LCLCTRL_GPIO_OUTPUT1),
2162 100);
Matt Carlson8d519ab2009-04-20 06:58:01 +00002163 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2164 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -07002165 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2166 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2167 GRC_LCLCTRL_GPIO_OE1 |
2168 GRC_LCLCTRL_GPIO_OE2 |
2169 GRC_LCLCTRL_GPIO_OUTPUT0 |
2170 GRC_LCLCTRL_GPIO_OUTPUT1 |
2171 tp->grc_local_ctrl;
2172 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2173
2174 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2175 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2176
2177 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2178 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 } else {
2180 u32 no_gpio2;
Michael Chandc56b7d2005-12-19 16:26:28 -08002181 u32 grc_local_ctrl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
Michael Chandc56b7d2005-12-19 16:26:28 -08002183 /* Workaround to prevent overdrawing Amps. */
2184 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2185 ASIC_REV_5714) {
2186 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chanb401e9e2005-12-19 16:27:04 -08002187 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2188 grc_local_ctrl, 100);
Michael Chandc56b7d2005-12-19 16:26:28 -08002189 }
2190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 /* On 5753 and variants, GPIO2 cannot be used. */
2192 no_gpio2 = tp->nic_sram_data_cfg &
2193 NIC_SRAM_DATA_CFG_NO_GPIO2;
2194
Michael Chandc56b7d2005-12-19 16:26:28 -08002195 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 GRC_LCLCTRL_GPIO_OE1 |
2197 GRC_LCLCTRL_GPIO_OE2 |
2198 GRC_LCLCTRL_GPIO_OUTPUT1 |
2199 GRC_LCLCTRL_GPIO_OUTPUT2;
2200 if (no_gpio2) {
2201 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2202 GRC_LCLCTRL_GPIO_OUTPUT2);
2203 }
Michael Chanb401e9e2005-12-19 16:27:04 -08002204 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2205 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
2207 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2208
Michael Chanb401e9e2005-12-19 16:27:04 -08002209 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2210 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
2212 if (!no_gpio2) {
2213 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chanb401e9e2005-12-19 16:27:04 -08002214 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2215 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 }
2217 }
2218 } else {
2219 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
2220 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002221 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2222 (GRC_LCLCTRL_GPIO_OE1 |
2223 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
Michael Chanb401e9e2005-12-19 16:27:04 -08002225 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2226 GRC_LCLCTRL_GPIO_OE1, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
Michael Chanb401e9e2005-12-19 16:27:04 -08002228 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2229 (GRC_LCLCTRL_GPIO_OE1 |
2230 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 }
2232 }
2233}
2234
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002235static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2236{
2237 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2238 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002239 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002240 if (speed != SPEED_10)
2241 return 1;
2242 } else if (speed == SPEED_10)
2243 return 1;
2244
2245 return 0;
2246}
2247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248static int tg3_setup_phy(struct tg3 *, int);
2249
2250#define RESET_KIND_SHUTDOWN 0
2251#define RESET_KIND_INIT 1
2252#define RESET_KIND_SUSPEND 2
2253
2254static void tg3_write_sig_post_reset(struct tg3 *, int);
2255static int tg3_halt_cpu(struct tg3 *, u32);
2256
Matt Carlson0a459aa2008-11-03 16:54:15 -08002257static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002258{
Matt Carlsonce057f02007-11-12 21:08:03 -08002259 u32 val;
2260
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002261 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002262 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2263 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2264 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2265
2266 sg_dig_ctrl |=
2267 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2268 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2269 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2270 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002271 return;
Michael Chan51297242007-02-13 12:17:57 -08002272 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002273
Michael Chan60189dd2006-12-17 17:08:07 -08002274 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002275 tg3_bmcr_reset(tp);
2276 val = tr32(GRC_MISC_CFG);
2277 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2278 udelay(40);
2279 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002280 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002281 u32 phytest;
2282 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2283 u32 phy;
2284
2285 tg3_writephy(tp, MII_ADVERTISE, 0);
2286 tg3_writephy(tp, MII_BMCR,
2287 BMCR_ANENABLE | BMCR_ANRESTART);
2288
2289 tg3_writephy(tp, MII_TG3_FET_TEST,
2290 phytest | MII_TG3_FET_SHADOW_EN);
2291 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2292 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2293 tg3_writephy(tp,
2294 MII_TG3_FET_SHDW_AUXMODE4,
2295 phy);
2296 }
2297 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2298 }
2299 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002300 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002301 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2302 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002303
2304 tg3_writephy(tp, MII_TG3_AUX_CTRL,
2305 MII_TG3_AUXCTL_SHDWSEL_PWRCTL |
2306 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2307 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2308 MII_TG3_AUXCTL_PCTL_VREG_11V);
Michael Chan715116a2006-09-27 16:09:25 -07002309 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002310
Michael Chan15c3b692006-03-22 01:06:52 -08002311 /* The PHY should not be powered down on some chips because
2312 * of bugs.
2313 */
2314 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2315 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2316 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002317 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Michael Chan15c3b692006-03-22 01:06:52 -08002318 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002319
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002320 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2321 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002322 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2323 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2324 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2325 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2326 }
2327
Michael Chan15c3b692006-03-22 01:06:52 -08002328 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2329}
2330
Matt Carlson3f007892008-11-03 16:51:36 -08002331/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002332static int tg3_nvram_lock(struct tg3 *tp)
2333{
2334 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2335 int i;
2336
2337 if (tp->nvram_lock_cnt == 0) {
2338 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2339 for (i = 0; i < 8000; i++) {
2340 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2341 break;
2342 udelay(20);
2343 }
2344 if (i == 8000) {
2345 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2346 return -ENODEV;
2347 }
2348 }
2349 tp->nvram_lock_cnt++;
2350 }
2351 return 0;
2352}
2353
2354/* tp->lock is held. */
2355static void tg3_nvram_unlock(struct tg3 *tp)
2356{
2357 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2358 if (tp->nvram_lock_cnt > 0)
2359 tp->nvram_lock_cnt--;
2360 if (tp->nvram_lock_cnt == 0)
2361 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2362 }
2363}
2364
2365/* tp->lock is held. */
2366static void tg3_enable_nvram_access(struct tg3 *tp)
2367{
2368 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +00002369 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002370 u32 nvaccess = tr32(NVRAM_ACCESS);
2371
2372 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2373 }
2374}
2375
2376/* tp->lock is held. */
2377static void tg3_disable_nvram_access(struct tg3 *tp)
2378{
2379 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +00002380 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002381 u32 nvaccess = tr32(NVRAM_ACCESS);
2382
2383 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2384 }
2385}
2386
2387static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2388 u32 offset, u32 *val)
2389{
2390 u32 tmp;
2391 int i;
2392
2393 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2394 return -EINVAL;
2395
2396 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2397 EEPROM_ADDR_DEVID_MASK |
2398 EEPROM_ADDR_READ);
2399 tw32(GRC_EEPROM_ADDR,
2400 tmp |
2401 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2402 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2403 EEPROM_ADDR_ADDR_MASK) |
2404 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2405
2406 for (i = 0; i < 1000; i++) {
2407 tmp = tr32(GRC_EEPROM_ADDR);
2408
2409 if (tmp & EEPROM_ADDR_COMPLETE)
2410 break;
2411 msleep(1);
2412 }
2413 if (!(tmp & EEPROM_ADDR_COMPLETE))
2414 return -EBUSY;
2415
Matt Carlson62cedd12009-04-20 14:52:29 -07002416 tmp = tr32(GRC_EEPROM_DATA);
2417
2418 /*
2419 * The data will always be opposite the native endian
2420 * format. Perform a blind byteswap to compensate.
2421 */
2422 *val = swab32(tmp);
2423
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002424 return 0;
2425}
2426
2427#define NVRAM_CMD_TIMEOUT 10000
2428
2429static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2430{
2431 int i;
2432
2433 tw32(NVRAM_CMD, nvram_cmd);
2434 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2435 udelay(10);
2436 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2437 udelay(10);
2438 break;
2439 }
2440 }
2441
2442 if (i == NVRAM_CMD_TIMEOUT)
2443 return -EBUSY;
2444
2445 return 0;
2446}
2447
2448static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2449{
2450 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2451 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2452 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2453 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2454 (tp->nvram_jedecnum == JEDEC_ATMEL))
2455
2456 addr = ((addr / tp->nvram_pagesize) <<
2457 ATMEL_AT45DB0X1B_PAGE_POS) +
2458 (addr % tp->nvram_pagesize);
2459
2460 return addr;
2461}
2462
2463static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2464{
2465 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2466 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2467 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2468 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2469 (tp->nvram_jedecnum == JEDEC_ATMEL))
2470
2471 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2472 tp->nvram_pagesize) +
2473 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2474
2475 return addr;
2476}
2477
Matt Carlsone4f34112009-02-25 14:25:00 +00002478/* NOTE: Data read in from NVRAM is byteswapped according to
2479 * the byteswapping settings for all other register accesses.
2480 * tg3 devices are BE devices, so on a BE machine, the data
2481 * returned will be exactly as it is seen in NVRAM. On a LE
2482 * machine, the 32-bit value will be byteswapped.
2483 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002484static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2485{
2486 int ret;
2487
2488 if (!(tp->tg3_flags & TG3_FLAG_NVRAM))
2489 return tg3_nvram_read_using_eeprom(tp, offset, val);
2490
2491 offset = tg3_nvram_phys_addr(tp, offset);
2492
2493 if (offset > NVRAM_ADDR_MSK)
2494 return -EINVAL;
2495
2496 ret = tg3_nvram_lock(tp);
2497 if (ret)
2498 return ret;
2499
2500 tg3_enable_nvram_access(tp);
2501
2502 tw32(NVRAM_ADDR, offset);
2503 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2504 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2505
2506 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002507 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002508
2509 tg3_disable_nvram_access(tp);
2510
2511 tg3_nvram_unlock(tp);
2512
2513 return ret;
2514}
2515
Matt Carlsona9dc5292009-02-25 14:25:30 +00002516/* Ensures NVRAM data is in bytestream format. */
2517static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002518{
2519 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002520 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002521 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002522 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002523 return res;
2524}
2525
2526/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08002527static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2528{
2529 u32 addr_high, addr_low;
2530 int i;
2531
2532 addr_high = ((tp->dev->dev_addr[0] << 8) |
2533 tp->dev->dev_addr[1]);
2534 addr_low = ((tp->dev->dev_addr[2] << 24) |
2535 (tp->dev->dev_addr[3] << 16) |
2536 (tp->dev->dev_addr[4] << 8) |
2537 (tp->dev->dev_addr[5] << 0));
2538 for (i = 0; i < 4; i++) {
2539 if (i == 1 && skip_mac_1)
2540 continue;
2541 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2542 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2543 }
2544
2545 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2546 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2547 for (i = 0; i < 12; i++) {
2548 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2549 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2550 }
2551 }
2552
2553 addr_high = (tp->dev->dev_addr[0] +
2554 tp->dev->dev_addr[1] +
2555 tp->dev->dev_addr[2] +
2556 tp->dev->dev_addr[3] +
2557 tp->dev->dev_addr[4] +
2558 tp->dev->dev_addr[5]) &
2559 TX_BACKOFF_SEED_MASK;
2560 tw32(MAC_TX_BACKOFF_SEED, addr_high);
2561}
2562
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002563static void tg3_enable_register_access(struct tg3 *tp)
2564{
2565 /*
2566 * Make sure register accesses (indirect or otherwise) will function
2567 * correctly.
2568 */
2569 pci_write_config_dword(tp->pdev,
2570 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
2571}
2572
2573static int tg3_power_up(struct tg3 *tp)
2574{
2575 tg3_enable_register_access(tp);
2576
2577 pci_set_power_state(tp->pdev, PCI_D0);
2578
2579 /* Switch out of Vaux if it is a NIC */
2580 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
2581 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl, 100);
2582
2583 return 0;
2584}
2585
2586static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587{
2588 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002589 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002591 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002592
2593 /* Restore the CLKREQ setting. */
2594 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
2595 u16 lnkctl;
2596
2597 pci_read_config_word(tp->pdev,
2598 tp->pcie_cap + PCI_EXP_LNKCTL,
2599 &lnkctl);
2600 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2601 pci_write_config_word(tp->pdev,
2602 tp->pcie_cap + PCI_EXP_LNKCTL,
2603 lnkctl);
2604 }
2605
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2607 tw32(TG3PCI_MISC_HOST_CTRL,
2608 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2609
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002610 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002611 (tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
2612
Matt Carlsondd477002008-05-25 23:45:58 -07002613 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002614 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002615 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00002616 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002617 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002618 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002619
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002620 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002621
Matt Carlson80096062010-08-02 11:26:06 +00002622 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002623
2624 tp->link_config.orig_speed = phydev->speed;
2625 tp->link_config.orig_duplex = phydev->duplex;
2626 tp->link_config.orig_autoneg = phydev->autoneg;
2627 tp->link_config.orig_advertising = phydev->advertising;
2628
2629 advertising = ADVERTISED_TP |
2630 ADVERTISED_Pause |
2631 ADVERTISED_Autoneg |
2632 ADVERTISED_10baseT_Half;
2633
2634 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002635 device_should_wake) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002636 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2637 advertising |=
2638 ADVERTISED_100baseT_Half |
2639 ADVERTISED_100baseT_Full |
2640 ADVERTISED_10baseT_Full;
2641 else
2642 advertising |= ADVERTISED_10baseT_Full;
2643 }
2644
2645 phydev->advertising = advertising;
2646
2647 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002648
2649 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00002650 if (phyid != PHY_ID_BCMAC131) {
2651 phyid &= PHY_BCM_OUI_MASK;
2652 if (phyid == PHY_BCM_OUI_1 ||
2653 phyid == PHY_BCM_OUI_2 ||
2654 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08002655 do_low_power = true;
2656 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002657 }
Matt Carlsondd477002008-05-25 23:45:58 -07002658 } else {
Matt Carlson20232762008-12-21 20:18:56 -08002659 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002660
Matt Carlson80096062010-08-02 11:26:06 +00002661 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
2662 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07002663 tp->link_config.orig_speed = tp->link_config.speed;
2664 tp->link_config.orig_duplex = tp->link_config.duplex;
2665 tp->link_config.orig_autoneg = tp->link_config.autoneg;
2666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002668 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Matt Carlsondd477002008-05-25 23:45:58 -07002669 tp->link_config.speed = SPEED_10;
2670 tp->link_config.duplex = DUPLEX_HALF;
2671 tp->link_config.autoneg = AUTONEG_ENABLE;
2672 tg3_setup_phy(tp, 0);
2673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 }
2675
Michael Chanb5d37722006-09-27 16:06:21 -07002676 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2677 u32 val;
2678
2679 val = tr32(GRC_VCPU_EXT_CTRL);
2680 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
2681 } else if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08002682 int i;
2683 u32 val;
2684
2685 for (i = 0; i < 200; i++) {
2686 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2687 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2688 break;
2689 msleep(1);
2690 }
2691 }
Gary Zambranoa85feb82007-05-05 11:52:19 -07002692 if (tp->tg3_flags & TG3_FLAG_WOL_CAP)
2693 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2694 WOL_DRV_STATE_SHUTDOWN |
2695 WOL_DRV_WOL |
2696 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08002697
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002698 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 u32 mac_mode;
2700
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002701 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002702 if (do_low_power) {
Matt Carlsondd477002008-05-25 23:45:58 -07002703 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x5a);
2704 udelay(40);
2705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002707 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07002708 mac_mode = MAC_MODE_PORT_MODE_GMII;
2709 else
2710 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002712 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2713 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2714 ASIC_REV_5700) {
2715 u32 speed = (tp->tg3_flags &
2716 TG3_FLAG_WOL_SPEED_100MB) ?
2717 SPEED_100 : SPEED_10;
2718 if (tg3_5700_link_polarity(tp, speed))
2719 mac_mode |= MAC_MODE_LINK_POLARITY;
2720 else
2721 mac_mode &= ~MAC_MODE_LINK_POLARITY;
2722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 } else {
2724 mac_mode = MAC_MODE_PORT_MODE_TBI;
2725 }
2726
John W. Linvillecbf46852005-04-21 17:01:29 -07002727 if (!(tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 tw32(MAC_LED_CTRL, tp->led_ctrl);
2729
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002730 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
2731 if (((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
2732 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) &&
2733 ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
2734 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)))
2735 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
Matt Carlsond2394e6b2010-11-24 08:31:47 +00002737 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
2738 mac_mode |= MAC_MODE_APE_TX_EN |
2739 MAC_MODE_APE_RX_EN |
2740 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07002741
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 tw32_f(MAC_MODE, mac_mode);
2743 udelay(100);
2744
2745 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2746 udelay(10);
2747 }
2748
2749 if (!(tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB) &&
2750 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2751 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2752 u32 base_val;
2753
2754 base_val = tp->pci_clock_ctrl;
2755 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2756 CLOCK_CTRL_TXCLK_DISABLE);
2757
Michael Chanb401e9e2005-12-19 16:27:04 -08002758 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
2759 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Michael Chand7b0a852007-02-13 12:17:38 -08002760 } else if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
Matt Carlson795d01c2007-10-07 23:28:17 -07002761 (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
Michael Chand7b0a852007-02-13 12:17:38 -08002762 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)) {
Michael Chan4cf78e42005-07-25 12:29:19 -07002763 /* do nothing */
Michael Chan85e94ce2005-04-21 17:05:28 -07002764 } else if (!((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF))) {
2766 u32 newbits1, newbits2;
2767
2768 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2769 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2770 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
2771 CLOCK_CTRL_TXCLK_DISABLE |
2772 CLOCK_CTRL_ALTCLK);
2773 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2774 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
2775 newbits1 = CLOCK_CTRL_625_CORE;
2776 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
2777 } else {
2778 newbits1 = CLOCK_CTRL_ALTCLK;
2779 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2780 }
2781
Michael Chanb401e9e2005-12-19 16:27:04 -08002782 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
2783 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
Michael Chanb401e9e2005-12-19 16:27:04 -08002785 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
2786 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787
2788 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
2789 u32 newbits3;
2790
2791 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2792 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2793 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
2794 CLOCK_CTRL_TXCLK_DISABLE |
2795 CLOCK_CTRL_44MHZ_CORE);
2796 } else {
2797 newbits3 = CLOCK_CTRL_44MHZ_CORE;
2798 }
2799
Michael Chanb401e9e2005-12-19 16:27:04 -08002800 tw32_wait_f(TG3PCI_CLOCK_CTRL,
2801 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 }
2803 }
2804
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002805 if (!(device_should_wake) &&
Matt Carlson22435842008-11-21 17:21:13 -08002806 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08002807 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08002808
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 tg3_frob_aux_power(tp);
2810
2811 /* Workaround for unstable PLL clock */
2812 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
2813 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
2814 u32 val = tr32(0x7d00);
2815
2816 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
2817 tw32(0x7d00, val);
Michael Chan6921d202005-12-13 21:15:53 -08002818 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08002819 int err;
2820
2821 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08002823 if (!err)
2824 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08002825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 }
2827
Michael Chanbbadf502006-04-06 21:46:34 -07002828 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
2829
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 return 0;
2831}
2832
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002833static void tg3_power_down(struct tg3 *tp)
2834{
2835 tg3_power_down_prepare(tp);
2836
2837 pci_wake_from_d3(tp->pdev, tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
2838 pci_set_power_state(tp->pdev, PCI_D3hot);
2839}
2840
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
2842{
2843 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
2844 case MII_TG3_AUX_STAT_10HALF:
2845 *speed = SPEED_10;
2846 *duplex = DUPLEX_HALF;
2847 break;
2848
2849 case MII_TG3_AUX_STAT_10FULL:
2850 *speed = SPEED_10;
2851 *duplex = DUPLEX_FULL;
2852 break;
2853
2854 case MII_TG3_AUX_STAT_100HALF:
2855 *speed = SPEED_100;
2856 *duplex = DUPLEX_HALF;
2857 break;
2858
2859 case MII_TG3_AUX_STAT_100FULL:
2860 *speed = SPEED_100;
2861 *duplex = DUPLEX_FULL;
2862 break;
2863
2864 case MII_TG3_AUX_STAT_1000HALF:
2865 *speed = SPEED_1000;
2866 *duplex = DUPLEX_HALF;
2867 break;
2868
2869 case MII_TG3_AUX_STAT_1000FULL:
2870 *speed = SPEED_1000;
2871 *duplex = DUPLEX_FULL;
2872 break;
2873
2874 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002875 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07002876 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
2877 SPEED_10;
2878 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
2879 DUPLEX_HALF;
2880 break;
2881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 *speed = SPEED_INVALID;
2883 *duplex = DUPLEX_INVALID;
2884 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886}
2887
2888static void tg3_phy_copper_begin(struct tg3 *tp)
2889{
2890 u32 new_adv;
2891 int i;
2892
Matt Carlson80096062010-08-02 11:26:06 +00002893 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 /* Entering low power mode. Disable gigabit and
2895 * 100baseT advertisements.
2896 */
2897 tg3_writephy(tp, MII_TG3_CTRL, 0);
2898
2899 new_adv = (ADVERTISE_10HALF | ADVERTISE_10FULL |
2900 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
2901 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2902 new_adv |= (ADVERTISE_100HALF | ADVERTISE_100FULL);
2903
2904 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2905 } else if (tp->link_config.speed == SPEED_INVALID) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002906 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 tp->link_config.advertising &=
2908 ~(ADVERTISED_1000baseT_Half |
2909 ADVERTISED_1000baseT_Full);
2910
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002911 new_adv = ADVERTISE_CSMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 if (tp->link_config.advertising & ADVERTISED_10baseT_Half)
2913 new_adv |= ADVERTISE_10HALF;
2914 if (tp->link_config.advertising & ADVERTISED_10baseT_Full)
2915 new_adv |= ADVERTISE_10FULL;
2916 if (tp->link_config.advertising & ADVERTISED_100baseT_Half)
2917 new_adv |= ADVERTISE_100HALF;
2918 if (tp->link_config.advertising & ADVERTISED_100baseT_Full)
2919 new_adv |= ADVERTISE_100FULL;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002920
2921 new_adv |= tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2922
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2924
2925 if (tp->link_config.advertising &
2926 (ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full)) {
2927 new_adv = 0;
2928 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
2929 new_adv |= MII_TG3_CTRL_ADV_1000_HALF;
2930 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
2931 new_adv |= MII_TG3_CTRL_ADV_1000_FULL;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002932 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2934 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0))
2935 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2936 MII_TG3_CTRL_ENABLE_AS_MASTER);
2937 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
2938 } else {
2939 tg3_writephy(tp, MII_TG3_CTRL, 0);
2940 }
2941 } else {
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002942 new_adv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2943 new_adv |= ADVERTISE_CSMA;
2944
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 /* Asking for a specific link mode. */
2946 if (tp->link_config.speed == SPEED_1000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2948
2949 if (tp->link_config.duplex == DUPLEX_FULL)
2950 new_adv = MII_TG3_CTRL_ADV_1000_FULL;
2951 else
2952 new_adv = MII_TG3_CTRL_ADV_1000_HALF;
2953 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2954 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
2955 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2956 MII_TG3_CTRL_ENABLE_AS_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 if (tp->link_config.speed == SPEED_100) {
2959 if (tp->link_config.duplex == DUPLEX_FULL)
2960 new_adv |= ADVERTISE_100FULL;
2961 else
2962 new_adv |= ADVERTISE_100HALF;
2963 } else {
2964 if (tp->link_config.duplex == DUPLEX_FULL)
2965 new_adv |= ADVERTISE_10FULL;
2966 else
2967 new_adv |= ADVERTISE_10HALF;
2968 }
2969 tg3_writephy(tp, MII_ADVERTISE, new_adv);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002970
2971 new_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002973
2974 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 }
2976
Matt Carlson52b02d02010-10-14 10:37:41 +00002977 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
Matt Carlsona6b68da2010-12-06 08:28:52 +00002978 u32 val;
Matt Carlson52b02d02010-10-14 10:37:41 +00002979
2980 tw32(TG3_CPMU_EEE_MODE,
2981 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2982
2983 /* Enable SM_DSP clock and tx 6dB coding. */
2984 val = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
2985 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
2986 MII_TG3_AUXCTL_ACTL_TX_6DB;
2987 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
2988
Matt Carlson21a00ab2011-01-25 15:58:55 +00002989 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
2990 case ASIC_REV_5717:
2991 case ASIC_REV_57765:
2992 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
2993 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
2994 MII_TG3_DSP_CH34TP2_HIBW01);
2995 /* Fall through */
2996 case ASIC_REV_5719:
2997 val = MII_TG3_DSP_TAP26_ALNOKO |
2998 MII_TG3_DSP_TAP26_RMRXSTO |
2999 MII_TG3_DSP_TAP26_OPCSINPT;
3000 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
3001 }
Matt Carlson52b02d02010-10-14 10:37:41 +00003002
Matt Carlsona6b68da2010-12-06 08:28:52 +00003003 val = 0;
Matt Carlson52b02d02010-10-14 10:37:41 +00003004 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3005 /* Advertise 100-BaseTX EEE ability */
3006 if (tp->link_config.advertising &
Matt Carlson3110f5f52010-12-06 08:28:50 +00003007 ADVERTISED_100baseT_Full)
3008 val |= MDIO_AN_EEE_ADV_100TX;
Matt Carlson52b02d02010-10-14 10:37:41 +00003009 /* Advertise 1000-BaseT EEE ability */
3010 if (tp->link_config.advertising &
Matt Carlson3110f5f52010-12-06 08:28:50 +00003011 ADVERTISED_1000baseT_Full)
3012 val |= MDIO_AN_EEE_ADV_1000T;
Matt Carlson52b02d02010-10-14 10:37:41 +00003013 }
Matt Carlson3110f5f52010-12-06 08:28:50 +00003014 tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlson52b02d02010-10-14 10:37:41 +00003015
3016 /* Turn off SM_DSP clock. */
3017 val = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
3018 MII_TG3_AUXCTL_ACTL_TX_6DB;
3019 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
3020 }
3021
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
3023 tp->link_config.speed != SPEED_INVALID) {
3024 u32 bmcr, orig_bmcr;
3025
3026 tp->link_config.active_speed = tp->link_config.speed;
3027 tp->link_config.active_duplex = tp->link_config.duplex;
3028
3029 bmcr = 0;
3030 switch (tp->link_config.speed) {
3031 default:
3032 case SPEED_10:
3033 break;
3034
3035 case SPEED_100:
3036 bmcr |= BMCR_SPEED100;
3037 break;
3038
3039 case SPEED_1000:
3040 bmcr |= TG3_BMCR_SPEED1000;
3041 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
3044 if (tp->link_config.duplex == DUPLEX_FULL)
3045 bmcr |= BMCR_FULLDPLX;
3046
3047 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3048 (bmcr != orig_bmcr)) {
3049 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3050 for (i = 0; i < 1500; i++) {
3051 u32 tmp;
3052
3053 udelay(10);
3054 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3055 tg3_readphy(tp, MII_BMSR, &tmp))
3056 continue;
3057 if (!(tmp & BMSR_LSTATUS)) {
3058 udelay(40);
3059 break;
3060 }
3061 }
3062 tg3_writephy(tp, MII_BMCR, bmcr);
3063 udelay(40);
3064 }
3065 } else {
3066 tg3_writephy(tp, MII_BMCR,
3067 BMCR_ANENABLE | BMCR_ANRESTART);
3068 }
3069}
3070
3071static int tg3_init_5401phy_dsp(struct tg3 *tp)
3072{
3073 int err;
3074
3075 /* Turn off tap power management. */
3076 /* Set Extended packet length bit */
3077 err = tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
3078
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003079 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3080 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3081 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3082 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3083 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
3085 udelay(40);
3086
3087 return err;
3088}
3089
Michael Chan3600d912006-12-07 00:21:48 -08003090static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091{
Michael Chan3600d912006-12-07 00:21:48 -08003092 u32 adv_reg, all_mask = 0;
3093
3094 if (mask & ADVERTISED_10baseT_Half)
3095 all_mask |= ADVERTISE_10HALF;
3096 if (mask & ADVERTISED_10baseT_Full)
3097 all_mask |= ADVERTISE_10FULL;
3098 if (mask & ADVERTISED_100baseT_Half)
3099 all_mask |= ADVERTISE_100HALF;
3100 if (mask & ADVERTISED_100baseT_Full)
3101 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102
3103 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
3104 return 0;
3105
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 if ((adv_reg & all_mask) != all_mask)
3107 return 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003108 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 u32 tg3_ctrl;
3110
Michael Chan3600d912006-12-07 00:21:48 -08003111 all_mask = 0;
3112 if (mask & ADVERTISED_1000baseT_Half)
3113 all_mask |= ADVERTISE_1000HALF;
3114 if (mask & ADVERTISED_1000baseT_Full)
3115 all_mask |= ADVERTISE_1000FULL;
3116
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 if (tg3_readphy(tp, MII_TG3_CTRL, &tg3_ctrl))
3118 return 0;
3119
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 if ((tg3_ctrl & all_mask) != all_mask)
3121 return 0;
3122 }
3123 return 1;
3124}
3125
Matt Carlsonef167e22007-12-20 20:10:01 -08003126static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
3127{
3128 u32 curadv, reqadv;
3129
3130 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3131 return 1;
3132
3133 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3134 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3135
3136 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3137 if (curadv != reqadv)
3138 return 0;
3139
3140 if (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)
3141 tg3_readphy(tp, MII_LPA, rmtadv);
3142 } else {
3143 /* Reprogram the advertisement register, even if it
3144 * does not affect the current link. If the link
3145 * gets renegotiated in the future, we can save an
3146 * additional renegotiation cycle by advertising
3147 * it correctly in the first place.
3148 */
3149 if (curadv != reqadv) {
3150 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3151 ADVERTISE_PAUSE_ASYM);
3152 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3153 }
3154 }
3155
3156 return 1;
3157}
3158
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3160{
3161 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003162 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08003163 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 u16 current_speed;
3165 u8 current_duplex;
3166 int i, err;
3167
3168 tw32(MAC_EVENT, 0);
3169
3170 tw32_f(MAC_STATUS,
3171 (MAC_STATUS_SYNC_CHANGED |
3172 MAC_STATUS_CFG_CHANGED |
3173 MAC_STATUS_MI_COMPLETION |
3174 MAC_STATUS_LNKSTATE_CHANGED));
3175 udelay(40);
3176
Matt Carlson8ef21422008-05-02 16:47:53 -07003177 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3178 tw32_f(MAC_MI_MODE,
3179 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3180 udelay(80);
3181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
3183 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x02);
3184
3185 /* Some third-party PHYs need to be reset on link going
3186 * down.
3187 */
3188 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3189 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3190 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3191 netif_carrier_ok(tp->dev)) {
3192 tg3_readphy(tp, MII_BMSR, &bmsr);
3193 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3194 !(bmsr & BMSR_LSTATUS))
3195 force_reset = 1;
3196 }
3197 if (force_reset)
3198 tg3_phy_reset(tp);
3199
Matt Carlson79eb6902010-02-17 15:17:03 +00003200 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 tg3_readphy(tp, MII_BMSR, &bmsr);
3202 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
3203 !(tp->tg3_flags & TG3_FLAG_INIT_COMPLETE))
3204 bmsr = 0;
3205
3206 if (!(bmsr & BMSR_LSTATUS)) {
3207 err = tg3_init_5401phy_dsp(tp);
3208 if (err)
3209 return err;
3210
3211 tg3_readphy(tp, MII_BMSR, &bmsr);
3212 for (i = 0; i < 1000; i++) {
3213 udelay(10);
3214 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3215 (bmsr & BMSR_LSTATUS)) {
3216 udelay(40);
3217 break;
3218 }
3219 }
3220
Matt Carlson79eb6902010-02-17 15:17:03 +00003221 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3222 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 !(bmsr & BMSR_LSTATUS) &&
3224 tp->link_config.active_speed == SPEED_1000) {
3225 err = tg3_phy_reset(tp);
3226 if (!err)
3227 err = tg3_init_5401phy_dsp(tp);
3228 if (err)
3229 return err;
3230 }
3231 }
3232 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3233 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3234 /* 5701 {A0,B0} CRC bug workaround */
3235 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00003236 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3237 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
3238 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 }
3240
3241 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003242 tg3_readphy(tp, MII_TG3_ISTAT, &val);
3243 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003245 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003247 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3249
3250 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3251 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3252 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3253 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3254 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3255 else
3256 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3257 }
3258
3259 current_link_up = 0;
3260 current_speed = SPEED_INVALID;
3261 current_duplex = DUPLEX_INVALID;
3262
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003263 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4007);
3265 tg3_readphy(tp, MII_TG3_AUX_CTRL, &val);
3266 if (!(val & (1 << 10))) {
3267 val |= (1 << 10);
3268 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
3269 goto relink;
3270 }
3271 }
3272
3273 bmsr = 0;
3274 for (i = 0; i < 100; i++) {
3275 tg3_readphy(tp, MII_BMSR, &bmsr);
3276 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3277 (bmsr & BMSR_LSTATUS))
3278 break;
3279 udelay(40);
3280 }
3281
3282 if (bmsr & BMSR_LSTATUS) {
3283 u32 aux_stat, bmcr;
3284
3285 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3286 for (i = 0; i < 2000; i++) {
3287 udelay(10);
3288 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3289 aux_stat)
3290 break;
3291 }
3292
3293 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3294 &current_speed,
3295 &current_duplex);
3296
3297 bmcr = 0;
3298 for (i = 0; i < 200; i++) {
3299 tg3_readphy(tp, MII_BMCR, &bmcr);
3300 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3301 continue;
3302 if (bmcr && bmcr != 0x7fff)
3303 break;
3304 udelay(10);
3305 }
3306
Matt Carlsonef167e22007-12-20 20:10:01 -08003307 lcl_adv = 0;
3308 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
Matt Carlsonef167e22007-12-20 20:10:01 -08003310 tp->link_config.active_speed = current_speed;
3311 tp->link_config.active_duplex = current_duplex;
3312
3313 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3314 if ((bmcr & BMCR_ANENABLE) &&
3315 tg3_copper_is_advertising_all(tp,
3316 tp->link_config.advertising)) {
3317 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3318 &rmt_adv))
3319 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 }
3321 } else {
3322 if (!(bmcr & BMCR_ANENABLE) &&
3323 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003324 tp->link_config.duplex == current_duplex &&
3325 tp->link_config.flowctrl ==
3326 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 }
3329 }
3330
Matt Carlsonef167e22007-12-20 20:10:01 -08003331 if (current_link_up == 1 &&
3332 tp->link_config.active_duplex == DUPLEX_FULL)
3333 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 }
3335
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336relink:
Matt Carlson80096062010-08-02 11:26:06 +00003337 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 tg3_phy_copper_begin(tp);
3339
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003340 tg3_readphy(tp, MII_BMSR, &bmsr);
3341 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3342 (bmsr & BMSR_LSTATUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 current_link_up = 1;
3344 }
3345
3346 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3347 if (current_link_up == 1) {
3348 if (tp->link_config.active_speed == SPEED_100 ||
3349 tp->link_config.active_speed == SPEED_10)
3350 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3351 else
3352 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003353 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003354 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3355 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3357
3358 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3359 if (tp->link_config.active_duplex == DUPLEX_HALF)
3360 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3361
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003363 if (current_link_up == 1 &&
3364 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003366 else
3367 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 }
3369
3370 /* ??? Without this setting Netgear GA302T PHY does not
3371 * ??? send/receive packets...
3372 */
Matt Carlson79eb6902010-02-17 15:17:03 +00003373 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3375 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3376 tw32_f(MAC_MI_MODE, tp->mi_mode);
3377 udelay(80);
3378 }
3379
3380 tw32_f(MAC_MODE, tp->mac_mode);
3381 udelay(40);
3382
Matt Carlson52b02d02010-10-14 10:37:41 +00003383 tg3_phy_eee_adjust(tp, current_link_up);
3384
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
3386 /* Polled via timer. */
3387 tw32_f(MAC_EVENT, 0);
3388 } else {
3389 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3390 }
3391 udelay(40);
3392
3393 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3394 current_link_up == 1 &&
3395 tp->link_config.active_speed == SPEED_1000 &&
3396 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) ||
3397 (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED))) {
3398 udelay(120);
3399 tw32_f(MAC_STATUS,
3400 (MAC_STATUS_SYNC_CHANGED |
3401 MAC_STATUS_CFG_CHANGED));
3402 udelay(40);
3403 tg3_write_mem(tp,
3404 NIC_SRAM_FIRMWARE_MBOX,
3405 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3406 }
3407
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003408 /* Prevent send BD corruption. */
3409 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
3410 u16 oldlnkctl, newlnkctl;
3411
3412 pci_read_config_word(tp->pdev,
3413 tp->pcie_cap + PCI_EXP_LNKCTL,
3414 &oldlnkctl);
3415 if (tp->link_config.active_speed == SPEED_100 ||
3416 tp->link_config.active_speed == SPEED_10)
3417 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3418 else
3419 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3420 if (newlnkctl != oldlnkctl)
3421 pci_write_config_word(tp->pdev,
3422 tp->pcie_cap + PCI_EXP_LNKCTL,
3423 newlnkctl);
3424 }
3425
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 if (current_link_up != netif_carrier_ok(tp->dev)) {
3427 if (current_link_up)
3428 netif_carrier_on(tp->dev);
3429 else
3430 netif_carrier_off(tp->dev);
3431 tg3_link_report(tp);
3432 }
3433
3434 return 0;
3435}
3436
3437struct tg3_fiber_aneginfo {
3438 int state;
3439#define ANEG_STATE_UNKNOWN 0
3440#define ANEG_STATE_AN_ENABLE 1
3441#define ANEG_STATE_RESTART_INIT 2
3442#define ANEG_STATE_RESTART 3
3443#define ANEG_STATE_DISABLE_LINK_OK 4
3444#define ANEG_STATE_ABILITY_DETECT_INIT 5
3445#define ANEG_STATE_ABILITY_DETECT 6
3446#define ANEG_STATE_ACK_DETECT_INIT 7
3447#define ANEG_STATE_ACK_DETECT 8
3448#define ANEG_STATE_COMPLETE_ACK_INIT 9
3449#define ANEG_STATE_COMPLETE_ACK 10
3450#define ANEG_STATE_IDLE_DETECT_INIT 11
3451#define ANEG_STATE_IDLE_DETECT 12
3452#define ANEG_STATE_LINK_OK 13
3453#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3454#define ANEG_STATE_NEXT_PAGE_WAIT 15
3455
3456 u32 flags;
3457#define MR_AN_ENABLE 0x00000001
3458#define MR_RESTART_AN 0x00000002
3459#define MR_AN_COMPLETE 0x00000004
3460#define MR_PAGE_RX 0x00000008
3461#define MR_NP_LOADED 0x00000010
3462#define MR_TOGGLE_TX 0x00000020
3463#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3464#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3465#define MR_LP_ADV_SYM_PAUSE 0x00000100
3466#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3467#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3468#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3469#define MR_LP_ADV_NEXT_PAGE 0x00001000
3470#define MR_TOGGLE_RX 0x00002000
3471#define MR_NP_RX 0x00004000
3472
3473#define MR_LINK_OK 0x80000000
3474
3475 unsigned long link_time, cur_time;
3476
3477 u32 ability_match_cfg;
3478 int ability_match_count;
3479
3480 char ability_match, idle_match, ack_match;
3481
3482 u32 txconfig, rxconfig;
3483#define ANEG_CFG_NP 0x00000080
3484#define ANEG_CFG_ACK 0x00000040
3485#define ANEG_CFG_RF2 0x00000020
3486#define ANEG_CFG_RF1 0x00000010
3487#define ANEG_CFG_PS2 0x00000001
3488#define ANEG_CFG_PS1 0x00008000
3489#define ANEG_CFG_HD 0x00004000
3490#define ANEG_CFG_FD 0x00002000
3491#define ANEG_CFG_INVAL 0x00001f06
3492
3493};
3494#define ANEG_OK 0
3495#define ANEG_DONE 1
3496#define ANEG_TIMER_ENAB 2
3497#define ANEG_FAILED -1
3498
3499#define ANEG_STATE_SETTLE_TIME 10000
3500
3501static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3502 struct tg3_fiber_aneginfo *ap)
3503{
Matt Carlson5be73b42007-12-20 20:09:29 -08003504 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 unsigned long delta;
3506 u32 rx_cfg_reg;
3507 int ret;
3508
3509 if (ap->state == ANEG_STATE_UNKNOWN) {
3510 ap->rxconfig = 0;
3511 ap->link_time = 0;
3512 ap->cur_time = 0;
3513 ap->ability_match_cfg = 0;
3514 ap->ability_match_count = 0;
3515 ap->ability_match = 0;
3516 ap->idle_match = 0;
3517 ap->ack_match = 0;
3518 }
3519 ap->cur_time++;
3520
3521 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3522 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3523
3524 if (rx_cfg_reg != ap->ability_match_cfg) {
3525 ap->ability_match_cfg = rx_cfg_reg;
3526 ap->ability_match = 0;
3527 ap->ability_match_count = 0;
3528 } else {
3529 if (++ap->ability_match_count > 1) {
3530 ap->ability_match = 1;
3531 ap->ability_match_cfg = rx_cfg_reg;
3532 }
3533 }
3534 if (rx_cfg_reg & ANEG_CFG_ACK)
3535 ap->ack_match = 1;
3536 else
3537 ap->ack_match = 0;
3538
3539 ap->idle_match = 0;
3540 } else {
3541 ap->idle_match = 1;
3542 ap->ability_match_cfg = 0;
3543 ap->ability_match_count = 0;
3544 ap->ability_match = 0;
3545 ap->ack_match = 0;
3546
3547 rx_cfg_reg = 0;
3548 }
3549
3550 ap->rxconfig = rx_cfg_reg;
3551 ret = ANEG_OK;
3552
Matt Carlson33f401a2010-04-05 10:19:27 +00003553 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 case ANEG_STATE_UNKNOWN:
3555 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3556 ap->state = ANEG_STATE_AN_ENABLE;
3557
3558 /* fallthru */
3559 case ANEG_STATE_AN_ENABLE:
3560 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3561 if (ap->flags & MR_AN_ENABLE) {
3562 ap->link_time = 0;
3563 ap->cur_time = 0;
3564 ap->ability_match_cfg = 0;
3565 ap->ability_match_count = 0;
3566 ap->ability_match = 0;
3567 ap->idle_match = 0;
3568 ap->ack_match = 0;
3569
3570 ap->state = ANEG_STATE_RESTART_INIT;
3571 } else {
3572 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3573 }
3574 break;
3575
3576 case ANEG_STATE_RESTART_INIT:
3577 ap->link_time = ap->cur_time;
3578 ap->flags &= ~(MR_NP_LOADED);
3579 ap->txconfig = 0;
3580 tw32(MAC_TX_AUTO_NEG, 0);
3581 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3582 tw32_f(MAC_MODE, tp->mac_mode);
3583 udelay(40);
3584
3585 ret = ANEG_TIMER_ENAB;
3586 ap->state = ANEG_STATE_RESTART;
3587
3588 /* fallthru */
3589 case ANEG_STATE_RESTART:
3590 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00003591 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00003593 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 break;
3596
3597 case ANEG_STATE_DISABLE_LINK_OK:
3598 ret = ANEG_DONE;
3599 break;
3600
3601 case ANEG_STATE_ABILITY_DETECT_INIT:
3602 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003603 ap->txconfig = ANEG_CFG_FD;
3604 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3605 if (flowctrl & ADVERTISE_1000XPAUSE)
3606 ap->txconfig |= ANEG_CFG_PS1;
3607 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3608 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3610 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3611 tw32_f(MAC_MODE, tp->mac_mode);
3612 udelay(40);
3613
3614 ap->state = ANEG_STATE_ABILITY_DETECT;
3615 break;
3616
3617 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00003618 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 break;
3621
3622 case ANEG_STATE_ACK_DETECT_INIT:
3623 ap->txconfig |= ANEG_CFG_ACK;
3624 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3625 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3626 tw32_f(MAC_MODE, tp->mac_mode);
3627 udelay(40);
3628
3629 ap->state = ANEG_STATE_ACK_DETECT;
3630
3631 /* fallthru */
3632 case ANEG_STATE_ACK_DETECT:
3633 if (ap->ack_match != 0) {
3634 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3635 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3636 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3637 } else {
3638 ap->state = ANEG_STATE_AN_ENABLE;
3639 }
3640 } else if (ap->ability_match != 0 &&
3641 ap->rxconfig == 0) {
3642 ap->state = ANEG_STATE_AN_ENABLE;
3643 }
3644 break;
3645
3646 case ANEG_STATE_COMPLETE_ACK_INIT:
3647 if (ap->rxconfig & ANEG_CFG_INVAL) {
3648 ret = ANEG_FAILED;
3649 break;
3650 }
3651 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3652 MR_LP_ADV_HALF_DUPLEX |
3653 MR_LP_ADV_SYM_PAUSE |
3654 MR_LP_ADV_ASYM_PAUSE |
3655 MR_LP_ADV_REMOTE_FAULT1 |
3656 MR_LP_ADV_REMOTE_FAULT2 |
3657 MR_LP_ADV_NEXT_PAGE |
3658 MR_TOGGLE_RX |
3659 MR_NP_RX);
3660 if (ap->rxconfig & ANEG_CFG_FD)
3661 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3662 if (ap->rxconfig & ANEG_CFG_HD)
3663 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3664 if (ap->rxconfig & ANEG_CFG_PS1)
3665 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3666 if (ap->rxconfig & ANEG_CFG_PS2)
3667 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3668 if (ap->rxconfig & ANEG_CFG_RF1)
3669 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3670 if (ap->rxconfig & ANEG_CFG_RF2)
3671 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3672 if (ap->rxconfig & ANEG_CFG_NP)
3673 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3674
3675 ap->link_time = ap->cur_time;
3676
3677 ap->flags ^= (MR_TOGGLE_TX);
3678 if (ap->rxconfig & 0x0008)
3679 ap->flags |= MR_TOGGLE_RX;
3680 if (ap->rxconfig & ANEG_CFG_NP)
3681 ap->flags |= MR_NP_RX;
3682 ap->flags |= MR_PAGE_RX;
3683
3684 ap->state = ANEG_STATE_COMPLETE_ACK;
3685 ret = ANEG_TIMER_ENAB;
3686 break;
3687
3688 case ANEG_STATE_COMPLETE_ACK:
3689 if (ap->ability_match != 0 &&
3690 ap->rxconfig == 0) {
3691 ap->state = ANEG_STATE_AN_ENABLE;
3692 break;
3693 }
3694 delta = ap->cur_time - ap->link_time;
3695 if (delta > ANEG_STATE_SETTLE_TIME) {
3696 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3697 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3698 } else {
3699 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3700 !(ap->flags & MR_NP_RX)) {
3701 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3702 } else {
3703 ret = ANEG_FAILED;
3704 }
3705 }
3706 }
3707 break;
3708
3709 case ANEG_STATE_IDLE_DETECT_INIT:
3710 ap->link_time = ap->cur_time;
3711 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3712 tw32_f(MAC_MODE, tp->mac_mode);
3713 udelay(40);
3714
3715 ap->state = ANEG_STATE_IDLE_DETECT;
3716 ret = ANEG_TIMER_ENAB;
3717 break;
3718
3719 case ANEG_STATE_IDLE_DETECT:
3720 if (ap->ability_match != 0 &&
3721 ap->rxconfig == 0) {
3722 ap->state = ANEG_STATE_AN_ENABLE;
3723 break;
3724 }
3725 delta = ap->cur_time - ap->link_time;
3726 if (delta > ANEG_STATE_SETTLE_TIME) {
3727 /* XXX another gem from the Broadcom driver :( */
3728 ap->state = ANEG_STATE_LINK_OK;
3729 }
3730 break;
3731
3732 case ANEG_STATE_LINK_OK:
3733 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3734 ret = ANEG_DONE;
3735 break;
3736
3737 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3738 /* ??? unimplemented */
3739 break;
3740
3741 case ANEG_STATE_NEXT_PAGE_WAIT:
3742 /* ??? unimplemented */
3743 break;
3744
3745 default:
3746 ret = ANEG_FAILED;
3747 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749
3750 return ret;
3751}
3752
Matt Carlson5be73b42007-12-20 20:09:29 -08003753static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754{
3755 int res = 0;
3756 struct tg3_fiber_aneginfo aninfo;
3757 int status = ANEG_FAILED;
3758 unsigned int tick;
3759 u32 tmp;
3760
3761 tw32_f(MAC_TX_AUTO_NEG, 0);
3762
3763 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3764 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3765 udelay(40);
3766
3767 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3768 udelay(40);
3769
3770 memset(&aninfo, 0, sizeof(aninfo));
3771 aninfo.flags |= MR_AN_ENABLE;
3772 aninfo.state = ANEG_STATE_UNKNOWN;
3773 aninfo.cur_time = 0;
3774 tick = 0;
3775 while (++tick < 195000) {
3776 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3777 if (status == ANEG_DONE || status == ANEG_FAILED)
3778 break;
3779
3780 udelay(1);
3781 }
3782
3783 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3784 tw32_f(MAC_MODE, tp->mac_mode);
3785 udelay(40);
3786
Matt Carlson5be73b42007-12-20 20:09:29 -08003787 *txflags = aninfo.txconfig;
3788 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789
3790 if (status == ANEG_DONE &&
3791 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3792 MR_LP_ADV_FULL_DUPLEX)))
3793 res = 1;
3794
3795 return res;
3796}
3797
3798static void tg3_init_bcm8002(struct tg3 *tp)
3799{
3800 u32 mac_status = tr32(MAC_STATUS);
3801 int i;
3802
3803 /* Reset when initting first time or we have a link. */
3804 if ((tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) &&
3805 !(mac_status & MAC_STATUS_PCS_SYNCED))
3806 return;
3807
3808 /* Set PLL lock range. */
3809 tg3_writephy(tp, 0x16, 0x8007);
3810
3811 /* SW reset */
3812 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
3813
3814 /* Wait for reset to complete. */
3815 /* XXX schedule_timeout() ... */
3816 for (i = 0; i < 500; i++)
3817 udelay(10);
3818
3819 /* Config mode; select PMA/Ch 1 regs. */
3820 tg3_writephy(tp, 0x10, 0x8411);
3821
3822 /* Enable auto-lock and comdet, select txclk for tx. */
3823 tg3_writephy(tp, 0x11, 0x0a10);
3824
3825 tg3_writephy(tp, 0x18, 0x00a0);
3826 tg3_writephy(tp, 0x16, 0x41ff);
3827
3828 /* Assert and deassert POR. */
3829 tg3_writephy(tp, 0x13, 0x0400);
3830 udelay(40);
3831 tg3_writephy(tp, 0x13, 0x0000);
3832
3833 tg3_writephy(tp, 0x11, 0x0a50);
3834 udelay(40);
3835 tg3_writephy(tp, 0x11, 0x0a10);
3836
3837 /* Wait for signal to stabilize */
3838 /* XXX schedule_timeout() ... */
3839 for (i = 0; i < 15000; i++)
3840 udelay(10);
3841
3842 /* Deselect the channel register so we can read the PHYID
3843 * later.
3844 */
3845 tg3_writephy(tp, 0x10, 0x8011);
3846}
3847
3848static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
3849{
Matt Carlson82cd3d12007-12-20 20:09:00 -08003850 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 u32 sg_dig_ctrl, sg_dig_status;
3852 u32 serdes_cfg, expected_sg_dig_ctrl;
3853 int workaround, port_a;
3854 int current_link_up;
3855
3856 serdes_cfg = 0;
3857 expected_sg_dig_ctrl = 0;
3858 workaround = 0;
3859 port_a = 1;
3860 current_link_up = 0;
3861
3862 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
3863 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
3864 workaround = 1;
3865 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
3866 port_a = 0;
3867
3868 /* preserve bits 0-11,13,14 for signal pre-emphasis */
3869 /* preserve bits 20-23 for voltage regulator */
3870 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
3871 }
3872
3873 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3874
3875 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003876 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 if (workaround) {
3878 u32 val = serdes_cfg;
3879
3880 if (port_a)
3881 val |= 0xc010000;
3882 else
3883 val |= 0x4010000;
3884 tw32_f(MAC_SERDES_CFG, val);
3885 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003886
3887 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 }
3889 if (mac_status & MAC_STATUS_PCS_SYNCED) {
3890 tg3_setup_flow_control(tp, 0, 0);
3891 current_link_up = 1;
3892 }
3893 goto out;
3894 }
3895
3896 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003897 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898
Matt Carlson82cd3d12007-12-20 20:09:00 -08003899 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3900 if (flowctrl & ADVERTISE_1000XPAUSE)
3901 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
3902 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3903 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904
3905 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003906 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07003907 tp->serdes_counter &&
3908 ((mac_status & (MAC_STATUS_PCS_SYNCED |
3909 MAC_STATUS_RCVD_CFG)) ==
3910 MAC_STATUS_PCS_SYNCED)) {
3911 tp->serdes_counter--;
3912 current_link_up = 1;
3913 goto out;
3914 }
3915restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 if (workaround)
3917 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003918 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 udelay(5);
3920 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
3921
Michael Chan3d3ebe72006-09-27 15:59:15 -07003922 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003923 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
3925 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003926 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 mac_status = tr32(MAC_STATUS);
3928
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003929 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08003931 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932
Matt Carlson82cd3d12007-12-20 20:09:00 -08003933 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
3934 local_adv |= ADVERTISE_1000XPAUSE;
3935 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
3936 local_adv |= ADVERTISE_1000XPSE_ASYM;
3937
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003938 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003939 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003940 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003941 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942
3943 tg3_setup_flow_control(tp, local_adv, remote_adv);
3944 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003945 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003946 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003947 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003948 if (tp->serdes_counter)
3949 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 else {
3951 if (workaround) {
3952 u32 val = serdes_cfg;
3953
3954 if (port_a)
3955 val |= 0xc010000;
3956 else
3957 val |= 0x4010000;
3958
3959 tw32_f(MAC_SERDES_CFG, val);
3960 }
3961
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003962 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 udelay(40);
3964
3965 /* Link parallel detection - link is up */
3966 /* only if we have PCS_SYNC and not */
3967 /* receiving config code words */
3968 mac_status = tr32(MAC_STATUS);
3969 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
3970 !(mac_status & MAC_STATUS_RCVD_CFG)) {
3971 tg3_setup_flow_control(tp, 0, 0);
3972 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003973 tp->phy_flags |=
3974 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003975 tp->serdes_counter =
3976 SERDES_PARALLEL_DET_TIMEOUT;
3977 } else
3978 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 }
3980 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07003981 } else {
3982 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003983 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 }
3985
3986out:
3987 return current_link_up;
3988}
3989
3990static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
3991{
3992 int current_link_up = 0;
3993
Michael Chan5cf64b8a2007-05-05 12:11:21 -07003994 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996
3997 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08003998 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004000
Matt Carlson5be73b42007-12-20 20:09:29 -08004001 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4002 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003
Matt Carlson5be73b42007-12-20 20:09:29 -08004004 if (txflags & ANEG_CFG_PS1)
4005 local_adv |= ADVERTISE_1000XPAUSE;
4006 if (txflags & ANEG_CFG_PS2)
4007 local_adv |= ADVERTISE_1000XPSE_ASYM;
4008
4009 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4010 remote_adv |= LPA_1000XPAUSE;
4011 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4012 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013
4014 tg3_setup_flow_control(tp, local_adv, remote_adv);
4015
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 current_link_up = 1;
4017 }
4018 for (i = 0; i < 30; i++) {
4019 udelay(20);
4020 tw32_f(MAC_STATUS,
4021 (MAC_STATUS_SYNC_CHANGED |
4022 MAC_STATUS_CFG_CHANGED));
4023 udelay(40);
4024 if ((tr32(MAC_STATUS) &
4025 (MAC_STATUS_SYNC_CHANGED |
4026 MAC_STATUS_CFG_CHANGED)) == 0)
4027 break;
4028 }
4029
4030 mac_status = tr32(MAC_STATUS);
4031 if (current_link_up == 0 &&
4032 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4033 !(mac_status & MAC_STATUS_RCVD_CFG))
4034 current_link_up = 1;
4035 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004036 tg3_setup_flow_control(tp, 0, 0);
4037
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 /* Forcing 1000FD link up. */
4039 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040
4041 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4042 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004043
4044 tw32_f(MAC_MODE, tp->mac_mode);
4045 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 }
4047
4048out:
4049 return current_link_up;
4050}
4051
4052static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4053{
4054 u32 orig_pause_cfg;
4055 u16 orig_active_speed;
4056 u8 orig_active_duplex;
4057 u32 mac_status;
4058 int current_link_up;
4059 int i;
4060
Matt Carlson8d018622007-12-20 20:05:44 -08004061 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 orig_active_speed = tp->link_config.active_speed;
4063 orig_active_duplex = tp->link_config.active_duplex;
4064
4065 if (!(tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG) &&
4066 netif_carrier_ok(tp->dev) &&
4067 (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)) {
4068 mac_status = tr32(MAC_STATUS);
4069 mac_status &= (MAC_STATUS_PCS_SYNCED |
4070 MAC_STATUS_SIGNAL_DET |
4071 MAC_STATUS_CFG_CHANGED |
4072 MAC_STATUS_RCVD_CFG);
4073 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4074 MAC_STATUS_SIGNAL_DET)) {
4075 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4076 MAC_STATUS_CFG_CHANGED));
4077 return 0;
4078 }
4079 }
4080
4081 tw32_f(MAC_TX_AUTO_NEG, 0);
4082
4083 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4084 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
4085 tw32_f(MAC_MODE, tp->mac_mode);
4086 udelay(40);
4087
Matt Carlson79eb6902010-02-17 15:17:03 +00004088 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 tg3_init_bcm8002(tp);
4090
4091 /* Enable link change event even when serdes polling. */
4092 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4093 udelay(40);
4094
4095 current_link_up = 0;
4096 mac_status = tr32(MAC_STATUS);
4097
4098 if (tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG)
4099 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4100 else
4101 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4102
Matt Carlson898a56f2009-08-28 14:02:40 +00004103 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00004105 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106
4107 for (i = 0; i < 100; i++) {
4108 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4109 MAC_STATUS_CFG_CHANGED));
4110 udelay(5);
4111 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07004112 MAC_STATUS_CFG_CHANGED |
4113 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 break;
4115 }
4116
4117 mac_status = tr32(MAC_STATUS);
4118 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4119 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004120 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4121 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 tw32_f(MAC_MODE, (tp->mac_mode |
4123 MAC_MODE_SEND_CONFIGS));
4124 udelay(1);
4125 tw32_f(MAC_MODE, tp->mac_mode);
4126 }
4127 }
4128
4129 if (current_link_up == 1) {
4130 tp->link_config.active_speed = SPEED_1000;
4131 tp->link_config.active_duplex = DUPLEX_FULL;
4132 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4133 LED_CTRL_LNKLED_OVERRIDE |
4134 LED_CTRL_1000MBPS_ON));
4135 } else {
4136 tp->link_config.active_speed = SPEED_INVALID;
4137 tp->link_config.active_duplex = DUPLEX_INVALID;
4138 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4139 LED_CTRL_LNKLED_OVERRIDE |
4140 LED_CTRL_TRAFFIC_OVERRIDE));
4141 }
4142
4143 if (current_link_up != netif_carrier_ok(tp->dev)) {
4144 if (current_link_up)
4145 netif_carrier_on(tp->dev);
4146 else
4147 netif_carrier_off(tp->dev);
4148 tg3_link_report(tp);
4149 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004150 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 if (orig_pause_cfg != now_pause_cfg ||
4152 orig_active_speed != tp->link_config.active_speed ||
4153 orig_active_duplex != tp->link_config.active_duplex)
4154 tg3_link_report(tp);
4155 }
4156
4157 return 0;
4158}
4159
Michael Chan747e8f82005-07-25 12:33:22 -07004160static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4161{
4162 int current_link_up, err = 0;
4163 u32 bmsr, bmcr;
4164 u16 current_speed;
4165 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004166 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004167
4168 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4169 tw32_f(MAC_MODE, tp->mac_mode);
4170 udelay(40);
4171
4172 tw32(MAC_EVENT, 0);
4173
4174 tw32_f(MAC_STATUS,
4175 (MAC_STATUS_SYNC_CHANGED |
4176 MAC_STATUS_CFG_CHANGED |
4177 MAC_STATUS_MI_COMPLETION |
4178 MAC_STATUS_LNKSTATE_CHANGED));
4179 udelay(40);
4180
4181 if (force_reset)
4182 tg3_phy_reset(tp);
4183
4184 current_link_up = 0;
4185 current_speed = SPEED_INVALID;
4186 current_duplex = DUPLEX_INVALID;
4187
4188 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4189 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004190 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4191 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4192 bmsr |= BMSR_LSTATUS;
4193 else
4194 bmsr &= ~BMSR_LSTATUS;
4195 }
Michael Chan747e8f82005-07-25 12:33:22 -07004196
4197 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4198
4199 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004200 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004201 /* do nothing, just check for link up at the end */
4202 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4203 u32 adv, new_adv;
4204
4205 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4206 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4207 ADVERTISE_1000XPAUSE |
4208 ADVERTISE_1000XPSE_ASYM |
4209 ADVERTISE_SLCT);
4210
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004211 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004212
4213 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4214 new_adv |= ADVERTISE_1000XHALF;
4215 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4216 new_adv |= ADVERTISE_1000XFULL;
4217
4218 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4219 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4220 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4221 tg3_writephy(tp, MII_BMCR, bmcr);
4222
4223 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004224 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004225 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004226
4227 return err;
4228 }
4229 } else {
4230 u32 new_bmcr;
4231
4232 bmcr &= ~BMCR_SPEED1000;
4233 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4234
4235 if (tp->link_config.duplex == DUPLEX_FULL)
4236 new_bmcr |= BMCR_FULLDPLX;
4237
4238 if (new_bmcr != bmcr) {
4239 /* BMCR_SPEED1000 is a reserved bit that needs
4240 * to be set on write.
4241 */
4242 new_bmcr |= BMCR_SPEED1000;
4243
4244 /* Force a linkdown */
4245 if (netif_carrier_ok(tp->dev)) {
4246 u32 adv;
4247
4248 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4249 adv &= ~(ADVERTISE_1000XFULL |
4250 ADVERTISE_1000XHALF |
4251 ADVERTISE_SLCT);
4252 tg3_writephy(tp, MII_ADVERTISE, adv);
4253 tg3_writephy(tp, MII_BMCR, bmcr |
4254 BMCR_ANRESTART |
4255 BMCR_ANENABLE);
4256 udelay(10);
4257 netif_carrier_off(tp->dev);
4258 }
4259 tg3_writephy(tp, MII_BMCR, new_bmcr);
4260 bmcr = new_bmcr;
4261 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4262 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004263 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4264 ASIC_REV_5714) {
4265 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4266 bmsr |= BMSR_LSTATUS;
4267 else
4268 bmsr &= ~BMSR_LSTATUS;
4269 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004270 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004271 }
4272 }
4273
4274 if (bmsr & BMSR_LSTATUS) {
4275 current_speed = SPEED_1000;
4276 current_link_up = 1;
4277 if (bmcr & BMCR_FULLDPLX)
4278 current_duplex = DUPLEX_FULL;
4279 else
4280 current_duplex = DUPLEX_HALF;
4281
Matt Carlsonef167e22007-12-20 20:10:01 -08004282 local_adv = 0;
4283 remote_adv = 0;
4284
Michael Chan747e8f82005-07-25 12:33:22 -07004285 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004286 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004287
4288 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4289 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4290 common = local_adv & remote_adv;
4291 if (common & (ADVERTISE_1000XHALF |
4292 ADVERTISE_1000XFULL)) {
4293 if (common & ADVERTISE_1000XFULL)
4294 current_duplex = DUPLEX_FULL;
4295 else
4296 current_duplex = DUPLEX_HALF;
Matt Carlson57d8b882010-06-05 17:24:35 +00004297 } else if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
4298 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00004299 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07004300 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00004301 }
Michael Chan747e8f82005-07-25 12:33:22 -07004302 }
4303 }
4304
Matt Carlsonef167e22007-12-20 20:10:01 -08004305 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4306 tg3_setup_flow_control(tp, local_adv, remote_adv);
4307
Michael Chan747e8f82005-07-25 12:33:22 -07004308 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4309 if (tp->link_config.active_duplex == DUPLEX_HALF)
4310 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4311
4312 tw32_f(MAC_MODE, tp->mac_mode);
4313 udelay(40);
4314
4315 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4316
4317 tp->link_config.active_speed = current_speed;
4318 tp->link_config.active_duplex = current_duplex;
4319
4320 if (current_link_up != netif_carrier_ok(tp->dev)) {
4321 if (current_link_up)
4322 netif_carrier_on(tp->dev);
4323 else {
4324 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004325 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004326 }
4327 tg3_link_report(tp);
4328 }
4329 return err;
4330}
4331
4332static void tg3_serdes_parallel_detect(struct tg3 *tp)
4333{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004334 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004335 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004336 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004337 return;
4338 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004339
Michael Chan747e8f82005-07-25 12:33:22 -07004340 if (!netif_carrier_ok(tp->dev) &&
4341 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4342 u32 bmcr;
4343
4344 tg3_readphy(tp, MII_BMCR, &bmcr);
4345 if (bmcr & BMCR_ANENABLE) {
4346 u32 phy1, phy2;
4347
4348 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004349 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
4350 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07004351
4352 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004353 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4354 MII_TG3_DSP_EXP1_INT_STAT);
4355 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
4356 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004357
4358 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4359 /* We have signal detect and not receiving
4360 * config code words, link is up by parallel
4361 * detection.
4362 */
4363
4364 bmcr &= ~BMCR_ANENABLE;
4365 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4366 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004367 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004368 }
4369 }
Matt Carlson859a588792010-04-05 10:19:28 +00004370 } else if (netif_carrier_ok(tp->dev) &&
4371 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004372 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004373 u32 phy2;
4374
4375 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004376 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4377 MII_TG3_DSP_EXP1_INT_STAT);
4378 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004379 if (phy2 & 0x20) {
4380 u32 bmcr;
4381
4382 /* Config code words received, turn on autoneg. */
4383 tg3_readphy(tp, MII_BMCR, &bmcr);
4384 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4385
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004386 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004387
4388 }
4389 }
4390}
4391
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4393{
Matt Carlsonf2096f92011-04-05 14:22:48 +00004394 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 int err;
4396
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004397 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004399 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07004400 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00004401 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403
Matt Carlsonbcb37f62008-11-03 16:52:09 -08004404 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00004405 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08004406
4407 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4408 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4409 scale = 65;
4410 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4411 scale = 6;
4412 else
4413 scale = 12;
4414
4415 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4416 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4417 tw32(GRC_MISC_CFG, val);
4418 }
4419
Matt Carlsonf2096f92011-04-05 14:22:48 +00004420 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4421 (6 << TX_LENGTHS_IPG_SHIFT);
4422 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
4423 val |= tr32(MAC_TX_LENGTHS) &
4424 (TX_LENGTHS_JMB_FRM_LEN_MSK |
4425 TX_LENGTHS_CNT_DWN_VAL_MSK);
4426
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 if (tp->link_config.active_speed == SPEED_1000 &&
4428 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00004429 tw32(MAC_TX_LENGTHS, val |
4430 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00004432 tw32(MAC_TX_LENGTHS, val |
4433 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434
4435 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
4436 if (netif_carrier_ok(tp->dev)) {
4437 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07004438 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 } else {
4440 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4441 }
4442 }
4443
Matt Carlson8ed5d972007-05-07 00:25:49 -07004444 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00004445 val = tr32(PCIE_PWR_MGMT_THRESH);
Matt Carlson8ed5d972007-05-07 00:25:49 -07004446 if (!netif_carrier_ok(tp->dev))
4447 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4448 tp->pwrmgmt_thresh;
4449 else
4450 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4451 tw32(PCIE_PWR_MGMT_THRESH, val);
4452 }
4453
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 return err;
4455}
4456
Matt Carlson66cfd1b2010-09-30 10:34:30 +00004457static inline int tg3_irq_sync(struct tg3 *tp)
4458{
4459 return tp->irq_sync;
4460}
4461
Matt Carlson97bd8e42011-04-13 11:05:04 +00004462static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
4463{
4464 int i;
4465
4466 dst = (u32 *)((u8 *)dst + off);
4467 for (i = 0; i < len; i += sizeof(u32))
4468 *dst++ = tr32(off + i);
4469}
4470
4471static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
4472{
4473 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
4474 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
4475 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
4476 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
4477 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
4478 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
4479 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
4480 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
4481 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
4482 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
4483 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
4484 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
4485 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
4486 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
4487 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
4488 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
4489 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
4490 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
4491 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
4492
4493 if (tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX)
4494 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
4495
4496 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
4497 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
4498 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
4499 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
4500 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
4501 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
4502 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
4503 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
4504
4505 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
4506 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
4507 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
4508 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
4509 }
4510
4511 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
4512 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
4513 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
4514 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
4515 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
4516
4517 if (tp->tg3_flags & TG3_FLAG_NVRAM)
4518 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
4519}
4520
4521static void tg3_dump_state(struct tg3 *tp)
4522{
4523 int i;
4524 u32 *regs;
4525
4526 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
4527 if (!regs) {
4528 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
4529 return;
4530 }
4531
4532 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
4533 /* Read up to but not including private PCI registers */
4534 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
4535 regs[i / sizeof(u32)] = tr32(i);
4536 } else
4537 tg3_dump_legacy_regs(tp, regs);
4538
4539 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
4540 if (!regs[i + 0] && !regs[i + 1] &&
4541 !regs[i + 2] && !regs[i + 3])
4542 continue;
4543
4544 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
4545 i * 4,
4546 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
4547 }
4548
4549 kfree(regs);
4550
4551 for (i = 0; i < tp->irq_cnt; i++) {
4552 struct tg3_napi *tnapi = &tp->napi[i];
4553
4554 /* SW status block */
4555 netdev_err(tp->dev,
4556 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
4557 i,
4558 tnapi->hw_status->status,
4559 tnapi->hw_status->status_tag,
4560 tnapi->hw_status->rx_jumbo_consumer,
4561 tnapi->hw_status->rx_consumer,
4562 tnapi->hw_status->rx_mini_consumer,
4563 tnapi->hw_status->idx[0].rx_producer,
4564 tnapi->hw_status->idx[0].tx_consumer);
4565
4566 netdev_err(tp->dev,
4567 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
4568 i,
4569 tnapi->last_tag, tnapi->last_irq_tag,
4570 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
4571 tnapi->rx_rcb_ptr,
4572 tnapi->prodring.rx_std_prod_idx,
4573 tnapi->prodring.rx_std_cons_idx,
4574 tnapi->prodring.rx_jmb_prod_idx,
4575 tnapi->prodring.rx_jmb_cons_idx);
4576 }
4577}
4578
Michael Chandf3e6542006-05-26 17:48:07 -07004579/* This is called whenever we suspect that the system chipset is re-
4580 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4581 * is bogus tx completions. We try to recover by setting the
4582 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4583 * in the workqueue.
4584 */
4585static void tg3_tx_recover(struct tg3 *tp)
4586{
4587 BUG_ON((tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) ||
4588 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4589
Matt Carlson5129c3a2010-04-05 10:19:23 +00004590 netdev_warn(tp->dev,
4591 "The system may be re-ordering memory-mapped I/O "
4592 "cycles to the network device, attempting to recover. "
4593 "Please report the problem to the driver maintainer "
4594 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07004595
4596 spin_lock(&tp->lock);
Michael Chandf3e6542006-05-26 17:48:07 -07004597 tp->tg3_flags |= TG3_FLAG_TX_RECOVERY_PENDING;
Michael Chandf3e6542006-05-26 17:48:07 -07004598 spin_unlock(&tp->lock);
4599}
4600
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004601static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07004602{
Matt Carlsonf65aac12010-08-02 11:26:03 +00004603 /* Tell compiler to fetch tx indices from memory. */
4604 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004605 return tnapi->tx_pending -
4606 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07004607}
4608
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609/* Tigon3 never reports partial packet sends. So we do not
4610 * need special logic to handle SKBs that have not had all
4611 * of their frags sent yet, like SunGEM does.
4612 */
Matt Carlson17375d22009-08-28 14:02:18 +00004613static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614{
Matt Carlson17375d22009-08-28 14:02:18 +00004615 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004616 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004617 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004618 struct netdev_queue *txq;
4619 int index = tnapi - tp->napi;
4620
Matt Carlson19cfaec2009-12-03 08:36:20 +00004621 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004622 index--;
4623
4624 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625
4626 while (sw_idx != hw_idx) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00004627 struct ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004629 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630
Michael Chandf3e6542006-05-26 17:48:07 -07004631 if (unlikely(skb == NULL)) {
4632 tg3_tx_recover(tp);
4633 return;
4634 }
4635
Alexander Duyckf4188d82009-12-02 16:48:38 +00004636 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004637 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004638 skb_headlen(skb),
4639 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640
4641 ri->skb = NULL;
4642
4643 sw_idx = NEXT_TX(sw_idx);
4644
4645 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004646 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004647 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4648 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00004649
4650 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004651 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004652 skb_shinfo(skb)->frags[i].size,
4653 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 sw_idx = NEXT_TX(sw_idx);
4655 }
4656
David S. Millerf47c11e2005-06-24 20:18:35 -07004657 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004658
4659 if (unlikely(tx_bug)) {
4660 tg3_tx_recover(tp);
4661 return;
4662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 }
4664
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004665 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666
Michael Chan1b2a7202006-08-07 21:46:02 -07004667 /* Need to make the tx_cons update visible to tg3_start_xmit()
4668 * before checking for netif_queue_stopped(). Without the
4669 * memory barrier, there is a small possibility that tg3_start_xmit()
4670 * will miss it and cause the queue to be stopped forever.
4671 */
4672 smp_mb();
4673
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004674 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004675 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004676 __netif_tx_lock(txq, smp_processor_id());
4677 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004678 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004679 netif_tx_wake_queue(txq);
4680 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07004681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682}
4683
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004684static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
4685{
4686 if (!ri->skb)
4687 return;
4688
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004689 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004690 map_sz, PCI_DMA_FROMDEVICE);
4691 dev_kfree_skb_any(ri->skb);
4692 ri->skb = NULL;
4693}
4694
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695/* Returns size of skb allocated or < 0 on error.
4696 *
4697 * We only need to fill in the address because the other members
4698 * of the RX descriptor are invariant, see tg3_init_rings.
4699 *
4700 * Note the purposeful assymetry of cpu vs. chip accesses. For
4701 * posting buffers we only dirty the first cache line of the RX
4702 * descriptor (containing the address). Whereas for the RX status
4703 * buffers the cpu only reads the last cacheline of the RX descriptor
4704 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4705 */
Matt Carlson86b21e52009-11-13 13:03:45 +00004706static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00004707 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708{
4709 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00004710 struct ring_info *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 struct sk_buff *skb;
4712 dma_addr_t mapping;
4713 int skb_size, dest_idx;
4714
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 switch (opaque_key) {
4716 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004717 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00004718 desc = &tpr->rx_std[dest_idx];
4719 map = &tpr->rx_std_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004720 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 break;
4722
4723 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004724 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004725 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004726 map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004727 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 break;
4729
4730 default:
4731 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733
4734 /* Do not overwrite any of the map or rp information
4735 * until we are sure we can commit to a new buffer.
4736 *
4737 * Callers depend upon this behavior and assume that
4738 * we leave everything unchanged if we fail.
4739 */
Matt Carlson287be122009-08-28 13:58:46 +00004740 skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 if (skb == NULL)
4742 return -ENOMEM;
4743
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 skb_reserve(skb, tp->rx_offset);
4745
Matt Carlson287be122009-08-28 13:58:46 +00004746 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00004748 if (pci_dma_mapping_error(tp->pdev, mapping)) {
4749 dev_kfree_skb(skb);
4750 return -EIO;
4751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752
4753 map->skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004754 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 desc->addr_hi = ((u64)mapping >> 32);
4757 desc->addr_lo = ((u64)mapping & 0xffffffff);
4758
4759 return skb_size;
4760}
4761
4762/* We only need to move over in the address because the other
4763 * members of the RX descriptor are invariant. See notes above
4764 * tg3_alloc_rx_skb for full details.
4765 */
Matt Carlsona3896162009-11-13 13:03:44 +00004766static void tg3_recycle_rx(struct tg3_napi *tnapi,
4767 struct tg3_rx_prodring_set *dpr,
4768 u32 opaque_key, int src_idx,
4769 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770{
Matt Carlson17375d22009-08-28 14:02:18 +00004771 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4773 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004774 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004775 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776
4777 switch (opaque_key) {
4778 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004779 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004780 dest_desc = &dpr->rx_std[dest_idx];
4781 dest_map = &dpr->rx_std_buffers[dest_idx];
4782 src_desc = &spr->rx_std[src_idx];
4783 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 break;
4785
4786 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004787 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004788 dest_desc = &dpr->rx_jmb[dest_idx].std;
4789 dest_map = &dpr->rx_jmb_buffers[dest_idx];
4790 src_desc = &spr->rx_jmb[src_idx].std;
4791 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 break;
4793
4794 default:
4795 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797
4798 dest_map->skb = src_map->skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004799 dma_unmap_addr_set(dest_map, mapping,
4800 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 dest_desc->addr_hi = src_desc->addr_hi;
4802 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00004803
4804 /* Ensure that the update to the skb happens after the physical
4805 * addresses have been transferred to the new BD location.
4806 */
4807 smp_wmb();
4808
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 src_map->skb = NULL;
4810}
4811
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812/* The RX ring scheme is composed of multiple rings which post fresh
4813 * buffers to the chip, and one special ring the chip uses to report
4814 * status back to the host.
4815 *
4816 * The special ring reports the status of received packets to the
4817 * host. The chip does not write into the original descriptor the
4818 * RX buffer was obtained from. The chip simply takes the original
4819 * descriptor as provided by the host, updates the status and length
4820 * field, then writes this into the next status ring entry.
4821 *
4822 * Each ring the host uses to post buffers to the chip is described
4823 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
4824 * it is first placed into the on-chip ram. When the packet's length
4825 * is known, it walks down the TG3_BDINFO entries to select the ring.
4826 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
4827 * which is within the range of the new packet's length is chosen.
4828 *
4829 * The "separate ring for rx status" scheme may sound queer, but it makes
4830 * sense from a cache coherency perspective. If only the host writes
4831 * to the buffer post rings, and only the chip writes to the rx status
4832 * rings, then cache lines never move beyond shared-modified state.
4833 * If both the host and chip were to write into the same ring, cache line
4834 * eviction could occur since both entities want it in an exclusive state.
4835 */
Matt Carlson17375d22009-08-28 14:02:18 +00004836static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837{
Matt Carlson17375d22009-08-28 14:02:18 +00004838 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07004839 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004840 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00004841 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07004842 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004844 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004846 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 /*
4848 * We need to order the read of hw_idx and the read of
4849 * the opaque cookie.
4850 */
4851 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 work_mask = 0;
4853 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004854 std_prod_idx = tpr->rx_std_prod_idx;
4855 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00004857 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00004858 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 unsigned int len;
4860 struct sk_buff *skb;
4861 dma_addr_t dma_addr;
4862 u32 opaque_key, desc_idx, *post_ptr;
4863
4864 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
4865 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
4866 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004867 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004868 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004869 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004870 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07004871 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004873 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004874 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004875 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004876 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00004877 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879
4880 work_mask |= opaque_key;
4881
4882 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
4883 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
4884 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00004885 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 desc_idx, *post_ptr);
4887 drop_it_no_recycle:
4888 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00004889 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 goto next_pkt;
4891 }
4892
Matt Carlsonad829262008-11-21 17:16:16 -08004893 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
4894 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895
Matt Carlsond2757fc2010-04-12 06:58:27 +00004896 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 int skb_size;
4898
Matt Carlson86b21e52009-11-13 13:03:45 +00004899 skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00004900 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 if (skb_size < 0)
4902 goto drop_it;
4903
Matt Carlson287be122009-08-28 13:58:46 +00004904 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 PCI_DMA_FROMDEVICE);
4906
Matt Carlson61e800c2010-02-17 15:16:54 +00004907 /* Ensure that the update to the skb happens
4908 * after the usage of the old DMA mapping.
4909 */
4910 smp_wmb();
4911
4912 ri->skb = NULL;
4913
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 skb_put(skb, len);
4915 } else {
4916 struct sk_buff *copy_skb;
4917
Matt Carlsona3896162009-11-13 13:03:44 +00004918 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 desc_idx, *post_ptr);
4920
Matt Carlsonbf933c82011-01-25 15:58:49 +00004921 copy_skb = netdev_alloc_skb(tp->dev, len +
Matt Carlson9dc7a112010-04-12 06:58:28 +00004922 TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 if (copy_skb == NULL)
4924 goto drop_it_no_recycle;
4925
Matt Carlsonbf933c82011-01-25 15:58:49 +00004926 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 skb_put(copy_skb, len);
4928 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03004929 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
4931
4932 /* We'll reuse the original ring buffer. */
4933 skb = copy_skb;
4934 }
4935
Michał Mirosławdc668912011-04-07 03:35:07 +00004936 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
4938 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
4939 >> RXD_TCPCSUM_SHIFT) == 0xffff))
4940 skb->ip_summed = CHECKSUM_UNNECESSARY;
4941 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07004942 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943
4944 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004945
4946 if (len > (tp->dev->mtu + ETH_HLEN) &&
4947 skb->protocol != htons(ETH_P_8021Q)) {
4948 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00004949 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004950 }
4951
Matt Carlson9dc7a112010-04-12 06:58:28 +00004952 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00004953 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
4954 __vlan_hwaccel_put_tag(skb,
4955 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00004956
Matt Carlsonbf933c82011-01-25 15:58:49 +00004957 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 received++;
4960 budget--;
4961
4962next_pkt:
4963 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07004964
4965 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00004966 tpr->rx_std_prod_idx = std_prod_idx &
4967 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00004968 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4969 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07004970 work_mask &= ~RXD_OPAQUE_RING_STD;
4971 rx_std_posted = 0;
4972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07004974 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00004975 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07004976
4977 /* Refresh hw_idx to see if there is new work */
4978 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004979 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07004980 rmb();
4981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 }
4983
4984 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00004985 tnapi->rx_rcb_ptr = sw_idx;
4986 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987
4988 /* Refill RX ring(s). */
Matt Carlsone4af1af2010-02-12 14:47:05 +00004989 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004990 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00004991 tpr->rx_std_prod_idx = std_prod_idx &
4992 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004993 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4994 tpr->rx_std_prod_idx);
4995 }
4996 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00004997 tpr->rx_jmb_prod_idx = jmb_prod_idx &
4998 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004999 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5000 tpr->rx_jmb_prod_idx);
5001 }
5002 mmiowb();
5003 } else if (work_mask) {
5004 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
5005 * updated before the producer indices can be updated.
5006 */
5007 smp_wmb();
5008
Matt Carlson2c49a442010-09-30 10:34:35 +00005009 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
5010 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005011
Matt Carlsone4af1af2010-02-12 14:47:05 +00005012 if (tnapi != &tp->napi[1])
5013 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015
5016 return received;
5017}
5018
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005019static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 /* handle link change and other phy events */
5022 if (!(tp->tg3_flags &
5023 (TG3_FLAG_USE_LINKCHG_REG |
5024 TG3_FLAG_POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005025 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
5026
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 if (sblk->status & SD_STATUS_LINK_CHG) {
5028 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005029 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07005030 spin_lock(&tp->lock);
Matt Carlsondd477002008-05-25 23:45:58 -07005031 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
5032 tw32_f(MAC_STATUS,
5033 (MAC_STATUS_SYNC_CHANGED |
5034 MAC_STATUS_CFG_CHANGED |
5035 MAC_STATUS_MI_COMPLETION |
5036 MAC_STATUS_LNKSTATE_CHANGED));
5037 udelay(40);
5038 } else
5039 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07005040 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 }
5042 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005043}
5044
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005045static int tg3_rx_prodring_xfer(struct tg3 *tp,
5046 struct tg3_rx_prodring_set *dpr,
5047 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005048{
5049 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005050 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005051
5052 while (1) {
5053 src_prod_idx = spr->rx_std_prod_idx;
5054
5055 /* Make sure updates to the rx_std_buffers[] entries and the
5056 * standard producer index are seen in the correct order.
5057 */
5058 smp_rmb();
5059
5060 if (spr->rx_std_cons_idx == src_prod_idx)
5061 break;
5062
5063 if (spr->rx_std_cons_idx < src_prod_idx)
5064 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
5065 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005066 cpycnt = tp->rx_std_ring_mask + 1 -
5067 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005068
Matt Carlson2c49a442010-09-30 10:34:35 +00005069 cpycnt = min(cpycnt,
5070 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005071
5072 si = spr->rx_std_cons_idx;
5073 di = dpr->rx_std_prod_idx;
5074
Matt Carlsone92967b2010-02-12 14:47:06 +00005075 for (i = di; i < di + cpycnt; i++) {
5076 if (dpr->rx_std_buffers[i].skb) {
5077 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005078 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005079 break;
5080 }
5081 }
5082
5083 if (!cpycnt)
5084 break;
5085
5086 /* Ensure that updates to the rx_std_buffers ring and the
5087 * shadowed hardware producer ring from tg3_recycle_skb() are
5088 * ordered correctly WRT the skb check above.
5089 */
5090 smp_rmb();
5091
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005092 memcpy(&dpr->rx_std_buffers[di],
5093 &spr->rx_std_buffers[si],
5094 cpycnt * sizeof(struct ring_info));
5095
5096 for (i = 0; i < cpycnt; i++, di++, si++) {
5097 struct tg3_rx_buffer_desc *sbd, *dbd;
5098 sbd = &spr->rx_std[si];
5099 dbd = &dpr->rx_std[di];
5100 dbd->addr_hi = sbd->addr_hi;
5101 dbd->addr_lo = sbd->addr_lo;
5102 }
5103
Matt Carlson2c49a442010-09-30 10:34:35 +00005104 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
5105 tp->rx_std_ring_mask;
5106 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
5107 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005108 }
5109
5110 while (1) {
5111 src_prod_idx = spr->rx_jmb_prod_idx;
5112
5113 /* Make sure updates to the rx_jmb_buffers[] entries and
5114 * the jumbo producer index are seen in the correct order.
5115 */
5116 smp_rmb();
5117
5118 if (spr->rx_jmb_cons_idx == src_prod_idx)
5119 break;
5120
5121 if (spr->rx_jmb_cons_idx < src_prod_idx)
5122 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
5123 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005124 cpycnt = tp->rx_jmb_ring_mask + 1 -
5125 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005126
5127 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00005128 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005129
5130 si = spr->rx_jmb_cons_idx;
5131 di = dpr->rx_jmb_prod_idx;
5132
Matt Carlsone92967b2010-02-12 14:47:06 +00005133 for (i = di; i < di + cpycnt; i++) {
5134 if (dpr->rx_jmb_buffers[i].skb) {
5135 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005136 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005137 break;
5138 }
5139 }
5140
5141 if (!cpycnt)
5142 break;
5143
5144 /* Ensure that updates to the rx_jmb_buffers ring and the
5145 * shadowed hardware producer ring from tg3_recycle_skb() are
5146 * ordered correctly WRT the skb check above.
5147 */
5148 smp_rmb();
5149
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005150 memcpy(&dpr->rx_jmb_buffers[di],
5151 &spr->rx_jmb_buffers[si],
5152 cpycnt * sizeof(struct ring_info));
5153
5154 for (i = 0; i < cpycnt; i++, di++, si++) {
5155 struct tg3_rx_buffer_desc *sbd, *dbd;
5156 sbd = &spr->rx_jmb[si].std;
5157 dbd = &dpr->rx_jmb[di].std;
5158 dbd->addr_hi = sbd->addr_hi;
5159 dbd->addr_lo = sbd->addr_lo;
5160 }
5161
Matt Carlson2c49a442010-09-30 10:34:35 +00005162 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
5163 tp->rx_jmb_ring_mask;
5164 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
5165 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005166 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005167
5168 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005169}
5170
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005171static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
5172{
5173 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174
5175 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005176 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00005177 tg3_tx(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005178 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
Michael Chan4fd7ab52007-10-12 01:39:50 -07005179 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 }
5181
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 /* run RX thread, within the bounds set by NAPI.
5183 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005184 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005186 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00005187 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005189 if ((tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005190 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005191 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00005192 u32 std_prod_idx = dpr->rx_std_prod_idx;
5193 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005194
Matt Carlsone4af1af2010-02-12 14:47:05 +00005195 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005196 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00005197 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005198
5199 wmb();
5200
Matt Carlsone4af1af2010-02-12 14:47:05 +00005201 if (std_prod_idx != dpr->rx_std_prod_idx)
5202 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5203 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005204
Matt Carlsone4af1af2010-02-12 14:47:05 +00005205 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5206 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5207 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005208
5209 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005210
5211 if (err)
5212 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005213 }
5214
David S. Miller6f535762007-10-11 18:08:29 -07005215 return work_done;
5216}
David S. Millerf7383c22005-05-18 22:50:53 -07005217
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005218static int tg3_poll_msix(struct napi_struct *napi, int budget)
5219{
5220 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5221 struct tg3 *tp = tnapi->tp;
5222 int work_done = 0;
5223 struct tg3_hw_status *sblk = tnapi->hw_status;
5224
5225 while (1) {
5226 work_done = tg3_poll_work(tnapi, work_done, budget);
5227
5228 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5229 goto tx_recovery;
5230
5231 if (unlikely(work_done >= budget))
5232 break;
5233
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005234 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005235 * to tell the hw how much work has been processed,
5236 * so we must read it before checking for more work.
5237 */
5238 tnapi->last_tag = sblk->status_tag;
5239 tnapi->last_irq_tag = tnapi->last_tag;
5240 rmb();
5241
5242 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005243 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5244 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005245 napi_complete(napi);
5246 /* Reenable interrupts. */
5247 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5248 mmiowb();
5249 break;
5250 }
5251 }
5252
5253 return work_done;
5254
5255tx_recovery:
5256 /* work_done is guaranteed to be less than budget. */
5257 napi_complete(napi);
5258 schedule_work(&tp->reset_task);
5259 return work_done;
5260}
5261
David S. Miller6f535762007-10-11 18:08:29 -07005262static int tg3_poll(struct napi_struct *napi, int budget)
5263{
Matt Carlson8ef04422009-08-28 14:01:37 +00005264 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5265 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07005266 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00005267 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07005268
5269 while (1) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005270 tg3_poll_link(tp);
5271
Matt Carlson17375d22009-08-28 14:02:18 +00005272 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07005273
5274 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5275 goto tx_recovery;
5276
5277 if (unlikely(work_done >= budget))
5278 break;
5279
Michael Chan4fd7ab52007-10-12 01:39:50 -07005280 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
Matt Carlson17375d22009-08-28 14:02:18 +00005281 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07005282 * to tell the hw how much work has been processed,
5283 * so we must read it before checking for more work.
5284 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005285 tnapi->last_tag = sblk->status_tag;
5286 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07005287 rmb();
5288 } else
5289 sblk->status &= ~SD_STATUS_UPDATED;
5290
Matt Carlson17375d22009-08-28 14:02:18 +00005291 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005292 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00005293 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005294 break;
5295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 }
5297
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005298 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07005299
5300tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07005301 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08005302 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07005303 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07005304 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305}
5306
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005307static void tg3_napi_disable(struct tg3 *tp)
5308{
5309 int i;
5310
5311 for (i = tp->irq_cnt - 1; i >= 0; i--)
5312 napi_disable(&tp->napi[i].napi);
5313}
5314
5315static void tg3_napi_enable(struct tg3 *tp)
5316{
5317 int i;
5318
5319 for (i = 0; i < tp->irq_cnt; i++)
5320 napi_enable(&tp->napi[i].napi);
5321}
5322
5323static void tg3_napi_init(struct tg3 *tp)
5324{
5325 int i;
5326
5327 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
5328 for (i = 1; i < tp->irq_cnt; i++)
5329 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
5330}
5331
5332static void tg3_napi_fini(struct tg3 *tp)
5333{
5334 int i;
5335
5336 for (i = 0; i < tp->irq_cnt; i++)
5337 netif_napi_del(&tp->napi[i].napi);
5338}
5339
5340static inline void tg3_netif_stop(struct tg3 *tp)
5341{
5342 tp->dev->trans_start = jiffies; /* prevent tx timeout */
5343 tg3_napi_disable(tp);
5344 netif_tx_disable(tp->dev);
5345}
5346
5347static inline void tg3_netif_start(struct tg3 *tp)
5348{
5349 /* NOTE: unconditional netif_tx_wake_all_queues is only
5350 * appropriate so long as all callers are assured to
5351 * have free tx slots (such as after tg3_init_hw)
5352 */
5353 netif_tx_wake_all_queues(tp->dev);
5354
5355 tg3_napi_enable(tp);
5356 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
5357 tg3_enable_ints(tp);
5358}
5359
David S. Millerf47c11e2005-06-24 20:18:35 -07005360static void tg3_irq_quiesce(struct tg3 *tp)
5361{
Matt Carlson4f125f42009-09-01 12:55:02 +00005362 int i;
5363
David S. Millerf47c11e2005-06-24 20:18:35 -07005364 BUG_ON(tp->irq_sync);
5365
5366 tp->irq_sync = 1;
5367 smp_mb();
5368
Matt Carlson4f125f42009-09-01 12:55:02 +00005369 for (i = 0; i < tp->irq_cnt; i++)
5370 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07005371}
5372
David S. Millerf47c11e2005-06-24 20:18:35 -07005373/* Fully shutdown all tg3 driver activity elsewhere in the system.
5374 * If irq_sync is non-zero, then the IRQ handler must be synchronized
5375 * with as well. Most of the time, this is not necessary except when
5376 * shutting down the device.
5377 */
5378static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
5379{
Michael Chan46966542007-07-11 19:47:19 -07005380 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07005381 if (irq_sync)
5382 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005383}
5384
5385static inline void tg3_full_unlock(struct tg3 *tp)
5386{
David S. Millerf47c11e2005-06-24 20:18:35 -07005387 spin_unlock_bh(&tp->lock);
5388}
5389
Michael Chanfcfa0a32006-03-20 22:28:41 -08005390/* One-shot MSI handler - Chip automatically disables interrupt
5391 * after sending MSI so driver doesn't have to do it.
5392 */
David Howells7d12e782006-10-05 14:55:46 +01005393static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08005394{
Matt Carlson09943a12009-08-28 14:01:57 +00005395 struct tg3_napi *tnapi = dev_id;
5396 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08005397
Matt Carlson898a56f2009-08-28 14:02:40 +00005398 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005399 if (tnapi->rx_rcb)
5400 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005401
5402 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005403 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005404
5405 return IRQ_HANDLED;
5406}
5407
Michael Chan88b06bc22005-04-21 17:13:25 -07005408/* MSI ISR - No need to check for interrupt sharing and no need to
5409 * flush status block and interrupt mailbox. PCI ordering rules
5410 * guarantee that MSI will arrive after the status block.
5411 */
David Howells7d12e782006-10-05 14:55:46 +01005412static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07005413{
Matt Carlson09943a12009-08-28 14:01:57 +00005414 struct tg3_napi *tnapi = dev_id;
5415 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07005416
Matt Carlson898a56f2009-08-28 14:02:40 +00005417 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005418 if (tnapi->rx_rcb)
5419 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07005420 /*
David S. Millerfac9b832005-05-18 22:46:34 -07005421 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07005422 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07005423 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07005424 * NIC to stop sending us irqs, engaging "in-intr-handler"
5425 * event coalescing.
5426 */
5427 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07005428 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005429 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07005430
Michael Chan88b06bc22005-04-21 17:13:25 -07005431 return IRQ_RETVAL(1);
5432}
5433
David Howells7d12e782006-10-05 14:55:46 +01005434static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435{
Matt Carlson09943a12009-08-28 14:01:57 +00005436 struct tg3_napi *tnapi = dev_id;
5437 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005438 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 unsigned int handled = 1;
5440
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 /* In INTx mode, it is possible for the interrupt to arrive at
5442 * the CPU before the status block posted prior to the interrupt.
5443 * Reading the PCI State register will confirm whether the
5444 * interrupt is ours and will flush the status block.
5445 */
Michael Chand18edcb2007-03-24 20:57:11 -07005446 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
5447 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5448 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5449 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005450 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07005451 }
Michael Chand18edcb2007-03-24 20:57:11 -07005452 }
5453
5454 /*
5455 * Writing any value to intr-mbox-0 clears PCI INTA# and
5456 * chip-internal interrupt pending events.
5457 * Writing non-zero to intr-mbox-0 additional tells the
5458 * NIC to stop sending us irqs, engaging "in-intr-handler"
5459 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005460 *
5461 * Flush the mailbox to de-assert the IRQ immediately to prevent
5462 * spurious interrupts. The flush impacts performance but
5463 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005464 */
Michael Chanc04cb342007-05-07 00:26:15 -07005465 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07005466 if (tg3_irq_sync(tp))
5467 goto out;
5468 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00005469 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00005470 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00005471 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07005472 } else {
5473 /* No work, shared interrupt perhaps? re-enable
5474 * interrupts, and flush that PCI write
5475 */
5476 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
5477 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07005478 }
David S. Millerf47c11e2005-06-24 20:18:35 -07005479out:
David S. Millerfac9b832005-05-18 22:46:34 -07005480 return IRQ_RETVAL(handled);
5481}
5482
David Howells7d12e782006-10-05 14:55:46 +01005483static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07005484{
Matt Carlson09943a12009-08-28 14:01:57 +00005485 struct tg3_napi *tnapi = dev_id;
5486 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005487 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07005488 unsigned int handled = 1;
5489
David S. Millerfac9b832005-05-18 22:46:34 -07005490 /* In INTx mode, it is possible for the interrupt to arrive at
5491 * the CPU before the status block posted prior to the interrupt.
5492 * Reading the PCI State register will confirm whether the
5493 * interrupt is ours and will flush the status block.
5494 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005495 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Michael Chand18edcb2007-03-24 20:57:11 -07005496 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5497 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5498 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005499 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 }
Michael Chand18edcb2007-03-24 20:57:11 -07005501 }
5502
5503 /*
5504 * writing any value to intr-mbox-0 clears PCI INTA# and
5505 * chip-internal interrupt pending events.
5506 * writing non-zero to intr-mbox-0 additional tells the
5507 * NIC to stop sending us irqs, engaging "in-intr-handler"
5508 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005509 *
5510 * Flush the mailbox to de-assert the IRQ immediately to prevent
5511 * spurious interrupts. The flush impacts performance but
5512 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005513 */
Michael Chanc04cb342007-05-07 00:26:15 -07005514 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00005515
5516 /*
5517 * In a shared interrupt configuration, sometimes other devices'
5518 * interrupts will scream. We record the current status tag here
5519 * so that the above check can report that the screaming interrupts
5520 * are unhandled. Eventually they will be silenced.
5521 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005522 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00005523
Michael Chand18edcb2007-03-24 20:57:11 -07005524 if (tg3_irq_sync(tp))
5525 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00005526
Matt Carlson72334482009-08-28 14:03:01 +00005527 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00005528
Matt Carlson09943a12009-08-28 14:01:57 +00005529 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00005530
David S. Millerf47c11e2005-06-24 20:18:35 -07005531out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 return IRQ_RETVAL(handled);
5533}
5534
Michael Chan79381092005-04-21 17:13:59 -07005535/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01005536static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07005537{
Matt Carlson09943a12009-08-28 14:01:57 +00005538 struct tg3_napi *tnapi = dev_id;
5539 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005540 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07005541
Michael Chanf9804dd2005-09-27 12:13:10 -07005542 if ((sblk->status & SD_STATUS_UPDATED) ||
5543 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07005544 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07005545 return IRQ_RETVAL(1);
5546 }
5547 return IRQ_RETVAL(0);
5548}
5549
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07005550static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07005551static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552
Michael Chanb9ec6c12006-07-25 16:37:27 -07005553/* Restart hardware after configuration changes, self-test, etc.
5554 * Invoked with tp->lock held.
5555 */
5556static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07005557 __releases(tp->lock)
5558 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005559{
5560 int err;
5561
5562 err = tg3_init_hw(tp, reset_phy);
5563 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00005564 netdev_err(tp->dev,
5565 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07005566 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
5567 tg3_full_unlock(tp);
5568 del_timer_sync(&tp->timer);
5569 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00005570 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005571 dev_close(tp->dev);
5572 tg3_full_lock(tp, 0);
5573 }
5574 return err;
5575}
5576
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577#ifdef CONFIG_NET_POLL_CONTROLLER
5578static void tg3_poll_controller(struct net_device *dev)
5579{
Matt Carlson4f125f42009-09-01 12:55:02 +00005580 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07005581 struct tg3 *tp = netdev_priv(dev);
5582
Matt Carlson4f125f42009-09-01 12:55:02 +00005583 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00005584 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585}
5586#endif
5587
David Howellsc4028952006-11-22 14:57:56 +00005588static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589{
David Howellsc4028952006-11-22 14:57:56 +00005590 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005591 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 unsigned int restart_timer;
5593
Michael Chan7faa0062006-02-02 17:29:28 -08005594 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08005595
5596 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08005597 tg3_full_unlock(tp);
5598 return;
5599 }
5600
5601 tg3_full_unlock(tp);
5602
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005603 tg3_phy_stop(tp);
5604
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 tg3_netif_stop(tp);
5606
David S. Millerf47c11e2005-06-24 20:18:35 -07005607 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608
5609 restart_timer = tp->tg3_flags2 & TG3_FLG2_RESTART_TIMER;
5610 tp->tg3_flags2 &= ~TG3_FLG2_RESTART_TIMER;
5611
Michael Chandf3e6542006-05-26 17:48:07 -07005612 if (tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING) {
5613 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5614 tp->write32_rx_mbox = tg3_write_flush_reg32;
5615 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
5616 tp->tg3_flags &= ~TG3_FLAG_TX_RECOVERY_PENDING;
5617 }
5618
Michael Chan944d9802005-05-29 14:57:48 -07005619 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005620 err = tg3_init_hw(tp, 1);
5621 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005622 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623
5624 tg3_netif_start(tp);
5625
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 if (restart_timer)
5627 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08005628
Michael Chanb9ec6c12006-07-25 16:37:27 -07005629out:
Michael Chan7faa0062006-02-02 17:29:28 -08005630 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005631
5632 if (!err)
5633 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634}
5635
5636static void tg3_tx_timeout(struct net_device *dev)
5637{
5638 struct tg3 *tp = netdev_priv(dev);
5639
Michael Chanb0408752007-02-13 12:18:30 -08005640 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00005641 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00005642 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08005643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644
5645 schedule_work(&tp->reset_task);
5646}
5647
Michael Chanc58ec932005-09-17 00:46:27 -07005648/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5649static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5650{
5651 u32 base = (u32) mapping & 0xffffffff;
5652
Eric Dumazet807540b2010-09-23 05:40:09 +00005653 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07005654}
5655
Michael Chan72f2afb2006-03-06 19:28:35 -08005656/* Test for DMA addresses > 40-bit */
5657static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5658 int len)
5659{
5660#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Michael Chan6728a8e2006-03-27 23:16:49 -08005661 if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG)
Eric Dumazet807540b2010-09-23 05:40:09 +00005662 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08005663 return 0;
5664#else
5665 return 0;
5666#endif
5667}
5668
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005669static void tg3_set_txd(struct tg3_napi *, int, dma_addr_t, int, u32, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670
Michael Chan72f2afb2006-03-06 19:28:35 -08005671/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005672static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
5673 struct sk_buff *skb, u32 last_plus_one,
5674 u32 *start, u32 base_flags, u32 mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675{
Matt Carlson24f4efd2009-11-13 13:03:35 +00005676 struct tg3 *tp = tnapi->tp;
Matt Carlson41588ba2008-04-19 18:12:33 -07005677 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07005678 dma_addr_t new_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 u32 entry = *start;
Michael Chanc58ec932005-09-17 00:46:27 -07005680 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681
Matt Carlson41588ba2008-04-19 18:12:33 -07005682 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
5683 new_skb = skb_copy(skb, GFP_ATOMIC);
5684 else {
5685 int more_headroom = 4 - ((unsigned long)skb->data & 3);
5686
5687 new_skb = skb_copy_expand(skb,
5688 skb_headroom(skb) + more_headroom,
5689 skb_tailroom(skb), GFP_ATOMIC);
5690 }
5691
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07005693 ret = -1;
5694 } else {
5695 /* New SKB is guaranteed to be linear. */
5696 entry = *start;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005697 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
5698 PCI_DMA_TODEVICE);
5699 /* Make sure the mapping succeeded */
5700 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
5701 ret = -1;
5702 dev_kfree_skb(new_skb);
5703 new_skb = NULL;
David S. Miller90079ce2008-09-11 04:52:51 -07005704
Michael Chanc58ec932005-09-17 00:46:27 -07005705 /* Make sure new skb does not cross any 4G boundaries.
5706 * Drop the packet if it does.
5707 */
Alexander Duyckf4188d82009-12-02 16:48:38 +00005708 } else if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5709 tg3_4g_overflow_test(new_addr, new_skb->len)) {
5710 pci_unmap_single(tp->pdev, new_addr, new_skb->len,
5711 PCI_DMA_TODEVICE);
Michael Chanc58ec932005-09-17 00:46:27 -07005712 ret = -1;
5713 dev_kfree_skb(new_skb);
5714 new_skb = NULL;
5715 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005716 tg3_set_txd(tnapi, entry, new_addr, new_skb->len,
Michael Chanc58ec932005-09-17 00:46:27 -07005717 base_flags, 1 | (mss << 1));
5718 *start = NEXT_TX(entry);
5719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720 }
5721
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722 /* Now clean up the sw ring entries. */
5723 i = 0;
5724 while (entry != last_plus_one) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00005725 int len;
5726
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005727 if (i == 0)
Alexander Duyckf4188d82009-12-02 16:48:38 +00005728 len = skb_headlen(skb);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005729 else
Alexander Duyckf4188d82009-12-02 16:48:38 +00005730 len = skb_shinfo(skb)->frags[i-1].size;
5731
5732 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005733 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00005734 mapping),
5735 len, PCI_DMA_TODEVICE);
5736 if (i == 0) {
5737 tnapi->tx_buffers[entry].skb = new_skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005738 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00005739 new_addr);
5740 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005741 tnapi->tx_buffers[entry].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 entry = NEXT_TX(entry);
5744 i++;
5745 }
5746
5747 dev_kfree_skb(skb);
5748
Michael Chanc58ec932005-09-17 00:46:27 -07005749 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750}
5751
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005752static void tg3_set_txd(struct tg3_napi *tnapi, int entry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753 dma_addr_t mapping, int len, u32 flags,
5754 u32 mss_and_is_end)
5755{
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005756 struct tg3_tx_buffer_desc *txd = &tnapi->tx_ring[entry];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 int is_end = (mss_and_is_end & 0x1);
5758 u32 mss = (mss_and_is_end >> 1);
5759 u32 vlan_tag = 0;
5760
5761 if (is_end)
5762 flags |= TXD_FLAG_END;
5763 if (flags & TXD_FLAG_VLAN) {
5764 vlan_tag = flags >> 16;
5765 flags &= 0xffff;
5766 }
5767 vlan_tag |= (mss << TXD_MSS_SHIFT);
5768
5769 txd->addr_hi = ((u64) mapping >> 32);
5770 txd->addr_lo = ((u64) mapping & 0xffffffff);
5771 txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
5772 txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
5773}
5774
Michael Chan5a6f3072006-03-20 22:28:05 -08005775/* hard_start_xmit for devices that don't have any bugs and
Matt Carlsone849cdc2009-11-13 13:03:38 +00005776 * support TG3_FLG2_HW_TSO_2 and TG3_FLG2_HW_TSO_3 only.
Michael Chan5a6f3072006-03-20 22:28:05 -08005777 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005778static netdev_tx_t tg3_start_xmit(struct sk_buff *skb,
5779 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780{
5781 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 u32 len, entry, base_flags, mss;
David S. Miller90079ce2008-09-11 04:52:51 -07005783 dma_addr_t mapping;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005784 struct tg3_napi *tnapi;
5785 struct netdev_queue *txq;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005786 unsigned int i, last;
5787
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005788 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5789 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Matt Carlson19cfaec2009-12-03 08:36:20 +00005790 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005791 tnapi++;
Michael Chan5a6f3072006-03-20 22:28:05 -08005792
Michael Chan00b70502006-06-17 21:58:45 -07005793 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005794 * and TX reclaim runs via tp->napi.poll inside of a software
Michael Chan5a6f3072006-03-20 22:28:05 -08005795 * interrupt. Furthermore, IRQ processing runs lockless so we have
5796 * no IRQ context deadlocks to worry about either. Rejoice!
5797 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005798 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005799 if (!netif_tx_queue_stopped(txq)) {
5800 netif_tx_stop_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005801
5802 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00005803 netdev_err(dev,
5804 "BUG! Tx Ring full when queue awake!\n");
Michael Chan5a6f3072006-03-20 22:28:05 -08005805 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005806 return NETDEV_TX_BUSY;
5807 }
5808
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005809 entry = tnapi->tx_prod;
Michael Chan5a6f3072006-03-20 22:28:05 -08005810 base_flags = 0;
Matt Carlsonbe98da62010-07-11 09:31:46 +00005811 mss = skb_shinfo(skb)->gso_size;
5812 if (mss) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005813 int tcp_opt_len, ip_tcp_len;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005814 u32 hdrlen;
Michael Chan5a6f3072006-03-20 22:28:05 -08005815
5816 if (skb_header_cloned(skb) &&
5817 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5818 dev_kfree_skb(skb);
5819 goto out_unlock;
5820 }
5821
Matt Carlson02e96082010-09-15 08:59:59 +00005822 if (skb_is_gso_v6(skb)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005823 hdrlen = skb_headlen(skb) - ETH_HLEN;
Matt Carlson02e96082010-09-15 08:59:59 +00005824 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005825 struct iphdr *iph = ip_hdr(skb);
5826
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005827 tcp_opt_len = tcp_optlen(skb);
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03005828 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
Michael Chanb0026622006-07-03 19:42:14 -07005829
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005830 iph->check = 0;
5831 iph->tot_len = htons(mss + ip_tcp_len + tcp_opt_len);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005832 hdrlen = ip_tcp_len + tcp_opt_len;
Michael Chanb0026622006-07-03 19:42:14 -07005833 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005834
Matt Carlsone849cdc2009-11-13 13:03:38 +00005835 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005836 mss |= (hdrlen & 0xc) << 12;
5837 if (hdrlen & 0x10)
5838 base_flags |= 0x00000010;
5839 base_flags |= (hdrlen & 0x3e0) << 5;
5840 } else
5841 mss |= hdrlen << 9;
5842
Michael Chan5a6f3072006-03-20 22:28:05 -08005843 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5844 TXD_FLAG_CPU_POST_DMA);
5845
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005846 tcp_hdr(skb)->check = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08005847
Matt Carlson859a588792010-04-05 10:19:28 +00005848 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005849 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson859a588792010-04-05 10:19:28 +00005850 }
5851
Jesse Grosseab6d182010-10-20 13:56:03 +00005852 if (vlan_tx_tag_present(skb))
Michael Chan5a6f3072006-03-20 22:28:05 -08005853 base_flags |= (TXD_FLAG_VLAN |
5854 (vlan_tx_tag_get(skb) << 16));
Michael Chan5a6f3072006-03-20 22:28:05 -08005855
Alexander Duyckf4188d82009-12-02 16:48:38 +00005856 len = skb_headlen(skb);
5857
5858 /* Queue skb data, a.k.a. the main skb fragment. */
5859 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
5860 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07005861 dev_kfree_skb(skb);
5862 goto out_unlock;
5863 }
5864
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005865 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005866 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005867
Matt Carlsonb703df62009-12-03 08:36:21 +00005868 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
Matt Carlson8fc2f992010-12-06 08:28:49 +00005869 !mss && skb->len > VLAN_ETH_FRAME_LEN)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005870 base_flags |= TXD_FLAG_JMB_PKT;
5871
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005872 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Michael Chan5a6f3072006-03-20 22:28:05 -08005873 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5874
5875 entry = NEXT_TX(entry);
5876
5877 /* Now loop through additional data fragments, and queue them. */
5878 if (skb_shinfo(skb)->nr_frags > 0) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005879 last = skb_shinfo(skb)->nr_frags - 1;
5880 for (i = 0; i <= last; i++) {
5881 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5882
5883 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005884 mapping = pci_map_page(tp->pdev,
5885 frag->page,
5886 frag->page_offset,
5887 len, PCI_DMA_TODEVICE);
5888 if (pci_dma_mapping_error(tp->pdev, mapping))
5889 goto dma_error;
5890
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005891 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005892 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00005893 mapping);
Michael Chan5a6f3072006-03-20 22:28:05 -08005894
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005895 tg3_set_txd(tnapi, entry, mapping, len,
Michael Chan5a6f3072006-03-20 22:28:05 -08005896 base_flags, (i == last) | (mss << 1));
5897
5898 entry = NEXT_TX(entry);
5899 }
5900 }
5901
5902 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005903 tw32_tx_mbox(tnapi->prodmbox, entry);
Michael Chan5a6f3072006-03-20 22:28:05 -08005904
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005905 tnapi->tx_prod = entry;
5906 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005907 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00005908
5909 /* netif_tx_stop_queue() must be done before checking
5910 * checking tx index in tg3_tx_avail() below, because in
5911 * tg3_tx(), we update tx index before checking for
5912 * netif_tx_queue_stopped().
5913 */
5914 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005915 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005916 netif_tx_wake_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005917 }
5918
5919out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005920 mmiowb();
Michael Chan5a6f3072006-03-20 22:28:05 -08005921
5922 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005923
5924dma_error:
5925 last = i;
5926 entry = tnapi->tx_prod;
5927 tnapi->tx_buffers[entry].skb = NULL;
5928 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005929 dma_unmap_addr(&tnapi->tx_buffers[entry], mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005930 skb_headlen(skb),
5931 PCI_DMA_TODEVICE);
5932 for (i = 0; i <= last; i++) {
5933 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5934 entry = NEXT_TX(entry);
5935
5936 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005937 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00005938 mapping),
5939 frag->size, PCI_DMA_TODEVICE);
5940 }
5941
5942 dev_kfree_skb(skb);
5943 return NETDEV_TX_OK;
Michael Chan5a6f3072006-03-20 22:28:05 -08005944}
5945
Stephen Hemminger613573252009-08-31 19:50:58 +00005946static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *,
5947 struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07005948
5949/* Use GSO to workaround a rare TSO bug that may be triggered when the
5950 * TSO header is greater than 80 bytes.
5951 */
5952static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
5953{
5954 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005955 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07005956
5957 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005958 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07005959 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00005960
5961 /* netif_tx_stop_queue() must be done before checking
5962 * checking tx index in tg3_tx_avail() below, because in
5963 * tg3_tx(), we update tx index before checking for
5964 * netif_tx_queue_stopped().
5965 */
5966 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005967 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08005968 return NETDEV_TX_BUSY;
5969
5970 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005971 }
5972
5973 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07005974 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07005975 goto tg3_tso_bug_end;
5976
5977 do {
5978 nskb = segs;
5979 segs = segs->next;
5980 nskb->next = NULL;
5981 tg3_start_xmit_dma_bug(nskb, tp->dev);
5982 } while (segs);
5983
5984tg3_tso_bug_end:
5985 dev_kfree_skb(skb);
5986
5987 return NETDEV_TX_OK;
5988}
Michael Chan52c0fd82006-06-29 20:15:54 -07005989
Michael Chan5a6f3072006-03-20 22:28:05 -08005990/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
5991 * support TG3_FLG2_HW_TSO_1 or firmware TSO only.
5992 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005993static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *skb,
5994 struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08005995{
5996 struct tg3 *tp = netdev_priv(dev);
Michael Chan5a6f3072006-03-20 22:28:05 -08005997 u32 len, entry, base_flags, mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998 int would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07005999 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006000 struct tg3_napi *tnapi;
6001 struct netdev_queue *txq;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006002 unsigned int i, last;
6003
Matt Carlson24f4efd2009-11-13 13:03:35 +00006004 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
6005 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Matt Carlson19cfaec2009-12-03 08:36:20 +00006006 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlson24f4efd2009-11-13 13:03:35 +00006007 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008
Michael Chan00b70502006-06-17 21:58:45 -07006009 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006010 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07006011 * interrupt. Furthermore, IRQ processing runs lockless so we have
6012 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006014 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006015 if (!netif_tx_queue_stopped(txq)) {
6016 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006017
6018 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00006019 netdev_err(dev,
6020 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022 return NETDEV_TX_BUSY;
6023 }
6024
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006025 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07006027 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006029
Matt Carlsonbe98da62010-07-11 09:31:46 +00006030 mss = skb_shinfo(skb)->gso_size;
6031 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006032 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00006033 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034
6035 if (skb_header_cloned(skb) &&
6036 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
6037 dev_kfree_skb(skb);
6038 goto out_unlock;
6039 }
6040
Matt Carlson34195c32010-07-11 09:31:42 +00006041 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07006042 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043
Matt Carlson02e96082010-09-15 08:59:59 +00006044 if (skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00006045 hdr_len = skb_headlen(skb) - ETH_HLEN;
6046 } else {
6047 u32 ip_tcp_len;
6048
6049 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
6050 hdr_len = ip_tcp_len + tcp_opt_len;
6051
6052 iph->check = 0;
6053 iph->tot_len = htons(mss + hdr_len);
6054 }
6055
Michael Chan52c0fd82006-06-29 20:15:54 -07006056 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Michael Chan7f62ad52007-02-20 23:25:40 -08006057 (tp->tg3_flags2 & TG3_FLG2_TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00006058 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07006059
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
6061 TXD_FLAG_CPU_POST_DMA);
6062
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006064 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006066 } else
6067 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
6068 iph->daddr, 0,
6069 IPPROTO_TCP,
6070 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071
Matt Carlson615774f2009-11-13 13:03:39 +00006072 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
6073 mss |= (hdr_len & 0xc) << 12;
6074 if (hdr_len & 0x10)
6075 base_flags |= 0x00000010;
6076 base_flags |= (hdr_len & 0x3e0) << 5;
6077 } else if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_2)
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006078 mss |= hdr_len << 9;
6079 else if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_1) ||
6080 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006081 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 int tsflags;
6083
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006084 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085 mss |= (tsflags << 11);
6086 }
6087 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006088 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089 int tsflags;
6090
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006091 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092 base_flags |= tsflags << 12;
6093 }
6094 }
6095 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00006096
Jesse Grosseab6d182010-10-20 13:56:03 +00006097 if (vlan_tx_tag_present(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098 base_flags |= (TXD_FLAG_VLAN |
6099 (vlan_tx_tag_get(skb) << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100
Matt Carlsonb703df62009-12-03 08:36:21 +00006101 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
Matt Carlson8fc2f992010-12-06 08:28:49 +00006102 !mss && skb->len > VLAN_ETH_FRAME_LEN)
Matt Carlson615774f2009-11-13 13:03:39 +00006103 base_flags |= TXD_FLAG_JMB_PKT;
6104
Alexander Duyckf4188d82009-12-02 16:48:38 +00006105 len = skb_headlen(skb);
6106
6107 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
6108 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07006109 dev_kfree_skb(skb);
6110 goto out_unlock;
6111 }
6112
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006113 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006114 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115
6116 would_hit_hwbug = 0;
6117
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006118 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) && len <= 8)
6119 would_hit_hwbug = 1;
6120
Matt Carlson0e1406d2009-11-02 12:33:33 +00006121 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
6122 tg3_4g_overflow_test(mapping, len))
Matt Carlson41588ba2008-04-19 18:12:33 -07006123 would_hit_hwbug = 1;
Matt Carlson0e1406d2009-11-02 12:33:33 +00006124
6125 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
6126 tg3_40bit_overflow_test(tp, mapping, len))
6127 would_hit_hwbug = 1;
6128
6129 if (tp->tg3_flags3 & TG3_FLG3_5701_DMA_BUG)
Michael Chanc58ec932005-09-17 00:46:27 -07006130 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006132 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
6134
6135 entry = NEXT_TX(entry);
6136
6137 /* Now loop through additional data fragments, and queue them. */
6138 if (skb_shinfo(skb)->nr_frags > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139 last = skb_shinfo(skb)->nr_frags - 1;
6140 for (i = 0; i <= last; i++) {
6141 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6142
6143 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006144 mapping = pci_map_page(tp->pdev,
6145 frag->page,
6146 frag->page_offset,
6147 len, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006149 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006150 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006151 mapping);
6152 if (pci_dma_mapping_error(tp->pdev, mapping))
6153 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006155 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) &&
6156 len <= 8)
6157 would_hit_hwbug = 1;
6158
Matt Carlson0e1406d2009-11-02 12:33:33 +00006159 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
6160 tg3_4g_overflow_test(mapping, len))
Michael Chanc58ec932005-09-17 00:46:27 -07006161 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162
Matt Carlson0e1406d2009-11-02 12:33:33 +00006163 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
6164 tg3_40bit_overflow_test(tp, mapping, len))
Michael Chan72f2afb2006-03-06 19:28:35 -08006165 would_hit_hwbug = 1;
6166
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006168 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169 base_flags, (i == last)|(mss << 1));
6170 else
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006171 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172 base_flags, (i == last));
6173
6174 entry = NEXT_TX(entry);
6175 }
6176 }
6177
6178 if (would_hit_hwbug) {
6179 u32 last_plus_one = entry;
6180 u32 start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181
Michael Chanc58ec932005-09-17 00:46:27 -07006182 start = entry - 1 - skb_shinfo(skb)->nr_frags;
6183 start &= (TG3_TX_RING_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184
6185 /* If the workaround fails due to memory/mapping
6186 * failure, silently drop this packet.
6187 */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006188 if (tigon3_dma_hwbug_workaround(tnapi, skb, last_plus_one,
Michael Chanc58ec932005-09-17 00:46:27 -07006189 &start, base_flags, mss))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190 goto out_unlock;
6191
6192 entry = start;
6193 }
6194
6195 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006196 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006198 tnapi->tx_prod = entry;
6199 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006200 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006201
6202 /* netif_tx_stop_queue() must be done before checking
6203 * checking tx index in tg3_tx_avail() below, because in
6204 * tg3_tx(), we update tx index before checking for
6205 * netif_tx_queue_stopped().
6206 */
6207 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006208 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006209 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211
6212out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00006213 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214
6215 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006216
6217dma_error:
6218 last = i;
6219 entry = tnapi->tx_prod;
6220 tnapi->tx_buffers[entry].skb = NULL;
6221 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006222 dma_unmap_addr(&tnapi->tx_buffers[entry], mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006223 skb_headlen(skb),
6224 PCI_DMA_TODEVICE);
6225 for (i = 0; i <= last; i++) {
6226 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6227 entry = NEXT_TX(entry);
6228
6229 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006230 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00006231 mapping),
6232 frag->size, PCI_DMA_TODEVICE);
6233 }
6234
6235 dev_kfree_skb(skb);
6236 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237}
6238
Michał Mirosławdc668912011-04-07 03:35:07 +00006239static u32 tg3_fix_features(struct net_device *dev, u32 features)
6240{
6241 struct tg3 *tp = netdev_priv(dev);
6242
6243 if (dev->mtu > ETH_DATA_LEN && (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
6244 features &= ~NETIF_F_ALL_TSO;
6245
6246 return features;
6247}
6248
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
6250 int new_mtu)
6251{
6252 dev->mtu = new_mtu;
6253
Michael Chanef7f5ec2005-07-25 12:32:25 -07006254 if (new_mtu > ETH_DATA_LEN) {
Michael Chana4e2b342005-10-26 15:46:52 -07006255 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michał Mirosławdc668912011-04-07 03:35:07 +00006256 netdev_update_features(dev);
Michael Chanef7f5ec2005-07-25 12:32:25 -07006257 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
Matt Carlson859a588792010-04-05 10:19:28 +00006258 } else {
Michael Chanef7f5ec2005-07-25 12:32:25 -07006259 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Matt Carlson859a588792010-04-05 10:19:28 +00006260 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07006261 } else {
Michał Mirosławdc668912011-04-07 03:35:07 +00006262 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanef7f5ec2005-07-25 12:32:25 -07006263 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
Michał Mirosławdc668912011-04-07 03:35:07 +00006264 netdev_update_features(dev);
6265 }
Michael Chan0f893dc2005-07-25 12:30:38 -07006266 tp->tg3_flags &= ~TG3_FLAG_JUMBO_RING_ENABLE;
Michael Chanef7f5ec2005-07-25 12:32:25 -07006267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268}
6269
6270static int tg3_change_mtu(struct net_device *dev, int new_mtu)
6271{
6272 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07006273 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274
6275 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
6276 return -EINVAL;
6277
6278 if (!netif_running(dev)) {
6279 /* We'll just catch it later when the
6280 * device is up'd.
6281 */
6282 tg3_set_mtu(dev, tp, new_mtu);
6283 return 0;
6284 }
6285
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006286 tg3_phy_stop(tp);
6287
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006289
6290 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291
Michael Chan944d9802005-05-29 14:57:48 -07006292 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293
6294 tg3_set_mtu(dev, tp, new_mtu);
6295
Michael Chanb9ec6c12006-07-25 16:37:27 -07006296 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297
Michael Chanb9ec6c12006-07-25 16:37:27 -07006298 if (!err)
6299 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006300
David S. Millerf47c11e2005-06-24 20:18:35 -07006301 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006303 if (!err)
6304 tg3_phy_start(tp);
6305
Michael Chanb9ec6c12006-07-25 16:37:27 -07006306 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307}
6308
Matt Carlson21f581a2009-08-28 14:00:25 +00006309static void tg3_rx_prodring_free(struct tg3 *tp,
6310 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006312 int i;
6313
Matt Carlson8fea32b2010-09-15 08:59:58 +00006314 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006315 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006316 i = (i + 1) & tp->rx_std_ring_mask)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006317 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6318 tp->rx_pkt_map_sz);
6319
6320 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
6321 for (i = tpr->rx_jmb_cons_idx;
6322 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006323 i = (i + 1) & tp->rx_jmb_ring_mask) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006324 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6325 TG3_RX_JMB_MAP_SZ);
6326 }
6327 }
6328
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006329 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006331
Matt Carlson2c49a442010-09-30 10:34:35 +00006332 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006333 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6334 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335
Matt Carlson48035722010-10-14 10:37:43 +00006336 if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
6337 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006338 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006339 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6340 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341 }
6342}
6343
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006344/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345 *
6346 * The chip has been shut down and the driver detached from
6347 * the networking, so no interrupts or new tx packets will
6348 * end up in the driver. tp->{tx,}lock are held and thus
6349 * we may not sleep.
6350 */
Matt Carlson21f581a2009-08-28 14:00:25 +00006351static int tg3_rx_prodring_alloc(struct tg3 *tp,
6352 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353{
Matt Carlson287be122009-08-28 13:58:46 +00006354 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006355
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006356 tpr->rx_std_cons_idx = 0;
6357 tpr->rx_std_prod_idx = 0;
6358 tpr->rx_jmb_cons_idx = 0;
6359 tpr->rx_jmb_prod_idx = 0;
6360
Matt Carlson8fea32b2010-09-15 08:59:58 +00006361 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006362 memset(&tpr->rx_std_buffers[0], 0,
6363 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00006364 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006365 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00006366 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006367 goto done;
6368 }
6369
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00006371 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006372
Matt Carlson287be122009-08-28 13:58:46 +00006373 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Michael Chana4e2b342005-10-26 15:46:52 -07006374 if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00006375 tp->dev->mtu > ETH_DATA_LEN)
6376 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
6377 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07006378
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379 /* Initialize invariants of the rings, we only set this
6380 * stuff once. This works because the card does not
6381 * write into the rx buffer posting rings.
6382 */
Matt Carlson2c49a442010-09-30 10:34:35 +00006383 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384 struct tg3_rx_buffer_desc *rxd;
6385
Matt Carlson21f581a2009-08-28 14:00:25 +00006386 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00006387 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
6389 rxd->opaque = (RXD_OPAQUE_RING_STD |
6390 (i << RXD_OPAQUE_INDEX_SHIFT));
6391 }
6392
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006393 /* Now allocate fresh SKBs for each rx ring. */
6394 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson86b21e52009-11-13 13:03:45 +00006395 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006396 netdev_warn(tp->dev,
6397 "Using a smaller RX standard ring. Only "
6398 "%d out of %d buffers were allocated "
6399 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006400 if (i == 0)
6401 goto initfail;
6402 tp->rx_pending = i;
6403 break;
6404 }
6405 }
6406
Matt Carlson48035722010-10-14 10:37:43 +00006407 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) ||
6408 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006409 goto done;
6410
Matt Carlson2c49a442010-09-30 10:34:35 +00006411 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006412
Matt Carlson0d86df82010-02-17 15:17:00 +00006413 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE))
6414 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006415
Matt Carlson2c49a442010-09-30 10:34:35 +00006416 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00006417 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418
Matt Carlson0d86df82010-02-17 15:17:00 +00006419 rxd = &tpr->rx_jmb[i].std;
6420 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
6421 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
6422 RXD_FLAG_JUMBO;
6423 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
6424 (i << RXD_OPAQUE_INDEX_SHIFT));
6425 }
6426
6427 for (i = 0; i < tp->rx_jumbo_pending; i++) {
6428 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006429 netdev_warn(tp->dev,
6430 "Using a smaller RX jumbo ring. Only %d "
6431 "out of %d buffers were allocated "
6432 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00006433 if (i == 0)
6434 goto initfail;
6435 tp->rx_jumbo_pending = i;
6436 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437 }
6438 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006439
6440done:
Michael Chan32d8c572006-07-25 16:38:29 -07006441 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006442
6443initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00006444 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006445 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006446}
6447
Matt Carlson21f581a2009-08-28 14:00:25 +00006448static void tg3_rx_prodring_fini(struct tg3 *tp,
6449 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450{
Matt Carlson21f581a2009-08-28 14:00:25 +00006451 kfree(tpr->rx_std_buffers);
6452 tpr->rx_std_buffers = NULL;
6453 kfree(tpr->rx_jmb_buffers);
6454 tpr->rx_jmb_buffers = NULL;
6455 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006456 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
6457 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006458 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006459 }
Matt Carlson21f581a2009-08-28 14:00:25 +00006460 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006461 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
6462 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006463 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006464 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006465}
6466
Matt Carlson21f581a2009-08-28 14:00:25 +00006467static int tg3_rx_prodring_init(struct tg3 *tp,
6468 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006469{
Matt Carlson2c49a442010-09-30 10:34:35 +00006470 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
6471 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006472 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006473 return -ENOMEM;
6474
Matt Carlson4bae65c2010-11-24 08:31:52 +00006475 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
6476 TG3_RX_STD_RING_BYTES(tp),
6477 &tpr->rx_std_mapping,
6478 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006479 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006480 goto err_out;
6481
Matt Carlson48035722010-10-14 10:37:43 +00006482 if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
6483 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006484 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00006485 GFP_KERNEL);
6486 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006487 goto err_out;
6488
Matt Carlson4bae65c2010-11-24 08:31:52 +00006489 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
6490 TG3_RX_JMB_RING_BYTES(tp),
6491 &tpr->rx_jmb_mapping,
6492 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006493 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006494 goto err_out;
6495 }
6496
6497 return 0;
6498
6499err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00006500 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006501 return -ENOMEM;
6502}
6503
6504/* Free up pending packets in all rx/tx rings.
6505 *
6506 * The chip has been shut down and the driver detached from
6507 * the networking, so no interrupts or new tx packets will
6508 * end up in the driver. tp->{tx,}lock is not held and we are not
6509 * in an interrupt context and thus may sleep.
6510 */
6511static void tg3_free_rings(struct tg3 *tp)
6512{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006513 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006514
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006515 for (j = 0; j < tp->irq_cnt; j++) {
6516 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006517
Matt Carlson8fea32b2010-09-15 08:59:58 +00006518 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00006519
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006520 if (!tnapi->tx_buffers)
6521 continue;
6522
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006523 for (i = 0; i < TG3_TX_RING_SIZE; ) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006524 struct ring_info *txp;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006525 struct sk_buff *skb;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006526 unsigned int k;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006527
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006528 txp = &tnapi->tx_buffers[i];
6529 skb = txp->skb;
6530
6531 if (skb == NULL) {
6532 i++;
6533 continue;
6534 }
6535
Alexander Duyckf4188d82009-12-02 16:48:38 +00006536 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006537 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006538 skb_headlen(skb),
6539 PCI_DMA_TODEVICE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006540 txp->skb = NULL;
6541
Alexander Duyckf4188d82009-12-02 16:48:38 +00006542 i++;
6543
6544 for (k = 0; k < skb_shinfo(skb)->nr_frags; k++) {
6545 txp = &tnapi->tx_buffers[i & (TG3_TX_RING_SIZE - 1)];
6546 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006547 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006548 skb_shinfo(skb)->frags[k].size,
6549 PCI_DMA_TODEVICE);
6550 i++;
6551 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006552
6553 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006554 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006555 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006556}
6557
6558/* Initialize tx/rx rings for packet processing.
6559 *
6560 * The chip has been shut down and the driver detached from
6561 * the networking, so no interrupts or new tx packets will
6562 * end up in the driver. tp->{tx,}lock are held and thus
6563 * we may not sleep.
6564 */
6565static int tg3_init_rings(struct tg3 *tp)
6566{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006567 int i;
Matt Carlson72334482009-08-28 14:03:01 +00006568
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006569 /* Free up all the SKBs. */
6570 tg3_free_rings(tp);
6571
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006572 for (i = 0; i < tp->irq_cnt; i++) {
6573 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006574
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006575 tnapi->last_tag = 0;
6576 tnapi->last_irq_tag = 0;
6577 tnapi->hw_status->status = 0;
6578 tnapi->hw_status->status_tag = 0;
6579 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6580
6581 tnapi->tx_prod = 0;
6582 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006583 if (tnapi->tx_ring)
6584 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006585
6586 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006587 if (tnapi->rx_rcb)
6588 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006589
Matt Carlson8fea32b2010-09-15 08:59:58 +00006590 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00006591 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006592 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006593 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006594 }
Matt Carlson72334482009-08-28 14:03:01 +00006595
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006596 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006597}
6598
6599/*
6600 * Must not be invoked with interrupt sources disabled and
6601 * the hardware shutdown down.
6602 */
6603static void tg3_free_consistent(struct tg3 *tp)
6604{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006605 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006606
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006607 for (i = 0; i < tp->irq_cnt; i++) {
6608 struct tg3_napi *tnapi = &tp->napi[i];
6609
6610 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006611 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006612 tnapi->tx_ring, tnapi->tx_desc_mapping);
6613 tnapi->tx_ring = NULL;
6614 }
6615
6616 kfree(tnapi->tx_buffers);
6617 tnapi->tx_buffers = NULL;
6618
6619 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006620 dma_free_coherent(&tp->pdev->dev,
6621 TG3_RX_RCB_RING_BYTES(tp),
6622 tnapi->rx_rcb,
6623 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006624 tnapi->rx_rcb = NULL;
6625 }
6626
Matt Carlson8fea32b2010-09-15 08:59:58 +00006627 tg3_rx_prodring_fini(tp, &tnapi->prodring);
6628
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006629 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006630 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
6631 tnapi->hw_status,
6632 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006633 tnapi->hw_status = NULL;
6634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006635 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006636
Linus Torvalds1da177e2005-04-16 15:20:36 -07006637 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006638 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
6639 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006640 tp->hw_stats = NULL;
6641 }
6642}
6643
6644/*
6645 * Must not be invoked with interrupt sources disabled and
6646 * the hardware shutdown down. Can sleep.
6647 */
6648static int tg3_alloc_consistent(struct tg3 *tp)
6649{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006650 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006651
Matt Carlson4bae65c2010-11-24 08:31:52 +00006652 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
6653 sizeof(struct tg3_hw_stats),
6654 &tp->stats_mapping,
6655 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656 if (!tp->hw_stats)
6657 goto err_out;
6658
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6660
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006661 for (i = 0; i < tp->irq_cnt; i++) {
6662 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006663 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006664
Matt Carlson4bae65c2010-11-24 08:31:52 +00006665 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
6666 TG3_HW_STATUS_SIZE,
6667 &tnapi->status_mapping,
6668 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006669 if (!tnapi->hw_status)
6670 goto err_out;
6671
6672 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006673 sblk = tnapi->hw_status;
6674
Matt Carlson8fea32b2010-09-15 08:59:58 +00006675 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
6676 goto err_out;
6677
Matt Carlson19cfaec2009-12-03 08:36:20 +00006678 /* If multivector TSS is enabled, vector 0 does not handle
6679 * tx interrupts. Don't allocate any resources for it.
6680 */
6681 if ((!i && !(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) ||
6682 (i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))) {
6683 tnapi->tx_buffers = kzalloc(sizeof(struct ring_info) *
6684 TG3_TX_RING_SIZE,
6685 GFP_KERNEL);
6686 if (!tnapi->tx_buffers)
6687 goto err_out;
6688
Matt Carlson4bae65c2010-11-24 08:31:52 +00006689 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
6690 TG3_TX_RING_BYTES,
6691 &tnapi->tx_desc_mapping,
6692 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00006693 if (!tnapi->tx_ring)
6694 goto err_out;
6695 }
6696
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006697 /*
6698 * When RSS is enabled, the status block format changes
6699 * slightly. The "rx_jumbo_consumer", "reserved",
6700 * and "rx_mini_consumer" members get mapped to the
6701 * other three rx return ring producer indexes.
6702 */
6703 switch (i) {
6704 default:
6705 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
6706 break;
6707 case 2:
6708 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
6709 break;
6710 case 3:
6711 tnapi->rx_rcb_prod_idx = &sblk->reserved;
6712 break;
6713 case 4:
6714 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
6715 break;
6716 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006717
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006718 /*
6719 * If multivector RSS is enabled, vector 0 does not handle
6720 * rx or tx interrupts. Don't allocate any resources for it.
6721 */
6722 if (!i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS))
6723 continue;
6724
Matt Carlson4bae65c2010-11-24 08:31:52 +00006725 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
6726 TG3_RX_RCB_RING_BYTES(tp),
6727 &tnapi->rx_rcb_mapping,
6728 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006729 if (!tnapi->rx_rcb)
6730 goto err_out;
6731
6732 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006733 }
6734
Linus Torvalds1da177e2005-04-16 15:20:36 -07006735 return 0;
6736
6737err_out:
6738 tg3_free_consistent(tp);
6739 return -ENOMEM;
6740}
6741
6742#define MAX_WAIT_CNT 1000
6743
6744/* To stop a block, clear the enable bit and poll till it
6745 * clears. tp->lock is held.
6746 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006747static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006748{
6749 unsigned int i;
6750 u32 val;
6751
6752 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
6753 switch (ofs) {
6754 case RCVLSC_MODE:
6755 case DMAC_MODE:
6756 case MBFREE_MODE:
6757 case BUFMGR_MODE:
6758 case MEMARB_MODE:
6759 /* We can't enable/disable these bits of the
6760 * 5705/5750, just say success.
6761 */
6762 return 0;
6763
6764 default:
6765 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767 }
6768
6769 val = tr32(ofs);
6770 val &= ~enable_bit;
6771 tw32_f(ofs, val);
6772
6773 for (i = 0; i < MAX_WAIT_CNT; i++) {
6774 udelay(100);
6775 val = tr32(ofs);
6776 if ((val & enable_bit) == 0)
6777 break;
6778 }
6779
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006780 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00006781 dev_err(&tp->pdev->dev,
6782 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
6783 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006784 return -ENODEV;
6785 }
6786
6787 return 0;
6788}
6789
6790/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006791static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006792{
6793 int i, err;
6794
6795 tg3_disable_ints(tp);
6796
6797 tp->rx_mode &= ~RX_MODE_ENABLE;
6798 tw32_f(MAC_RX_MODE, tp->rx_mode);
6799 udelay(10);
6800
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006801 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
6802 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
6803 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
6804 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
6805 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
6806 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006807
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006808 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
6809 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
6810 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
6811 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
6812 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
6813 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
6814 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006815
6816 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
6817 tw32_f(MAC_MODE, tp->mac_mode);
6818 udelay(40);
6819
6820 tp->tx_mode &= ~TX_MODE_ENABLE;
6821 tw32_f(MAC_TX_MODE, tp->tx_mode);
6822
6823 for (i = 0; i < MAX_WAIT_CNT; i++) {
6824 udelay(100);
6825 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
6826 break;
6827 }
6828 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00006829 dev_err(&tp->pdev->dev,
6830 "%s timed out, TX_MODE_ENABLE will not clear "
6831 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07006832 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833 }
6834
Michael Chane6de8ad2005-05-05 14:42:41 -07006835 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006836 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
6837 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838
6839 tw32(FTQ_RESET, 0xffffffff);
6840 tw32(FTQ_RESET, 0x00000000);
6841
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006842 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
6843 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006844
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006845 for (i = 0; i < tp->irq_cnt; i++) {
6846 struct tg3_napi *tnapi = &tp->napi[i];
6847 if (tnapi->hw_status)
6848 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006850 if (tp->hw_stats)
6851 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6852
Linus Torvalds1da177e2005-04-16 15:20:36 -07006853 return err;
6854}
6855
Matt Carlson0d3031d2007-10-10 18:02:43 -07006856static void tg3_ape_send_event(struct tg3 *tp, u32 event)
6857{
6858 int i;
6859 u32 apedata;
6860
Matt Carlsondc6d0742010-09-15 08:59:55 +00006861 /* NCSI does not support APE events */
6862 if (tp->tg3_flags3 & TG3_FLG3_APE_HAS_NCSI)
6863 return;
6864
Matt Carlson0d3031d2007-10-10 18:02:43 -07006865 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
6866 if (apedata != APE_SEG_SIG_MAGIC)
6867 return;
6868
6869 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07006870 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006871 return;
6872
6873 /* Wait for up to 1 millisecond for APE to service previous event. */
6874 for (i = 0; i < 10; i++) {
6875 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
6876 return;
6877
6878 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
6879
6880 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6881 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
6882 event | APE_EVENT_STATUS_EVENT_PENDING);
6883
6884 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
6885
6886 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6887 break;
6888
6889 udelay(100);
6890 }
6891
6892 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6893 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
6894}
6895
6896static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
6897{
6898 u32 event;
6899 u32 apedata;
6900
6901 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
6902 return;
6903
6904 switch (kind) {
Matt Carlson33f401a2010-04-05 10:19:27 +00006905 case RESET_KIND_INIT:
6906 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
6907 APE_HOST_SEG_SIG_MAGIC);
6908 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
6909 APE_HOST_SEG_LEN_MAGIC);
6910 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
6911 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
6912 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
Matt Carlson6867c842010-07-11 09:31:44 +00006913 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
Matt Carlson33f401a2010-04-05 10:19:27 +00006914 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
6915 APE_HOST_BEHAV_NO_PHYLOCK);
Matt Carlsondc6d0742010-09-15 08:59:55 +00006916 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
6917 TG3_APE_HOST_DRVR_STATE_START);
Matt Carlson0d3031d2007-10-10 18:02:43 -07006918
Matt Carlson33f401a2010-04-05 10:19:27 +00006919 event = APE_EVENT_STATUS_STATE_START;
6920 break;
6921 case RESET_KIND_SHUTDOWN:
6922 /* With the interface we are currently using,
6923 * APE does not track driver state. Wiping
6924 * out the HOST SEGMENT SIGNATURE forces
6925 * the APE to assume OS absent status.
6926 */
6927 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
Matt Carlsonb2aee152008-11-03 16:51:11 -08006928
Matt Carlsondc6d0742010-09-15 08:59:55 +00006929 if (device_may_wakeup(&tp->pdev->dev) &&
6930 (tp->tg3_flags & TG3_FLAG_WOL_ENABLE)) {
6931 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
6932 TG3_APE_HOST_WOL_SPEED_AUTO);
6933 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
6934 } else
6935 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
6936
6937 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
6938
Matt Carlson33f401a2010-04-05 10:19:27 +00006939 event = APE_EVENT_STATUS_STATE_UNLOAD;
6940 break;
6941 case RESET_KIND_SUSPEND:
6942 event = APE_EVENT_STATUS_STATE_SUSPEND;
6943 break;
6944 default:
6945 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006946 }
6947
6948 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
6949
6950 tg3_ape_send_event(tp, event);
6951}
6952
Michael Chane6af3012005-04-21 17:12:05 -07006953/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006954static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
6955{
David S. Millerf49639e2006-06-09 11:58:36 -07006956 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
6957 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006958
6959 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6960 switch (kind) {
6961 case RESET_KIND_INIT:
6962 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6963 DRV_STATE_START);
6964 break;
6965
6966 case RESET_KIND_SHUTDOWN:
6967 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6968 DRV_STATE_UNLOAD);
6969 break;
6970
6971 case RESET_KIND_SUSPEND:
6972 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6973 DRV_STATE_SUSPEND);
6974 break;
6975
6976 default:
6977 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006979 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006980
6981 if (kind == RESET_KIND_INIT ||
6982 kind == RESET_KIND_SUSPEND)
6983 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006984}
6985
6986/* tp->lock is held. */
6987static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
6988{
6989 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6990 switch (kind) {
6991 case RESET_KIND_INIT:
6992 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6993 DRV_STATE_START_DONE);
6994 break;
6995
6996 case RESET_KIND_SHUTDOWN:
6997 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6998 DRV_STATE_UNLOAD_DONE);
6999 break;
7000
7001 default:
7002 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007004 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07007005
7006 if (kind == RESET_KIND_SHUTDOWN)
7007 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007008}
7009
7010/* tp->lock is held. */
7011static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
7012{
7013 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
7014 switch (kind) {
7015 case RESET_KIND_INIT:
7016 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7017 DRV_STATE_START);
7018 break;
7019
7020 case RESET_KIND_SHUTDOWN:
7021 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7022 DRV_STATE_UNLOAD);
7023 break;
7024
7025 case RESET_KIND_SUSPEND:
7026 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7027 DRV_STATE_SUSPEND);
7028 break;
7029
7030 default:
7031 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007033 }
7034}
7035
Michael Chan7a6f4362006-09-27 16:03:31 -07007036static int tg3_poll_fw(struct tg3 *tp)
7037{
7038 int i;
7039 u32 val;
7040
Michael Chanb5d37722006-09-27 16:06:21 -07007041 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08007042 /* Wait up to 20ms for init done. */
7043 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07007044 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
7045 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08007046 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07007047 }
7048 return -ENODEV;
7049 }
7050
Michael Chan7a6f4362006-09-27 16:03:31 -07007051 /* Wait for firmware initialization to complete. */
7052 for (i = 0; i < 100000; i++) {
7053 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
7054 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
7055 break;
7056 udelay(10);
7057 }
7058
7059 /* Chip might not be fitted with firmware. Some Sun onboard
7060 * parts are configured like that. So don't signal the timeout
7061 * of the above loop as an error, but do report the lack of
7062 * running firmware once.
7063 */
7064 if (i >= 100000 &&
7065 !(tp->tg3_flags2 & TG3_FLG2_NO_FWARE_REPORTED)) {
7066 tp->tg3_flags2 |= TG3_FLG2_NO_FWARE_REPORTED;
7067
Joe Perches05dbe002010-02-17 19:44:19 +00007068 netdev_info(tp->dev, "No firmware running\n");
Michael Chan7a6f4362006-09-27 16:03:31 -07007069 }
7070
Matt Carlson6b10c162010-02-12 14:47:08 +00007071 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
7072 /* The 57765 A0 needs a little more
7073 * time to do some important work.
7074 */
7075 mdelay(10);
7076 }
7077
Michael Chan7a6f4362006-09-27 16:03:31 -07007078 return 0;
7079}
7080
Michael Chanee6a99b2007-07-18 21:49:10 -07007081/* Save PCI command register before chip reset */
7082static void tg3_save_pci_state(struct tg3 *tp)
7083{
Matt Carlson8a6eac92007-10-21 16:17:55 -07007084 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007085}
7086
7087/* Restore PCI state after chip reset */
7088static void tg3_restore_pci_state(struct tg3 *tp)
7089{
7090 u32 val;
7091
7092 /* Re-enable indirect register accesses. */
7093 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7094 tp->misc_host_ctrl);
7095
7096 /* Set MAX PCI retry to zero. */
7097 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7098 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
7099 (tp->tg3_flags & TG3_FLAG_PCIX_MODE))
7100 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007101 /* Allow reads and writes to the APE register and memory space. */
7102 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
7103 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00007104 PCISTATE_ALLOW_APE_SHMEM_WR |
7105 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07007106 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7107
Matt Carlson8a6eac92007-10-21 16:17:55 -07007108 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007109
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007110 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
7111 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
Matt Carlsoncf790032010-11-24 08:31:48 +00007112 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007113 else {
7114 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7115 tp->pci_cacheline_sz);
7116 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7117 tp->pci_lat_timer);
7118 }
Michael Chan114342f2007-10-15 02:12:26 -07007119 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08007120
Michael Chanee6a99b2007-07-18 21:49:10 -07007121 /* Make sure PCI-X relaxed ordering bit is clear. */
Matt Carlson52f44902008-11-21 17:17:04 -08007122 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Matt Carlson9974a352007-10-07 23:27:28 -07007123 u16 pcix_cmd;
7124
7125 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7126 &pcix_cmd);
7127 pcix_cmd &= ~PCI_X_CMD_ERO;
7128 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7129 pcix_cmd);
7130 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007131
7132 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007133
7134 /* Chip reset on 5780 will reset MSI enable bit,
7135 * so need to restore it.
7136 */
7137 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
7138 u16 ctrl;
7139
7140 pci_read_config_word(tp->pdev,
7141 tp->msi_cap + PCI_MSI_FLAGS,
7142 &ctrl);
7143 pci_write_config_word(tp->pdev,
7144 tp->msi_cap + PCI_MSI_FLAGS,
7145 ctrl | PCI_MSI_FLAGS_ENABLE);
7146 val = tr32(MSGINT_MODE);
7147 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7148 }
7149 }
7150}
7151
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152static void tg3_stop_fw(struct tg3 *);
7153
7154/* tp->lock is held. */
7155static int tg3_chip_reset(struct tg3 *tp)
7156{
7157 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007158 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007159 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007160
David S. Millerf49639e2006-06-09 11:58:36 -07007161 tg3_nvram_lock(tp);
7162
Matt Carlson77b483f2008-08-15 14:07:24 -07007163 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7164
David S. Millerf49639e2006-06-09 11:58:36 -07007165 /* No matching tg3_nvram_unlock() after this because
7166 * chip reset below will undo the nvram lock.
7167 */
7168 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007169
Michael Chanee6a99b2007-07-18 21:49:10 -07007170 /* GRC_MISC_CFG core clock reset will clear the memory
7171 * enable bit in PCI register 4 and the MSI enable bit
7172 * on some chips, so we save relevant registers here.
7173 */
7174 tg3_save_pci_state(tp);
7175
Michael Chand9ab5ad12006-03-20 22:27:35 -08007176 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08007177 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08007178 tw32(GRC_FASTBOOT_PC, 0);
7179
Linus Torvalds1da177e2005-04-16 15:20:36 -07007180 /*
7181 * We must avoid the readl() that normally takes place.
7182 * It locks machines, causes machine checks, and other
7183 * fun things. So, temporarily disable the 5701
7184 * hardware workaround, while we do the reset.
7185 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007186 write_op = tp->write32;
7187 if (write_op == tg3_write_flush_reg32)
7188 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007189
Michael Chand18edcb2007-03-24 20:57:11 -07007190 /* Prevent the irq handler from reading or writing PCI registers
7191 * during chip reset when the memory enable bit in the PCI command
7192 * register may be cleared. The chip does not generate interrupt
7193 * at this time, but the irq handler may still be called due to irq
7194 * sharing or irqpoll.
7195 */
7196 tp->tg3_flags |= TG3_FLAG_CHIP_RESETTING;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007197 for (i = 0; i < tp->irq_cnt; i++) {
7198 struct tg3_napi *tnapi = &tp->napi[i];
7199 if (tnapi->hw_status) {
7200 tnapi->hw_status->status = 0;
7201 tnapi->hw_status->status_tag = 0;
7202 }
7203 tnapi->last_tag = 0;
7204 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007205 }
Michael Chand18edcb2007-03-24 20:57:11 -07007206 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007207
7208 for (i = 0; i < tp->irq_cnt; i++)
7209 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007210
Matt Carlson255ca312009-08-25 10:07:27 +00007211 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7212 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7213 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7214 }
7215
Linus Torvalds1da177e2005-04-16 15:20:36 -07007216 /* do the reset */
7217 val = GRC_MISC_CFG_CORECLK_RESET;
7218
7219 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
Matt Carlson88075d92010-08-02 11:25:58 +00007220 /* Force PCIe 1.0a mode */
7221 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlson1407deb2011-04-05 14:22:44 +00007222 !(tp->tg3_flags3 & TG3_FLG3_57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00007223 tr32(TG3_PCIE_PHY_TSTCTL) ==
7224 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7225 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7226
Linus Torvalds1da177e2005-04-16 15:20:36 -07007227 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7228 tw32(GRC_MISC_CFG, (1 << 29));
7229 val |= (1 << 29);
7230 }
7231 }
7232
Michael Chanb5d37722006-09-27 16:06:21 -07007233 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7234 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7235 tw32(GRC_VCPU_EXT_CTRL,
7236 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7237 }
7238
Matt Carlsonf37500d2010-08-02 11:25:59 +00007239 /* Manage gphy power for all CPMU absent PCIe devices. */
7240 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
7241 !(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007242 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007243
Linus Torvalds1da177e2005-04-16 15:20:36 -07007244 tw32(GRC_MISC_CFG, val);
7245
Michael Chan1ee582d2005-08-09 20:16:46 -07007246 /* restore 5701 hardware bug workaround write method */
7247 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007248
7249 /* Unfortunately, we have to delay before the PCI read back.
7250 * Some 575X chips even will not respond to a PCI cfg access
7251 * when the reset command is given to the chip.
7252 *
7253 * How do these hardware designers expect things to work
7254 * properly if the PCI write is posted for a long period
7255 * of time? It is always necessary to have some method by
7256 * which a register read back can occur to push the write
7257 * out which does the reset.
7258 *
7259 * For most tg3 variants the trick below was working.
7260 * Ho hum...
7261 */
7262 udelay(120);
7263
7264 /* Flush PCI posted writes. The normal MMIO registers
7265 * are inaccessible at this time so this is the only
7266 * way to make this reliably (actually, this is no longer
7267 * the case, see above). I tried to use indirect
7268 * register read/write but this upset some 5701 variants.
7269 */
7270 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7271
7272 udelay(120);
7273
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007274 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) && tp->pcie_cap) {
Matt Carlsone7126992009-08-25 10:08:16 +00007275 u16 val16;
7276
Linus Torvalds1da177e2005-04-16 15:20:36 -07007277 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7278 int i;
7279 u32 cfg_val;
7280
7281 /* Wait for link training to complete. */
7282 for (i = 0; i < 5000; i++)
7283 udelay(100);
7284
7285 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7286 pci_write_config_dword(tp->pdev, 0xc4,
7287 cfg_val | (1 << 15));
7288 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007289
Matt Carlsone7126992009-08-25 10:08:16 +00007290 /* Clear the "no snoop" and "relaxed ordering" bits. */
7291 pci_read_config_word(tp->pdev,
7292 tp->pcie_cap + PCI_EXP_DEVCTL,
7293 &val16);
7294 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7295 PCI_EXP_DEVCTL_NOSNOOP_EN);
7296 /*
7297 * Older PCIe devices only support the 128 byte
7298 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007299 */
Matt Carlson6de34cb2010-08-02 11:25:55 +00007300 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007301 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007302 pci_write_config_word(tp->pdev,
7303 tp->pcie_cap + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007304 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007305
Matt Carlsoncf790032010-11-24 08:31:48 +00007306 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007307
7308 /* Clear error status */
7309 pci_write_config_word(tp->pdev,
7310 tp->pcie_cap + PCI_EXP_DEVSTA,
7311 PCI_EXP_DEVSTA_CED |
7312 PCI_EXP_DEVSTA_NFED |
7313 PCI_EXP_DEVSTA_FED |
7314 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007315 }
7316
Michael Chanee6a99b2007-07-18 21:49:10 -07007317 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007318
Michael Chand18edcb2007-03-24 20:57:11 -07007319 tp->tg3_flags &= ~TG3_FLAG_CHIP_RESETTING;
7320
Michael Chanee6a99b2007-07-18 21:49:10 -07007321 val = 0;
7322 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chan4cf78e42005-07-25 12:29:19 -07007323 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007324 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007325
7326 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7327 tg3_stop_fw(tp);
7328 tw32(0x5000, 0x400);
7329 }
7330
7331 tw32(GRC_MODE, tp->grc_mode);
7332
7333 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007334 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007335
7336 tw32(0xc4, val | (1 << 15));
7337 }
7338
7339 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7340 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7341 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7342 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7343 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7344 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7345 }
7346
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007347 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
7348 tp->mac_mode = MAC_MODE_APE_TX_EN |
7349 MAC_MODE_APE_RX_EN |
7350 MAC_MODE_TDE_ENABLE;
7351
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007352 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007353 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
7354 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007355 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007356 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7357 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007358 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007359 val = 0;
7360
7361 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007362 udelay(40);
7363
Matt Carlson77b483f2008-08-15 14:07:24 -07007364 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7365
Michael Chan7a6f4362006-09-27 16:03:31 -07007366 err = tg3_poll_fw(tp);
7367 if (err)
7368 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007369
Matt Carlson0a9140c2009-08-28 12:27:50 +00007370 tg3_mdio_start(tp);
7371
Linus Torvalds1da177e2005-04-16 15:20:36 -07007372 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007373 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7374 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlson1407deb2011-04-05 14:22:44 +00007375 !(tp->tg3_flags3 & TG3_FLG3_57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007376 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007377
7378 tw32(0x7c00, val | (1 << 25));
7379 }
7380
Matt Carlsond78b59f2011-04-05 14:22:46 +00007381 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
7382 val = tr32(TG3_CPMU_CLCK_ORIDE);
7383 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
7384 }
7385
Linus Torvalds1da177e2005-04-16 15:20:36 -07007386 /* Reprobe ASF enable state. */
7387 tp->tg3_flags &= ~TG3_FLAG_ENABLE_ASF;
7388 tp->tg3_flags2 &= ~TG3_FLG2_ASF_NEW_HANDSHAKE;
7389 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7390 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7391 u32 nic_cfg;
7392
7393 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7394 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
7395 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
Matt Carlson4ba526c2008-08-15 14:10:04 -07007396 tp->last_event_jiffies = jiffies;
John W. Linvillecbf46852005-04-21 17:01:29 -07007397 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007398 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
7399 }
7400 }
7401
7402 return 0;
7403}
7404
7405/* tp->lock is held. */
7406static void tg3_stop_fw(struct tg3 *tp)
7407{
Matt Carlson0d3031d2007-10-10 18:02:43 -07007408 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
7409 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07007410 /* Wait for RX cpu to ACK the previous event. */
7411 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007412
7413 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007414
7415 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007416
Matt Carlson7c5026a2008-05-02 16:49:29 -07007417 /* Wait for RX cpu to ACK this event. */
7418 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007419 }
7420}
7421
7422/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007423static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007424{
7425 int err;
7426
7427 tg3_stop_fw(tp);
7428
Michael Chan944d9802005-05-29 14:57:48 -07007429 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007430
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007431 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007432 err = tg3_chip_reset(tp);
7433
Matt Carlsondaba2a62009-04-20 06:58:52 +00007434 __tg3_set_mac_addr(tp, 0);
7435
Michael Chan944d9802005-05-29 14:57:48 -07007436 tg3_write_sig_legacy(tp, kind);
7437 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007438
7439 if (err)
7440 return err;
7441
7442 return 0;
7443}
7444
Linus Torvalds1da177e2005-04-16 15:20:36 -07007445#define RX_CPU_SCRATCH_BASE 0x30000
7446#define RX_CPU_SCRATCH_SIZE 0x04000
7447#define TX_CPU_SCRATCH_BASE 0x34000
7448#define TX_CPU_SCRATCH_SIZE 0x04000
7449
7450/* tp->lock is held. */
7451static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
7452{
7453 int i;
7454
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +02007455 BUG_ON(offset == TX_CPU_BASE &&
7456 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007457
Michael Chanb5d37722006-09-27 16:06:21 -07007458 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7459 u32 val = tr32(GRC_VCPU_EXT_CTRL);
7460
7461 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
7462 return 0;
7463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007464 if (offset == RX_CPU_BASE) {
7465 for (i = 0; i < 10000; i++) {
7466 tw32(offset + CPU_STATE, 0xffffffff);
7467 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7468 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7469 break;
7470 }
7471
7472 tw32(offset + CPU_STATE, 0xffffffff);
7473 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
7474 udelay(10);
7475 } else {
7476 for (i = 0; i < 10000; i++) {
7477 tw32(offset + CPU_STATE, 0xffffffff);
7478 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7479 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7480 break;
7481 }
7482 }
7483
7484 if (i >= 10000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007485 netdev_err(tp->dev, "%s timed out, %s CPU\n",
7486 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007487 return -ENODEV;
7488 }
Michael Chanec41c7d2006-01-17 02:40:55 -08007489
7490 /* Clear firmware's nvram arbitration. */
7491 if (tp->tg3_flags & TG3_FLAG_NVRAM)
7492 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007493 return 0;
7494}
7495
7496struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007497 unsigned int fw_base;
7498 unsigned int fw_len;
7499 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007500};
7501
7502/* tp->lock is held. */
7503static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
7504 int cpu_scratch_size, struct fw_info *info)
7505{
Michael Chanec41c7d2006-01-17 02:40:55 -08007506 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007507 void (*write_op)(struct tg3 *, u32, u32);
7508
7509 if (cpu_base == TX_CPU_BASE &&
7510 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007511 netdev_err(tp->dev,
7512 "%s: Trying to load TX cpu firmware which is 5705\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007513 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007514 return -EINVAL;
7515 }
7516
7517 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
7518 write_op = tg3_write_mem;
7519 else
7520 write_op = tg3_write_indirect_reg32;
7521
Michael Chan1b628152005-05-29 14:59:49 -07007522 /* It is possible that bootcode is still loading at this point.
7523 * Get the nvram lock first before halting the cpu.
7524 */
Michael Chanec41c7d2006-01-17 02:40:55 -08007525 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007526 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08007527 if (!lock_err)
7528 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007529 if (err)
7530 goto out;
7531
7532 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
7533 write_op(tp, cpu_scratch_base + i, 0);
7534 tw32(cpu_base + CPU_STATE, 0xffffffff);
7535 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007536 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007537 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007538 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07007539 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007540 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007541
7542 err = 0;
7543
7544out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007545 return err;
7546}
7547
7548/* tp->lock is held. */
7549static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
7550{
7551 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007552 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007553 int err, i;
7554
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007555 fw_data = (void *)tp->fw->data;
7556
7557 /* Firmware blob starts with version numbers, followed by
7558 start address and length. We are setting complete length.
7559 length = end_address_of_bss - start_address_of_text.
7560 Remainder is the blob to be loaded contiguously
7561 from start address. */
7562
7563 info.fw_base = be32_to_cpu(fw_data[1]);
7564 info.fw_len = tp->fw->size - 12;
7565 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007566
7567 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
7568 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
7569 &info);
7570 if (err)
7571 return err;
7572
7573 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
7574 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
7575 &info);
7576 if (err)
7577 return err;
7578
7579 /* Now startup only the RX cpu. */
7580 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007581 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007582
7583 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007584 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007585 break;
7586 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7587 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007588 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007589 udelay(1000);
7590 }
7591 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007592 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
7593 "should be %08x\n", __func__,
Joe Perches05dbe002010-02-17 19:44:19 +00007594 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007595 return -ENODEV;
7596 }
7597 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7598 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
7599
7600 return 0;
7601}
7602
Linus Torvalds1da177e2005-04-16 15:20:36 -07007603/* 5705 needs a special version of the TSO firmware. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007604
7605/* tp->lock is held. */
7606static int tg3_load_tso_firmware(struct tg3 *tp)
7607{
7608 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007609 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007610 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
7611 int err, i;
7612
7613 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
7614 return 0;
7615
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007616 fw_data = (void *)tp->fw->data;
7617
7618 /* Firmware blob starts with version numbers, followed by
7619 start address and length. We are setting complete length.
7620 length = end_address_of_bss - start_address_of_text.
7621 Remainder is the blob to be loaded contiguously
7622 from start address. */
7623
7624 info.fw_base = be32_to_cpu(fw_data[1]);
7625 cpu_scratch_size = tp->fw_len;
7626 info.fw_len = tp->fw->size - 12;
7627 info.fw_data = &fw_data[3];
7628
Linus Torvalds1da177e2005-04-16 15:20:36 -07007629 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007630 cpu_base = RX_CPU_BASE;
7631 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007632 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007633 cpu_base = TX_CPU_BASE;
7634 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
7635 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
7636 }
7637
7638 err = tg3_load_firmware_cpu(tp, cpu_base,
7639 cpu_scratch_base, cpu_scratch_size,
7640 &info);
7641 if (err)
7642 return err;
7643
7644 /* Now startup the cpu. */
7645 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007646 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007647
7648 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007649 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007650 break;
7651 tw32(cpu_base + CPU_STATE, 0xffffffff);
7652 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007653 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007654 udelay(1000);
7655 }
7656 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007657 netdev_err(tp->dev,
7658 "%s fails to set CPU PC, is %08x should be %08x\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007659 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007660 return -ENODEV;
7661 }
7662 tw32(cpu_base + CPU_STATE, 0xffffffff);
7663 tw32_f(cpu_base + CPU_MODE, 0x00000000);
7664 return 0;
7665}
7666
Linus Torvalds1da177e2005-04-16 15:20:36 -07007667
Linus Torvalds1da177e2005-04-16 15:20:36 -07007668static int tg3_set_mac_addr(struct net_device *dev, void *p)
7669{
7670 struct tg3 *tp = netdev_priv(dev);
7671 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007672 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007673
Michael Chanf9804dd2005-09-27 12:13:10 -07007674 if (!is_valid_ether_addr(addr->sa_data))
7675 return -EINVAL;
7676
Linus Torvalds1da177e2005-04-16 15:20:36 -07007677 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7678
Michael Chane75f7c92006-03-20 21:33:26 -08007679 if (!netif_running(dev))
7680 return 0;
7681
Michael Chan58712ef2006-04-29 18:58:01 -07007682 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007683 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007684
Michael Chan986e0ae2007-05-05 12:10:20 -07007685 addr0_high = tr32(MAC_ADDR_0_HIGH);
7686 addr0_low = tr32(MAC_ADDR_0_LOW);
7687 addr1_high = tr32(MAC_ADDR_1_HIGH);
7688 addr1_low = tr32(MAC_ADDR_1_LOW);
7689
7690 /* Skip MAC addr 1 if ASF is using it. */
7691 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7692 !(addr1_high == 0 && addr1_low == 0))
7693 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007694 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007695 spin_lock_bh(&tp->lock);
7696 __tg3_set_mac_addr(tp, skip_mac_1);
7697 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007698
Michael Chanb9ec6c12006-07-25 16:37:27 -07007699 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007700}
7701
7702/* tp->lock is held. */
7703static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7704 dma_addr_t mapping, u32 maxlen_flags,
7705 u32 nic_addr)
7706{
7707 tg3_write_mem(tp,
7708 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7709 ((u64) mapping >> 32));
7710 tg3_write_mem(tp,
7711 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7712 ((u64) mapping & 0xffffffff));
7713 tg3_write_mem(tp,
7714 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7715 maxlen_flags);
7716
7717 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7718 tg3_write_mem(tp,
7719 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7720 nic_addr);
7721}
7722
7723static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007724static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007725{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007726 int i;
7727
Matt Carlson19cfaec2009-12-03 08:36:20 +00007728 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007729 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7730 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7731 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007732 } else {
7733 tw32(HOSTCC_TXCOL_TICKS, 0);
7734 tw32(HOSTCC_TXMAX_FRAMES, 0);
7735 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007736 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007737
Matt Carlson20d73752010-07-11 09:31:41 +00007738 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007739 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
7740 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
7741 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
7742 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007743 tw32(HOSTCC_RXCOL_TICKS, 0);
7744 tw32(HOSTCC_RXMAX_FRAMES, 0);
7745 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07007746 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007747
David S. Miller15f98502005-05-18 22:49:26 -07007748 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7749 u32 val = ec->stats_block_coalesce_usecs;
7750
Matt Carlsonb6080e12009-09-01 13:12:00 +00007751 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
7752 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
7753
David S. Miller15f98502005-05-18 22:49:26 -07007754 if (!netif_carrier_ok(tp->dev))
7755 val = 0;
7756
7757 tw32(HOSTCC_STAT_COAL_TICKS, val);
7758 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007759
7760 for (i = 0; i < tp->irq_cnt - 1; i++) {
7761 u32 reg;
7762
7763 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
7764 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007765 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
7766 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007767 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
7768 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007769
7770 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7771 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
7772 tw32(reg, ec->tx_coalesce_usecs);
7773 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
7774 tw32(reg, ec->tx_max_coalesced_frames);
7775 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
7776 tw32(reg, ec->tx_max_coalesced_frames_irq);
7777 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007778 }
7779
7780 for (; i < tp->irq_max - 1; i++) {
7781 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007782 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007783 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007784
7785 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7786 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
7787 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
7788 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7789 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007790 }
David S. Miller15f98502005-05-18 22:49:26 -07007791}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007792
7793/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00007794static void tg3_rings_reset(struct tg3 *tp)
7795{
7796 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007797 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007798 struct tg3_napi *tnapi = &tp->napi[0];
7799
7800 /* Disable all transmit rings but the first. */
7801 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7802 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlson0a58d662011-04-05 14:22:45 +00007803 else if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlson3d377282010-10-14 10:37:39 +00007804 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlsonb703df62009-12-03 08:36:21 +00007805 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
7806 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007807 else
7808 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7809
7810 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7811 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
7812 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
7813 BDINFO_FLAGS_DISABLED);
7814
7815
7816 /* Disable all receive return rings but the first. */
Matt Carlson0a58d662011-04-05 14:22:45 +00007817 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007818 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
7819 else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007820 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007821 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7822 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00007823 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
7824 else
7825 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7826
7827 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7828 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
7829 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
7830 BDINFO_FLAGS_DISABLED);
7831
7832 /* Disable interrupts */
7833 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
7834
7835 /* Zero mailbox registers. */
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007836 if (tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00007837 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007838 tp->napi[i].tx_prod = 0;
7839 tp->napi[i].tx_cons = 0;
Matt Carlsonc2353a32010-01-20 16:58:08 +00007840 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
7841 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007842 tw32_rx_mbox(tp->napi[i].consmbox, 0);
7843 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
7844 }
Matt Carlsonc2353a32010-01-20 16:58:08 +00007845 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))
7846 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007847 } else {
7848 tp->napi[0].tx_prod = 0;
7849 tp->napi[0].tx_cons = 0;
7850 tw32_mailbox(tp->napi[0].prodmbox, 0);
7851 tw32_rx_mbox(tp->napi[0].consmbox, 0);
7852 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007853
7854 /* Make sure the NIC-based send BD rings are disabled. */
7855 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7856 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
7857 for (i = 0; i < 16; i++)
7858 tw32_tx_mbox(mbox + i * 8, 0);
7859 }
7860
7861 txrcb = NIC_SRAM_SEND_RCB;
7862 rxrcb = NIC_SRAM_RCV_RET_RCB;
7863
7864 /* Clear status block in ram. */
7865 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7866
7867 /* Set status block DMA address */
7868 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7869 ((u64) tnapi->status_mapping >> 32));
7870 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7871 ((u64) tnapi->status_mapping & 0xffffffff));
7872
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007873 if (tnapi->tx_ring) {
7874 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7875 (TG3_TX_RING_SIZE <<
7876 BDINFO_FLAGS_MAXLEN_SHIFT),
7877 NIC_SRAM_TX_BUFFER_DESC);
7878 txrcb += TG3_BDINFO_SIZE;
7879 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007880
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007881 if (tnapi->rx_rcb) {
7882 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007883 (tp->rx_ret_ring_mask + 1) <<
7884 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007885 rxrcb += TG3_BDINFO_SIZE;
7886 }
7887
7888 stblk = HOSTCC_STATBLCK_RING1;
7889
7890 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
7891 u64 mapping = (u64)tnapi->status_mapping;
7892 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
7893 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
7894
7895 /* Clear status block in ram. */
7896 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7897
Matt Carlson19cfaec2009-12-03 08:36:20 +00007898 if (tnapi->tx_ring) {
7899 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7900 (TG3_TX_RING_SIZE <<
7901 BDINFO_FLAGS_MAXLEN_SHIFT),
7902 NIC_SRAM_TX_BUFFER_DESC);
7903 txrcb += TG3_BDINFO_SIZE;
7904 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007905
7906 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007907 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007908 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7909
7910 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007911 rxrcb += TG3_BDINFO_SIZE;
7912 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007913}
7914
7915/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007916static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007917{
7918 u32 val, rdmac_mode;
7919 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00007920 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007921
7922 tg3_disable_ints(tp);
7923
7924 tg3_stop_fw(tp);
7925
7926 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
7927
Matt Carlson859a588792010-04-05 10:19:28 +00007928 if (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)
Michael Chane6de8ad2005-05-05 14:42:41 -07007929 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007930
Matt Carlson699c0192010-12-06 08:28:51 +00007931 /* Enable MAC control of LPI */
7932 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
7933 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
7934 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
7935 TG3_CPMU_EEE_LNKIDL_UART_IDL);
7936
7937 tw32_f(TG3_CPMU_EEE_CTRL,
7938 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
7939
Matt Carlsona386b902010-12-06 08:28:53 +00007940 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
7941 TG3_CPMU_EEEMD_LPI_IN_TX |
7942 TG3_CPMU_EEEMD_LPI_IN_RX |
7943 TG3_CPMU_EEEMD_EEE_ENABLE;
7944
7945 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
7946 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
7947
7948 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
7949 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
7950
7951 tw32_f(TG3_CPMU_EEE_MODE, val);
7952
7953 tw32_f(TG3_CPMU_EEE_DBTMR1,
7954 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
7955 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
7956
7957 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00007958 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00007959 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00007960 }
7961
Matt Carlson603f1172010-02-12 14:47:10 +00007962 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08007963 tg3_phy_reset(tp);
7964
Linus Torvalds1da177e2005-04-16 15:20:36 -07007965 err = tg3_chip_reset(tp);
7966 if (err)
7967 return err;
7968
7969 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
7970
Matt Carlsonbcb37f62008-11-03 16:52:09 -08007971 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007972 val = tr32(TG3_CPMU_CTRL);
7973 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
7974 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08007975
7976 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7977 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7978 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7979 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
7980
7981 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
7982 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
7983 val |= CPMU_LNK_AWARE_MACCLK_6_25;
7984 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
7985
7986 val = tr32(TG3_CPMU_HST_ACC);
7987 val &= ~CPMU_HST_ACC_MACCLK_MASK;
7988 val |= CPMU_HST_ACC_MACCLK_6_25;
7989 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07007990 }
7991
Matt Carlson33466d932009-04-20 06:57:41 +00007992 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7993 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
7994 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
7995 PCIE_PWR_MGMT_L1_THRESH_4MS;
7996 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00007997
7998 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
7999 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
8000
8001 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d932009-04-20 06:57:41 +00008002
Matt Carlsonf40386c2009-11-02 14:24:02 +00008003 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8004 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00008005 }
8006
Matt Carlson614b0592010-01-20 16:58:02 +00008007 if (tp->tg3_flags3 & TG3_FLG3_L1PLLPD_EN) {
8008 u32 grc_mode = tr32(GRC_MODE);
8009
8010 /* Access the lower 1K of PL PCIE block registers. */
8011 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8012 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
8013
8014 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
8015 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
8016 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
8017
8018 tw32(GRC_MODE, grc_mode);
8019 }
8020
Matt Carlson5093eed2010-11-24 08:31:45 +00008021 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
8022 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
8023 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00008024
Matt Carlson5093eed2010-11-24 08:31:45 +00008025 /* Access the lower 1K of PL PCIE block registers. */
8026 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8027 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00008028
Matt Carlson5093eed2010-11-24 08:31:45 +00008029 val = tr32(TG3_PCIE_TLDLPL_PORT +
8030 TG3_PCIE_PL_LO_PHYCTL5);
8031 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
8032 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00008033
Matt Carlson5093eed2010-11-24 08:31:45 +00008034 tw32(GRC_MODE, grc_mode);
8035 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00008036
8037 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8038 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8039 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8040 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008041 }
8042
Linus Torvalds1da177e2005-04-16 15:20:36 -07008043 /* This works around an issue with Athlon chipsets on
8044 * B3 tigon3 silicon. This bit has no effect on any
8045 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008046 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008047 */
Matt Carlson795d01c2007-10-07 23:28:17 -07008048 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)) {
8049 if (!(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
8050 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8051 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008053
8054 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
8055 (tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
8056 val = tr32(TG3PCI_PCISTATE);
8057 val |= PCISTATE_RETRY_SAME_DMA;
8058 tw32(TG3PCI_PCISTATE, val);
8059 }
8060
Matt Carlson0d3031d2007-10-10 18:02:43 -07008061 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
8062 /* Allow reads and writes to the
8063 * APE register and memory space.
8064 */
8065 val = tr32(TG3PCI_PCISTATE);
8066 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008067 PCISTATE_ALLOW_APE_SHMEM_WR |
8068 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008069 tw32(TG3PCI_PCISTATE, val);
8070 }
8071
Linus Torvalds1da177e2005-04-16 15:20:36 -07008072 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8073 /* Enable some hw fixes. */
8074 val = tr32(TG3PCI_MSI_DATA);
8075 val |= (1 << 26) | (1 << 28) | (1 << 29);
8076 tw32(TG3PCI_MSI_DATA, val);
8077 }
8078
8079 /* Descriptor ring init may make accesses to the
8080 * NIC SRAM area to setup the TX descriptors, so we
8081 * can only do this after the hardware has been
8082 * successfully reset.
8083 */
Michael Chan32d8c572006-07-25 16:38:29 -07008084 err = tg3_init_rings(tp);
8085 if (err)
8086 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008087
Matt Carlson1407deb2011-04-05 14:22:44 +00008088 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008089 val = tr32(TG3PCI_DMA_RW_CTRL) &
8090 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008091 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8092 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008093 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8094 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8095 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008096 /* This value is determined during the probe time DMA
8097 * engine test, tg3_test_dma.
8098 */
8099 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008101
8102 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8103 GRC_MODE_4X_NIC_SEND_RINGS |
8104 GRC_MODE_NO_TX_PHDR_CSUM |
8105 GRC_MODE_NO_RX_PHDR_CSUM);
8106 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07008107
8108 /* Pseudo-header checksum is done by hardware logic and not
8109 * the offload processers, so make the chip do the pseudo-
8110 * header checksums on receive. For transmit it is more
8111 * convenient to do the pseudo-header checksum in software
8112 * as Linux does that on transmit for us in all cases.
8113 */
8114 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008115
8116 tw32(GRC_MODE,
8117 tp->grc_mode |
8118 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8119
8120 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8121 val = tr32(GRC_MISC_CFG);
8122 val &= ~0xff;
8123 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8124 tw32(GRC_MISC_CFG, val);
8125
8126 /* Initialize MBUF/DESC pool. */
John W. Linvillecbf46852005-04-21 17:01:29 -07008127 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008128 /* Do nothing. */
8129 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8130 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8131 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8132 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8133 else
8134 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8135 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8136 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Matt Carlson859a588792010-04-05 10:19:28 +00008137 } else if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008138 int fw_len;
8139
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008140 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008141 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8142 tw32(BUFMGR_MB_POOL_ADDR,
8143 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8144 tw32(BUFMGR_MB_POOL_SIZE,
8145 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008147
Michael Chan0f893dc2005-07-25 12:30:38 -07008148 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008149 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8150 tp->bufmgr_config.mbuf_read_dma_low_water);
8151 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8152 tp->bufmgr_config.mbuf_mac_rx_low_water);
8153 tw32(BUFMGR_MB_HIGH_WATER,
8154 tp->bufmgr_config.mbuf_high_water);
8155 } else {
8156 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8157 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8158 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8159 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8160 tw32(BUFMGR_MB_HIGH_WATER,
8161 tp->bufmgr_config.mbuf_high_water_jumbo);
8162 }
8163 tw32(BUFMGR_DMA_LOW_WATER,
8164 tp->bufmgr_config.dma_low_water);
8165 tw32(BUFMGR_DMA_HIGH_WATER,
8166 tp->bufmgr_config.dma_high_water);
8167
Matt Carlsond309a462010-09-30 10:34:31 +00008168 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8169 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8170 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
8171 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008172 for (i = 0; i < 2000; i++) {
8173 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8174 break;
8175 udelay(10);
8176 }
8177 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008178 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008179 return -ENODEV;
8180 }
8181
8182 /* Setup replenish threshold. */
Michael Chanf92905d2006-06-29 20:14:29 -07008183 val = tp->rx_pending / 8;
8184 if (val == 0)
8185 val = 1;
8186 else if (val > tp->rx_std_max_post)
8187 val = tp->rx_std_max_post;
Michael Chanb5d37722006-09-27 16:06:21 -07008188 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
8189 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8190 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
8191
8192 if (val > (TG3_RX_INTERNAL_RING_SZ_5906 / 2))
8193 val = TG3_RX_INTERNAL_RING_SZ_5906 / 2;
8194 }
Michael Chanf92905d2006-06-29 20:14:29 -07008195
8196 tw32(RCVBDI_STD_THRESH, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008197
8198 /* Initialize TG3_BDINFO's at:
8199 * RCVDBDI_STD_BD: standard eth size rx ring
8200 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8201 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8202 *
8203 * like so:
8204 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8205 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8206 * ring attribute flags
8207 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8208 *
8209 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8210 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8211 *
8212 * The size of each ring is fixed in the firmware, but the location is
8213 * configurable.
8214 */
8215 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008216 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008217 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008218 ((u64) tpr->rx_std_mapping & 0xffffffff));
Matt Carlson0a58d662011-04-05 14:22:45 +00008219 if (!(tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00008220 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8221 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008222
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008223 /* Disable the mini ring */
8224 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008225 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8226 BDINFO_FLAGS_DISABLED);
8227
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008228 /* Program the jumbo buffer descriptor ring control
8229 * blocks on those devices that have them.
8230 */
Matt Carlsonbb18bb92011-03-09 16:58:19 +00008231 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson4d163b72011-01-25 15:58:48 +00008232 ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
8233 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008234 /* Setup replenish threshold. */
8235 tw32(RCVBDI_JUMBO_THRESH, tp->rx_jumbo_pending / 8);
8236
Michael Chan0f893dc2005-07-25 12:30:38 -07008237 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008238 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008239 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008240 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008241 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008242 val = TG3_RX_JMB_RING_SIZE(tp) <<
8243 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008244 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008245 val | BDINFO_FLAGS_USE_EXT_RECV);
Matt Carlsona50d0792010-06-05 17:24:37 +00008246 if (!(tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) ||
8247 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson87668d32009-11-13 13:03:34 +00008248 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8249 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008250 } else {
8251 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8252 BDINFO_FLAGS_DISABLED);
8253 }
8254
Matt Carlson1407deb2011-04-05 14:22:44 +00008255 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008256 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsonde9f5232011-04-05 14:22:43 +00008257 val = TG3_RX_STD_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008258 else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008259 val = TG3_RX_STD_MAX_SIZE_5717;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008260 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8261 val |= (TG3_RX_STD_DMA_SZ << 2);
8262 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008263 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008264 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008265 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008266
8267 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008268
Matt Carlson411da642009-11-13 13:03:46 +00008269 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e662009-11-13 13:03:49 +00008270 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008271
Matt Carlson411da642009-11-13 13:03:46 +00008272 tpr->rx_jmb_prod_idx = (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) ?
Matt Carlson21f581a2009-08-28 14:00:25 +00008273 tp->rx_jumbo_pending : 0;
Matt Carlson66711e662009-11-13 13:03:49 +00008274 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008275
Matt Carlson1407deb2011-04-05 14:22:44 +00008276 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008277 tw32(STD_REPLENISH_LWM, 32);
8278 tw32(JMB_REPLENISH_LWM, 16);
8279 }
8280
Matt Carlson2d31eca2009-09-01 12:53:31 +00008281 tg3_rings_reset(tp);
8282
Linus Torvalds1da177e2005-04-16 15:20:36 -07008283 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008284 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008285
8286 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008287 tw32(MAC_RX_MTU_SIZE,
8288 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008289
8290 /* The slot time is changed by tg3_setup_phy if we
8291 * run at gigabit with half duplex.
8292 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00008293 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8294 (6 << TX_LENGTHS_IPG_SHIFT) |
8295 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8296
8297 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8298 val |= tr32(MAC_TX_LENGTHS) &
8299 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8300 TX_LENGTHS_CNT_DWN_VAL_MSK);
8301
8302 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008303
8304 /* Receive rules. */
8305 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8306 tw32(RCVLPC_CONFIG, 0x0181);
8307
8308 /* Calculate RDMAC_MODE setting early, we need it to determine
8309 * the RCVLPC_STATE_ENABLE mask.
8310 */
8311 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8312 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8313 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8314 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8315 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008316
Matt Carlsondeabaac2010-11-24 08:31:50 +00008317 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008318 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8319
Matt Carlson57e69832008-05-25 23:48:31 -07008320 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008321 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8322 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008323 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8324 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8325 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8326
Matt Carlsonc5908932011-03-09 16:58:25 +00008327 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8328 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008329 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008330 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008331 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8332 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
8333 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
8334 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8335 }
8336 }
8337
Michael Chan85e94ce2005-04-21 17:05:28 -07008338 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
8339 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8340
Linus Torvalds1da177e2005-04-16 15:20:36 -07008341 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlson027455a2008-12-21 20:19:30 -08008342 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8343
Matt Carlsone849cdc2009-11-13 13:03:38 +00008344 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
8345 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008346 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8347 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008348
Matt Carlsonf2096f92011-04-05 14:22:48 +00008349 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8350 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8351
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008352 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8353 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8354 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8355 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson1407deb2011-04-05 14:22:44 +00008356 (tp->tg3_flags3 & TG3_FLG3_57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008357 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00008358 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8359 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008360 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8361 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8362 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8363 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8364 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8365 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008366 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008367 tw32(TG3_RDMA_RSRVCTRL_REG,
8368 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8369 }
8370
Matt Carlsond78b59f2011-04-05 14:22:46 +00008371 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8372 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00008373 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8374 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8375 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8376 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8377 }
8378
Linus Torvalds1da177e2005-04-16 15:20:36 -07008379 /* Receive/send statistics. */
Michael Chan16613942006-06-29 20:15:13 -07008380 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
8381 val = tr32(RCVLPC_STATS_ENABLE);
8382 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8383 tw32(RCVLPC_STATS_ENABLE, val);
8384 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
8385 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008386 val = tr32(RCVLPC_STATS_ENABLE);
8387 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8388 tw32(RCVLPC_STATS_ENABLE, val);
8389 } else {
8390 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8391 }
8392 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8393 tw32(SNDDATAI_STATSENAB, 0xffffff);
8394 tw32(SNDDATAI_STATSCTRL,
8395 (SNDDATAI_SCTRL_ENABLE |
8396 SNDDATAI_SCTRL_FASTUPD));
8397
8398 /* Setup host coalescing engine. */
8399 tw32(HOSTCC_MODE, 0);
8400 for (i = 0; i < 2000; i++) {
8401 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8402 break;
8403 udelay(10);
8404 }
8405
Michael Chand244c892005-07-05 14:42:33 -07008406 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008407
Linus Torvalds1da177e2005-04-16 15:20:36 -07008408 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
8409 /* Status/statistics block address. See tg3_timer,
8410 * the tg3_periodic_fetch_stats call there, and
8411 * tg3_get_stats to see how this works for 5705/5750 chips.
8412 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008413 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8414 ((u64) tp->stats_mapping >> 32));
8415 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8416 ((u64) tp->stats_mapping & 0xffffffff));
8417 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008418
Linus Torvalds1da177e2005-04-16 15:20:36 -07008419 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008420
8421 /* Clear statistics and status block memory areas */
8422 for (i = NIC_SRAM_STATS_BLK;
8423 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8424 i += sizeof(u32)) {
8425 tg3_write_mem(tp, i, 0);
8426 udelay(40);
8427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008428 }
8429
8430 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8431
8432 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8433 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
8434 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8435 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8436
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008437 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8438 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07008439 /* reset to prevent losing 1st rx packet intermittently */
8440 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8441 udelay(10);
8442 }
8443
Matt Carlson3bda1252008-08-15 14:08:22 -07008444 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008445 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -07008446 else
8447 tp->mac_mode = 0;
8448 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07008449 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008450 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008451 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008452 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8453 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008454 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8455 udelay(40);
8456
Michael Chan314fba32005-04-21 17:07:04 -07008457 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Michael Chan9d26e212006-12-07 00:21:14 -08008458 * If TG3_FLG2_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008459 * register to preserve the GPIO settings for LOMs. The GPIOs,
8460 * whether used as inputs or outputs, are set by boot code after
8461 * reset.
8462 */
Michael Chan9d26e212006-12-07 00:21:14 -08008463 if (!(tp->tg3_flags2 & TG3_FLG2_IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008464 u32 gpio_mask;
8465
Michael Chan9d26e212006-12-07 00:21:14 -08008466 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8467 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8468 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008469
8470 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8471 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8472 GRC_LCLCTRL_GPIO_OUTPUT3;
8473
Michael Chanaf36e6b2006-03-23 01:28:06 -08008474 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8475 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8476
Gary Zambranoaaf84462007-05-05 11:51:45 -07008477 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008478 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8479
8480 /* GPIO1 must be driven high for eeprom write protect */
Michael Chan9d26e212006-12-07 00:21:14 -08008481 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT)
8482 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8483 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008485 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8486 udelay(100);
8487
Matt Carlson0583d522011-01-25 15:58:50 +00008488 if ((tp->tg3_flags2 & TG3_FLG2_USING_MSIX) &&
8489 tp->irq_cnt > 1) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008490 val = tr32(MSGINT_MODE);
8491 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
8492 tw32(MSGINT_MODE, val);
8493 }
8494
Linus Torvalds1da177e2005-04-16 15:20:36 -07008495 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
8496 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8497 udelay(40);
8498 }
8499
8500 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8501 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8502 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8503 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8504 WDMAC_MODE_LNGREAD_ENAB);
8505
Matt Carlsonc5908932011-03-09 16:58:25 +00008506 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8507 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Matt Carlson29ea0952009-08-25 10:07:54 +00008508 if ((tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008509 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8510 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8511 /* nothing */
8512 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Matt Carlsonc5908932011-03-09 16:58:25 +00008513 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008514 val |= WDMAC_MODE_RX_ACCEL;
8515 }
8516 }
8517
Michael Chand9ab5ad12006-03-20 22:27:35 -08008518 /* Enable host coalescing bug fix */
Matt Carlson321d32a2008-11-21 17:22:19 -08008519 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlsonf51f3562008-05-25 23:45:08 -07008520 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -08008521
Matt Carlson788a0352009-11-02 14:26:03 +00008522 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8523 val |= WDMAC_MODE_BURST_ALL_DATA;
8524
Linus Torvalds1da177e2005-04-16 15:20:36 -07008525 tw32_f(WDMAC_MODE, val);
8526 udelay(40);
8527
Matt Carlson9974a352007-10-07 23:27:28 -07008528 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
8529 u16 pcix_cmd;
8530
8531 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8532 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008533 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008534 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8535 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008536 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008537 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8538 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008539 }
Matt Carlson9974a352007-10-07 23:27:28 -07008540 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8541 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008542 }
8543
8544 tw32_f(RDMAC_MODE, rdmac_mode);
8545 udelay(40);
8546
8547 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
8548 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8549 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008550
8551 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8552 tw32(SNDDATAC_MODE,
8553 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8554 else
8555 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8556
Linus Torvalds1da177e2005-04-16 15:20:36 -07008557 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8558 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008559 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Matt Carlsonde9f5232011-04-05 14:22:43 +00008560 if (tp->tg3_flags3 & TG3_FLG3_LRG_PROD_RING_CAP)
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008561 val |= RCVDBDI_MODE_LRG_RING_SZ;
8562 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008563 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008564 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
8565 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008566 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Matt Carlson19cfaec2009-12-03 08:36:20 +00008567 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008568 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8569 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008570 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8571
8572 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8573 err = tg3_load_5701_a0_firmware_fix(tp);
8574 if (err)
8575 return err;
8576 }
8577
Linus Torvalds1da177e2005-04-16 15:20:36 -07008578 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
8579 err = tg3_load_tso_firmware(tp);
8580 if (err)
8581 return err;
8582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008583
8584 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008585
Matt Carlsonb1d05212010-06-05 17:24:31 +00008586 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
8587 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8588 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008589
8590 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8591 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
8592 tp->tx_mode &= ~val;
8593 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
8594 }
8595
Linus Torvalds1da177e2005-04-16 15:20:36 -07008596 tw32_f(MAC_TX_MODE, tp->tx_mode);
8597 udelay(100);
8598
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008599 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) {
8600 u32 reg = MAC_RSS_INDIR_TBL_0;
8601 u8 *ent = (u8 *)&val;
8602
8603 /* Setup the indirection table */
8604 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8605 int idx = i % sizeof(val);
8606
Matt Carlson5efeeea2010-07-11 09:31:40 +00008607 ent[idx] = i % (tp->irq_cnt - 1);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008608 if (idx == sizeof(val) - 1) {
8609 tw32(reg, val);
8610 reg += 4;
8611 }
8612 }
8613
8614 /* Setup the "secret" hash key. */
8615 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8616 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8617 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8618 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8619 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8620 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8621 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8622 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8623 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8624 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8625 }
8626
Linus Torvalds1da177e2005-04-16 15:20:36 -07008627 tp->rx_mode = RX_MODE_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08008628 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chanaf36e6b2006-03-23 01:28:06 -08008629 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8630
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008631 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)
8632 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8633 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8634 RX_MODE_RSS_IPV6_HASH_EN |
8635 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8636 RX_MODE_RSS_IPV4_HASH_EN |
8637 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8638
Linus Torvalds1da177e2005-04-16 15:20:36 -07008639 tw32_f(MAC_RX_MODE, tp->rx_mode);
8640 udelay(10);
8641
Linus Torvalds1da177e2005-04-16 15:20:36 -07008642 tw32(MAC_LED_CTRL, tp->led_ctrl);
8643
8644 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008645 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008646 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8647 udelay(10);
8648 }
8649 tw32_f(MAC_RX_MODE, tp->rx_mode);
8650 udelay(10);
8651
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008652 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008653 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008654 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008655 /* Set drive transmission level to 1.2V */
8656 /* only if the signal pre-emphasis bit is not set */
8657 val = tr32(MAC_SERDES_CFG);
8658 val &= 0xfffff000;
8659 val |= 0x880;
8660 tw32(MAC_SERDES_CFG, val);
8661 }
8662 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8663 tw32(MAC_SERDES_CFG, 0x616000);
8664 }
8665
8666 /* Prevent chip from dropping frames when flow control
8667 * is enabled.
8668 */
Matt Carlson666bc832010-01-20 16:58:03 +00008669 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8670 val = 1;
8671 else
8672 val = 2;
8673 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008674
8675 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008676 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008677 /* Use hardware link auto-negotiation */
8678 tp->tg3_flags2 |= TG3_FLG2_HW_AUTONEG;
8679 }
8680
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008681 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Michael Chand4d2c552006-03-20 17:47:20 -08008682 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)) {
8683 u32 tmp;
8684
8685 tmp = tr32(SERDES_RX_CTRL);
8686 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
8687 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
8688 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
8689 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8690 }
8691
Matt Carlsondd477002008-05-25 23:45:58 -07008692 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
Matt Carlson80096062010-08-02 11:26:06 +00008693 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
8694 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07008695 tp->link_config.speed = tp->link_config.orig_speed;
8696 tp->link_config.duplex = tp->link_config.orig_duplex;
8697 tp->link_config.autoneg = tp->link_config.orig_autoneg;
8698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008699
Matt Carlsondd477002008-05-25 23:45:58 -07008700 err = tg3_setup_phy(tp, 0);
8701 if (err)
8702 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008703
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008704 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
8705 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07008706 u32 tmp;
8707
8708 /* Clear CRC stats. */
8709 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
8710 tg3_writephy(tp, MII_TG3_TEST1,
8711 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00008712 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07008713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008714 }
8715 }
8716
8717 __tg3_set_rx_mode(tp->dev);
8718
8719 /* Initialize receive rules. */
8720 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
8721 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
8722 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
8723 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
8724
Michael Chan4cf78e42005-07-25 12:29:19 -07008725 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Michael Chana4e2b342005-10-26 15:46:52 -07008726 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008727 limit = 8;
8728 else
8729 limit = 16;
8730 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
8731 limit -= 4;
8732 switch (limit) {
8733 case 16:
8734 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
8735 case 15:
8736 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
8737 case 14:
8738 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
8739 case 13:
8740 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
8741 case 12:
8742 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
8743 case 11:
8744 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
8745 case 10:
8746 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
8747 case 9:
8748 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
8749 case 8:
8750 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
8751 case 7:
8752 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
8753 case 6:
8754 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
8755 case 5:
8756 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
8757 case 4:
8758 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
8759 case 3:
8760 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
8761 case 2:
8762 case 1:
8763
8764 default:
8765 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008767
Matt Carlson9ce768e2007-10-11 19:49:11 -07008768 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
8769 /* Write our heartbeat update interval to APE. */
8770 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
8771 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07008772
Linus Torvalds1da177e2005-04-16 15:20:36 -07008773 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
8774
Linus Torvalds1da177e2005-04-16 15:20:36 -07008775 return 0;
8776}
8777
8778/* Called at device open time to get the chip ready for
8779 * packet processing. Invoked with tp->lock held.
8780 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008781static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008782{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008783 tg3_switch_clocks(tp);
8784
8785 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
8786
Matt Carlson2f751b62008-08-04 23:17:34 -07008787 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008788}
8789
8790#define TG3_STAT_ADD32(PSTAT, REG) \
8791do { u32 __val = tr32(REG); \
8792 (PSTAT)->low += __val; \
8793 if ((PSTAT)->low < __val) \
8794 (PSTAT)->high += 1; \
8795} while (0)
8796
8797static void tg3_periodic_fetch_stats(struct tg3 *tp)
8798{
8799 struct tg3_hw_stats *sp = tp->hw_stats;
8800
8801 if (!netif_carrier_ok(tp->dev))
8802 return;
8803
8804 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
8805 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
8806 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
8807 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
8808 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
8809 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
8810 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
8811 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
8812 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
8813 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
8814 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
8815 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
8816 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
8817
8818 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
8819 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
8820 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
8821 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
8822 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
8823 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
8824 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
8825 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
8826 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
8827 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
8828 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
8829 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
8830 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
8831 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07008832
8833 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
8834 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
8835 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008836}
8837
8838static void tg3_timer(unsigned long __opaque)
8839{
8840 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008841
Michael Chanf475f162006-03-27 23:20:14 -08008842 if (tp->irq_sync)
8843 goto restart_timer;
8844
David S. Millerf47c11e2005-06-24 20:18:35 -07008845 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008846
David S. Millerfac9b832005-05-18 22:46:34 -07008847 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
8848 /* All of this garbage is because when using non-tagged
8849 * IRQ status the mailbox/status_block protocol the chip
8850 * uses with the cpu is race prone.
8851 */
Matt Carlson898a56f2009-08-28 14:02:40 +00008852 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07008853 tw32(GRC_LOCAL_CTRL,
8854 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
8855 } else {
8856 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008857 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07008858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008859
David S. Millerfac9b832005-05-18 22:46:34 -07008860 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
8861 tp->tg3_flags2 |= TG3_FLG2_RESTART_TIMER;
David S. Millerf47c11e2005-06-24 20:18:35 -07008862 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07008863 schedule_work(&tp->reset_task);
8864 return;
8865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008866 }
8867
Linus Torvalds1da177e2005-04-16 15:20:36 -07008868 /* This part only runs once per second. */
8869 if (!--tp->timer_counter) {
David S. Millerfac9b832005-05-18 22:46:34 -07008870 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
8871 tg3_periodic_fetch_stats(tp);
8872
Matt Carlson52b02d02010-10-14 10:37:41 +00008873 if (tp->setlpicnt && !--tp->setlpicnt) {
8874 u32 val = tr32(TG3_CPMU_EEE_MODE);
8875 tw32(TG3_CPMU_EEE_MODE,
8876 val | TG3_CPMU_EEEMD_LPI_ENABLE);
8877 }
8878
Linus Torvalds1da177e2005-04-16 15:20:36 -07008879 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
8880 u32 mac_stat;
8881 int phy_event;
8882
8883 mac_stat = tr32(MAC_STATUS);
8884
8885 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008886 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008887 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
8888 phy_event = 1;
8889 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
8890 phy_event = 1;
8891
8892 if (phy_event)
8893 tg3_setup_phy(tp, 0);
8894 } else if (tp->tg3_flags & TG3_FLAG_POLL_SERDES) {
8895 u32 mac_stat = tr32(MAC_STATUS);
8896 int need_setup = 0;
8897
8898 if (netif_carrier_ok(tp->dev) &&
8899 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
8900 need_setup = 1;
8901 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00008902 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008903 (mac_stat & (MAC_STATUS_PCS_SYNCED |
8904 MAC_STATUS_SIGNAL_DET))) {
8905 need_setup = 1;
8906 }
8907 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07008908 if (!tp->serdes_counter) {
8909 tw32_f(MAC_MODE,
8910 (tp->mac_mode &
8911 ~MAC_MODE_PORT_MODE_MASK));
8912 udelay(40);
8913 tw32_f(MAC_MODE, tp->mac_mode);
8914 udelay(40);
8915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008916 tg3_setup_phy(tp, 0);
8917 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008918 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson2138c002010-07-11 09:31:43 +00008919 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07008920 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00008921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008922
8923 tp->timer_counter = tp->timer_multiplier;
8924 }
8925
Michael Chan130b8e42006-09-27 16:00:40 -07008926 /* Heartbeat is only sent once every 2 seconds.
8927 *
8928 * The heartbeat is to tell the ASF firmware that the host
8929 * driver is still alive. In the event that the OS crashes,
8930 * ASF needs to reset the hardware to free up the FIFO space
8931 * that may be filled with rx packets destined for the host.
8932 * If the FIFO is full, ASF will no longer function properly.
8933 *
8934 * Unintended resets have been reported on real time kernels
8935 * where the timer doesn't run on time. Netpoll will also have
8936 * same problem.
8937 *
8938 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
8939 * to check the ring condition when the heartbeat is expiring
8940 * before doing the reset. This will prevent most unintended
8941 * resets.
8942 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008943 if (!--tp->asf_counter) {
Matt Carlsonbc7959b2008-08-15 14:08:55 -07008944 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
8945 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07008946 tg3_wait_for_event_ack(tp);
8947
Michael Chanbbadf502006-04-06 21:46:34 -07008948 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07008949 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07008950 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00008951 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
8952 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008953
8954 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008955 }
8956 tp->asf_counter = tp->asf_multiplier;
8957 }
8958
David S. Millerf47c11e2005-06-24 20:18:35 -07008959 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008960
Michael Chanf475f162006-03-27 23:20:14 -08008961restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07008962 tp->timer.expires = jiffies + tp->timer_offset;
8963 add_timer(&tp->timer);
8964}
8965
Matt Carlson4f125f42009-09-01 12:55:02 +00008966static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08008967{
David Howells7d12e782006-10-05 14:55:46 +01008968 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008969 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00008970 char *name;
8971 struct tg3_napi *tnapi = &tp->napi[irq_num];
8972
8973 if (tp->irq_cnt == 1)
8974 name = tp->dev->name;
8975 else {
8976 name = &tnapi->irq_lbl[0];
8977 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
8978 name[IFNAMSIZ-1] = 0;
8979 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008980
Matt Carlson679563f2009-09-01 12:55:46 +00008981 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08008982 fn = tg3_msi;
8983 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
8984 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00008985 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008986 } else {
8987 fn = tg3_interrupt;
8988 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
8989 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00008990 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008991 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008992
8993 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008994}
8995
Michael Chan79381092005-04-21 17:13:59 -07008996static int tg3_test_interrupt(struct tg3 *tp)
8997{
Matt Carlson09943a12009-08-28 14:01:57 +00008998 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07008999 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07009000 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009001 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07009002
Michael Chand4bc3922005-05-29 14:59:20 -07009003 if (!netif_running(dev))
9004 return -ENODEV;
9005
Michael Chan79381092005-04-21 17:13:59 -07009006 tg3_disable_ints(tp);
9007
Matt Carlson4f125f42009-09-01 12:55:02 +00009008 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009009
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009010 /*
9011 * Turn off MSI one shot mode. Otherwise this test has no
9012 * observable way to know whether the interrupt was delivered.
9013 */
Matt Carlson1407deb2011-04-05 14:22:44 +00009014 if ((tp->tg3_flags3 & TG3_FLG3_57765_PLUS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009015 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
9016 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
9017 tw32(MSGINT_MODE, val);
9018 }
9019
Matt Carlson4f125f42009-09-01 12:55:02 +00009020 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00009021 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009022 if (err)
9023 return err;
9024
Matt Carlson898a56f2009-08-28 14:02:40 +00009025 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07009026 tg3_enable_ints(tp);
9027
9028 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009029 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07009030
9031 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07009032 u32 int_mbox, misc_host_ctrl;
9033
Matt Carlson898a56f2009-08-28 14:02:40 +00009034 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07009035 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
9036
9037 if ((int_mbox != 0) ||
9038 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
9039 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07009040 break;
Michael Chanb16250e2006-09-27 16:10:14 -07009041 }
9042
Michael Chan79381092005-04-21 17:13:59 -07009043 msleep(10);
9044 }
9045
9046 tg3_disable_ints(tp);
9047
Matt Carlson4f125f42009-09-01 12:55:02 +00009048 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009049
Matt Carlson4f125f42009-09-01 12:55:02 +00009050 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009051
9052 if (err)
9053 return err;
9054
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009055 if (intr_ok) {
9056 /* Reenable MSI one shot mode. */
Matt Carlson1407deb2011-04-05 14:22:44 +00009057 if ((tp->tg3_flags3 & TG3_FLG3_57765_PLUS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009058 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
9059 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
9060 tw32(MSGINT_MODE, val);
9061 }
Michael Chan79381092005-04-21 17:13:59 -07009062 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009063 }
Michael Chan79381092005-04-21 17:13:59 -07009064
9065 return -EIO;
9066}
9067
9068/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
9069 * successfully restored
9070 */
9071static int tg3_test_msi(struct tg3 *tp)
9072{
Michael Chan79381092005-04-21 17:13:59 -07009073 int err;
9074 u16 pci_cmd;
9075
9076 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSI))
9077 return 0;
9078
9079 /* Turn off SERR reporting in case MSI terminates with Master
9080 * Abort.
9081 */
9082 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
9083 pci_write_config_word(tp->pdev, PCI_COMMAND,
9084 pci_cmd & ~PCI_COMMAND_SERR);
9085
9086 err = tg3_test_interrupt(tp);
9087
9088 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
9089
9090 if (!err)
9091 return 0;
9092
9093 /* other failures */
9094 if (err != -EIO)
9095 return err;
9096
9097 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009098 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
9099 "to INTx mode. Please report this failure to the PCI "
9100 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07009101
Matt Carlson4f125f42009-09-01 12:55:02 +00009102 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00009103
Michael Chan79381092005-04-21 17:13:59 -07009104 pci_disable_msi(tp->pdev);
9105
9106 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI;
Andre Detschdc8bf1b2010-04-26 07:27:07 +00009107 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07009108
Matt Carlson4f125f42009-09-01 12:55:02 +00009109 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009110 if (err)
9111 return err;
9112
9113 /* Need to reset the chip because the MSI cycle may have terminated
9114 * with Master Abort.
9115 */
David S. Millerf47c11e2005-06-24 20:18:35 -07009116 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009117
Michael Chan944d9802005-05-29 14:57:48 -07009118 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009119 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009120
David S. Millerf47c11e2005-06-24 20:18:35 -07009121 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009122
9123 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00009124 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07009125
9126 return err;
9127}
9128
Matt Carlson9e9fd122009-01-19 16:57:45 -08009129static int tg3_request_firmware(struct tg3 *tp)
9130{
9131 const __be32 *fw_data;
9132
9133 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009134 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
9135 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009136 return -ENOENT;
9137 }
9138
9139 fw_data = (void *)tp->fw->data;
9140
9141 /* Firmware blob starts with version numbers, followed by
9142 * start address and _full_ length including BSS sections
9143 * (which must be longer than the actual data, of course
9144 */
9145
9146 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9147 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009148 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9149 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009150 release_firmware(tp->fw);
9151 tp->fw = NULL;
9152 return -EINVAL;
9153 }
9154
9155 /* We no longer need firmware; we have it. */
9156 tp->fw_needed = NULL;
9157 return 0;
9158}
9159
Matt Carlson679563f2009-09-01 12:55:46 +00009160static bool tg3_enable_msix(struct tg3 *tp)
9161{
9162 int i, rc, cpus = num_online_cpus();
9163 struct msix_entry msix_ent[tp->irq_max];
9164
9165 if (cpus == 1)
9166 /* Just fallback to the simpler MSI mode. */
9167 return false;
9168
9169 /*
9170 * We want as many rx rings enabled as there are cpus.
9171 * The first MSIX vector only deals with link interrupts, etc,
9172 * so we add one to the number of vectors we are requesting.
9173 */
9174 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
9175
9176 for (i = 0; i < tp->irq_max; i++) {
9177 msix_ent[i].entry = i;
9178 msix_ent[i].vector = 0;
9179 }
9180
9181 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009182 if (rc < 0) {
9183 return false;
9184 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009185 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9186 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009187 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9188 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009189 tp->irq_cnt = rc;
9190 }
9191
9192 for (i = 0; i < tp->irq_max; i++)
9193 tp->napi[i].irq_vec = msix_ent[i].vector;
9194
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009195 netif_set_real_num_tx_queues(tp->dev, 1);
9196 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9197 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9198 pci_disable_msix(tp->pdev);
9199 return false;
9200 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009201
9202 if (tp->irq_cnt > 1) {
Matt Carlson2430b032010-06-05 17:24:34 +00009203 tp->tg3_flags3 |= TG3_FLG3_ENABLE_RSS;
Matt Carlsond78b59f2011-04-05 14:22:46 +00009204
9205 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9206 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb92b9042010-11-24 08:31:51 +00009207 tp->tg3_flags3 |= TG3_FLG3_ENABLE_TSS;
9208 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9209 }
9210 }
Matt Carlson2430b032010-06-05 17:24:34 +00009211
Matt Carlson679563f2009-09-01 12:55:46 +00009212 return true;
9213}
9214
Matt Carlson07b01732009-08-28 14:01:15 +00009215static void tg3_ints_init(struct tg3 *tp)
9216{
Matt Carlson679563f2009-09-01 12:55:46 +00009217 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI_OR_MSIX) &&
9218 !(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009219 /* All MSI supporting chips should support tagged
9220 * status. Assert that this is the case.
9221 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009222 netdev_warn(tp->dev,
9223 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009224 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009225 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009226
Matt Carlson679563f2009-09-01 12:55:46 +00009227 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) && tg3_enable_msix(tp))
9228 tp->tg3_flags2 |= TG3_FLG2_USING_MSIX;
9229 else if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI) &&
9230 pci_enable_msi(tp->pdev) == 0)
9231 tp->tg3_flags2 |= TG3_FLG2_USING_MSI;
9232
9233 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
9234 u32 msi_mode = tr32(MSGINT_MODE);
Matt Carlson0583d522011-01-25 15:58:50 +00009235 if ((tp->tg3_flags2 & TG3_FLG2_USING_MSIX) &&
9236 tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009237 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson679563f2009-09-01 12:55:46 +00009238 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9239 }
9240defcfg:
9241 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSIX)) {
9242 tp->irq_cnt = 1;
9243 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009244 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009245 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009246 }
Matt Carlson07b01732009-08-28 14:01:15 +00009247}
9248
9249static void tg3_ints_fini(struct tg3 *tp)
9250{
Matt Carlson679563f2009-09-01 12:55:46 +00009251 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
9252 pci_disable_msix(tp->pdev);
9253 else if (tp->tg3_flags2 & TG3_FLG2_USING_MSI)
9254 pci_disable_msi(tp->pdev);
9255 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI_OR_MSIX;
Matt Carlson774ee752010-08-02 11:25:56 +00009256 tp->tg3_flags3 &= ~(TG3_FLG3_ENABLE_RSS | TG3_FLG3_ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009257}
9258
Linus Torvalds1da177e2005-04-16 15:20:36 -07009259static int tg3_open(struct net_device *dev)
9260{
9261 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009262 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009263
Matt Carlson9e9fd122009-01-19 16:57:45 -08009264 if (tp->fw_needed) {
9265 err = tg3_request_firmware(tp);
9266 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9267 if (err)
9268 return err;
9269 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009270 netdev_warn(tp->dev, "TSO capability disabled\n");
Matt Carlson9e9fd122009-01-19 16:57:45 -08009271 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
9272 } else if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009273 netdev_notice(tp->dev, "TSO capability restored\n");
Matt Carlson9e9fd122009-01-19 16:57:45 -08009274 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
9275 }
9276 }
9277
Michael Chanc49a1562006-12-17 17:07:29 -08009278 netif_carrier_off(tp->dev);
9279
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009280 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -07009281 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009282 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009283
9284 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009285
Linus Torvalds1da177e2005-04-16 15:20:36 -07009286 tg3_disable_ints(tp);
9287 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
9288
David S. Millerf47c11e2005-06-24 20:18:35 -07009289 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009290
Matt Carlson679563f2009-09-01 12:55:46 +00009291 /*
9292 * Setup interrupts first so we know how
9293 * many NAPI resources to allocate
9294 */
9295 tg3_ints_init(tp);
9296
Linus Torvalds1da177e2005-04-16 15:20:36 -07009297 /* The placement of this call is tied
9298 * to the setup and use of Host TX descriptors.
9299 */
9300 err = tg3_alloc_consistent(tp);
9301 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009302 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009303
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009304 tg3_napi_init(tp);
9305
Matt Carlsonfed97812009-09-01 13:10:19 +00009306 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07009307
Matt Carlson4f125f42009-09-01 12:55:02 +00009308 for (i = 0; i < tp->irq_cnt; i++) {
9309 struct tg3_napi *tnapi = &tp->napi[i];
9310 err = tg3_request_irq(tp, i);
9311 if (err) {
9312 for (i--; i >= 0; i--)
9313 free_irq(tnapi->irq_vec, tnapi);
9314 break;
9315 }
9316 }
Matt Carlson07b01732009-08-28 14:01:15 +00009317
9318 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009319 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00009320
David S. Millerf47c11e2005-06-24 20:18:35 -07009321 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009322
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009323 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009324 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07009325 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009326 tg3_free_rings(tp);
9327 } else {
David S. Millerfac9b832005-05-18 22:46:34 -07009328 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
9329 tp->timer_offset = HZ;
9330 else
9331 tp->timer_offset = HZ / 10;
9332
9333 BUG_ON(tp->timer_offset > HZ);
9334 tp->timer_counter = tp->timer_multiplier =
9335 (HZ / tp->timer_offset);
9336 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07009337 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009338
9339 init_timer(&tp->timer);
9340 tp->timer.expires = jiffies + tp->timer_offset;
9341 tp->timer.data = (unsigned long) tp;
9342 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009343 }
9344
David S. Millerf47c11e2005-06-24 20:18:35 -07009345 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009346
Matt Carlson07b01732009-08-28 14:01:15 +00009347 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009348 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009349
Michael Chan79381092005-04-21 17:13:59 -07009350 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
9351 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07009352
Michael Chan79381092005-04-21 17:13:59 -07009353 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009354 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07009355 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07009356 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07009357 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009358
Matt Carlson679563f2009-09-01 12:55:46 +00009359 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07009360 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009361
Matt Carlson1407deb2011-04-05 14:22:44 +00009362 if (!(tp->tg3_flags3 & TG3_FLG3_57765_PLUS) &&
Matt Carlsonc885e822010-08-02 11:25:57 +00009363 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009364 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009365
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009366 tw32(PCIE_TRANSACTION_CFG,
9367 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009368 }
Michael Chan79381092005-04-21 17:13:59 -07009369 }
9370
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009371 tg3_phy_start(tp);
9372
David S. Millerf47c11e2005-06-24 20:18:35 -07009373 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009374
Michael Chan79381092005-04-21 17:13:59 -07009375 add_timer(&tp->timer);
9376 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009377 tg3_enable_ints(tp);
9378
David S. Millerf47c11e2005-06-24 20:18:35 -07009379 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009380
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009381 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009382
9383 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00009384
Matt Carlson679563f2009-09-01 12:55:46 +00009385err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00009386 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9387 struct tg3_napi *tnapi = &tp->napi[i];
9388 free_irq(tnapi->irq_vec, tnapi);
9389 }
Matt Carlson07b01732009-08-28 14:01:15 +00009390
Matt Carlson679563f2009-09-01 12:55:46 +00009391err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00009392 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009393 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009394 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00009395
9396err_out1:
9397 tg3_ints_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009398 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009399}
9400
Eric Dumazet511d2222010-07-07 20:44:24 +00009401static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
9402 struct rtnl_link_stats64 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009403static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
9404
9405static int tg3_close(struct net_device *dev)
9406{
Matt Carlson4f125f42009-09-01 12:55:02 +00009407 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009408 struct tg3 *tp = netdev_priv(dev);
9409
Matt Carlsonfed97812009-09-01 13:10:19 +00009410 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07009411 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08009412
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009413 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009414
9415 del_timer_sync(&tp->timer);
9416
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009417 tg3_phy_stop(tp);
9418
David S. Millerf47c11e2005-06-24 20:18:35 -07009419 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009420
9421 tg3_disable_ints(tp);
9422
Michael Chan944d9802005-05-29 14:57:48 -07009423 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009424 tg3_free_rings(tp);
Michael Chan5cf64b8a2007-05-05 12:11:21 -07009425 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009426
David S. Millerf47c11e2005-06-24 20:18:35 -07009427 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009428
Matt Carlson4f125f42009-09-01 12:55:02 +00009429 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9430 struct tg3_napi *tnapi = &tp->napi[i];
9431 free_irq(tnapi->irq_vec, tnapi);
9432 }
Matt Carlson07b01732009-08-28 14:01:15 +00009433
9434 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009435
Eric Dumazet511d2222010-07-07 20:44:24 +00009436 tg3_get_stats64(tp->dev, &tp->net_stats_prev);
9437
Linus Torvalds1da177e2005-04-16 15:20:36 -07009438 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9439 sizeof(tp->estats_prev));
9440
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009441 tg3_napi_fini(tp);
9442
Linus Torvalds1da177e2005-04-16 15:20:36 -07009443 tg3_free_consistent(tp);
9444
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009445 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -08009446
9447 netif_carrier_off(tp->dev);
9448
Linus Torvalds1da177e2005-04-16 15:20:36 -07009449 return 0;
9450}
9451
Eric Dumazet511d2222010-07-07 20:44:24 +00009452static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -07009453{
9454 return ((u64)val->high << 32) | ((u64)val->low);
9455}
9456
Eric Dumazet511d2222010-07-07 20:44:24 +00009457static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009458{
9459 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9460
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009461 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009462 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9463 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009464 u32 val;
9465
David S. Millerf47c11e2005-06-24 20:18:35 -07009466 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009467 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9468 tg3_writephy(tp, MII_TG3_TEST1,
9469 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009470 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009471 } else
9472 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009473 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009474
9475 tp->phy_crc_errors += val;
9476
9477 return tp->phy_crc_errors;
9478 }
9479
9480 return get_stat64(&hw_stats->rx_fcs_errors);
9481}
9482
9483#define ESTAT_ADD(member) \
9484 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +00009485 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009486
9487static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9488{
9489 struct tg3_ethtool_stats *estats = &tp->estats;
9490 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9491 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9492
9493 if (!hw_stats)
9494 return old_estats;
9495
9496 ESTAT_ADD(rx_octets);
9497 ESTAT_ADD(rx_fragments);
9498 ESTAT_ADD(rx_ucast_packets);
9499 ESTAT_ADD(rx_mcast_packets);
9500 ESTAT_ADD(rx_bcast_packets);
9501 ESTAT_ADD(rx_fcs_errors);
9502 ESTAT_ADD(rx_align_errors);
9503 ESTAT_ADD(rx_xon_pause_rcvd);
9504 ESTAT_ADD(rx_xoff_pause_rcvd);
9505 ESTAT_ADD(rx_mac_ctrl_rcvd);
9506 ESTAT_ADD(rx_xoff_entered);
9507 ESTAT_ADD(rx_frame_too_long_errors);
9508 ESTAT_ADD(rx_jabbers);
9509 ESTAT_ADD(rx_undersize_packets);
9510 ESTAT_ADD(rx_in_length_errors);
9511 ESTAT_ADD(rx_out_length_errors);
9512 ESTAT_ADD(rx_64_or_less_octet_packets);
9513 ESTAT_ADD(rx_65_to_127_octet_packets);
9514 ESTAT_ADD(rx_128_to_255_octet_packets);
9515 ESTAT_ADD(rx_256_to_511_octet_packets);
9516 ESTAT_ADD(rx_512_to_1023_octet_packets);
9517 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9518 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9519 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9520 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9521 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9522
9523 ESTAT_ADD(tx_octets);
9524 ESTAT_ADD(tx_collisions);
9525 ESTAT_ADD(tx_xon_sent);
9526 ESTAT_ADD(tx_xoff_sent);
9527 ESTAT_ADD(tx_flow_control);
9528 ESTAT_ADD(tx_mac_errors);
9529 ESTAT_ADD(tx_single_collisions);
9530 ESTAT_ADD(tx_mult_collisions);
9531 ESTAT_ADD(tx_deferred);
9532 ESTAT_ADD(tx_excessive_collisions);
9533 ESTAT_ADD(tx_late_collisions);
9534 ESTAT_ADD(tx_collide_2times);
9535 ESTAT_ADD(tx_collide_3times);
9536 ESTAT_ADD(tx_collide_4times);
9537 ESTAT_ADD(tx_collide_5times);
9538 ESTAT_ADD(tx_collide_6times);
9539 ESTAT_ADD(tx_collide_7times);
9540 ESTAT_ADD(tx_collide_8times);
9541 ESTAT_ADD(tx_collide_9times);
9542 ESTAT_ADD(tx_collide_10times);
9543 ESTAT_ADD(tx_collide_11times);
9544 ESTAT_ADD(tx_collide_12times);
9545 ESTAT_ADD(tx_collide_13times);
9546 ESTAT_ADD(tx_collide_14times);
9547 ESTAT_ADD(tx_collide_15times);
9548 ESTAT_ADD(tx_ucast_packets);
9549 ESTAT_ADD(tx_mcast_packets);
9550 ESTAT_ADD(tx_bcast_packets);
9551 ESTAT_ADD(tx_carrier_sense_errors);
9552 ESTAT_ADD(tx_discards);
9553 ESTAT_ADD(tx_errors);
9554
9555 ESTAT_ADD(dma_writeq_full);
9556 ESTAT_ADD(dma_write_prioq_full);
9557 ESTAT_ADD(rxbds_empty);
9558 ESTAT_ADD(rx_discards);
9559 ESTAT_ADD(rx_errors);
9560 ESTAT_ADD(rx_threshold_hit);
9561
9562 ESTAT_ADD(dma_readq_full);
9563 ESTAT_ADD(dma_read_prioq_full);
9564 ESTAT_ADD(tx_comp_queue_full);
9565
9566 ESTAT_ADD(ring_set_send_prod_index);
9567 ESTAT_ADD(ring_status_update);
9568 ESTAT_ADD(nic_irqs);
9569 ESTAT_ADD(nic_avoided_irqs);
9570 ESTAT_ADD(nic_tx_threshold_hit);
9571
9572 return estats;
9573}
9574
Eric Dumazet511d2222010-07-07 20:44:24 +00009575static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9576 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009577{
9578 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +00009579 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009580 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9581
9582 if (!hw_stats)
9583 return old_stats;
9584
9585 stats->rx_packets = old_stats->rx_packets +
9586 get_stat64(&hw_stats->rx_ucast_packets) +
9587 get_stat64(&hw_stats->rx_mcast_packets) +
9588 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009589
Linus Torvalds1da177e2005-04-16 15:20:36 -07009590 stats->tx_packets = old_stats->tx_packets +
9591 get_stat64(&hw_stats->tx_ucast_packets) +
9592 get_stat64(&hw_stats->tx_mcast_packets) +
9593 get_stat64(&hw_stats->tx_bcast_packets);
9594
9595 stats->rx_bytes = old_stats->rx_bytes +
9596 get_stat64(&hw_stats->rx_octets);
9597 stats->tx_bytes = old_stats->tx_bytes +
9598 get_stat64(&hw_stats->tx_octets);
9599
9600 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009601 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009602 stats->tx_errors = old_stats->tx_errors +
9603 get_stat64(&hw_stats->tx_errors) +
9604 get_stat64(&hw_stats->tx_mac_errors) +
9605 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9606 get_stat64(&hw_stats->tx_discards);
9607
9608 stats->multicast = old_stats->multicast +
9609 get_stat64(&hw_stats->rx_mcast_packets);
9610 stats->collisions = old_stats->collisions +
9611 get_stat64(&hw_stats->tx_collisions);
9612
9613 stats->rx_length_errors = old_stats->rx_length_errors +
9614 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9615 get_stat64(&hw_stats->rx_undersize_packets);
9616
9617 stats->rx_over_errors = old_stats->rx_over_errors +
9618 get_stat64(&hw_stats->rxbds_empty);
9619 stats->rx_frame_errors = old_stats->rx_frame_errors +
9620 get_stat64(&hw_stats->rx_align_errors);
9621 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9622 get_stat64(&hw_stats->tx_discards);
9623 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
9624 get_stat64(&hw_stats->tx_carrier_sense_errors);
9625
9626 stats->rx_crc_errors = old_stats->rx_crc_errors +
9627 calc_crc_errors(tp);
9628
John W. Linville4f63b872005-09-12 14:43:18 -07009629 stats->rx_missed_errors = old_stats->rx_missed_errors +
9630 get_stat64(&hw_stats->rx_discards);
9631
Eric Dumazetb0057c52010-10-10 19:55:52 +00009632 stats->rx_dropped = tp->rx_dropped;
9633
Linus Torvalds1da177e2005-04-16 15:20:36 -07009634 return stats;
9635}
9636
9637static inline u32 calc_crc(unsigned char *buf, int len)
9638{
9639 u32 reg;
9640 u32 tmp;
9641 int j, k;
9642
9643 reg = 0xffffffff;
9644
9645 for (j = 0; j < len; j++) {
9646 reg ^= buf[j];
9647
9648 for (k = 0; k < 8; k++) {
9649 tmp = reg & 0x01;
9650
9651 reg >>= 1;
9652
Matt Carlson859a588792010-04-05 10:19:28 +00009653 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009654 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009655 }
9656 }
9657
9658 return ~reg;
9659}
9660
9661static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9662{
9663 /* accept or reject all multicast frames */
9664 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9665 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9666 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9667 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9668}
9669
9670static void __tg3_set_rx_mode(struct net_device *dev)
9671{
9672 struct tg3 *tp = netdev_priv(dev);
9673 u32 rx_mode;
9674
9675 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9676 RX_MODE_KEEP_VLAN_TAG);
9677
Matt Carlsonbf933c82011-01-25 15:58:49 +00009678#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009679 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9680 * flag clear.
9681 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009682 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
9683 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9684#endif
9685
9686 if (dev->flags & IFF_PROMISC) {
9687 /* Promiscuous mode. */
9688 rx_mode |= RX_MODE_PROMISC;
9689 } else if (dev->flags & IFF_ALLMULTI) {
9690 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009691 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00009692 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009693 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009694 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009695 } else {
9696 /* Accept one or more multicast(s). */
Jiri Pirko22bedad32010-04-01 21:22:57 +00009697 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009698 u32 mc_filter[4] = { 0, };
9699 u32 regidx;
9700 u32 bit;
9701 u32 crc;
9702
Jiri Pirko22bedad32010-04-01 21:22:57 +00009703 netdev_for_each_mc_addr(ha, dev) {
9704 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009705 bit = ~crc & 0x7f;
9706 regidx = (bit & 0x60) >> 5;
9707 bit &= 0x1f;
9708 mc_filter[regidx] |= (1 << bit);
9709 }
9710
9711 tw32(MAC_HASH_REG_0, mc_filter[0]);
9712 tw32(MAC_HASH_REG_1, mc_filter[1]);
9713 tw32(MAC_HASH_REG_2, mc_filter[2]);
9714 tw32(MAC_HASH_REG_3, mc_filter[3]);
9715 }
9716
9717 if (rx_mode != tp->rx_mode) {
9718 tp->rx_mode = rx_mode;
9719 tw32_f(MAC_RX_MODE, rx_mode);
9720 udelay(10);
9721 }
9722}
9723
9724static void tg3_set_rx_mode(struct net_device *dev)
9725{
9726 struct tg3 *tp = netdev_priv(dev);
9727
Michael Chane75f7c92006-03-20 21:33:26 -08009728 if (!netif_running(dev))
9729 return;
9730
David S. Millerf47c11e2005-06-24 20:18:35 -07009731 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009732 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -07009733 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009734}
9735
Linus Torvalds1da177e2005-04-16 15:20:36 -07009736static int tg3_get_regs_len(struct net_device *dev)
9737{
Matt Carlson97bd8e42011-04-13 11:05:04 +00009738 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009739}
9740
9741static void tg3_get_regs(struct net_device *dev,
9742 struct ethtool_regs *regs, void *_p)
9743{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009744 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009745
9746 regs->version = 0;
9747
Matt Carlson97bd8e42011-04-13 11:05:04 +00009748 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009749
Matt Carlson80096062010-08-02 11:26:06 +00009750 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009751 return;
9752
David S. Millerf47c11e2005-06-24 20:18:35 -07009753 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009754
Matt Carlson97bd8e42011-04-13 11:05:04 +00009755 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009756
David S. Millerf47c11e2005-06-24 20:18:35 -07009757 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009758}
9759
9760static int tg3_get_eeprom_len(struct net_device *dev)
9761{
9762 struct tg3 *tp = netdev_priv(dev);
9763
9764 return tp->nvram_size;
9765}
9766
Linus Torvalds1da177e2005-04-16 15:20:36 -07009767static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9768{
9769 struct tg3 *tp = netdev_priv(dev);
9770 int ret;
9771 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -08009772 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009773 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009774
Matt Carlsondf259d82009-04-20 06:57:14 +00009775 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
9776 return -EINVAL;
9777
Matt Carlson80096062010-08-02 11:26:06 +00009778 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009779 return -EAGAIN;
9780
Linus Torvalds1da177e2005-04-16 15:20:36 -07009781 offset = eeprom->offset;
9782 len = eeprom->len;
9783 eeprom->len = 0;
9784
9785 eeprom->magic = TG3_EEPROM_MAGIC;
9786
9787 if (offset & 3) {
9788 /* adjustments to start on required 4 byte boundary */
9789 b_offset = offset & 3;
9790 b_count = 4 - b_offset;
9791 if (b_count > len) {
9792 /* i.e. offset=1 len=2 */
9793 b_count = len;
9794 }
Matt Carlsona9dc5292009-02-25 14:25:30 +00009795 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009796 if (ret)
9797 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +00009798 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009799 len -= b_count;
9800 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009801 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009802 }
9803
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009804 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009805 pd = &data[eeprom->len];
9806 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00009807 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009808 if (ret) {
9809 eeprom->len += i;
9810 return ret;
9811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009812 memcpy(pd + i, &val, 4);
9813 }
9814 eeprom->len += i;
9815
9816 if (len & 3) {
9817 /* read last bytes not ending on 4 byte boundary */
9818 pd = &data[eeprom->len];
9819 b_count = len & 3;
9820 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009821 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009822 if (ret)
9823 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009824 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009825 eeprom->len += b_count;
9826 }
9827 return 0;
9828}
9829
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009830static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009831
9832static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9833{
9834 struct tg3 *tp = netdev_priv(dev);
9835 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009836 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009837 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009838 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009839
Matt Carlson80096062010-08-02 11:26:06 +00009840 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009841 return -EAGAIN;
9842
Matt Carlsondf259d82009-04-20 06:57:14 +00009843 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
9844 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009845 return -EINVAL;
9846
9847 offset = eeprom->offset;
9848 len = eeprom->len;
9849
9850 if ((b_offset = (offset & 3))) {
9851 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +00009852 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009853 if (ret)
9854 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009855 len += b_offset;
9856 offset &= ~3;
Michael Chan1c8594b42005-04-21 17:12:46 -07009857 if (len < 4)
9858 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009859 }
9860
9861 odd_len = 0;
Michael Chan1c8594b42005-04-21 17:12:46 -07009862 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009863 /* adjustments to end on required 4 byte boundary */
9864 odd_len = 1;
9865 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009866 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009867 if (ret)
9868 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009869 }
9870
9871 buf = data;
9872 if (b_offset || odd_len) {
9873 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009874 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009875 return -ENOMEM;
9876 if (b_offset)
9877 memcpy(buf, &start, 4);
9878 if (odd_len)
9879 memcpy(buf+len-4, &end, 4);
9880 memcpy(buf + b_offset, data, eeprom->len);
9881 }
9882
9883 ret = tg3_nvram_write_block(tp, offset, len, buf);
9884
9885 if (buf != data)
9886 kfree(buf);
9887
9888 return ret;
9889}
9890
9891static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9892{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009893 struct tg3 *tp = netdev_priv(dev);
9894
9895 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009896 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009897 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009898 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009899 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9900 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009901 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009902
Linus Torvalds1da177e2005-04-16 15:20:36 -07009903 cmd->supported = (SUPPORTED_Autoneg);
9904
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009905 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009906 cmd->supported |= (SUPPORTED_1000baseT_Half |
9907 SUPPORTED_1000baseT_Full);
9908
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009909 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009910 cmd->supported |= (SUPPORTED_100baseT_Half |
9911 SUPPORTED_100baseT_Full |
9912 SUPPORTED_10baseT_Half |
9913 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -08009914 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -07009915 cmd->port = PORT_TP;
9916 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009917 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -07009918 cmd->port = PORT_FIBRE;
9919 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009920
Linus Torvalds1da177e2005-04-16 15:20:36 -07009921 cmd->advertising = tp->link_config.advertising;
9922 if (netif_running(dev)) {
9923 cmd->speed = tp->link_config.active_speed;
9924 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson64c22182010-10-14 10:37:44 +00009925 } else {
9926 cmd->speed = SPEED_INVALID;
9927 cmd->duplex = DUPLEX_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009928 }
Matt Carlson882e9792009-09-01 13:21:36 +00009929 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009930 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009931 cmd->autoneg = tp->link_config.autoneg;
9932 cmd->maxtxpkt = 0;
9933 cmd->maxrxpkt = 0;
9934 return 0;
9935}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009936
Linus Torvalds1da177e2005-04-16 15:20:36 -07009937static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9938{
9939 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009940
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009941 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009942 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009943 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009944 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009945 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9946 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009947 }
9948
Matt Carlson7e5856b2009-02-25 14:23:01 +00009949 if (cmd->autoneg != AUTONEG_ENABLE &&
9950 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -07009951 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009952
9953 if (cmd->autoneg == AUTONEG_DISABLE &&
9954 cmd->duplex != DUPLEX_FULL &&
9955 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -07009956 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009957
Matt Carlson7e5856b2009-02-25 14:23:01 +00009958 if (cmd->autoneg == AUTONEG_ENABLE) {
9959 u32 mask = ADVERTISED_Autoneg |
9960 ADVERTISED_Pause |
9961 ADVERTISED_Asym_Pause;
9962
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009963 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +00009964 mask |= ADVERTISED_1000baseT_Half |
9965 ADVERTISED_1000baseT_Full;
9966
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009967 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +00009968 mask |= ADVERTISED_100baseT_Half |
9969 ADVERTISED_100baseT_Full |
9970 ADVERTISED_10baseT_Half |
9971 ADVERTISED_10baseT_Full |
9972 ADVERTISED_TP;
9973 else
9974 mask |= ADVERTISED_FIBRE;
9975
9976 if (cmd->advertising & ~mask)
9977 return -EINVAL;
9978
9979 mask &= (ADVERTISED_1000baseT_Half |
9980 ADVERTISED_1000baseT_Full |
9981 ADVERTISED_100baseT_Half |
9982 ADVERTISED_100baseT_Full |
9983 ADVERTISED_10baseT_Half |
9984 ADVERTISED_10baseT_Full);
9985
9986 cmd->advertising &= mask;
9987 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009988 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
Matt Carlson7e5856b2009-02-25 14:23:01 +00009989 if (cmd->speed != SPEED_1000)
9990 return -EINVAL;
9991
9992 if (cmd->duplex != DUPLEX_FULL)
9993 return -EINVAL;
9994 } else {
9995 if (cmd->speed != SPEED_100 &&
9996 cmd->speed != SPEED_10)
9997 return -EINVAL;
9998 }
9999 }
10000
David S. Millerf47c11e2005-06-24 20:18:35 -070010001 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010002
10003 tp->link_config.autoneg = cmd->autoneg;
10004 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070010005 tp->link_config.advertising = (cmd->advertising |
10006 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010007 tp->link_config.speed = SPEED_INVALID;
10008 tp->link_config.duplex = DUPLEX_INVALID;
10009 } else {
10010 tp->link_config.advertising = 0;
10011 tp->link_config.speed = cmd->speed;
10012 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010013 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010014
Michael Chan24fcad62006-12-17 17:06:46 -080010015 tp->link_config.orig_speed = tp->link_config.speed;
10016 tp->link_config.orig_duplex = tp->link_config.duplex;
10017 tp->link_config.orig_autoneg = tp->link_config.autoneg;
10018
Linus Torvalds1da177e2005-04-16 15:20:36 -070010019 if (netif_running(dev))
10020 tg3_setup_phy(tp, 1);
10021
David S. Millerf47c11e2005-06-24 20:18:35 -070010022 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010023
Linus Torvalds1da177e2005-04-16 15:20:36 -070010024 return 0;
10025}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010026
Linus Torvalds1da177e2005-04-16 15:20:36 -070010027static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10028{
10029 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010030
Linus Torvalds1da177e2005-04-16 15:20:36 -070010031 strcpy(info->driver, DRV_MODULE_NAME);
10032 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -080010033 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010034 strcpy(info->bus_info, pci_name(tp->pdev));
10035}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010036
Linus Torvalds1da177e2005-04-16 15:20:36 -070010037static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10038{
10039 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010040
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010041 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
10042 device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070010043 wol->supported = WAKE_MAGIC;
10044 else
10045 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010046 wol->wolopts = 0;
Matt Carlson05ac4cb2008-11-03 16:53:46 -080010047 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) &&
10048 device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010049 wol->wolopts = WAKE_MAGIC;
10050 memset(&wol->sopass, 0, sizeof(wol->sopass));
10051}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010052
Linus Torvalds1da177e2005-04-16 15:20:36 -070010053static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10054{
10055 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010056 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010057
Linus Torvalds1da177e2005-04-16 15:20:36 -070010058 if (wol->wolopts & ~WAKE_MAGIC)
10059 return -EINVAL;
10060 if ((wol->wolopts & WAKE_MAGIC) &&
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010061 !((tp->tg3_flags & TG3_FLAG_WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010062 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010063
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010064 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
10065
David S. Millerf47c11e2005-06-24 20:18:35 -070010066 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010067 if (device_may_wakeup(dp))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010068 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010069 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070010070 tp->tg3_flags &= ~TG3_FLAG_WOL_ENABLE;
David S. Millerf47c11e2005-06-24 20:18:35 -070010071 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010072
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010073
Linus Torvalds1da177e2005-04-16 15:20:36 -070010074 return 0;
10075}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010076
Linus Torvalds1da177e2005-04-16 15:20:36 -070010077static u32 tg3_get_msglevel(struct net_device *dev)
10078{
10079 struct tg3 *tp = netdev_priv(dev);
10080 return tp->msg_enable;
10081}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010082
Linus Torvalds1da177e2005-04-16 15:20:36 -070010083static void tg3_set_msglevel(struct net_device *dev, u32 value)
10084{
10085 struct tg3 *tp = netdev_priv(dev);
10086 tp->msg_enable = value;
10087}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010088
Linus Torvalds1da177e2005-04-16 15:20:36 -070010089static int tg3_nway_reset(struct net_device *dev)
10090{
10091 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010092 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010093
Linus Torvalds1da177e2005-04-16 15:20:36 -070010094 if (!netif_running(dev))
10095 return -EAGAIN;
10096
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010097 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010098 return -EINVAL;
10099
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010100 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010101 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010102 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010103 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010104 } else {
10105 u32 bmcr;
10106
10107 spin_lock_bh(&tp->lock);
10108 r = -EINVAL;
10109 tg3_readphy(tp, MII_BMCR, &bmcr);
10110 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10111 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010112 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010113 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10114 BMCR_ANENABLE);
10115 r = 0;
10116 }
10117 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010118 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010119
Linus Torvalds1da177e2005-04-16 15:20:36 -070010120 return r;
10121}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010122
Linus Torvalds1da177e2005-04-16 15:20:36 -070010123static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10124{
10125 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010126
Matt Carlson2c49a442010-09-30 10:34:35 +000010127 ering->rx_max_pending = tp->rx_std_ring_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010128 ering->rx_mini_max_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -080010129 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
Matt Carlson2c49a442010-09-30 10:34:35 +000010130 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010131 else
10132 ering->rx_jumbo_max_pending = 0;
10133
10134 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010135
10136 ering->rx_pending = tp->rx_pending;
10137 ering->rx_mini_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -080010138 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
10139 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10140 else
10141 ering->rx_jumbo_pending = 0;
10142
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010143 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010144}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010145
Linus Torvalds1da177e2005-04-16 15:20:36 -070010146static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10147{
10148 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010149 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010150
Matt Carlson2c49a442010-09-30 10:34:35 +000010151 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10152 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010153 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10154 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Michael Chan7f62ad52007-02-20 23:25:40 -080010155 ((tp->tg3_flags2 & TG3_FLG2_TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010156 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010157 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010158
Michael Chanbbe832c2005-06-24 20:20:04 -070010159 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010160 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010161 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010162 irq_sync = 1;
10163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010164
Michael Chanbbe832c2005-06-24 20:20:04 -070010165 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010166
Linus Torvalds1da177e2005-04-16 15:20:36 -070010167 tp->rx_pending = ering->rx_pending;
10168
10169 if ((tp->tg3_flags2 & TG3_FLG2_MAX_RXPEND_64) &&
10170 tp->rx_pending > 63)
10171 tp->rx_pending = 63;
10172 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010173
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010174 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010175 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010176
10177 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010178 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010179 err = tg3_restart_hw(tp, 1);
10180 if (!err)
10181 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010182 }
10183
David S. Millerf47c11e2005-06-24 20:18:35 -070010184 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010185
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010186 if (irq_sync && !err)
10187 tg3_phy_start(tp);
10188
Michael Chanb9ec6c12006-07-25 16:37:27 -070010189 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010190}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010191
Linus Torvalds1da177e2005-04-16 15:20:36 -070010192static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10193{
10194 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010195
Linus Torvalds1da177e2005-04-16 15:20:36 -070010196 epause->autoneg = (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG) != 0;
Matt Carlson8d018622007-12-20 20:05:44 -080010197
Steve Glendinninge18ce342008-12-16 02:00:00 -080010198 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010199 epause->rx_pause = 1;
10200 else
10201 epause->rx_pause = 0;
10202
Steve Glendinninge18ce342008-12-16 02:00:00 -080010203 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010204 epause->tx_pause = 1;
10205 else
10206 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010207}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010208
Linus Torvalds1da177e2005-04-16 15:20:36 -070010209static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10210{
10211 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010212 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010213
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010214 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson27121682010-02-17 15:16:57 +000010215 u32 newadv;
10216 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010217
Matt Carlson27121682010-02-17 15:16:57 +000010218 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010219
Matt Carlson27121682010-02-17 15:16:57 +000010220 if (!(phydev->supported & SUPPORTED_Pause) ||
10221 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010222 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010223 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010224
Matt Carlson27121682010-02-17 15:16:57 +000010225 tp->link_config.flowctrl = 0;
10226 if (epause->rx_pause) {
10227 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010228
Matt Carlson27121682010-02-17 15:16:57 +000010229 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010230 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010231 newadv = ADVERTISED_Pause;
10232 } else
10233 newadv = ADVERTISED_Pause |
10234 ADVERTISED_Asym_Pause;
10235 } else if (epause->tx_pause) {
10236 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10237 newadv = ADVERTISED_Asym_Pause;
10238 } else
10239 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010240
Matt Carlson27121682010-02-17 15:16:57 +000010241 if (epause->autoneg)
10242 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
10243 else
10244 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
10245
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010246 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010247 u32 oldadv = phydev->advertising &
10248 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10249 if (oldadv != newadv) {
10250 phydev->advertising &=
10251 ~(ADVERTISED_Pause |
10252 ADVERTISED_Asym_Pause);
10253 phydev->advertising |= newadv;
10254 if (phydev->autoneg) {
10255 /*
10256 * Always renegotiate the link to
10257 * inform our link partner of our
10258 * flow control settings, even if the
10259 * flow control is forced. Let
10260 * tg3_adjust_link() do the final
10261 * flow control setup.
10262 */
10263 return phy_start_aneg(phydev);
10264 }
10265 }
10266
10267 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010268 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010269 } else {
10270 tp->link_config.orig_advertising &=
10271 ~(ADVERTISED_Pause |
10272 ADVERTISED_Asym_Pause);
10273 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010274 }
10275 } else {
10276 int irq_sync = 0;
10277
10278 if (netif_running(dev)) {
10279 tg3_netif_stop(tp);
10280 irq_sync = 1;
10281 }
10282
10283 tg3_full_lock(tp, irq_sync);
10284
10285 if (epause->autoneg)
10286 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
10287 else
10288 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
10289 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010290 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010291 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010292 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010293 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010294 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010295 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010296 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010297
10298 if (netif_running(dev)) {
10299 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10300 err = tg3_restart_hw(tp, 1);
10301 if (!err)
10302 tg3_netif_start(tp);
10303 }
10304
10305 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010307
Michael Chanb9ec6c12006-07-25 16:37:27 -070010308 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010309}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010310
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010311static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010312{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010313 switch (sset) {
10314 case ETH_SS_TEST:
10315 return TG3_NUM_TEST;
10316 case ETH_SS_STATS:
10317 return TG3_NUM_STATS;
10318 default:
10319 return -EOPNOTSUPP;
10320 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010321}
10322
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010323static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010324{
10325 switch (stringset) {
10326 case ETH_SS_STATS:
10327 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10328 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010329 case ETH_SS_TEST:
10330 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10331 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010332 default:
10333 WARN_ON(1); /* we need a WARN() */
10334 break;
10335 }
10336}
10337
stephen hemminger81b87092011-04-04 08:43:50 +000010338static int tg3_set_phys_id(struct net_device *dev,
10339 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070010340{
10341 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070010342
10343 if (!netif_running(tp->dev))
10344 return -EAGAIN;
10345
stephen hemminger81b87092011-04-04 08:43:50 +000010346 switch (state) {
10347 case ETHTOOL_ID_ACTIVE:
10348 return -EINVAL;
Michael Chan4009a932005-09-05 17:52:54 -070010349
stephen hemminger81b87092011-04-04 08:43:50 +000010350 case ETHTOOL_ID_ON:
10351 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10352 LED_CTRL_1000MBPS_ON |
10353 LED_CTRL_100MBPS_ON |
10354 LED_CTRL_10MBPS_ON |
10355 LED_CTRL_TRAFFIC_OVERRIDE |
10356 LED_CTRL_TRAFFIC_BLINK |
10357 LED_CTRL_TRAFFIC_LED);
10358 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010359
stephen hemminger81b87092011-04-04 08:43:50 +000010360 case ETHTOOL_ID_OFF:
10361 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10362 LED_CTRL_TRAFFIC_OVERRIDE);
10363 break;
Michael Chan4009a932005-09-05 17:52:54 -070010364
stephen hemminger81b87092011-04-04 08:43:50 +000010365 case ETHTOOL_ID_INACTIVE:
10366 tw32(MAC_LED_CTRL, tp->led_ctrl);
10367 break;
Michael Chan4009a932005-09-05 17:52:54 -070010368 }
stephen hemminger81b87092011-04-04 08:43:50 +000010369
Michael Chan4009a932005-09-05 17:52:54 -070010370 return 0;
10371}
10372
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010373static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010374 struct ethtool_stats *estats, u64 *tmp_stats)
10375{
10376 struct tg3 *tp = netdev_priv(dev);
10377 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10378}
10379
Michael Chan566f86a2005-05-29 14:56:58 -070010380#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010381#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10382#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10383#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Michael Chanb16250e2006-09-27 16:10:14 -070010384#define NVRAM_SELFBOOT_HW_SIZE 0x20
10385#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010386
10387static int tg3_test_nvram(struct tg3 *tp)
10388{
Al Virob9fc7dc2007-12-17 22:59:57 -080010389 u32 csum, magic;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010390 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010391 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010392
Matt Carlsondf259d82009-04-20 06:57:14 +000010393 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
10394 return 0;
10395
Matt Carlsone4f34112009-02-25 14:25:00 +000010396 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010397 return -EIO;
10398
Michael Chan1b277772006-03-20 22:27:48 -080010399 if (magic == TG3_EEPROM_MAGIC)
10400 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010401 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010402 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10403 TG3_EEPROM_SB_FORMAT_1) {
10404 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10405 case TG3_EEPROM_SB_REVISION_0:
10406 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10407 break;
10408 case TG3_EEPROM_SB_REVISION_2:
10409 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10410 break;
10411 case TG3_EEPROM_SB_REVISION_3:
10412 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10413 break;
10414 default:
10415 return 0;
10416 }
10417 } else
Michael Chan1b277772006-03-20 22:27:48 -080010418 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010419 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10420 size = NVRAM_SELFBOOT_HW_SIZE;
10421 else
Michael Chan1b277772006-03-20 22:27:48 -080010422 return -EIO;
10423
10424 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010425 if (buf == NULL)
10426 return -ENOMEM;
10427
Michael Chan1b277772006-03-20 22:27:48 -080010428 err = -EIO;
10429 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010430 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10431 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010432 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010433 }
Michael Chan1b277772006-03-20 22:27:48 -080010434 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010435 goto out;
10436
Michael Chan1b277772006-03-20 22:27:48 -080010437 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010438 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010439 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010440 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010441 u8 *buf8 = (u8 *) buf, csum8 = 0;
10442
Al Virob9fc7dc2007-12-17 22:59:57 -080010443 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010444 TG3_EEPROM_SB_REVISION_2) {
10445 /* For rev 2, the csum doesn't include the MBA. */
10446 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10447 csum8 += buf8[i];
10448 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10449 csum8 += buf8[i];
10450 } else {
10451 for (i = 0; i < size; i++)
10452 csum8 += buf8[i];
10453 }
Michael Chan1b277772006-03-20 22:27:48 -080010454
Adrian Bunkad96b482006-04-05 22:21:04 -070010455 if (csum8 == 0) {
10456 err = 0;
10457 goto out;
10458 }
10459
10460 err = -EIO;
10461 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010462 }
Michael Chan566f86a2005-05-29 14:56:58 -070010463
Al Virob9fc7dc2007-12-17 22:59:57 -080010464 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010465 TG3_EEPROM_MAGIC_HW) {
10466 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010467 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010468 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010469
10470 /* Separate the parity bits and the data bytes. */
10471 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10472 if ((i == 0) || (i == 8)) {
10473 int l;
10474 u8 msk;
10475
10476 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10477 parity[k++] = buf8[i] & msk;
10478 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000010479 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010480 int l;
10481 u8 msk;
10482
10483 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10484 parity[k++] = buf8[i] & msk;
10485 i++;
10486
10487 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10488 parity[k++] = buf8[i] & msk;
10489 i++;
10490 }
10491 data[j++] = buf8[i];
10492 }
10493
10494 err = -EIO;
10495 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10496 u8 hw8 = hweight8(data[i]);
10497
10498 if ((hw8 & 0x1) && parity[i])
10499 goto out;
10500 else if (!(hw8 & 0x1) && !parity[i])
10501 goto out;
10502 }
10503 err = 0;
10504 goto out;
10505 }
10506
Matt Carlson01c3a392011-03-09 16:58:20 +000010507 err = -EIO;
10508
Michael Chan566f86a2005-05-29 14:56:58 -070010509 /* Bootstrap checksum at offset 0x10 */
10510 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000010511 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010512 goto out;
10513
10514 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10515 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000010516 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000010517 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010518
Matt Carlsond4894f32011-03-09 16:58:21 +000010519 for (i = 0; i < TG3_NVM_VPD_LEN; i += 4) {
10520 /* The data is in little-endian format in NVRAM.
10521 * Use the big-endian read routines to preserve
10522 * the byte order as it exists in NVRAM.
10523 */
10524 if (tg3_nvram_read_be32(tp, TG3_NVM_VPD_OFF + i, &buf[i/4]))
10525 goto out;
10526 }
10527
10528 i = pci_vpd_find_tag((u8 *)buf, 0, TG3_NVM_VPD_LEN,
10529 PCI_VPD_LRDT_RO_DATA);
10530 if (i > 0) {
10531 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
10532 if (j < 0)
10533 goto out;
10534
10535 if (i + PCI_VPD_LRDT_TAG_SIZE + j > TG3_NVM_VPD_LEN)
10536 goto out;
10537
10538 i += PCI_VPD_LRDT_TAG_SIZE;
10539 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
10540 PCI_VPD_RO_KEYWORD_CHKSUM);
10541 if (j > 0) {
10542 u8 csum8 = 0;
10543
10544 j += PCI_VPD_INFO_FLD_HDR_SIZE;
10545
10546 for (i = 0; i <= j; i++)
10547 csum8 += ((u8 *)buf)[i];
10548
10549 if (csum8)
10550 goto out;
10551 }
10552 }
10553
Michael Chan566f86a2005-05-29 14:56:58 -070010554 err = 0;
10555
10556out:
10557 kfree(buf);
10558 return err;
10559}
10560
Michael Chanca430072005-05-29 14:57:23 -070010561#define TG3_SERDES_TIMEOUT_SEC 2
10562#define TG3_COPPER_TIMEOUT_SEC 6
10563
10564static int tg3_test_link(struct tg3 *tp)
10565{
10566 int i, max;
10567
10568 if (!netif_running(tp->dev))
10569 return -ENODEV;
10570
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010571 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070010572 max = TG3_SERDES_TIMEOUT_SEC;
10573 else
10574 max = TG3_COPPER_TIMEOUT_SEC;
10575
10576 for (i = 0; i < max; i++) {
10577 if (netif_carrier_ok(tp->dev))
10578 return 0;
10579
10580 if (msleep_interruptible(1000))
10581 break;
10582 }
10583
10584 return -EIO;
10585}
10586
Michael Chana71116d2005-05-29 14:58:11 -070010587/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080010588static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070010589{
Michael Chanb16250e2006-09-27 16:10:14 -070010590 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070010591 u32 offset, read_mask, write_mask, val, save_val, read_val;
10592 static struct {
10593 u16 offset;
10594 u16 flags;
10595#define TG3_FL_5705 0x1
10596#define TG3_FL_NOT_5705 0x2
10597#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070010598#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070010599 u32 read_mask;
10600 u32 write_mask;
10601 } reg_tbl[] = {
10602 /* MAC Control Registers */
10603 { MAC_MODE, TG3_FL_NOT_5705,
10604 0x00000000, 0x00ef6f8c },
10605 { MAC_MODE, TG3_FL_5705,
10606 0x00000000, 0x01ef6b8c },
10607 { MAC_STATUS, TG3_FL_NOT_5705,
10608 0x03800107, 0x00000000 },
10609 { MAC_STATUS, TG3_FL_5705,
10610 0x03800100, 0x00000000 },
10611 { MAC_ADDR_0_HIGH, 0x0000,
10612 0x00000000, 0x0000ffff },
10613 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010614 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070010615 { MAC_RX_MTU_SIZE, 0x0000,
10616 0x00000000, 0x0000ffff },
10617 { MAC_TX_MODE, 0x0000,
10618 0x00000000, 0x00000070 },
10619 { MAC_TX_LENGTHS, 0x0000,
10620 0x00000000, 0x00003fff },
10621 { MAC_RX_MODE, TG3_FL_NOT_5705,
10622 0x00000000, 0x000007fc },
10623 { MAC_RX_MODE, TG3_FL_5705,
10624 0x00000000, 0x000007dc },
10625 { MAC_HASH_REG_0, 0x0000,
10626 0x00000000, 0xffffffff },
10627 { MAC_HASH_REG_1, 0x0000,
10628 0x00000000, 0xffffffff },
10629 { MAC_HASH_REG_2, 0x0000,
10630 0x00000000, 0xffffffff },
10631 { MAC_HASH_REG_3, 0x0000,
10632 0x00000000, 0xffffffff },
10633
10634 /* Receive Data and Receive BD Initiator Control Registers. */
10635 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
10636 0x00000000, 0xffffffff },
10637 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
10638 0x00000000, 0xffffffff },
10639 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
10640 0x00000000, 0x00000003 },
10641 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
10642 0x00000000, 0xffffffff },
10643 { RCVDBDI_STD_BD+0, 0x0000,
10644 0x00000000, 0xffffffff },
10645 { RCVDBDI_STD_BD+4, 0x0000,
10646 0x00000000, 0xffffffff },
10647 { RCVDBDI_STD_BD+8, 0x0000,
10648 0x00000000, 0xffff0002 },
10649 { RCVDBDI_STD_BD+0xc, 0x0000,
10650 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010651
Michael Chana71116d2005-05-29 14:58:11 -070010652 /* Receive BD Initiator Control Registers. */
10653 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
10654 0x00000000, 0xffffffff },
10655 { RCVBDI_STD_THRESH, TG3_FL_5705,
10656 0x00000000, 0x000003ff },
10657 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
10658 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010659
Michael Chana71116d2005-05-29 14:58:11 -070010660 /* Host Coalescing Control Registers. */
10661 { HOSTCC_MODE, TG3_FL_NOT_5705,
10662 0x00000000, 0x00000004 },
10663 { HOSTCC_MODE, TG3_FL_5705,
10664 0x00000000, 0x000000f6 },
10665 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
10666 0x00000000, 0xffffffff },
10667 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
10668 0x00000000, 0x000003ff },
10669 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
10670 0x00000000, 0xffffffff },
10671 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
10672 0x00000000, 0x000003ff },
10673 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
10674 0x00000000, 0xffffffff },
10675 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10676 0x00000000, 0x000000ff },
10677 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
10678 0x00000000, 0xffffffff },
10679 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10680 0x00000000, 0x000000ff },
10681 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
10682 0x00000000, 0xffffffff },
10683 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
10684 0x00000000, 0xffffffff },
10685 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10686 0x00000000, 0xffffffff },
10687 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10688 0x00000000, 0x000000ff },
10689 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10690 0x00000000, 0xffffffff },
10691 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10692 0x00000000, 0x000000ff },
10693 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
10694 0x00000000, 0xffffffff },
10695 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
10696 0x00000000, 0xffffffff },
10697 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
10698 0x00000000, 0xffffffff },
10699 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
10700 0x00000000, 0xffffffff },
10701 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
10702 0x00000000, 0xffffffff },
10703 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
10704 0xffffffff, 0x00000000 },
10705 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
10706 0xffffffff, 0x00000000 },
10707
10708 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070010709 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010710 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070010711 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010712 0x00000000, 0x007fffff },
10713 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
10714 0x00000000, 0x0000003f },
10715 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
10716 0x00000000, 0x000001ff },
10717 { BUFMGR_MB_HIGH_WATER, 0x0000,
10718 0x00000000, 0x000001ff },
10719 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
10720 0xffffffff, 0x00000000 },
10721 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
10722 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010723
Michael Chana71116d2005-05-29 14:58:11 -070010724 /* Mailbox Registers */
10725 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
10726 0x00000000, 0x000001ff },
10727 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
10728 0x00000000, 0x000001ff },
10729 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
10730 0x00000000, 0x000007ff },
10731 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
10732 0x00000000, 0x000001ff },
10733
10734 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
10735 };
10736
Michael Chanb16250e2006-09-27 16:10:14 -070010737 is_5705 = is_5750 = 0;
10738 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chana71116d2005-05-29 14:58:11 -070010739 is_5705 = 1;
Michael Chanb16250e2006-09-27 16:10:14 -070010740 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
10741 is_5750 = 1;
10742 }
Michael Chana71116d2005-05-29 14:58:11 -070010743
10744 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
10745 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
10746 continue;
10747
10748 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
10749 continue;
10750
10751 if ((tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
10752 (reg_tbl[i].flags & TG3_FL_NOT_5788))
10753 continue;
10754
Michael Chanb16250e2006-09-27 16:10:14 -070010755 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
10756 continue;
10757
Michael Chana71116d2005-05-29 14:58:11 -070010758 offset = (u32) reg_tbl[i].offset;
10759 read_mask = reg_tbl[i].read_mask;
10760 write_mask = reg_tbl[i].write_mask;
10761
10762 /* Save the original register content */
10763 save_val = tr32(offset);
10764
10765 /* Determine the read-only value. */
10766 read_val = save_val & read_mask;
10767
10768 /* Write zero to the register, then make sure the read-only bits
10769 * are not changed and the read/write bits are all zeros.
10770 */
10771 tw32(offset, 0);
10772
10773 val = tr32(offset);
10774
10775 /* Test the read-only and read/write bits. */
10776 if (((val & read_mask) != read_val) || (val & write_mask))
10777 goto out;
10778
10779 /* Write ones to all the bits defined by RdMask and WrMask, then
10780 * make sure the read-only bits are not changed and the
10781 * read/write bits are all ones.
10782 */
10783 tw32(offset, read_mask | write_mask);
10784
10785 val = tr32(offset);
10786
10787 /* Test the read-only bits. */
10788 if ((val & read_mask) != read_val)
10789 goto out;
10790
10791 /* Test the read/write bits. */
10792 if ((val & write_mask) != write_mask)
10793 goto out;
10794
10795 tw32(offset, save_val);
10796 }
10797
10798 return 0;
10799
10800out:
Michael Chan9f88f292006-12-07 00:22:54 -080010801 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000010802 netdev_err(tp->dev,
10803 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070010804 tw32(offset, save_val);
10805 return -EIO;
10806}
10807
Michael Chan7942e1d2005-05-29 14:58:36 -070010808static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
10809{
Arjan van de Venf71e1302006-03-03 21:33:57 -050010810 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070010811 int i;
10812 u32 j;
10813
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020010814 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070010815 for (j = 0; j < len; j += 4) {
10816 u32 val;
10817
10818 tg3_write_mem(tp, offset + j, test_pattern[i]);
10819 tg3_read_mem(tp, offset + j, &val);
10820 if (val != test_pattern[i])
10821 return -EIO;
10822 }
10823 }
10824 return 0;
10825}
10826
10827static int tg3_test_memory(struct tg3 *tp)
10828{
10829 static struct mem_entry {
10830 u32 offset;
10831 u32 len;
10832 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080010833 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070010834 { 0x00002000, 0x1c000},
10835 { 0xffffffff, 0x00000}
10836 }, mem_tbl_5705[] = {
10837 { 0x00000100, 0x0000c},
10838 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070010839 { 0x00004000, 0x00800},
10840 { 0x00006000, 0x01000},
10841 { 0x00008000, 0x02000},
10842 { 0x00010000, 0x0e000},
10843 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080010844 }, mem_tbl_5755[] = {
10845 { 0x00000200, 0x00008},
10846 { 0x00004000, 0x00800},
10847 { 0x00006000, 0x00800},
10848 { 0x00008000, 0x02000},
10849 { 0x00010000, 0x0c000},
10850 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070010851 }, mem_tbl_5906[] = {
10852 { 0x00000200, 0x00008},
10853 { 0x00004000, 0x00400},
10854 { 0x00006000, 0x00400},
10855 { 0x00008000, 0x01000},
10856 { 0x00010000, 0x01000},
10857 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010858 }, mem_tbl_5717[] = {
10859 { 0x00000200, 0x00008},
10860 { 0x00010000, 0x0a000},
10861 { 0x00020000, 0x13c00},
10862 { 0xffffffff, 0x00000}
10863 }, mem_tbl_57765[] = {
10864 { 0x00000200, 0x00008},
10865 { 0x00004000, 0x00800},
10866 { 0x00006000, 0x09800},
10867 { 0x00010000, 0x0a000},
10868 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070010869 };
10870 struct mem_entry *mem_tbl;
10871 int err = 0;
10872 int i;
10873
Matt Carlson0a58d662011-04-05 14:22:45 +000010874 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010875 mem_tbl = mem_tbl_5717;
10876 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
10877 mem_tbl = mem_tbl_57765;
10878 else if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlson321d32a2008-11-21 17:22:19 -080010879 mem_tbl = mem_tbl_5755;
10880 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
10881 mem_tbl = mem_tbl_5906;
10882 else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
10883 mem_tbl = mem_tbl_5705;
10884 else
Michael Chan7942e1d2005-05-29 14:58:36 -070010885 mem_tbl = mem_tbl_570x;
10886
10887 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000010888 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
10889 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070010890 break;
10891 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010892
Michael Chan7942e1d2005-05-29 14:58:36 -070010893 return err;
10894}
10895
Michael Chan9f40dea2005-09-05 17:53:06 -070010896#define TG3_MAC_LOOPBACK 0
10897#define TG3_PHY_LOOPBACK 1
10898
10899static int tg3_run_loopback(struct tg3 *tp, int loopback_mode)
Michael Chanc76949a2005-05-29 14:58:59 -070010900{
Michael Chan9f40dea2005-09-05 17:53:06 -070010901 u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010902 u32 desc_idx, coal_now;
Michael Chanc76949a2005-05-29 14:58:59 -070010903 struct sk_buff *skb, *rx_skb;
10904 u8 *tx_data;
10905 dma_addr_t map;
10906 int num_pkts, tx_len, rx_len, i, err;
10907 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000010908 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000010909 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070010910
Matt Carlsonc8873402010-02-12 14:47:11 +000010911 tnapi = &tp->napi[0];
10912 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010913 if (tp->irq_cnt > 1) {
Matt Carlson1da85aa2010-09-30 10:34:34 +000010914 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)
10915 rnapi = &tp->napi[1];
Matt Carlsonc8873402010-02-12 14:47:11 +000010916 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
10917 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010918 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010919 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000010920
Michael Chan9f40dea2005-09-05 17:53:06 -070010921 if (loopback_mode == TG3_MAC_LOOPBACK) {
Michael Chanc94e3942005-09-27 12:12:42 -070010922 /* HW errata - mac loopback fails in some cases on 5780.
10923 * Normal traffic and PHY loopback are not affected by
Matt Carlsonaba49f22011-01-25 15:58:53 +000010924 * errata. Also, the MAC loopback test is deprecated for
10925 * all newer ASIC revisions.
Michael Chanc94e3942005-09-27 12:12:42 -070010926 */
Matt Carlsonaba49f22011-01-25 15:58:53 +000010927 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
10928 (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT))
Michael Chanc94e3942005-09-27 12:12:42 -070010929 return 0;
10930
Matt Carlson49692ca2011-01-25 15:58:52 +000010931 mac_mode = tp->mac_mode &
10932 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
10933 mac_mode |= MAC_MODE_PORT_INT_LPBACK;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010934 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
10935 mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010936 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
Michael Chan3f7045c2006-09-27 16:02:29 -070010937 mac_mode |= MAC_MODE_PORT_MODE_MII;
10938 else
10939 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chan9f40dea2005-09-05 17:53:06 -070010940 tw32(MAC_MODE, mac_mode);
10941 } else if (loopback_mode == TG3_PHY_LOOPBACK) {
Michael Chan3f7045c2006-09-27 16:02:29 -070010942 u32 val;
10943
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010944 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +000010945 tg3_phy_fet_toggle_apd(tp, false);
Michael Chan5d64ad32006-12-07 00:19:40 -080010946 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
10947 } else
10948 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
Michael Chan3f7045c2006-09-27 16:02:29 -070010949
Matt Carlson9ef8ca92007-07-11 19:48:29 -070010950 tg3_phy_toggle_automdix(tp, 0);
10951
Michael Chan3f7045c2006-09-27 16:02:29 -070010952 tg3_writephy(tp, MII_BMCR, val);
Michael Chanc94e3942005-09-27 12:12:42 -070010953 udelay(40);
Michael Chan5d64ad32006-12-07 00:19:40 -080010954
Matt Carlson49692ca2011-01-25 15:58:52 +000010955 mac_mode = tp->mac_mode &
10956 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010957 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson1061b7c2010-02-12 14:47:12 +000010958 tg3_writephy(tp, MII_TG3_FET_PTEST,
10959 MII_TG3_FET_PTEST_FRC_TX_LINK |
10960 MII_TG3_FET_PTEST_FRC_TX_LOCK);
10961 /* The write needs to be flushed for the AC131 */
10962 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
10963 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
Michael Chan5d64ad32006-12-07 00:19:40 -080010964 mac_mode |= MAC_MODE_PORT_MODE_MII;
10965 } else
10966 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chanb16250e2006-09-27 16:10:14 -070010967
Michael Chanc94e3942005-09-27 12:12:42 -070010968 /* reset to prevent losing 1st rx packet intermittently */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010969 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Michael Chanc94e3942005-09-27 12:12:42 -070010970 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10971 udelay(10);
10972 tw32_f(MAC_RX_MODE, tp->rx_mode);
10973 }
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010974 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlson79eb6902010-02-17 15:17:03 +000010975 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
10976 if (masked_phy_id == TG3_PHY_ID_BCM5401)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010977 mac_mode &= ~MAC_MODE_LINK_POLARITY;
Matt Carlson79eb6902010-02-17 15:17:03 +000010978 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010979 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chanff18ff02006-03-27 23:17:27 -080010980 tg3_writephy(tp, MII_TG3_EXT_CTRL,
10981 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
10982 }
Michael Chan9f40dea2005-09-05 17:53:06 -070010983 tw32(MAC_MODE, mac_mode);
Matt Carlson49692ca2011-01-25 15:58:52 +000010984
10985 /* Wait for link */
10986 for (i = 0; i < 100; i++) {
10987 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
10988 break;
10989 mdelay(1);
10990 }
Matt Carlson859a588792010-04-05 10:19:28 +000010991 } else {
Michael Chan9f40dea2005-09-05 17:53:06 -070010992 return -EINVAL;
Matt Carlson859a588792010-04-05 10:19:28 +000010993 }
Michael Chanc76949a2005-05-29 14:58:59 -070010994
10995 err = -EIO;
10996
Michael Chanc76949a2005-05-29 14:58:59 -070010997 tx_len = 1514;
David S. Millera20e9c62006-07-31 22:38:16 -070010998 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070010999 if (!skb)
11000 return -ENOMEM;
11001
Michael Chanc76949a2005-05-29 14:58:59 -070011002 tx_data = skb_put(skb, tx_len);
11003 memcpy(tx_data, tp->dev->dev_addr, 6);
11004 memset(tx_data + 6, 0x0, 8);
11005
11006 tw32(MAC_RX_MTU_SIZE, tx_len + 4);
11007
11008 for (i = 14; i < tx_len; i++)
11009 tx_data[i] = (u8) (i & 0xff);
11010
Alexander Duyckf4188d82009-12-02 16:48:38 +000011011 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
11012 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000011013 dev_kfree_skb(skb);
11014 return -EIO;
11015 }
Michael Chanc76949a2005-05-29 14:58:59 -070011016
11017 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011018 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011019
11020 udelay(10);
11021
Matt Carlson898a56f2009-08-28 14:02:40 +000011022 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011023
Michael Chanc76949a2005-05-29 14:58:59 -070011024 num_pkts = 0;
11025
Alexander Duyckf4188d82009-12-02 16:48:38 +000011026 tg3_set_txd(tnapi, tnapi->tx_prod, map, tx_len, 0, 1);
Michael Chanc76949a2005-05-29 14:58:59 -070011027
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011028 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011029 num_pkts++;
11030
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011031 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11032 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011033
11034 udelay(10);
11035
Matt Carlson303fc922009-11-02 14:27:34 +000011036 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11037 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011038 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011039 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011040
11041 udelay(10);
11042
Matt Carlson898a56f2009-08-28 14:02:40 +000011043 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11044 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011045 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011046 (rx_idx == (rx_start_idx + num_pkts)))
11047 break;
11048 }
11049
Alexander Duyckf4188d82009-12-02 16:48:38 +000011050 pci_unmap_single(tp->pdev, map, tx_len, PCI_DMA_TODEVICE);
Michael Chanc76949a2005-05-29 14:58:59 -070011051 dev_kfree_skb(skb);
11052
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011053 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011054 goto out;
11055
11056 if (rx_idx != rx_start_idx + num_pkts)
11057 goto out;
11058
Matt Carlson72334482009-08-28 14:03:01 +000011059 desc = &rnapi->rx_rcb[rx_start_idx];
Michael Chanc76949a2005-05-29 14:58:59 -070011060 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11061 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
11062 if (opaque_key != RXD_OPAQUE_RING_STD)
11063 goto out;
11064
11065 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11066 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
11067 goto out;
11068
11069 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) - 4;
11070 if (rx_len != tx_len)
11071 goto out;
11072
Matt Carlson21f581a2009-08-28 14:00:25 +000011073 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
Michael Chanc76949a2005-05-29 14:58:59 -070011074
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +000011075 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx], mapping);
Michael Chanc76949a2005-05-29 14:58:59 -070011076 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len, PCI_DMA_FROMDEVICE);
11077
11078 for (i = 14; i < tx_len; i++) {
11079 if (*(rx_skb->data + i) != (u8) (i & 0xff))
11080 goto out;
11081 }
11082 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011083
Michael Chanc76949a2005-05-29 14:58:59 -070011084 /* tg3_free_rings will unmap and free the rx_skb */
11085out:
11086 return err;
11087}
11088
Michael Chan9f40dea2005-09-05 17:53:06 -070011089#define TG3_MAC_LOOPBACK_FAILED 1
11090#define TG3_PHY_LOOPBACK_FAILED 2
11091#define TG3_LOOPBACK_FAILED (TG3_MAC_LOOPBACK_FAILED | \
11092 TG3_PHY_LOOPBACK_FAILED)
11093
11094static int tg3_test_loopback(struct tg3 *tp)
11095{
11096 int err = 0;
Matt Carlsonab789042011-01-25 15:58:54 +000011097 u32 eee_cap, cpmuctrl = 0;
Michael Chan9f40dea2005-09-05 17:53:06 -070011098
11099 if (!netif_running(tp->dev))
11100 return TG3_LOOPBACK_FAILED;
11101
Matt Carlsonab789042011-01-25 15:58:54 +000011102 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11103 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11104
Michael Chanb9ec6c12006-07-25 16:37:27 -070011105 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011106 if (err) {
11107 err = TG3_LOOPBACK_FAILED;
11108 goto done;
11109 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011110
Matt Carlson6833c042008-11-21 17:18:59 -080011111 /* Turn off gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011112 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080011113 tg3_phy_toggle_apd(tp, false);
11114
Matt Carlson321d32a2008-11-21 17:22:19 -080011115 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070011116 int i;
11117 u32 status;
11118
11119 tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
11120
11121 /* Wait for up to 40 microseconds to acquire lock. */
11122 for (i = 0; i < 4; i++) {
11123 status = tr32(TG3_CPMU_MUTEX_GNT);
11124 if (status == CPMU_MUTEX_GNT_DRIVER)
11125 break;
11126 udelay(10);
11127 }
11128
Matt Carlsonab789042011-01-25 15:58:54 +000011129 if (status != CPMU_MUTEX_GNT_DRIVER) {
11130 err = TG3_LOOPBACK_FAILED;
11131 goto done;
11132 }
Matt Carlson9936bcf2007-10-10 18:03:07 -070011133
Matt Carlsonb2a5c192008-04-03 21:44:44 -070011134 /* Turn off link-based power management. */
Matt Carlsone8750932007-11-12 21:11:51 -080011135 cpmuctrl = tr32(TG3_CPMU_CTRL);
Matt Carlson109115e2008-05-02 16:48:59 -070011136 tw32(TG3_CPMU_CTRL,
11137 cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
11138 CPMU_CTRL_LINK_AWARE_MODE));
Matt Carlson9936bcf2007-10-10 18:03:07 -070011139 }
11140
Michael Chan9f40dea2005-09-05 17:53:06 -070011141 if (tg3_run_loopback(tp, TG3_MAC_LOOPBACK))
11142 err |= TG3_MAC_LOOPBACK_FAILED;
Matt Carlson9936bcf2007-10-10 18:03:07 -070011143
Matt Carlson321d32a2008-11-21 17:22:19 -080011144 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070011145 tw32(TG3_CPMU_CTRL, cpmuctrl);
11146
11147 /* Release the mutex */
11148 tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
11149 }
11150
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011151 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsondd477002008-05-25 23:45:58 -070011152 !(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
Michael Chan9f40dea2005-09-05 17:53:06 -070011153 if (tg3_run_loopback(tp, TG3_PHY_LOOPBACK))
11154 err |= TG3_PHY_LOOPBACK_FAILED;
11155 }
11156
Matt Carlson6833c042008-11-21 17:18:59 -080011157 /* Re-enable gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011158 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080011159 tg3_phy_toggle_apd(tp, true);
11160
Matt Carlsonab789042011-01-25 15:58:54 +000011161done:
11162 tp->phy_flags |= eee_cap;
11163
Michael Chan9f40dea2005-09-05 17:53:06 -070011164 return err;
11165}
11166
Michael Chan4cafd3f2005-05-29 14:56:34 -070011167static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11168 u64 *data)
11169{
Michael Chan566f86a2005-05-29 14:56:58 -070011170 struct tg3 *tp = netdev_priv(dev);
11171
Matt Carlson80096062010-08-02 11:26:06 +000011172 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011173 tg3_power_up(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011174
Michael Chan566f86a2005-05-29 14:56:58 -070011175 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
11176
11177 if (tg3_test_nvram(tp) != 0) {
11178 etest->flags |= ETH_TEST_FL_FAILED;
11179 data[0] = 1;
11180 }
Michael Chanca430072005-05-29 14:57:23 -070011181 if (tg3_test_link(tp) != 0) {
11182 etest->flags |= ETH_TEST_FL_FAILED;
11183 data[1] = 1;
11184 }
Michael Chana71116d2005-05-29 14:58:11 -070011185 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011186 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070011187
Michael Chanbbe832c2005-06-24 20:20:04 -070011188 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011189 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011190 tg3_netif_stop(tp);
11191 irq_sync = 1;
11192 }
11193
11194 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070011195
11196 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080011197 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011198 tg3_halt_cpu(tp, RX_CPU_BASE);
11199 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
11200 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080011201 if (!err)
11202 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011203
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011204 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad12006-03-20 22:27:35 -080011205 tg3_phy_reset(tp);
11206
Michael Chana71116d2005-05-29 14:58:11 -070011207 if (tg3_test_registers(tp) != 0) {
11208 etest->flags |= ETH_TEST_FL_FAILED;
11209 data[2] = 1;
11210 }
Michael Chan7942e1d2005-05-29 14:58:36 -070011211 if (tg3_test_memory(tp) != 0) {
11212 etest->flags |= ETH_TEST_FL_FAILED;
11213 data[3] = 1;
11214 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011215 if ((data[4] = tg3_test_loopback(tp)) != 0)
Michael Chanc76949a2005-05-29 14:58:59 -070011216 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070011217
David S. Millerf47c11e2005-06-24 20:18:35 -070011218 tg3_full_unlock(tp);
11219
Michael Chand4bc3922005-05-29 14:59:20 -070011220 if (tg3_test_interrupt(tp) != 0) {
11221 etest->flags |= ETH_TEST_FL_FAILED;
11222 data[5] = 1;
11223 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011224
11225 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070011226
Michael Chana71116d2005-05-29 14:58:11 -070011227 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11228 if (netif_running(dev)) {
11229 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011230 err2 = tg3_restart_hw(tp, 1);
11231 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070011232 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011233 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011234
11235 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011236
11237 if (irq_sync && !err2)
11238 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011239 }
Matt Carlson80096062010-08-02 11:26:06 +000011240 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011241 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011242
Michael Chan4cafd3f2005-05-29 14:56:34 -070011243}
11244
Linus Torvalds1da177e2005-04-16 15:20:36 -070011245static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
11246{
11247 struct mii_ioctl_data *data = if_mii(ifr);
11248 struct tg3 *tp = netdev_priv(dev);
11249 int err;
11250
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011251 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011252 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011253 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011254 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011255 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000011256 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011257 }
11258
Matt Carlson33f401a2010-04-05 10:19:27 +000011259 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011260 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000011261 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011262
11263 /* fallthru */
11264 case SIOCGMIIREG: {
11265 u32 mii_regval;
11266
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011267 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011268 break; /* We have no PHY */
11269
Matt Carlsonf746a312011-01-25 15:58:51 +000011270 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) ||
11271 ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
11272 !netif_running(dev)))
Michael Chanbc1c7562006-03-20 17:48:03 -080011273 return -EAGAIN;
11274
David S. Millerf47c11e2005-06-24 20:18:35 -070011275 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011276 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070011277 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011278
11279 data->val_out = mii_regval;
11280
11281 return err;
11282 }
11283
11284 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011285 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011286 break; /* We have no PHY */
11287
Matt Carlsonf746a312011-01-25 15:58:51 +000011288 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) ||
11289 ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
11290 !netif_running(dev)))
Michael Chanbc1c7562006-03-20 17:48:03 -080011291 return -EAGAIN;
11292
David S. Millerf47c11e2005-06-24 20:18:35 -070011293 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011294 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070011295 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011296
11297 return err;
11298
11299 default:
11300 /* do nothing */
11301 break;
11302 }
11303 return -EOPNOTSUPP;
11304}
11305
David S. Miller15f98502005-05-18 22:49:26 -070011306static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11307{
11308 struct tg3 *tp = netdev_priv(dev);
11309
11310 memcpy(ec, &tp->coal, sizeof(*ec));
11311 return 0;
11312}
11313
Michael Chand244c892005-07-05 14:42:33 -070011314static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11315{
11316 struct tg3 *tp = netdev_priv(dev);
11317 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11318 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11319
11320 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
11321 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11322 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11323 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11324 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11325 }
11326
11327 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11328 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11329 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11330 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11331 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11332 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11333 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11334 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11335 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11336 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11337 return -EINVAL;
11338
11339 /* No rx interrupts will be generated if both are zero */
11340 if ((ec->rx_coalesce_usecs == 0) &&
11341 (ec->rx_max_coalesced_frames == 0))
11342 return -EINVAL;
11343
11344 /* No tx interrupts will be generated if both are zero */
11345 if ((ec->tx_coalesce_usecs == 0) &&
11346 (ec->tx_max_coalesced_frames == 0))
11347 return -EINVAL;
11348
11349 /* Only copy relevant parameters, ignore all others. */
11350 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11351 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11352 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11353 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11354 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11355 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11356 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11357 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11358 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11359
11360 if (netif_running(dev)) {
11361 tg3_full_lock(tp, 0);
11362 __tg3_set_coalesce(tp, &tp->coal);
11363 tg3_full_unlock(tp);
11364 }
11365 return 0;
11366}
11367
Jeff Garzik7282d492006-09-13 14:30:00 -040011368static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011369 .get_settings = tg3_get_settings,
11370 .set_settings = tg3_set_settings,
11371 .get_drvinfo = tg3_get_drvinfo,
11372 .get_regs_len = tg3_get_regs_len,
11373 .get_regs = tg3_get_regs,
11374 .get_wol = tg3_get_wol,
11375 .set_wol = tg3_set_wol,
11376 .get_msglevel = tg3_get_msglevel,
11377 .set_msglevel = tg3_set_msglevel,
11378 .nway_reset = tg3_nway_reset,
11379 .get_link = ethtool_op_get_link,
11380 .get_eeprom_len = tg3_get_eeprom_len,
11381 .get_eeprom = tg3_get_eeprom,
11382 .set_eeprom = tg3_set_eeprom,
11383 .get_ringparam = tg3_get_ringparam,
11384 .set_ringparam = tg3_set_ringparam,
11385 .get_pauseparam = tg3_get_pauseparam,
11386 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011387 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011388 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000011389 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011390 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011391 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011392 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011393 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011394};
11395
11396static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11397{
Michael Chan1b277772006-03-20 22:27:48 -080011398 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011399
11400 tp->nvram_size = EEPROM_CHIP_SIZE;
11401
Matt Carlsone4f34112009-02-25 14:25:00 +000011402 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011403 return;
11404
Michael Chanb16250e2006-09-27 16:10:14 -070011405 if ((magic != TG3_EEPROM_MAGIC) &&
11406 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11407 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011408 return;
11409
11410 /*
11411 * Size the chip by reading offsets at increasing powers of two.
11412 * When we encounter our validation signature, we know the addressing
11413 * has wrapped around, and thus have our chip size.
11414 */
Michael Chan1b277772006-03-20 22:27:48 -080011415 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011416
11417 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011418 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011419 return;
11420
Michael Chan18201802006-03-20 22:29:15 -080011421 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011422 break;
11423
11424 cursize <<= 1;
11425 }
11426
11427 tp->nvram_size = cursize;
11428}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011429
Linus Torvalds1da177e2005-04-16 15:20:36 -070011430static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11431{
11432 u32 val;
11433
Matt Carlsondf259d82009-04-20 06:57:14 +000011434 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
11435 tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011436 return;
11437
11438 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011439 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011440 tg3_get_eeprom_size(tp);
11441 return;
11442 }
11443
Matt Carlson6d348f22009-02-25 14:25:52 +000011444 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011445 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011446 /* This is confusing. We want to operate on the
11447 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11448 * call will read from NVRAM and byteswap the data
11449 * according to the byteswapping settings for all
11450 * other register accesses. This ensures the data we
11451 * want will always reside in the lower 16-bits.
11452 * However, the data in NVRAM is in LE format, which
11453 * means the data from the NVRAM read will always be
11454 * opposite the endianness of the CPU. The 16-bit
11455 * byteswap then brings the data to CPU endianness.
11456 */
11457 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011458 return;
11459 }
11460 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070011461 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011462}
11463
11464static void __devinit tg3_get_nvram_info(struct tg3 *tp)
11465{
11466 u32 nvcfg1;
11467
11468 nvcfg1 = tr32(NVRAM_CFG1);
11469 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
11470 tp->tg3_flags2 |= TG3_FLG2_FLASH;
Matt Carlson8590a602009-08-28 12:29:16 +000011471 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011472 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11473 tw32(NVRAM_CFG1, nvcfg1);
11474 }
11475
Michael Chan4c987482005-09-05 17:52:38 -070011476 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) ||
Michael Chana4e2b342005-10-26 15:46:52 -070011477 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011478 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011479 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
11480 tp->nvram_jedecnum = JEDEC_ATMEL;
11481 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11482 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11483 break;
11484 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
11485 tp->nvram_jedecnum = JEDEC_ATMEL;
11486 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
11487 break;
11488 case FLASH_VENDOR_ATMEL_EEPROM:
11489 tp->nvram_jedecnum = JEDEC_ATMEL;
11490 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11491 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11492 break;
11493 case FLASH_VENDOR_ST:
11494 tp->nvram_jedecnum = JEDEC_ST;
11495 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
11496 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11497 break;
11498 case FLASH_VENDOR_SAIFUN:
11499 tp->nvram_jedecnum = JEDEC_SAIFUN;
11500 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
11501 break;
11502 case FLASH_VENDOR_SST_SMALL:
11503 case FLASH_VENDOR_SST_LARGE:
11504 tp->nvram_jedecnum = JEDEC_SST;
11505 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
11506 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011507 }
Matt Carlson8590a602009-08-28 12:29:16 +000011508 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011509 tp->nvram_jedecnum = JEDEC_ATMEL;
11510 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11511 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11512 }
11513}
11514
Matt Carlsona1b950d2009-09-01 13:20:17 +000011515static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
11516{
11517 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
11518 case FLASH_5752PAGE_SIZE_256:
11519 tp->nvram_pagesize = 256;
11520 break;
11521 case FLASH_5752PAGE_SIZE_512:
11522 tp->nvram_pagesize = 512;
11523 break;
11524 case FLASH_5752PAGE_SIZE_1K:
11525 tp->nvram_pagesize = 1024;
11526 break;
11527 case FLASH_5752PAGE_SIZE_2K:
11528 tp->nvram_pagesize = 2048;
11529 break;
11530 case FLASH_5752PAGE_SIZE_4K:
11531 tp->nvram_pagesize = 4096;
11532 break;
11533 case FLASH_5752PAGE_SIZE_264:
11534 tp->nvram_pagesize = 264;
11535 break;
11536 case FLASH_5752PAGE_SIZE_528:
11537 tp->nvram_pagesize = 528;
11538 break;
11539 }
11540}
11541
Michael Chan361b4ac2005-04-21 17:11:21 -070011542static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
11543{
11544 u32 nvcfg1;
11545
11546 nvcfg1 = tr32(NVRAM_CFG1);
11547
Michael Chane6af3012005-04-21 17:12:05 -070011548 /* NVRAM protection for TPM */
11549 if (nvcfg1 & (1 << 27))
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011550 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Michael Chane6af3012005-04-21 17:12:05 -070011551
Michael Chan361b4ac2005-04-21 17:11:21 -070011552 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011553 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
11554 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
11555 tp->nvram_jedecnum = JEDEC_ATMEL;
11556 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11557 break;
11558 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11559 tp->nvram_jedecnum = JEDEC_ATMEL;
11560 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11561 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11562 break;
11563 case FLASH_5752VENDOR_ST_M45PE10:
11564 case FLASH_5752VENDOR_ST_M45PE20:
11565 case FLASH_5752VENDOR_ST_M45PE40:
11566 tp->nvram_jedecnum = JEDEC_ST;
11567 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11568 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11569 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070011570 }
11571
11572 if (tp->tg3_flags2 & TG3_FLG2_FLASH) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000011573 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000011574 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070011575 /* For eeprom, set pagesize to maximum eeprom size */
11576 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11577
11578 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11579 tw32(NVRAM_CFG1, nvcfg1);
11580 }
11581}
11582
Michael Chand3c7b882006-03-23 01:28:25 -080011583static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
11584{
Matt Carlson989a9d22007-05-05 11:51:05 -070011585 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080011586
11587 nvcfg1 = tr32(NVRAM_CFG1);
11588
11589 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070011590 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011591 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson989a9d22007-05-05 11:51:05 -070011592 protect = 1;
11593 }
Michael Chand3c7b882006-03-23 01:28:25 -080011594
Matt Carlson989a9d22007-05-05 11:51:05 -070011595 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11596 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011597 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11598 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11599 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11600 case FLASH_5755VENDOR_ATMEL_FLASH_5:
11601 tp->nvram_jedecnum = JEDEC_ATMEL;
11602 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11603 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11604 tp->nvram_pagesize = 264;
11605 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
11606 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
11607 tp->nvram_size = (protect ? 0x3e200 :
11608 TG3_NVRAM_SIZE_512KB);
11609 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
11610 tp->nvram_size = (protect ? 0x1f200 :
11611 TG3_NVRAM_SIZE_256KB);
11612 else
11613 tp->nvram_size = (protect ? 0x1f200 :
11614 TG3_NVRAM_SIZE_128KB);
11615 break;
11616 case FLASH_5752VENDOR_ST_M45PE10:
11617 case FLASH_5752VENDOR_ST_M45PE20:
11618 case FLASH_5752VENDOR_ST_M45PE40:
11619 tp->nvram_jedecnum = JEDEC_ST;
11620 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11621 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11622 tp->nvram_pagesize = 256;
11623 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
11624 tp->nvram_size = (protect ?
11625 TG3_NVRAM_SIZE_64KB :
11626 TG3_NVRAM_SIZE_128KB);
11627 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
11628 tp->nvram_size = (protect ?
11629 TG3_NVRAM_SIZE_64KB :
11630 TG3_NVRAM_SIZE_256KB);
11631 else
11632 tp->nvram_size = (protect ?
11633 TG3_NVRAM_SIZE_128KB :
11634 TG3_NVRAM_SIZE_512KB);
11635 break;
Michael Chand3c7b882006-03-23 01:28:25 -080011636 }
11637}
11638
Michael Chan1b277772006-03-20 22:27:48 -080011639static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
11640{
11641 u32 nvcfg1;
11642
11643 nvcfg1 = tr32(NVRAM_CFG1);
11644
11645 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011646 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
11647 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11648 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
11649 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11650 tp->nvram_jedecnum = JEDEC_ATMEL;
11651 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11652 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080011653
Matt Carlson8590a602009-08-28 12:29:16 +000011654 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11655 tw32(NVRAM_CFG1, nvcfg1);
11656 break;
11657 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11658 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11659 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11660 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11661 tp->nvram_jedecnum = JEDEC_ATMEL;
11662 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11663 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11664 tp->nvram_pagesize = 264;
11665 break;
11666 case FLASH_5752VENDOR_ST_M45PE10:
11667 case FLASH_5752VENDOR_ST_M45PE20:
11668 case FLASH_5752VENDOR_ST_M45PE40:
11669 tp->nvram_jedecnum = JEDEC_ST;
11670 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11671 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11672 tp->nvram_pagesize = 256;
11673 break;
Michael Chan1b277772006-03-20 22:27:48 -080011674 }
11675}
11676
Matt Carlson6b91fa02007-10-10 18:01:09 -070011677static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
11678{
11679 u32 nvcfg1, protect = 0;
11680
11681 nvcfg1 = tr32(NVRAM_CFG1);
11682
11683 /* NVRAM protection for TPM */
11684 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011685 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011686 protect = 1;
11687 }
11688
11689 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11690 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011691 case FLASH_5761VENDOR_ATMEL_ADB021D:
11692 case FLASH_5761VENDOR_ATMEL_ADB041D:
11693 case FLASH_5761VENDOR_ATMEL_ADB081D:
11694 case FLASH_5761VENDOR_ATMEL_ADB161D:
11695 case FLASH_5761VENDOR_ATMEL_MDB021D:
11696 case FLASH_5761VENDOR_ATMEL_MDB041D:
11697 case FLASH_5761VENDOR_ATMEL_MDB081D:
11698 case FLASH_5761VENDOR_ATMEL_MDB161D:
11699 tp->nvram_jedecnum = JEDEC_ATMEL;
11700 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11701 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11702 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
11703 tp->nvram_pagesize = 256;
11704 break;
11705 case FLASH_5761VENDOR_ST_A_M45PE20:
11706 case FLASH_5761VENDOR_ST_A_M45PE40:
11707 case FLASH_5761VENDOR_ST_A_M45PE80:
11708 case FLASH_5761VENDOR_ST_A_M45PE16:
11709 case FLASH_5761VENDOR_ST_M_M45PE20:
11710 case FLASH_5761VENDOR_ST_M_M45PE40:
11711 case FLASH_5761VENDOR_ST_M_M45PE80:
11712 case FLASH_5761VENDOR_ST_M_M45PE16:
11713 tp->nvram_jedecnum = JEDEC_ST;
11714 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11715 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11716 tp->nvram_pagesize = 256;
11717 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011718 }
11719
11720 if (protect) {
11721 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
11722 } else {
11723 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011724 case FLASH_5761VENDOR_ATMEL_ADB161D:
11725 case FLASH_5761VENDOR_ATMEL_MDB161D:
11726 case FLASH_5761VENDOR_ST_A_M45PE16:
11727 case FLASH_5761VENDOR_ST_M_M45PE16:
11728 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
11729 break;
11730 case FLASH_5761VENDOR_ATMEL_ADB081D:
11731 case FLASH_5761VENDOR_ATMEL_MDB081D:
11732 case FLASH_5761VENDOR_ST_A_M45PE80:
11733 case FLASH_5761VENDOR_ST_M_M45PE80:
11734 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
11735 break;
11736 case FLASH_5761VENDOR_ATMEL_ADB041D:
11737 case FLASH_5761VENDOR_ATMEL_MDB041D:
11738 case FLASH_5761VENDOR_ST_A_M45PE40:
11739 case FLASH_5761VENDOR_ST_M_M45PE40:
11740 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11741 break;
11742 case FLASH_5761VENDOR_ATMEL_ADB021D:
11743 case FLASH_5761VENDOR_ATMEL_MDB021D:
11744 case FLASH_5761VENDOR_ST_A_M45PE20:
11745 case FLASH_5761VENDOR_ST_M_M45PE20:
11746 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11747 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011748 }
11749 }
11750}
11751
Michael Chanb5d37722006-09-27 16:06:21 -070011752static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
11753{
11754 tp->nvram_jedecnum = JEDEC_ATMEL;
11755 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11756 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11757}
11758
Matt Carlson321d32a2008-11-21 17:22:19 -080011759static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
11760{
11761 u32 nvcfg1;
11762
11763 nvcfg1 = tr32(NVRAM_CFG1);
11764
11765 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11766 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11767 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11768 tp->nvram_jedecnum = JEDEC_ATMEL;
11769 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11770 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11771
11772 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11773 tw32(NVRAM_CFG1, nvcfg1);
11774 return;
11775 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11776 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11777 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11778 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11779 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11780 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11781 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11782 tp->nvram_jedecnum = JEDEC_ATMEL;
11783 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11784 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11785
11786 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11787 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11788 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11789 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11790 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11791 break;
11792 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11793 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11794 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11795 break;
11796 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11797 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11798 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11799 break;
11800 }
11801 break;
11802 case FLASH_5752VENDOR_ST_M45PE10:
11803 case FLASH_5752VENDOR_ST_M45PE20:
11804 case FLASH_5752VENDOR_ST_M45PE40:
11805 tp->nvram_jedecnum = JEDEC_ST;
11806 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11807 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11808
11809 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11810 case FLASH_5752VENDOR_ST_M45PE10:
11811 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11812 break;
11813 case FLASH_5752VENDOR_ST_M45PE20:
11814 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11815 break;
11816 case FLASH_5752VENDOR_ST_M45PE40:
11817 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11818 break;
11819 }
11820 break;
11821 default:
Matt Carlsondf259d82009-04-20 06:57:14 +000011822 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
Matt Carlson321d32a2008-11-21 17:22:19 -080011823 return;
11824 }
11825
Matt Carlsona1b950d2009-09-01 13:20:17 +000011826 tg3_nvram_get_pagesize(tp, nvcfg1);
11827 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Matt Carlson321d32a2008-11-21 17:22:19 -080011828 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011829}
11830
11831
11832static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
11833{
11834 u32 nvcfg1;
11835
11836 nvcfg1 = tr32(NVRAM_CFG1);
11837
11838 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11839 case FLASH_5717VENDOR_ATMEL_EEPROM:
11840 case FLASH_5717VENDOR_MICRO_EEPROM:
11841 tp->nvram_jedecnum = JEDEC_ATMEL;
11842 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11843 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11844
11845 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11846 tw32(NVRAM_CFG1, nvcfg1);
11847 return;
11848 case FLASH_5717VENDOR_ATMEL_MDB011D:
11849 case FLASH_5717VENDOR_ATMEL_ADB011B:
11850 case FLASH_5717VENDOR_ATMEL_ADB011D:
11851 case FLASH_5717VENDOR_ATMEL_MDB021D:
11852 case FLASH_5717VENDOR_ATMEL_ADB021B:
11853 case FLASH_5717VENDOR_ATMEL_ADB021D:
11854 case FLASH_5717VENDOR_ATMEL_45USPT:
11855 tp->nvram_jedecnum = JEDEC_ATMEL;
11856 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11857 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11858
11859 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11860 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000011861 /* Detect size with tg3_nvram_get_size() */
11862 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011863 case FLASH_5717VENDOR_ATMEL_ADB021B:
11864 case FLASH_5717VENDOR_ATMEL_ADB021D:
11865 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11866 break;
11867 default:
11868 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11869 break;
11870 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011871 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011872 case FLASH_5717VENDOR_ST_M_M25PE10:
11873 case FLASH_5717VENDOR_ST_A_M25PE10:
11874 case FLASH_5717VENDOR_ST_M_M45PE10:
11875 case FLASH_5717VENDOR_ST_A_M45PE10:
11876 case FLASH_5717VENDOR_ST_M_M25PE20:
11877 case FLASH_5717VENDOR_ST_A_M25PE20:
11878 case FLASH_5717VENDOR_ST_M_M45PE20:
11879 case FLASH_5717VENDOR_ST_A_M45PE20:
11880 case FLASH_5717VENDOR_ST_25USPT:
11881 case FLASH_5717VENDOR_ST_45USPT:
11882 tp->nvram_jedecnum = JEDEC_ST;
11883 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11884 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11885
11886 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11887 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000011888 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000011889 /* Detect size with tg3_nvram_get_size() */
11890 break;
11891 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000011892 case FLASH_5717VENDOR_ST_A_M45PE20:
11893 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11894 break;
11895 default:
11896 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11897 break;
11898 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011899 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011900 default:
11901 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
11902 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080011903 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000011904
11905 tg3_nvram_get_pagesize(tp, nvcfg1);
11906 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
11907 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlson321d32a2008-11-21 17:22:19 -080011908}
11909
Matt Carlson9b91b5f2011-04-05 14:22:47 +000011910static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
11911{
11912 u32 nvcfg1, nvmpinstrp;
11913
11914 nvcfg1 = tr32(NVRAM_CFG1);
11915 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
11916
11917 switch (nvmpinstrp) {
11918 case FLASH_5720_EEPROM_HD:
11919 case FLASH_5720_EEPROM_LD:
11920 tp->nvram_jedecnum = JEDEC_ATMEL;
11921 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11922
11923 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11924 tw32(NVRAM_CFG1, nvcfg1);
11925 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
11926 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11927 else
11928 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
11929 return;
11930 case FLASH_5720VENDOR_M_ATMEL_DB011D:
11931 case FLASH_5720VENDOR_A_ATMEL_DB011B:
11932 case FLASH_5720VENDOR_A_ATMEL_DB011D:
11933 case FLASH_5720VENDOR_M_ATMEL_DB021D:
11934 case FLASH_5720VENDOR_A_ATMEL_DB021B:
11935 case FLASH_5720VENDOR_A_ATMEL_DB021D:
11936 case FLASH_5720VENDOR_M_ATMEL_DB041D:
11937 case FLASH_5720VENDOR_A_ATMEL_DB041B:
11938 case FLASH_5720VENDOR_A_ATMEL_DB041D:
11939 case FLASH_5720VENDOR_M_ATMEL_DB081D:
11940 case FLASH_5720VENDOR_A_ATMEL_DB081D:
11941 case FLASH_5720VENDOR_ATMEL_45USPT:
11942 tp->nvram_jedecnum = JEDEC_ATMEL;
11943 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11944 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11945
11946 switch (nvmpinstrp) {
11947 case FLASH_5720VENDOR_M_ATMEL_DB021D:
11948 case FLASH_5720VENDOR_A_ATMEL_DB021B:
11949 case FLASH_5720VENDOR_A_ATMEL_DB021D:
11950 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11951 break;
11952 case FLASH_5720VENDOR_M_ATMEL_DB041D:
11953 case FLASH_5720VENDOR_A_ATMEL_DB041B:
11954 case FLASH_5720VENDOR_A_ATMEL_DB041D:
11955 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11956 break;
11957 case FLASH_5720VENDOR_M_ATMEL_DB081D:
11958 case FLASH_5720VENDOR_A_ATMEL_DB081D:
11959 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
11960 break;
11961 default:
11962 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11963 break;
11964 }
11965 break;
11966 case FLASH_5720VENDOR_M_ST_M25PE10:
11967 case FLASH_5720VENDOR_M_ST_M45PE10:
11968 case FLASH_5720VENDOR_A_ST_M25PE10:
11969 case FLASH_5720VENDOR_A_ST_M45PE10:
11970 case FLASH_5720VENDOR_M_ST_M25PE20:
11971 case FLASH_5720VENDOR_M_ST_M45PE20:
11972 case FLASH_5720VENDOR_A_ST_M25PE20:
11973 case FLASH_5720VENDOR_A_ST_M45PE20:
11974 case FLASH_5720VENDOR_M_ST_M25PE40:
11975 case FLASH_5720VENDOR_M_ST_M45PE40:
11976 case FLASH_5720VENDOR_A_ST_M25PE40:
11977 case FLASH_5720VENDOR_A_ST_M45PE40:
11978 case FLASH_5720VENDOR_M_ST_M25PE80:
11979 case FLASH_5720VENDOR_M_ST_M45PE80:
11980 case FLASH_5720VENDOR_A_ST_M25PE80:
11981 case FLASH_5720VENDOR_A_ST_M45PE80:
11982 case FLASH_5720VENDOR_ST_25USPT:
11983 case FLASH_5720VENDOR_ST_45USPT:
11984 tp->nvram_jedecnum = JEDEC_ST;
11985 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11986 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11987
11988 switch (nvmpinstrp) {
11989 case FLASH_5720VENDOR_M_ST_M25PE20:
11990 case FLASH_5720VENDOR_M_ST_M45PE20:
11991 case FLASH_5720VENDOR_A_ST_M25PE20:
11992 case FLASH_5720VENDOR_A_ST_M45PE20:
11993 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11994 break;
11995 case FLASH_5720VENDOR_M_ST_M25PE40:
11996 case FLASH_5720VENDOR_M_ST_M45PE40:
11997 case FLASH_5720VENDOR_A_ST_M25PE40:
11998 case FLASH_5720VENDOR_A_ST_M45PE40:
11999 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12000 break;
12001 case FLASH_5720VENDOR_M_ST_M25PE80:
12002 case FLASH_5720VENDOR_M_ST_M45PE80:
12003 case FLASH_5720VENDOR_A_ST_M25PE80:
12004 case FLASH_5720VENDOR_A_ST_M45PE80:
12005 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12006 break;
12007 default:
12008 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12009 break;
12010 }
12011 break;
12012 default:
12013 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
12014 return;
12015 }
12016
12017 tg3_nvram_get_pagesize(tp, nvcfg1);
12018 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
12019 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
12020}
12021
Linus Torvalds1da177e2005-04-16 15:20:36 -070012022/* Chips other than 5700/5701 use the NVRAM for fetching info. */
12023static void __devinit tg3_nvram_init(struct tg3 *tp)
12024{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012025 tw32_f(GRC_EEPROM_ADDR,
12026 (EEPROM_ADDR_FSM_RESET |
12027 (EEPROM_DEFAULT_CLOCK_PERIOD <<
12028 EEPROM_ADDR_CLKPERD_SHIFT)));
12029
Michael Chan9d57f012006-12-07 00:23:25 -080012030 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012031
12032 /* Enable seeprom accesses. */
12033 tw32_f(GRC_LOCAL_CTRL,
12034 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
12035 udelay(100);
12036
12037 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12038 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
12039 tp->tg3_flags |= TG3_FLAG_NVRAM;
12040
Michael Chanec41c7d2006-01-17 02:40:55 -080012041 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000012042 netdev_warn(tp->dev,
12043 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000012044 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080012045 return;
12046 }
Michael Chane6af3012005-04-21 17:12:05 -070012047 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012048
Matt Carlson989a9d22007-05-05 11:51:05 -070012049 tp->nvram_size = 0;
12050
Michael Chan361b4ac2005-04-21 17:11:21 -070012051 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12052 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080012053 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
12054 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070012055 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012056 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12057 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080012058 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012059 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
12060 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070012061 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12062 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000012063 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
12064 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080012065 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012066 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12067 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000012068 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012069 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
12070 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070012071 else
12072 tg3_get_nvram_info(tp);
12073
Matt Carlson989a9d22007-05-05 11:51:05 -070012074 if (tp->nvram_size == 0)
12075 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012076
Michael Chane6af3012005-04-21 17:12:05 -070012077 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080012078 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012079
12080 } else {
12081 tp->tg3_flags &= ~(TG3_FLAG_NVRAM | TG3_FLAG_NVRAM_BUFFERED);
12082
12083 tg3_get_eeprom_size(tp);
12084 }
12085}
12086
Linus Torvalds1da177e2005-04-16 15:20:36 -070012087static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
12088 u32 offset, u32 len, u8 *buf)
12089{
12090 int i, j, rc = 0;
12091 u32 val;
12092
12093 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012094 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012095 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012096
12097 addr = offset + i;
12098
12099 memcpy(&data, buf + i, 4);
12100
Matt Carlson62cedd12009-04-20 14:52:29 -070012101 /*
12102 * The SEEPROM interface expects the data to always be opposite
12103 * the native endian format. We accomplish this by reversing
12104 * all the operations that would have been performed on the
12105 * data from a call to tg3_nvram_read_be32().
12106 */
12107 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012108
12109 val = tr32(GRC_EEPROM_ADDR);
12110 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
12111
12112 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
12113 EEPROM_ADDR_READ);
12114 tw32(GRC_EEPROM_ADDR, val |
12115 (0 << EEPROM_ADDR_DEVID_SHIFT) |
12116 (addr & EEPROM_ADDR_ADDR_MASK) |
12117 EEPROM_ADDR_START |
12118 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012119
Michael Chan9d57f012006-12-07 00:23:25 -080012120 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012121 val = tr32(GRC_EEPROM_ADDR);
12122
12123 if (val & EEPROM_ADDR_COMPLETE)
12124 break;
Michael Chan9d57f012006-12-07 00:23:25 -080012125 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012126 }
12127 if (!(val & EEPROM_ADDR_COMPLETE)) {
12128 rc = -EBUSY;
12129 break;
12130 }
12131 }
12132
12133 return rc;
12134}
12135
12136/* offset and length are dword aligned */
12137static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
12138 u8 *buf)
12139{
12140 int ret = 0;
12141 u32 pagesize = tp->nvram_pagesize;
12142 u32 pagemask = pagesize - 1;
12143 u32 nvram_cmd;
12144 u8 *tmp;
12145
12146 tmp = kmalloc(pagesize, GFP_KERNEL);
12147 if (tmp == NULL)
12148 return -ENOMEM;
12149
12150 while (len) {
12151 int j;
Michael Chane6af3012005-04-21 17:12:05 -070012152 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012153
12154 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012155
Linus Torvalds1da177e2005-04-16 15:20:36 -070012156 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012157 ret = tg3_nvram_read_be32(tp, phy_addr + j,
12158 (__be32 *) (tmp + j));
12159 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012160 break;
12161 }
12162 if (ret)
12163 break;
12164
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012165 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012166 size = pagesize;
12167 if (len < size)
12168 size = len;
12169
12170 len -= size;
12171
12172 memcpy(tmp + page_off, buf, size);
12173
12174 offset = offset + (pagesize - page_off);
12175
Michael Chane6af3012005-04-21 17:12:05 -070012176 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012177
12178 /*
12179 * Before we can erase the flash page, we need
12180 * to issue a special "write enable" command.
12181 */
12182 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12183
12184 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12185 break;
12186
12187 /* Erase the target page */
12188 tw32(NVRAM_ADDR, phy_addr);
12189
12190 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
12191 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
12192
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012193 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012194 break;
12195
12196 /* Issue another write enable to start the write. */
12197 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12198
12199 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12200 break;
12201
12202 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012203 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012204
Al Virob9fc7dc2007-12-17 22:59:57 -080012205 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000012206
Al Virob9fc7dc2007-12-17 22:59:57 -080012207 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012208
12209 tw32(NVRAM_ADDR, phy_addr + j);
12210
12211 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
12212 NVRAM_CMD_WR;
12213
12214 if (j == 0)
12215 nvram_cmd |= NVRAM_CMD_FIRST;
12216 else if (j == (pagesize - 4))
12217 nvram_cmd |= NVRAM_CMD_LAST;
12218
12219 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12220 break;
12221 }
12222 if (ret)
12223 break;
12224 }
12225
12226 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12227 tg3_nvram_exec_cmd(tp, nvram_cmd);
12228
12229 kfree(tmp);
12230
12231 return ret;
12232}
12233
12234/* offset and length are dword aligned */
12235static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
12236 u8 *buf)
12237{
12238 int i, ret = 0;
12239
12240 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012241 u32 page_off, phy_addr, nvram_cmd;
12242 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012243
12244 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080012245 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012246
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012247 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012248
Michael Chan18201802006-03-20 22:29:15 -080012249 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012250
12251 tw32(NVRAM_ADDR, phy_addr);
12252
12253 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
12254
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012255 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012256 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070012257 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012258 nvram_cmd |= NVRAM_CMD_LAST;
12259
12260 if (i == (len - 4))
12261 nvram_cmd |= NVRAM_CMD_LAST;
12262
Matt Carlson321d32a2008-11-21 17:22:19 -080012263 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
12264 !(tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070012265 (tp->nvram_jedecnum == JEDEC_ST) &&
12266 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012267
12268 if ((ret = tg3_nvram_exec_cmd(tp,
12269 NVRAM_CMD_WREN | NVRAM_CMD_GO |
12270 NVRAM_CMD_DONE)))
12271
12272 break;
12273 }
12274 if (!(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
12275 /* We always do complete word writes to eeprom. */
12276 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
12277 }
12278
12279 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12280 break;
12281 }
12282 return ret;
12283}
12284
12285/* offset and length are dword aligned */
12286static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
12287{
12288 int ret;
12289
Linus Torvalds1da177e2005-04-16 15:20:36 -070012290 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070012291 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
12292 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012293 udelay(40);
12294 }
12295
12296 if (!(tp->tg3_flags & TG3_FLAG_NVRAM)) {
12297 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a588792010-04-05 10:19:28 +000012298 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012299 u32 grc_mode;
12300
Michael Chanec41c7d2006-01-17 02:40:55 -080012301 ret = tg3_nvram_lock(tp);
12302 if (ret)
12303 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012304
Michael Chane6af3012005-04-21 17:12:05 -070012305 tg3_enable_nvram_access(tp);
12306 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +000012307 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012308 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012309
12310 grc_mode = tr32(GRC_MODE);
12311 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
12312
12313 if ((tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) ||
12314 !(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
12315
12316 ret = tg3_nvram_write_block_buffered(tp, offset, len,
12317 buf);
Matt Carlson859a588792010-04-05 10:19:28 +000012318 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012319 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
12320 buf);
12321 }
12322
12323 grc_mode = tr32(GRC_MODE);
12324 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
12325
Michael Chane6af3012005-04-21 17:12:05 -070012326 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012327 tg3_nvram_unlock(tp);
12328 }
12329
12330 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070012331 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012332 udelay(40);
12333 }
12334
12335 return ret;
12336}
12337
12338struct subsys_tbl_ent {
12339 u16 subsys_vendor, subsys_devid;
12340 u32 phy_id;
12341};
12342
Matt Carlson24daf2b2010-02-17 15:17:02 +000012343static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012344 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012345 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012346 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012347 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012348 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012349 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012350 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012351 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12352 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
12353 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012354 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012355 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012356 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012357 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12358 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
12359 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012360 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012361 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012362 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012363 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012364 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012365 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012366 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012367
12368 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012369 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012370 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012371 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012372 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012373 { TG3PCI_SUBVENDOR_ID_3COM,
12374 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
12375 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012376 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012377 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012378 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012379
12380 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012381 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012382 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012383 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012384 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012385 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012386 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012387 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012388 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012389
12390 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012391 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012392 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012393 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012394 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012395 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12396 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
12397 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012398 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012399 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012400 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012401
12402 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012403 { TG3PCI_SUBVENDOR_ID_IBM,
12404 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012405};
12406
Matt Carlson24daf2b2010-02-17 15:17:02 +000012407static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012408{
12409 int i;
12410
12411 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
12412 if ((subsys_id_to_phy_id[i].subsys_vendor ==
12413 tp->pdev->subsystem_vendor) &&
12414 (subsys_id_to_phy_id[i].subsys_devid ==
12415 tp->pdev->subsystem_device))
12416 return &subsys_id_to_phy_id[i];
12417 }
12418 return NULL;
12419}
12420
Michael Chan7d0c41e2005-04-21 17:06:20 -070012421static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012422{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012423 u32 val;
Michael Chancaf636c72006-03-22 01:05:31 -080012424 u16 pmcsr;
12425
12426 /* On some early chips the SRAM cannot be accessed in D3hot state,
12427 * so need make sure we're in D0.
12428 */
12429 pci_read_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, &pmcsr);
12430 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
12431 pci_write_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, pmcsr);
12432 msleep(1);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012433
12434 /* Make sure register accesses (indirect or otherwise)
12435 * will function correctly.
12436 */
12437 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12438 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012439
David S. Millerf49639e2006-06-09 11:58:36 -070012440 /* The memory arbiter has to be enabled in order for SRAM accesses
12441 * to succeed. Normally on powerup the tg3 chip firmware will make
12442 * sure it is enabled, but other entities such as system netboot
12443 * code might disable it.
12444 */
12445 val = tr32(MEMARB_MODE);
12446 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
12447
Matt Carlson79eb6902010-02-17 15:17:03 +000012448 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012449 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12450
Gary Zambranoa85feb82007-05-05 11:52:19 -070012451 /* Assume an onboard device and WOL capable by default. */
12452 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT | TG3_FLAG_WOL_CAP;
David S. Miller72b845e2006-03-14 14:11:48 -080012453
Michael Chanb5d37722006-09-27 16:06:21 -070012454 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012455 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Michael Chanb5d37722006-09-27 16:06:21 -070012456 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012457 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12458 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012459 val = tr32(VCPU_CFGSHDW);
12460 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Matt Carlson8ed5d972007-05-07 00:25:49 -070012461 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
Matt Carlson0527ba32007-10-10 18:03:30 -070012462 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Matt Carlson20232762008-12-21 20:18:56 -080012463 (val & VCPU_CFGSHDW_WOL_MAGPKT))
Matt Carlson0527ba32007-10-10 18:03:30 -070012464 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012465 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070012466 }
12467
Linus Torvalds1da177e2005-04-16 15:20:36 -070012468 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12469 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12470 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012471 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012472 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012473
12474 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12475 tp->nic_sram_data_cfg = nic_cfg;
12476
12477 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
12478 ver >>= NIC_SRAM_DATA_VER_SHIFT;
12479 if ((GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700) &&
12480 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) &&
12481 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703) &&
12482 (ver > 0) && (ver < 0x100))
12483 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
12484
Matt Carlsona9daf362008-05-25 23:49:44 -070012485 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
12486 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
12487
Linus Torvalds1da177e2005-04-16 15:20:36 -070012488 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
12489 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
12490 eeprom_phy_serdes = 1;
12491
12492 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
12493 if (nic_phy_id != 0) {
12494 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
12495 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
12496
12497 eeprom_phy_id = (id1 >> 16) << 10;
12498 eeprom_phy_id |= (id2 & 0xfc00) << 16;
12499 eeprom_phy_id |= (id2 & 0x03ff) << 0;
12500 } else
12501 eeprom_phy_id = 0;
12502
Michael Chan7d0c41e2005-04-21 17:06:20 -070012503 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070012504 if (eeprom_phy_serdes) {
Matt Carlsona50d0792010-06-05 17:24:37 +000012505 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012506 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000012507 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012508 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070012509 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070012510
John W. Linvillecbf46852005-04-21 17:01:29 -070012511 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012512 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
12513 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070012514 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070012515 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
12516
12517 switch (led_cfg) {
12518 default:
12519 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
12520 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12521 break;
12522
12523 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
12524 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12525 break;
12526
12527 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
12528 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070012529
12530 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
12531 * read on some older 5700/5701 bootcode.
12532 */
12533 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
12534 ASIC_REV_5700 ||
12535 GET_ASIC_REV(tp->pci_chip_rev_id) ==
12536 ASIC_REV_5701)
12537 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12538
Linus Torvalds1da177e2005-04-16 15:20:36 -070012539 break;
12540
12541 case SHASTA_EXT_LED_SHARED:
12542 tp->led_ctrl = LED_CTRL_MODE_SHARED;
12543 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
12544 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
12545 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12546 LED_CTRL_MODE_PHY_2);
12547 break;
12548
12549 case SHASTA_EXT_LED_MAC:
12550 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
12551 break;
12552
12553 case SHASTA_EXT_LED_COMBO:
12554 tp->led_ctrl = LED_CTRL_MODE_COMBO;
12555 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
12556 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12557 LED_CTRL_MODE_PHY_2);
12558 break;
12559
Stephen Hemminger855e1112008-04-16 16:37:28 -070012560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012561
12562 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12563 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
12564 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
12565 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12566
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012567 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
12568 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080012569
Michael Chan9d26e212006-12-07 00:21:14 -080012570 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012571 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012572 if ((tp->pdev->subsystem_vendor ==
12573 PCI_VENDOR_ID_ARIMA) &&
12574 (tp->pdev->subsystem_device == 0x205a ||
12575 tp->pdev->subsystem_device == 0x2063))
12576 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
12577 } else {
David S. Millerf49639e2006-06-09 11:58:36 -070012578 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012579 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012581
12582 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
12583 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
John W. Linvillecbf46852005-04-21 17:01:29 -070012584 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012585 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
12586 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012587
12588 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
12589 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Matt Carlson0d3031d2007-10-10 18:02:43 -070012590 tp->tg3_flags3 |= TG3_FLG3_ENABLE_APE;
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012591
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012592 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070012593 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
12594 tp->tg3_flags &= ~TG3_FLAG_WOL_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012595
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070012596 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012597 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE))
Matt Carlson0527ba32007-10-10 18:03:30 -070012598 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
12599
Linus Torvalds1da177e2005-04-16 15:20:36 -070012600 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012601 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012602
12603 /* serdes signal pre-emphasis in register 0x590 set by */
12604 /* bootcode if bit 18 is set */
12605 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012606 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070012607
Matt Carlson1407deb2011-04-05 14:22:44 +000012608 if (((tp->tg3_flags3 & TG3_FLG3_57765_PLUS) ||
Matt Carlson2e1e3292010-11-24 08:31:53 +000012609 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
12610 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX))) &&
Matt Carlson6833c042008-11-21 17:18:59 -080012611 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012612 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080012613
Matt Carlson8c69b1e2010-08-02 11:26:00 +000012614 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
12615 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlson1407deb2011-04-05 14:22:44 +000012616 !(tp->tg3_flags3 & TG3_FLG3_57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070012617 u32 cfg3;
12618
12619 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
12620 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
12621 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
12622 }
Matt Carlsona9daf362008-05-25 23:49:44 -070012623
Matt Carlson14417062010-02-17 15:16:59 +000012624 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
12625 tp->tg3_flags3 |= TG3_FLG3_RGMII_INBAND_DISABLE;
Matt Carlsona9daf362008-05-25 23:49:44 -070012626 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
12627 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_RX_EN;
12628 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
12629 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_TX_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012630 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012631done:
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000012632 if (tp->tg3_flags & TG3_FLAG_WOL_CAP)
12633 device_set_wakeup_enable(&tp->pdev->dev,
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012634 tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000012635 else
12636 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012637}
12638
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012639static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
12640{
12641 int i;
12642 u32 val;
12643
12644 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
12645 tw32(OTP_CTRL, cmd);
12646
12647 /* Wait for up to 1 ms for command to execute. */
12648 for (i = 0; i < 100; i++) {
12649 val = tr32(OTP_STATUS);
12650 if (val & OTP_STATUS_CMD_DONE)
12651 break;
12652 udelay(10);
12653 }
12654
12655 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
12656}
12657
12658/* Read the gphy configuration from the OTP region of the chip. The gphy
12659 * configuration is a 32-bit value that straddles the alignment boundary.
12660 * We do two 32-bit reads and then shift and merge the results.
12661 */
12662static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
12663{
12664 u32 bhalf_otp, thalf_otp;
12665
12666 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
12667
12668 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
12669 return 0;
12670
12671 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
12672
12673 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12674 return 0;
12675
12676 thalf_otp = tr32(OTP_READ_DATA);
12677
12678 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
12679
12680 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12681 return 0;
12682
12683 bhalf_otp = tr32(OTP_READ_DATA);
12684
12685 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
12686}
12687
Matt Carlsone256f8a2011-03-09 16:58:24 +000012688static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
12689{
12690 u32 adv = ADVERTISED_Autoneg |
12691 ADVERTISED_Pause;
12692
12693 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
12694 adv |= ADVERTISED_1000baseT_Half |
12695 ADVERTISED_1000baseT_Full;
12696
12697 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
12698 adv |= ADVERTISED_100baseT_Half |
12699 ADVERTISED_100baseT_Full |
12700 ADVERTISED_10baseT_Half |
12701 ADVERTISED_10baseT_Full |
12702 ADVERTISED_TP;
12703 else
12704 adv |= ADVERTISED_FIBRE;
12705
12706 tp->link_config.advertising = adv;
12707 tp->link_config.speed = SPEED_INVALID;
12708 tp->link_config.duplex = DUPLEX_INVALID;
12709 tp->link_config.autoneg = AUTONEG_ENABLE;
12710 tp->link_config.active_speed = SPEED_INVALID;
12711 tp->link_config.active_duplex = DUPLEX_INVALID;
12712 tp->link_config.orig_speed = SPEED_INVALID;
12713 tp->link_config.orig_duplex = DUPLEX_INVALID;
12714 tp->link_config.orig_autoneg = AUTONEG_INVALID;
12715}
12716
Michael Chan7d0c41e2005-04-21 17:06:20 -070012717static int __devinit tg3_phy_probe(struct tg3 *tp)
12718{
12719 u32 hw_phy_id_1, hw_phy_id_2;
12720 u32 hw_phy_id, hw_phy_id_masked;
12721 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012722
Matt Carlsone256f8a2011-03-09 16:58:24 +000012723 /* flow control autonegotiation is default behavior */
12724 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
12725 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
12726
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012727 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
12728 return tg3_phy_init(tp);
12729
Linus Torvalds1da177e2005-04-16 15:20:36 -070012730 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010012731 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012732 */
12733 err = 0;
Matt Carlson0d3031d2007-10-10 18:02:43 -070012734 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
12735 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000012736 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012737 } else {
12738 /* Now read the physical PHY_ID from the chip and verify
12739 * that it is sane. If it doesn't look good, we fall back
12740 * to either the hard-coded table based PHY_ID and failing
12741 * that the value found in the eeprom area.
12742 */
12743 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
12744 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
12745
12746 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
12747 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
12748 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
12749
Matt Carlson79eb6902010-02-17 15:17:03 +000012750 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012751 }
12752
Matt Carlson79eb6902010-02-17 15:17:03 +000012753 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012754 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000012755 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012756 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070012757 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012758 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012759 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000012760 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070012761 /* Do nothing, phy ID already set up in
12762 * tg3_get_eeprom_hw_cfg().
12763 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012764 } else {
12765 struct subsys_tbl_ent *p;
12766
12767 /* No eeprom signature? Try the hardcoded
12768 * subsys device table.
12769 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012770 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012771 if (!p)
12772 return -ENODEV;
12773
12774 tp->phy_id = p->phy_id;
12775 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000012776 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012777 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012778 }
12779 }
12780
Matt Carlsona6b68da2010-12-06 08:28:52 +000012781 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
12782 ((tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
12783 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
12784 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12785 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000012786 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
12787
Matt Carlsone256f8a2011-03-09 16:58:24 +000012788 tg3_phy_init_link_config(tp);
12789
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012790 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlson0d3031d2007-10-10 18:02:43 -070012791 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012792 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan3600d912006-12-07 00:21:48 -080012793 u32 bmsr, adv_reg, tg3_ctrl, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012794
12795 tg3_readphy(tp, MII_BMSR, &bmsr);
12796 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
12797 (bmsr & BMSR_LSTATUS))
12798 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012799
Linus Torvalds1da177e2005-04-16 15:20:36 -070012800 err = tg3_phy_reset(tp);
12801 if (err)
12802 return err;
12803
12804 adv_reg = (ADVERTISE_10HALF | ADVERTISE_10FULL |
12805 ADVERTISE_100HALF | ADVERTISE_100FULL |
12806 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
12807 tg3_ctrl = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012808 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012809 tg3_ctrl = (MII_TG3_CTRL_ADV_1000_HALF |
12810 MII_TG3_CTRL_ADV_1000_FULL);
12811 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
12812 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
12813 tg3_ctrl |= (MII_TG3_CTRL_AS_MASTER |
12814 MII_TG3_CTRL_ENABLE_AS_MASTER);
12815 }
12816
Michael Chan3600d912006-12-07 00:21:48 -080012817 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
12818 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
12819 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
12820 if (!tg3_copper_is_advertising_all(tp, mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012821 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
12822
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012823 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012824 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12825
12826 tg3_writephy(tp, MII_BMCR,
12827 BMCR_ANENABLE | BMCR_ANRESTART);
12828 }
12829 tg3_phy_set_wirespeed(tp);
12830
12831 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012832 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012833 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12834 }
12835
12836skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000012837 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012838 err = tg3_init_5401phy_dsp(tp);
12839 if (err)
12840 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012841
Linus Torvalds1da177e2005-04-16 15:20:36 -070012842 err = tg3_init_5401phy_dsp(tp);
12843 }
12844
Linus Torvalds1da177e2005-04-16 15:20:36 -070012845 return err;
12846}
12847
Matt Carlson184b8902010-04-05 10:19:25 +000012848static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012849{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000012850 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012851 unsigned int block_end, rosize, len;
Matt Carlson184b8902010-04-05 10:19:25 +000012852 int j, i = 0;
Michael Chan1b277772006-03-20 22:27:48 -080012853 u32 magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012854
Matt Carlsondf259d82009-04-20 06:57:14 +000012855 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
12856 tg3_nvram_read(tp, 0x0, &magic))
Matt Carlsona4a8bb12010-09-15 09:00:00 +000012857 goto out_no_vpd;
12858
12859 vpd_data = kmalloc(TG3_NVM_VPD_LEN, GFP_KERNEL);
12860 if (!vpd_data)
12861 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012862
Michael Chan18201802006-03-20 22:29:15 -080012863 if (magic == TG3_EEPROM_MAGIC) {
Matt Carlson141518c2009-12-03 08:36:22 +000012864 for (i = 0; i < TG3_NVM_VPD_LEN; i += 4) {
Michael Chan1b277772006-03-20 22:27:48 -080012865 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012866
Matt Carlson6d348f22009-02-25 14:25:52 +000012867 /* The data is in little-endian format in NVRAM.
12868 * Use the big-endian read routines to preserve
12869 * the byte order as it exists in NVRAM.
12870 */
Matt Carlson141518c2009-12-03 08:36:22 +000012871 if (tg3_nvram_read_be32(tp, TG3_NVM_VPD_OFF + i, &tmp))
Michael Chan1b277772006-03-20 22:27:48 -080012872 goto out_not_found;
12873
Matt Carlson6d348f22009-02-25 14:25:52 +000012874 memcpy(&vpd_data[i], &tmp, sizeof(tmp));
Michael Chan1b277772006-03-20 22:27:48 -080012875 }
12876 } else {
Matt Carlson94c982b2009-12-03 08:36:23 +000012877 ssize_t cnt;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012878 unsigned int pos = 0;
Michael Chan1b277772006-03-20 22:27:48 -080012879
Matt Carlson94c982b2009-12-03 08:36:23 +000012880 for (; pos < TG3_NVM_VPD_LEN && i < 3; i++, pos += cnt) {
12881 cnt = pci_read_vpd(tp->pdev, pos,
12882 TG3_NVM_VPD_LEN - pos,
12883 &vpd_data[pos]);
David Sterba824f5f32010-12-29 03:40:31 +000012884 if (cnt == -ETIMEDOUT || cnt == -EINTR)
Matt Carlson94c982b2009-12-03 08:36:23 +000012885 cnt = 0;
12886 else if (cnt < 0)
David S. Millerf49639e2006-06-09 11:58:36 -070012887 goto out_not_found;
Michael Chan1b277772006-03-20 22:27:48 -080012888 }
Matt Carlson94c982b2009-12-03 08:36:23 +000012889 if (pos != TG3_NVM_VPD_LEN)
12890 goto out_not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012891 }
12892
Matt Carlson4181b2c2010-02-26 14:04:45 +000012893 i = pci_vpd_find_tag(vpd_data, 0, TG3_NVM_VPD_LEN,
12894 PCI_VPD_LRDT_RO_DATA);
12895 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012896 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012897
12898 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
12899 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
12900 i += PCI_VPD_LRDT_TAG_SIZE;
12901
12902 if (block_end > TG3_NVM_VPD_LEN)
12903 goto out_not_found;
12904
Matt Carlson184b8902010-04-05 10:19:25 +000012905 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12906 PCI_VPD_RO_KEYWORD_MFR_ID);
12907 if (j > 0) {
12908 len = pci_vpd_info_field_size(&vpd_data[j]);
12909
12910 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12911 if (j + len > block_end || len != 4 ||
12912 memcmp(&vpd_data[j], "1028", 4))
12913 goto partno;
12914
12915 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12916 PCI_VPD_RO_KEYWORD_VENDOR0);
12917 if (j < 0)
12918 goto partno;
12919
12920 len = pci_vpd_info_field_size(&vpd_data[j]);
12921
12922 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12923 if (j + len > block_end)
12924 goto partno;
12925
12926 memcpy(tp->fw_ver, &vpd_data[j], len);
12927 strncat(tp->fw_ver, " bc ", TG3_NVM_VPD_LEN - len - 1);
12928 }
12929
12930partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000012931 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12932 PCI_VPD_RO_KEYWORD_PARTNO);
12933 if (i < 0)
12934 goto out_not_found;
12935
12936 len = pci_vpd_info_field_size(&vpd_data[i]);
12937
12938 i += PCI_VPD_INFO_FLD_HDR_SIZE;
12939 if (len > TG3_BPN_SIZE ||
12940 (len + i) > TG3_NVM_VPD_LEN)
12941 goto out_not_found;
12942
12943 memcpy(tp->board_part_number, &vpd_data[i], len);
12944
Linus Torvalds1da177e2005-04-16 15:20:36 -070012945out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000012946 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000012947 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000012948 return;
12949
12950out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000012951 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
12952 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
12953 strcpy(tp->board_part_number, "BCM5717");
12954 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
12955 strcpy(tp->board_part_number, "BCM5718");
12956 else
12957 goto nomatch;
12958 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
12959 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
12960 strcpy(tp->board_part_number, "BCM57780");
12961 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
12962 strcpy(tp->board_part_number, "BCM57760");
12963 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
12964 strcpy(tp->board_part_number, "BCM57790");
12965 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
12966 strcpy(tp->board_part_number, "BCM57788");
12967 else
12968 goto nomatch;
12969 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
12970 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
12971 strcpy(tp->board_part_number, "BCM57761");
12972 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
12973 strcpy(tp->board_part_number, "BCM57765");
12974 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
12975 strcpy(tp->board_part_number, "BCM57781");
12976 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
12977 strcpy(tp->board_part_number, "BCM57785");
12978 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
12979 strcpy(tp->board_part_number, "BCM57791");
12980 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
12981 strcpy(tp->board_part_number, "BCM57795");
12982 else
12983 goto nomatch;
12984 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070012985 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000012986 } else {
12987nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070012988 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000012989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012990}
12991
Matt Carlson9c8a6202007-10-21 16:16:08 -070012992static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
12993{
12994 u32 val;
12995
Matt Carlsone4f34112009-02-25 14:25:00 +000012996 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012997 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000012998 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012999 val != 0)
13000 return 0;
13001
13002 return 1;
13003}
13004
Matt Carlsonacd9c112009-02-25 14:26:33 +000013005static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13006{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013007 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000013008 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013009 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013010
13011 if (tg3_nvram_read(tp, 0xc, &offset) ||
13012 tg3_nvram_read(tp, 0x4, &start))
13013 return;
13014
13015 offset = tg3_nvram_logical_addr(tp, offset);
13016
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013017 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013018 return;
13019
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013020 if ((val & 0xfc000000) == 0x0c000000) {
13021 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013022 return;
13023
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013024 if (val == 0)
13025 newver = true;
13026 }
13027
Matt Carlson75f99362010-04-05 10:19:24 +000013028 dst_off = strlen(tp->fw_ver);
13029
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013030 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013031 if (TG3_VER_SIZE - dst_off < 16 ||
13032 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013033 return;
13034
13035 offset = offset + ver_offset - start;
13036 for (i = 0; i < 16; i += 4) {
13037 __be32 v;
13038 if (tg3_nvram_read_be32(tp, offset + i, &v))
13039 return;
13040
Matt Carlson75f99362010-04-05 10:19:24 +000013041 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013042 }
13043 } else {
13044 u32 major, minor;
13045
13046 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13047 return;
13048
13049 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13050 TG3_NVM_BCVER_MAJSFT;
13051 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013052 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13053 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013054 }
13055}
13056
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013057static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13058{
13059 u32 val, major, minor;
13060
13061 /* Use native endian representation */
13062 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13063 return;
13064
13065 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13066 TG3_NVM_HWSB_CFG1_MAJSFT;
13067 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13068 TG3_NVM_HWSB_CFG1_MINSFT;
13069
13070 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13071}
13072
Matt Carlsondfe00d72008-11-21 17:19:41 -080013073static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13074{
13075 u32 offset, major, minor, build;
13076
Matt Carlson75f99362010-04-05 10:19:24 +000013077 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013078
13079 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13080 return;
13081
13082 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13083 case TG3_EEPROM_SB_REVISION_0:
13084 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13085 break;
13086 case TG3_EEPROM_SB_REVISION_2:
13087 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13088 break;
13089 case TG3_EEPROM_SB_REVISION_3:
13090 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13091 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013092 case TG3_EEPROM_SB_REVISION_4:
13093 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13094 break;
13095 case TG3_EEPROM_SB_REVISION_5:
13096 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13097 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013098 case TG3_EEPROM_SB_REVISION_6:
13099 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13100 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013101 default:
13102 return;
13103 }
13104
Matt Carlsone4f34112009-02-25 14:25:00 +000013105 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013106 return;
13107
13108 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13109 TG3_EEPROM_SB_EDH_BLD_SHFT;
13110 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13111 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13112 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13113
13114 if (minor > 99 || build > 26)
13115 return;
13116
Matt Carlson75f99362010-04-05 10:19:24 +000013117 offset = strlen(tp->fw_ver);
13118 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13119 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013120
13121 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013122 offset = strlen(tp->fw_ver);
13123 if (offset < TG3_VER_SIZE - 1)
13124 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013125 }
13126}
13127
Matt Carlsonacd9c112009-02-25 14:26:33 +000013128static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013129{
13130 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013131 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013132
13133 for (offset = TG3_NVM_DIR_START;
13134 offset < TG3_NVM_DIR_END;
13135 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013136 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013137 return;
13138
13139 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13140 break;
13141 }
13142
13143 if (offset == TG3_NVM_DIR_END)
13144 return;
13145
13146 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
13147 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013148 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013149 return;
13150
Matt Carlsone4f34112009-02-25 14:25:00 +000013151 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013152 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013153 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013154 return;
13155
13156 offset += val - start;
13157
Matt Carlsonacd9c112009-02-25 14:26:33 +000013158 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013159
Matt Carlsonacd9c112009-02-25 14:26:33 +000013160 tp->fw_ver[vlen++] = ',';
13161 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013162
13163 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013164 __be32 v;
13165 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013166 return;
13167
Al Virob9fc7dc2007-12-17 22:59:57 -080013168 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013169
Matt Carlsonacd9c112009-02-25 14:26:33 +000013170 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13171 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013172 break;
13173 }
13174
Matt Carlsonacd9c112009-02-25 14:26:33 +000013175 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13176 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013177 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013178}
13179
Matt Carlson7fd76442009-02-25 14:27:20 +000013180static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13181{
13182 int vlen;
13183 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013184 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013185
13186 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) ||
13187 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
13188 return;
13189
13190 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13191 if (apedata != APE_SEG_SIG_MAGIC)
13192 return;
13193
13194 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13195 if (!(apedata & APE_FW_STATUS_READY))
13196 return;
13197
13198 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13199
Matt Carlsondc6d0742010-09-15 08:59:55 +000013200 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
13201 tp->tg3_flags3 |= TG3_FLG3_APE_HAS_NCSI;
Matt Carlsonecc79642010-08-02 11:26:01 +000013202 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013203 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013204 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013205 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013206
Matt Carlson7fd76442009-02-25 14:27:20 +000013207 vlen = strlen(tp->fw_ver);
13208
Matt Carlsonecc79642010-08-02 11:26:01 +000013209 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13210 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013211 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13212 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13213 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13214 (apedata & APE_FW_VERSION_BLDMSK));
13215}
13216
Matt Carlsonacd9c112009-02-25 14:26:33 +000013217static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13218{
13219 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013220 bool vpd_vers = false;
13221
13222 if (tp->fw_ver[0] != 0)
13223 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013224
Matt Carlsondf259d82009-04-20 06:57:14 +000013225 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) {
Matt Carlson75f99362010-04-05 10:19:24 +000013226 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013227 return;
13228 }
13229
Matt Carlsonacd9c112009-02-25 14:26:33 +000013230 if (tg3_nvram_read(tp, 0, &val))
13231 return;
13232
13233 if (val == TG3_EEPROM_MAGIC)
13234 tg3_read_bc_ver(tp);
13235 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13236 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013237 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13238 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013239 else
13240 return;
13241
13242 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson75f99362010-04-05 10:19:24 +000013243 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) || vpd_vers)
13244 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013245
13246 tg3_read_mgmtfw_ver(tp);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013247
Matt Carlson75f99362010-04-05 10:19:24 +000013248done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013249 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013250}
13251
Michael Chan7544b092007-05-05 13:08:32 -070013252static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
13253
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013254static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13255{
Matt Carlsonde9f5232011-04-05 14:22:43 +000013256 if (tp->tg3_flags3 & TG3_FLG3_LRG_PROD_RING_CAP)
13257 return TG3_RX_RET_MAX_SIZE_5717;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013258 else if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
13259 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013260 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013261 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013262 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013263}
13264
Matt Carlson41434702011-03-09 16:58:22 +000013265static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013266 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13267 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13268 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13269 { },
13270};
13271
Linus Torvalds1da177e2005-04-16 15:20:36 -070013272static int __devinit tg3_get_invariants(struct tg3 *tp)
13273{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013274 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013275 u32 pci_state_reg, grc_misc_cfg;
13276 u32 val;
13277 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013278 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013279
Linus Torvalds1da177e2005-04-16 15:20:36 -070013280 /* Force memory write invalidate off. If we leave it on,
13281 * then on 5700_BX chips we have to enable a workaround.
13282 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13283 * to match the cacheline size. The Broadcom driver have this
13284 * workaround but turns MWI off all the times so never uses
13285 * it. This seems to suggest that the workaround is insufficient.
13286 */
13287 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13288 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13289 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13290
13291 /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
13292 * has the register indirect write enable bit set before
13293 * we try to access any of the MMIO registers. It is also
13294 * critical that the PCI-X hw workaround situation is decided
13295 * before that as well.
13296 */
13297 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13298 &misc_ctrl_reg);
13299
13300 tp->pci_chip_rev_id = (misc_ctrl_reg >>
13301 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070013302 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13303 u32 prod_id_asic_rev;
13304
Matt Carlson5001e2f2009-11-13 13:03:51 +000013305 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13306 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013307 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
13308 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013309 pci_read_config_dword(tp->pdev,
13310 TG3PCI_GEN2_PRODID_ASICREV,
13311 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000013312 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13313 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13314 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13315 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13316 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13317 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13318 pci_read_config_dword(tp->pdev,
13319 TG3PCI_GEN15_PRODID_ASICREV,
13320 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013321 else
13322 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
13323 &prod_id_asic_rev);
13324
Matt Carlson321d32a2008-11-21 17:22:19 -080013325 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070013326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013327
Michael Chanff645be2005-04-21 17:09:53 -070013328 /* Wrong chip ID in 5752 A0. This code can be removed later
13329 * as A0 is not in production.
13330 */
13331 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13332 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13333
Michael Chan68929142005-08-09 20:17:14 -070013334 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
13335 * we need to disable memory and use config. cycles
13336 * only to access all registers. The 5702/03 chips
13337 * can mistakenly decode the special cycles from the
13338 * ICH chipsets as memory write cycles, causing corruption
13339 * of register and memory space. Only certain ICH bridges
13340 * will drive special cycles with non-zero data during the
13341 * address phase which can fall within the 5703's address
13342 * range. This is not an ICH bug as the PCI spec allows
13343 * non-zero address during special cycles. However, only
13344 * these ICH bridges are known to drive non-zero addresses
13345 * during special cycles.
13346 *
13347 * Since special cycles do not cross PCI bridges, we only
13348 * enable this workaround if the 5703 is on the secondary
13349 * bus of these ICH bridges.
13350 */
13351 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
13352 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
13353 static struct tg3_dev_id {
13354 u32 vendor;
13355 u32 device;
13356 u32 rev;
13357 } ich_chipsets[] = {
13358 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
13359 PCI_ANY_ID },
13360 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
13361 PCI_ANY_ID },
13362 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
13363 0xa },
13364 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
13365 PCI_ANY_ID },
13366 { },
13367 };
13368 struct tg3_dev_id *pci_id = &ich_chipsets[0];
13369 struct pci_dev *bridge = NULL;
13370
13371 while (pci_id->vendor != 0) {
13372 bridge = pci_get_device(pci_id->vendor, pci_id->device,
13373 bridge);
13374 if (!bridge) {
13375 pci_id++;
13376 continue;
13377 }
13378 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070013379 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070013380 continue;
13381 }
13382 if (bridge->subordinate &&
13383 (bridge->subordinate->number ==
13384 tp->pdev->bus->number)) {
13385
13386 tp->tg3_flags2 |= TG3_FLG2_ICH_WORKAROUND;
13387 pci_dev_put(bridge);
13388 break;
13389 }
13390 }
13391 }
13392
Matt Carlson41588ba2008-04-19 18:12:33 -070013393 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
13394 static struct tg3_dev_id {
13395 u32 vendor;
13396 u32 device;
13397 } bridge_chipsets[] = {
13398 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
13399 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
13400 { },
13401 };
13402 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
13403 struct pci_dev *bridge = NULL;
13404
13405 while (pci_id->vendor != 0) {
13406 bridge = pci_get_device(pci_id->vendor,
13407 pci_id->device,
13408 bridge);
13409 if (!bridge) {
13410 pci_id++;
13411 continue;
13412 }
13413 if (bridge->subordinate &&
13414 (bridge->subordinate->number <=
13415 tp->pdev->bus->number) &&
13416 (bridge->subordinate->subordinate >=
13417 tp->pdev->bus->number)) {
13418 tp->tg3_flags3 |= TG3_FLG3_5701_DMA_BUG;
13419 pci_dev_put(bridge);
13420 break;
13421 }
13422 }
13423 }
13424
Michael Chan4a29cc22006-03-19 13:21:12 -080013425 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
13426 * DMA addresses > 40-bit. This bridge may have other additional
13427 * 57xx devices behind it in some 4-port NIC designs for example.
13428 * Any tg3 device found behind the bridge will also need the 40-bit
13429 * DMA workaround.
13430 */
Michael Chana4e2b342005-10-26 15:46:52 -070013431 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
13432 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
13433 tp->tg3_flags2 |= TG3_FLG2_5780_CLASS;
Michael Chan4a29cc22006-03-19 13:21:12 -080013434 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
Michael Chan4cf78e42005-07-25 12:29:19 -070013435 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000013436 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080013437 struct pci_dev *bridge = NULL;
13438
13439 do {
13440 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
13441 PCI_DEVICE_ID_SERVERWORKS_EPB,
13442 bridge);
13443 if (bridge && bridge->subordinate &&
13444 (bridge->subordinate->number <=
13445 tp->pdev->bus->number) &&
13446 (bridge->subordinate->subordinate >=
13447 tp->pdev->bus->number)) {
13448 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
13449 pci_dev_put(bridge);
13450 break;
13451 }
13452 } while (bridge);
13453 }
Michael Chan4cf78e42005-07-25 12:29:19 -070013454
Linus Torvalds1da177e2005-04-16 15:20:36 -070013455 /* Initialize misc host control in PCI block. */
13456 tp->misc_host_ctrl |= (misc_ctrl_reg &
13457 MISC_HOST_CTRL_CHIPREV);
13458 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13459 tp->misc_host_ctrl);
13460
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013461 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
13462 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013463 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13464 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Michael Chan7544b092007-05-05 13:08:32 -070013465 tp->pdev_peer = tg3_find_peer(tp);
13466
Matt Carlsonc885e822010-08-02 11:25:57 +000013467 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013468 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13469 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Matt Carlson0a58d662011-04-05 14:22:45 +000013470 tp->tg3_flags3 |= TG3_FLG3_5717_PLUS;
13471
13472 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
13473 (tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
Matt Carlson1407deb2011-04-05 14:22:44 +000013474 tp->tg3_flags3 |= TG3_FLG3_57765_PLUS;
Matt Carlsonc885e822010-08-02 11:25:57 +000013475
Matt Carlson321d32a2008-11-21 17:22:19 -080013476 /* Intentionally exclude ASIC_REV_5906 */
13477 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad12006-03-20 22:27:35 -080013478 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013479 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013480 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013481 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013482 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson1407deb2011-04-05 14:22:44 +000013483 (tp->tg3_flags3 & TG3_FLG3_57765_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013484 tp->tg3_flags3 |= TG3_FLG3_5755_PLUS;
13485
13486 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13487 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070013488 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013489 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chana4e2b342005-10-26 15:46:52 -070013490 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
John W. Linville6708e5c2005-04-21 17:00:52 -070013491 tp->tg3_flags2 |= TG3_FLG2_5750_PLUS;
13492
John W. Linville1b440c562005-04-21 17:03:18 -070013493 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) ||
13494 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
13495 tp->tg3_flags2 |= TG3_FLG2_5705_PLUS;
13496
Matt Carlson027455a2008-12-21 20:19:30 -080013497 /* 5700 B0 chips do not support checksumming correctly due
13498 * to hardware bugs.
13499 */
Michał Mirosławdc668912011-04-07 03:35:07 +000013500 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
13501 u32 features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013502
Matt Carlson027455a2008-12-21 20:19:30 -080013503 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013504 features |= NETIF_F_IPV6_CSUM;
13505 tp->dev->features |= features;
Michał Mirosławdc668912011-04-07 03:35:07 +000013506 tp->dev->hw_features |= features;
13507 tp->dev->vlan_features |= features;
Matt Carlson027455a2008-12-21 20:19:30 -080013508 }
13509
Matt Carlson507399f2009-11-13 13:03:37 +000013510 /* Determine TSO capabilities */
Matt Carlson2866d952011-02-10 20:06:46 -080013511 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlson4d163b72011-01-25 15:58:48 +000013512 ; /* Do nothing. HW bug. */
Matt Carlson1407deb2011-04-05 14:22:44 +000013513 else if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS)
Matt Carlsone849cdc2009-11-13 13:03:38 +000013514 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_3;
13515 else if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
13516 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlson507399f2009-11-13 13:03:37 +000013517 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_2;
13518 else if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
13519 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_1 | TG3_FLG2_TSO_BUG;
13520 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13521 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
13522 tp->tg3_flags2 &= ~TG3_FLG2_TSO_BUG;
13523 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13524 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13525 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
13526 tp->tg3_flags2 |= TG3_FLG2_TSO_BUG;
13527 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13528 tp->fw_needed = FIRMWARE_TG3TSO5;
13529 else
13530 tp->fw_needed = FIRMWARE_TG3TSO;
13531 }
13532
13533 tp->irq_max = 1;
13534
Michael Chan5a6f3072006-03-20 22:28:05 -080013535 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Michael Chan7544b092007-05-05 13:08:32 -070013536 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSI;
13537 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
13538 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
13539 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
13540 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
13541 tp->pdev_peer == tp->pdev))
13542 tp->tg3_flags &= ~TG3_FLAG_SUPPORT_MSI;
13543
Matt Carlson321d32a2008-11-21 17:22:19 -080013544 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070013545 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080013546 tp->tg3_flags2 |= TG3_FLG2_1SHOT_MSI;
Michael Chan52c0fd82006-06-29 20:15:54 -070013547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013548
Matt Carlson1407deb2011-04-05 14:22:44 +000013549 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
Matt Carlson507399f2009-11-13 13:03:37 +000013550 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSIX;
13551 tp->irq_max = TG3_IRQ_MAX_VECS;
13552 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013553 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000013554
Matt Carlson615774f2009-11-13 13:03:39 +000013555 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsona50d0792010-06-05 17:24:37 +000013556 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson615774f2009-11-13 13:03:39 +000013557 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13558 tp->tg3_flags3 |= TG3_FLG3_SHORT_DMA_BUG;
13559 else if (!(tp->tg3_flags3 & TG3_FLG3_5755_PLUS)) {
13560 tp->tg3_flags3 |= TG3_FLG3_4G_DMA_BNDRY_BUG;
13561 tp->tg3_flags3 |= TG3_FLG3_40BIT_DMA_LIMIT_BUG;
Matt Carlson0e1406d2009-11-02 12:33:33 +000013562 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013563
Matt Carlson0a58d662011-04-05 14:22:45 +000013564 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlsonde9f5232011-04-05 14:22:43 +000013565 tp->tg3_flags3 |= TG3_FLG3_LRG_PROD_RING_CAP;
13566
Matt Carlson1407deb2011-04-05 14:22:44 +000013567 if ((tp->tg3_flags3 & TG3_FLG3_57765_PLUS) &&
Matt Carlson2866d952011-02-10 20:06:46 -080013568 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
Matt Carlsonb703df62009-12-03 08:36:21 +000013569 tp->tg3_flags3 |= TG3_FLG3_USE_JUMBO_BDFLAG;
13570
Matt Carlsonf51f3562008-05-25 23:45:08 -070013571 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsonc6cdf432010-04-05 10:19:26 +000013572 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
13573 (tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG))
Matt Carlson8f666b02009-08-28 13:58:24 +000013574 tp->tg3_flags |= TG3_FLAG_JUMBO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -070013575
Matt Carlson52f44902008-11-21 17:17:04 -080013576 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13577 &pci_state_reg);
13578
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013579 tp->pcie_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_EXP);
13580 if (tp->pcie_cap != 0) {
13581 u16 lnkctl;
13582
Linus Torvalds1da177e2005-04-16 15:20:36 -070013583 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013584
Matt Carlsoncf790032010-11-24 08:31:48 +000013585 tp->pcie_readrq = 4096;
Matt Carlsond78b59f2011-04-05 14:22:46 +000013586 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13587 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Matt Carlsonb4495ed2011-01-25 15:58:47 +000013588 tp->pcie_readrq = 2048;
Matt Carlsoncf790032010-11-24 08:31:48 +000013589
13590 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013591
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013592 pci_read_config_word(tp->pdev,
13593 tp->pcie_cap + PCI_EXP_LNKCTL,
13594 &lnkctl);
13595 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
13596 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanc7835a72006-11-15 21:14:42 -080013597 tp->tg3_flags2 &= ~TG3_FLG2_HW_TSO_2;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013598 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013599 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000013600 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
13601 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013602 tp->tg3_flags3 |= TG3_FLG3_CLKREQ_BUG;
Matt Carlson614b0592010-01-20 16:58:02 +000013603 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
13604 tp->tg3_flags3 |= TG3_FLG3_L1PLLPD_EN;
Michael Chanc7835a72006-11-15 21:14:42 -080013605 }
Matt Carlson52f44902008-11-21 17:17:04 -080013606 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -080013607 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson52f44902008-11-21 17:17:04 -080013608 } else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
13609 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
13610 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
13611 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000013612 dev_err(&tp->pdev->dev,
13613 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080013614 return -EIO;
13615 }
13616
13617 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
13618 tp->tg3_flags |= TG3_FLAG_PCIX_MODE;
13619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013620
Michael Chan399de502005-10-03 14:02:39 -070013621 /* If we have an AMD 762 or VIA K8T800 chipset, write
13622 * reordering to the mailbox registers done by the host
13623 * controller can cause major troubles. We read back from
13624 * every mailbox register write to force the writes to be
13625 * posted to the chip in order.
13626 */
Matt Carlson41434702011-03-09 16:58:22 +000013627 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Michael Chan399de502005-10-03 14:02:39 -070013628 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13629 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
13630
Matt Carlson69fc4052008-12-21 20:19:57 -080013631 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
13632 &tp->pci_cacheline_sz);
13633 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13634 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013635 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13636 tp->pci_lat_timer < 64) {
13637 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080013638 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13639 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013640 }
13641
Matt Carlson52f44902008-11-21 17:17:04 -080013642 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
13643 /* 5700 BX chips need to have their TX producer index
13644 * mailboxes written twice to workaround a bug.
13645 */
13646 tp->tg3_flags |= TG3_FLAG_TXD_MBOX_HWBUG;
Matt Carlson9974a352007-10-07 23:27:28 -070013647
Matt Carlson52f44902008-11-21 17:17:04 -080013648 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013649 *
13650 * The workaround is to use indirect register accesses
13651 * for all chip writes not to mailbox registers.
13652 */
Matt Carlson52f44902008-11-21 17:17:04 -080013653 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013654 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013655
13656 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13657
13658 /* The chip can have it's power management PCI config
13659 * space registers clobbered due to this bug.
13660 * So explicitly force the chip into D0 here.
13661 */
Matt Carlson9974a352007-10-07 23:27:28 -070013662 pci_read_config_dword(tp->pdev,
13663 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013664 &pm_reg);
13665 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
13666 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070013667 pci_write_config_dword(tp->pdev,
13668 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013669 pm_reg);
13670
13671 /* Also, force SERR#/PERR# in PCI command. */
13672 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13673 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
13674 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13675 }
13676 }
13677
Linus Torvalds1da177e2005-04-16 15:20:36 -070013678 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
13679 tp->tg3_flags |= TG3_FLAG_PCI_HIGH_SPEED;
13680 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
13681 tp->tg3_flags |= TG3_FLAG_PCI_32BIT;
13682
13683 /* Chip-specific fixup from Broadcom driver */
13684 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
13685 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
13686 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
13687 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
13688 }
13689
Michael Chan1ee582d2005-08-09 20:16:46 -070013690 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070013691 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013692 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070013693 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070013694 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013695 tp->write32_tx_mbox = tg3_write32;
13696 tp->write32_rx_mbox = tg3_write32;
13697
13698 /* Various workaround register access methods */
13699 if (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG)
13700 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013701 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
13702 ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
13703 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
13704 /*
13705 * Back to back register writes can cause problems on these
13706 * chips, the workaround is to read back all reg writes
13707 * except those to mailbox regs.
13708 *
13709 * See tg3_write_indirect_reg32().
13710 */
Michael Chan1ee582d2005-08-09 20:16:46 -070013711 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013712 }
13713
Michael Chan1ee582d2005-08-09 20:16:46 -070013714 if ((tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG) ||
13715 (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)) {
13716 tp->write32_tx_mbox = tg3_write32_tx_mbox;
13717 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
13718 tp->write32_rx_mbox = tg3_write_flush_reg32;
13719 }
Michael Chan20094932005-08-09 20:16:32 -070013720
Michael Chan68929142005-08-09 20:17:14 -070013721 if (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND) {
13722 tp->read32 = tg3_read_indirect_reg32;
13723 tp->write32 = tg3_write_indirect_reg32;
13724 tp->read32_mbox = tg3_read_indirect_mbox;
13725 tp->write32_mbox = tg3_write_indirect_mbox;
13726 tp->write32_tx_mbox = tg3_write_indirect_mbox;
13727 tp->write32_rx_mbox = tg3_write_indirect_mbox;
13728
13729 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070013730 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070013731
13732 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13733 pci_cmd &= ~PCI_COMMAND_MEMORY;
13734 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13735 }
Michael Chanb5d37722006-09-27 16:06:21 -070013736 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13737 tp->read32_mbox = tg3_read32_mbox_5906;
13738 tp->write32_mbox = tg3_write32_mbox_5906;
13739 tp->write32_tx_mbox = tg3_write32_mbox_5906;
13740 tp->write32_rx_mbox = tg3_write32_mbox_5906;
13741 }
Michael Chan68929142005-08-09 20:17:14 -070013742
Michael Chanbbadf502006-04-06 21:46:34 -070013743 if (tp->write32 == tg3_write_indirect_reg32 ||
13744 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13745 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070013746 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Michael Chanbbadf502006-04-06 21:46:34 -070013747 tp->tg3_flags |= TG3_FLAG_SRAM_USE_CONFIG;
13748
Michael Chan7d0c41e2005-04-21 17:06:20 -070013749 /* Get eeprom hw config before calling tg3_set_power_state().
Michael Chan9d26e212006-12-07 00:21:14 -080013750 * In particular, the TG3_FLG2_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070013751 * determined before calling tg3_set_power_state() so that
13752 * we know whether or not to switch out of Vaux power.
13753 * When the flag is set, it means that GPIO1 is used for eeprom
13754 * write protect and also implies that it is a LOM where GPIOs
13755 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013756 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070013757 tg3_get_eeprom_hw_cfg(tp);
13758
Matt Carlson0d3031d2007-10-10 18:02:43 -070013759 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
13760 /* Allow reads and writes to the
13761 * APE register and memory space.
13762 */
13763 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000013764 PCISTATE_ALLOW_APE_SHMEM_WR |
13765 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070013766 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
13767 pci_state_reg);
13768 }
13769
Matt Carlson9936bcf2007-10-10 18:03:07 -070013770 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013771 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013772 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013773 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson1407deb2011-04-05 14:22:44 +000013774 (tp->tg3_flags3 & TG3_FLG3_57765_PLUS))
Matt Carlsond30cdd22007-10-07 23:28:35 -070013775 tp->tg3_flags |= TG3_FLAG_CPMU_PRESENT;
13776
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000013777 /* Set up tp->grc_local_ctrl before calling tg_power_up().
Michael Chan314fba32005-04-21 17:07:04 -070013778 * GPIO1 driven high will bring 5700's external PHY out of reset.
13779 * It is also used as eeprom write protect on LOMs.
13780 */
13781 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
13782 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13783 (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT))
13784 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
13785 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070013786 /* Unused GPIO3 must be driven as output on 5752 because there
13787 * are no pull-up resistors on unused GPIO pins.
13788 */
13789 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
13790 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070013791
Matt Carlson321d32a2008-11-21 17:22:19 -080013792 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000013793 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
13794 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Michael Chanaf36e6b2006-03-23 01:28:06 -080013795 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13796
Matt Carlson8d519ab2009-04-20 06:58:01 +000013797 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
13798 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070013799 /* Turn off the debug UART. */
13800 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13801 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
13802 /* Keep VMain power. */
13803 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
13804 GRC_LCLCTRL_GPIO_OUTPUT0;
13805 }
13806
Linus Torvalds1da177e2005-04-16 15:20:36 -070013807 /* Force the chip into D0. */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000013808 err = tg3_power_up(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013809 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000013810 dev_err(&tp->pdev->dev, "Transition to D0 failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070013811 return err;
13812 }
13813
Linus Torvalds1da177e2005-04-16 15:20:36 -070013814 /* Derive initial jumbo mode from MTU assigned in
13815 * ether_setup() via the alloc_etherdev() call
13816 */
Michael Chan0f893dc2005-07-25 12:30:38 -070013817 if (tp->dev->mtu > ETH_DATA_LEN &&
Michael Chana4e2b342005-10-26 15:46:52 -070013818 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan0f893dc2005-07-25 12:30:38 -070013819 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013820
13821 /* Determine WakeOnLan speed to use. */
13822 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13823 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
13824 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
13825 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
13826 tp->tg3_flags &= ~(TG3_FLAG_WOL_SPEED_100MB);
13827 } else {
13828 tp->tg3_flags |= TG3_FLAG_WOL_SPEED_100MB;
13829 }
13830
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013831 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013832 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013833
Linus Torvalds1da177e2005-04-16 15:20:36 -070013834 /* A few boards don't want Ethernet@WireSpeed phy feature */
13835 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13836 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
13837 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070013838 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013839 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
13840 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13841 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013842
13843 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
13844 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013845 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013846 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013847 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013848
Matt Carlson321d32a2008-11-21 17:22:19 -080013849 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013850 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080013851 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013852 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Matt Carlson1407deb2011-04-05 14:22:44 +000013853 !(tp->tg3_flags3 & TG3_FLG3_57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070013854 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013855 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013856 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13857 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080013858 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
13859 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013860 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080013861 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013862 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080013863 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013864 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070013865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013866
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013867 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13868 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
13869 tp->phy_otp = tg3_read_otp_phycfg(tp);
13870 if (tp->phy_otp == 0)
13871 tp->phy_otp = TG3_OTP_DEFAULT;
13872 }
13873
Matt Carlsonf51f3562008-05-25 23:45:08 -070013874 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)
Matt Carlson8ef21422008-05-02 16:47:53 -070013875 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
13876 else
13877 tp->mi_mode = MAC_MI_MODE_BASE;
13878
Linus Torvalds1da177e2005-04-16 15:20:36 -070013879 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013880 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
13881 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
13882 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
13883
Matt Carlson321d32a2008-11-21 17:22:19 -080013884 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
13885 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson57e69832008-05-25 23:48:31 -070013886 tp->tg3_flags3 |= TG3_FLG3_USE_PHYLIB;
13887
Matt Carlson158d7ab2008-05-29 01:37:54 -070013888 err = tg3_mdio_init(tp);
13889 if (err)
13890 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013891
13892 /* Initialize data/descriptor byte/word swapping. */
13893 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000013894 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
13895 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
13896 GRC_MODE_WORD_SWAP_B2HRX_DATA |
13897 GRC_MODE_B2HRX_ENABLE |
13898 GRC_MODE_HTX2B_ENABLE |
13899 GRC_MODE_HOST_STACKUP);
13900 else
13901 val &= GRC_MODE_HOST_STACKUP;
13902
Linus Torvalds1da177e2005-04-16 15:20:36 -070013903 tw32(GRC_MODE, val | tp->grc_mode);
13904
13905 tg3_switch_clocks(tp);
13906
13907 /* Clear this out for sanity. */
13908 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
13909
13910 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13911 &pci_state_reg);
13912 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
13913 (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) == 0) {
13914 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
13915
13916 if (chiprevid == CHIPREV_ID_5701_A0 ||
13917 chiprevid == CHIPREV_ID_5701_B0 ||
13918 chiprevid == CHIPREV_ID_5701_B2 ||
13919 chiprevid == CHIPREV_ID_5701_B5) {
13920 void __iomem *sram_base;
13921
13922 /* Write some dummy words into the SRAM status block
13923 * area, see if it reads back correctly. If the return
13924 * value is bad, force enable the PCIX workaround.
13925 */
13926 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
13927
13928 writel(0x00000000, sram_base);
13929 writel(0x00000000, sram_base + 4);
13930 writel(0xffffffff, sram_base + 4);
13931 if (readl(sram_base) != 0x00000000)
13932 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13933 }
13934 }
13935
13936 udelay(50);
13937 tg3_nvram_init(tp);
13938
13939 grc_misc_cfg = tr32(GRC_MISC_CFG);
13940 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
13941
Linus Torvalds1da177e2005-04-16 15:20:36 -070013942 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13943 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
13944 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
13945 tp->tg3_flags2 |= TG3_FLG2_IS_5788;
13946
David S. Millerfac9b832005-05-18 22:46:34 -070013947 if (!(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
13948 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700))
13949 tp->tg3_flags |= TG3_FLAG_TAGGED_STATUS;
13950 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
13951 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
13952 HOSTCC_MODE_CLRTICK_TXBD);
13953
13954 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
13955 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13956 tp->misc_host_ctrl);
13957 }
13958
Matt Carlson3bda1252008-08-15 14:08:22 -070013959 /* Preserve the APE MAC_MODE bits */
13960 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
Matt Carlsond2394e6b2010-11-24 08:31:47 +000013961 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070013962 else
13963 tp->mac_mode = TG3_DEF_MAC_MODE;
13964
Linus Torvalds1da177e2005-04-16 15:20:36 -070013965 /* these are limited to 10/100 only */
13966 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13967 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
13968 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13969 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13970 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
13971 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
13972 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
13973 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13974 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080013975 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
13976 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013977 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000013978 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13979 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013980 (tp->phy_flags & TG3_PHYFLG_IS_FET))
13981 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013982
13983 err = tg3_phy_probe(tp);
13984 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000013985 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013986 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013987 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013988 }
13989
Matt Carlson184b8902010-04-05 10:19:25 +000013990 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080013991 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013992
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013993 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
13994 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013995 } else {
13996 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013997 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013998 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013999 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014000 }
14001
14002 /* 5700 {AX,BX} chips have a broken status block link
14003 * change bit implementation, so we must use the
14004 * status register in those cases.
14005 */
14006 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
14007 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
14008 else
14009 tp->tg3_flags &= ~TG3_FLAG_USE_LINKCHG_REG;
14010
14011 /* The led_ctrl is set during tg3_phy_probe, here we might
14012 * have to force the link status polling mechanism based
14013 * upon subsystem IDs.
14014 */
14015 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070014016 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014017 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
14018 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
14019 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014020 }
14021
14022 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014023 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014024 tp->tg3_flags |= TG3_FLAG_POLL_SERDES;
14025 else
14026 tp->tg3_flags &= ~TG3_FLAG_POLL_SERDES;
14027
Matt Carlsonbf933c82011-01-25 15:58:49 +000014028 tp->rx_offset = NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014029 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014030 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsond2757fc2010-04-12 06:58:27 +000014031 (tp->tg3_flags & TG3_FLAG_PCIX_MODE) != 0) {
Matt Carlsonbf933c82011-01-25 15:58:49 +000014032 tp->rx_offset = 0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014033#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000014034 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014035#endif
14036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014037
Matt Carlson2c49a442010-09-30 10:34:35 +000014038 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14039 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014040 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14041
Matt Carlson2c49a442010-09-30 10:34:35 +000014042 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070014043
14044 /* Increment the rx prod index on the rx std ring by at most
14045 * 8 for these chips to workaround hw errata.
14046 */
14047 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14048 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14049 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14050 tp->rx_std_max_post = 8;
14051
Matt Carlson8ed5d972007-05-07 00:25:49 -070014052 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND)
14053 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14054 PCIE_PWR_MGMT_L1_THRESH_MSK;
14055
Linus Torvalds1da177e2005-04-16 15:20:36 -070014056 return err;
14057}
14058
David S. Miller49b6e95f2007-03-29 01:38:42 -070014059#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014060static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14061{
14062 struct net_device *dev = tp->dev;
14063 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014064 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070014065 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014066 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014067
David S. Miller49b6e95f2007-03-29 01:38:42 -070014068 addr = of_get_property(dp, "local-mac-address", &len);
14069 if (addr && len == 6) {
14070 memcpy(dev->dev_addr, addr, 6);
14071 memcpy(dev->perm_addr, dev->dev_addr, 6);
14072 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014073 }
14074 return -ENODEV;
14075}
14076
14077static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14078{
14079 struct net_device *dev = tp->dev;
14080
14081 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070014082 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014083 return 0;
14084}
14085#endif
14086
14087static int __devinit tg3_get_device_address(struct tg3 *tp)
14088{
14089 struct net_device *dev = tp->dev;
14090 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080014091 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014092
David S. Miller49b6e95f2007-03-29 01:38:42 -070014093#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014094 if (!tg3_get_macaddr_sparc(tp))
14095 return 0;
14096#endif
14097
14098 mac_offset = 0x7c;
David S. Millerf49639e2006-06-09 11:58:36 -070014099 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
Michael Chana4e2b342005-10-26 15:46:52 -070014100 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014101 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14102 mac_offset = 0xcc;
14103 if (tg3_nvram_lock(tp))
14104 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14105 else
14106 tg3_nvram_unlock(tp);
Matt Carlson0a58d662011-04-05 14:22:45 +000014107 } else if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlsona50d0792010-06-05 17:24:37 +000014108 if (PCI_FUNC(tp->pdev->devfn) & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014109 mac_offset = 0xcc;
Matt Carlsona50d0792010-06-05 17:24:37 +000014110 if (PCI_FUNC(tp->pdev->devfn) > 1)
14111 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014112 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014113 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014114
14115 /* First try to get it from MAC address mailbox. */
14116 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14117 if ((hi >> 16) == 0x484b) {
14118 dev->dev_addr[0] = (hi >> 8) & 0xff;
14119 dev->dev_addr[1] = (hi >> 0) & 0xff;
14120
14121 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14122 dev->dev_addr[2] = (lo >> 24) & 0xff;
14123 dev->dev_addr[3] = (lo >> 16) & 0xff;
14124 dev->dev_addr[4] = (lo >> 8) & 0xff;
14125 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014126
Michael Chan008652b2006-03-27 23:14:53 -080014127 /* Some old bootcode may report a 0 MAC address in SRAM */
14128 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14129 }
14130 if (!addr_ok) {
14131 /* Next, try NVRAM. */
Matt Carlsondf259d82009-04-20 06:57:14 +000014132 if (!(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) &&
14133 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000014134 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070014135 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14136 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080014137 }
14138 /* Finally just fetch it out of the MAC control regs. */
14139 else {
14140 hi = tr32(MAC_ADDR_0_HIGH);
14141 lo = tr32(MAC_ADDR_0_LOW);
14142
14143 dev->dev_addr[5] = lo & 0xff;
14144 dev->dev_addr[4] = (lo >> 8) & 0xff;
14145 dev->dev_addr[3] = (lo >> 16) & 0xff;
14146 dev->dev_addr[2] = (lo >> 24) & 0xff;
14147 dev->dev_addr[1] = hi & 0xff;
14148 dev->dev_addr[0] = (hi >> 8) & 0xff;
14149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014150 }
14151
14152 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014153#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014154 if (!tg3_get_default_macaddr_sparc(tp))
14155 return 0;
14156#endif
14157 return -EINVAL;
14158 }
John W. Linville2ff43692005-09-12 14:44:20 -070014159 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014160 return 0;
14161}
14162
David S. Miller59e6b432005-05-18 22:50:10 -070014163#define BOUNDARY_SINGLE_CACHELINE 1
14164#define BOUNDARY_MULTI_CACHELINE 2
14165
14166static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14167{
14168 int cacheline_size;
14169 u8 byte;
14170 int goal;
14171
14172 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14173 if (byte == 0)
14174 cacheline_size = 1024;
14175 else
14176 cacheline_size = (int) byte * 4;
14177
14178 /* On 5703 and later chips, the boundary bits have no
14179 * effect.
14180 */
14181 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14182 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
14183 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
14184 goto out;
14185
14186#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14187 goal = BOUNDARY_MULTI_CACHELINE;
14188#else
14189#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14190 goal = BOUNDARY_SINGLE_CACHELINE;
14191#else
14192 goal = 0;
14193#endif
14194#endif
14195
Matt Carlson1407deb2011-04-05 14:22:44 +000014196 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014197 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14198 goto out;
14199 }
14200
David S. Miller59e6b432005-05-18 22:50:10 -070014201 if (!goal)
14202 goto out;
14203
14204 /* PCI controllers on most RISC systems tend to disconnect
14205 * when a device tries to burst across a cache-line boundary.
14206 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14207 *
14208 * Unfortunately, for PCI-E there are only limited
14209 * write-side controls for this, and thus for reads
14210 * we will still get the disconnects. We'll also waste
14211 * these PCI cycles for both read and write for chips
14212 * other than 5700 and 5701 which do not implement the
14213 * boundary bits.
14214 */
14215 if ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
14216 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
14217 switch (cacheline_size) {
14218 case 16:
14219 case 32:
14220 case 64:
14221 case 128:
14222 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14223 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14224 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14225 } else {
14226 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14227 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14228 }
14229 break;
14230
14231 case 256:
14232 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14233 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14234 break;
14235
14236 default:
14237 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14238 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14239 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014240 }
David S. Miller59e6b432005-05-18 22:50:10 -070014241 } else if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14242 switch (cacheline_size) {
14243 case 16:
14244 case 32:
14245 case 64:
14246 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14247 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14248 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14249 break;
14250 }
14251 /* fallthrough */
14252 case 128:
14253 default:
14254 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14255 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14256 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014257 }
David S. Miller59e6b432005-05-18 22:50:10 -070014258 } else {
14259 switch (cacheline_size) {
14260 case 16:
14261 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14262 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14263 DMA_RWCTRL_WRITE_BNDRY_16);
14264 break;
14265 }
14266 /* fallthrough */
14267 case 32:
14268 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14269 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14270 DMA_RWCTRL_WRITE_BNDRY_32);
14271 break;
14272 }
14273 /* fallthrough */
14274 case 64:
14275 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14276 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14277 DMA_RWCTRL_WRITE_BNDRY_64);
14278 break;
14279 }
14280 /* fallthrough */
14281 case 128:
14282 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14283 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14284 DMA_RWCTRL_WRITE_BNDRY_128);
14285 break;
14286 }
14287 /* fallthrough */
14288 case 256:
14289 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14290 DMA_RWCTRL_WRITE_BNDRY_256);
14291 break;
14292 case 512:
14293 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14294 DMA_RWCTRL_WRITE_BNDRY_512);
14295 break;
14296 case 1024:
14297 default:
14298 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14299 DMA_RWCTRL_WRITE_BNDRY_1024);
14300 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014301 }
David S. Miller59e6b432005-05-18 22:50:10 -070014302 }
14303
14304out:
14305 return val;
14306}
14307
Linus Torvalds1da177e2005-04-16 15:20:36 -070014308static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
14309{
14310 struct tg3_internal_buffer_desc test_desc;
14311 u32 sram_dma_descs;
14312 int i, ret;
14313
14314 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
14315
14316 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
14317 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
14318 tw32(RDMAC_STATUS, 0);
14319 tw32(WDMAC_STATUS, 0);
14320
14321 tw32(BUFMGR_MODE, 0);
14322 tw32(FTQ_RESET, 0);
14323
14324 test_desc.addr_hi = ((u64) buf_dma) >> 32;
14325 test_desc.addr_lo = buf_dma & 0xffffffff;
14326 test_desc.nic_mbuf = 0x00002100;
14327 test_desc.len = size;
14328
14329 /*
14330 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
14331 * the *second* time the tg3 driver was getting loaded after an
14332 * initial scan.
14333 *
14334 * Broadcom tells me:
14335 * ...the DMA engine is connected to the GRC block and a DMA
14336 * reset may affect the GRC block in some unpredictable way...
14337 * The behavior of resets to individual blocks has not been tested.
14338 *
14339 * Broadcom noted the GRC reset will also reset all sub-components.
14340 */
14341 if (to_device) {
14342 test_desc.cqid_sqid = (13 << 8) | 2;
14343
14344 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
14345 udelay(40);
14346 } else {
14347 test_desc.cqid_sqid = (16 << 8) | 7;
14348
14349 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
14350 udelay(40);
14351 }
14352 test_desc.flags = 0x00000005;
14353
14354 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
14355 u32 val;
14356
14357 val = *(((u32 *)&test_desc) + i);
14358 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
14359 sram_dma_descs + (i * sizeof(u32)));
14360 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
14361 }
14362 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
14363
Matt Carlson859a588792010-04-05 10:19:28 +000014364 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014365 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000014366 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014367 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014368
14369 ret = -ENODEV;
14370 for (i = 0; i < 40; i++) {
14371 u32 val;
14372
14373 if (to_device)
14374 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
14375 else
14376 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
14377 if ((val & 0xffff) == sram_dma_descs) {
14378 ret = 0;
14379 break;
14380 }
14381
14382 udelay(100);
14383 }
14384
14385 return ret;
14386}
14387
David S. Millerded73402005-05-23 13:59:47 -070014388#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070014389
Matt Carlson41434702011-03-09 16:58:22 +000014390static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080014391 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14392 { },
14393};
14394
Linus Torvalds1da177e2005-04-16 15:20:36 -070014395static int __devinit tg3_test_dma(struct tg3 *tp)
14396{
14397 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070014398 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014399 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014400
Matt Carlson4bae65c2010-11-24 08:31:52 +000014401 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
14402 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014403 if (!buf) {
14404 ret = -ENOMEM;
14405 goto out_nofree;
14406 }
14407
14408 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
14409 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
14410
David S. Miller59e6b432005-05-18 22:50:10 -070014411 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014412
Matt Carlson1407deb2011-04-05 14:22:44 +000014413 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS)
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014414 goto out;
14415
Linus Torvalds1da177e2005-04-16 15:20:36 -070014416 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14417 /* DMA read watermark not used on PCIE */
14418 tp->dma_rwctrl |= 0x00180000;
14419 } else if (!(tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070014420 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14421 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014422 tp->dma_rwctrl |= 0x003f0000;
14423 else
14424 tp->dma_rwctrl |= 0x003f000f;
14425 } else {
14426 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14427 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
14428 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080014429 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014430
Michael Chan4a29cc22006-03-19 13:21:12 -080014431 /* If the 5704 is behind the EPB bridge, we can
14432 * do the less restrictive ONE_DMA workaround for
14433 * better performance.
14434 */
14435 if ((tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) &&
14436 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14437 tp->dma_rwctrl |= 0x8000;
14438 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014439 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
14440
Michael Chan49afdeb2007-02-13 12:17:03 -080014441 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
14442 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070014443 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080014444 tp->dma_rwctrl |=
14445 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
14446 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
14447 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070014448 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
14449 /* 5780 always in PCIX mode */
14450 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070014451 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
14452 /* 5714 always in PCIX mode */
14453 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014454 } else {
14455 tp->dma_rwctrl |= 0x001b000f;
14456 }
14457 }
14458
14459 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14460 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14461 tp->dma_rwctrl &= 0xfffffff0;
14462
14463 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14464 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
14465 /* Remove this if it causes problems for some boards. */
14466 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
14467
14468 /* On 5700/5701 chips, we need to set this bit.
14469 * Otherwise the chip will issue cacheline transactions
14470 * to streamable DMA memory with not all the byte
14471 * enables turned on. This is an error on several
14472 * RISC PCI controllers, in particular sparc64.
14473 *
14474 * On 5703/5704 chips, this bit has been reassigned
14475 * a different meaning. In particular, it is used
14476 * on those chips to enable a PCI-X workaround.
14477 */
14478 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
14479 }
14480
14481 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14482
14483#if 0
14484 /* Unneeded, already done by tg3_get_invariants. */
14485 tg3_switch_clocks(tp);
14486#endif
14487
Linus Torvalds1da177e2005-04-16 15:20:36 -070014488 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14489 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
14490 goto out;
14491
David S. Miller59e6b432005-05-18 22:50:10 -070014492 /* It is best to perform DMA test with maximum write burst size
14493 * to expose the 5700/5701 write DMA bug.
14494 */
14495 saved_dma_rwctrl = tp->dma_rwctrl;
14496 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14497 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14498
Linus Torvalds1da177e2005-04-16 15:20:36 -070014499 while (1) {
14500 u32 *p = buf, i;
14501
14502 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
14503 p[i] = i;
14504
14505 /* Send the buffer to the chip. */
14506 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
14507 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000014508 dev_err(&tp->pdev->dev,
14509 "%s: Buffer write failed. err = %d\n",
14510 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014511 break;
14512 }
14513
14514#if 0
14515 /* validate data reached card RAM correctly. */
14516 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14517 u32 val;
14518 tg3_read_mem(tp, 0x2100 + (i*4), &val);
14519 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000014520 dev_err(&tp->pdev->dev,
14521 "%s: Buffer corrupted on device! "
14522 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014523 /* ret = -ENODEV here? */
14524 }
14525 p[i] = 0;
14526 }
14527#endif
14528 /* Now read it back. */
14529 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
14530 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014531 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
14532 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014533 break;
14534 }
14535
14536 /* Verify it. */
14537 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14538 if (p[i] == i)
14539 continue;
14540
David S. Miller59e6b432005-05-18 22:50:10 -070014541 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14542 DMA_RWCTRL_WRITE_BNDRY_16) {
14543 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014544 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
14545 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14546 break;
14547 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000014548 dev_err(&tp->pdev->dev,
14549 "%s: Buffer corrupted on read back! "
14550 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014551 ret = -ENODEV;
14552 goto out;
14553 }
14554 }
14555
14556 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
14557 /* Success. */
14558 ret = 0;
14559 break;
14560 }
14561 }
David S. Miller59e6b432005-05-18 22:50:10 -070014562 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14563 DMA_RWCTRL_WRITE_BNDRY_16) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014564
David S. Miller59e6b432005-05-18 22:50:10 -070014565 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070014566 * now look for chipsets that are known to expose the
14567 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070014568 */
Matt Carlson41434702011-03-09 16:58:22 +000014569 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014570 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14571 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000014572 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014573 /* Safe to use the calculated DMA boundary. */
14574 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000014575 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070014576
David S. Miller59e6b432005-05-18 22:50:10 -070014577 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014579
14580out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000014581 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014582out_nofree:
14583 return ret;
14584}
14585
Linus Torvalds1da177e2005-04-16 15:20:36 -070014586static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
14587{
Matt Carlson1407deb2011-04-05 14:22:44 +000014588 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
Matt Carlson666bc832010-01-20 16:58:03 +000014589 tp->bufmgr_config.mbuf_read_dma_low_water =
14590 DEFAULT_MB_RDMA_LOW_WATER_5705;
14591 tp->bufmgr_config.mbuf_mac_rx_low_water =
14592 DEFAULT_MB_MACRX_LOW_WATER_57765;
14593 tp->bufmgr_config.mbuf_high_water =
14594 DEFAULT_MB_HIGH_WATER_57765;
14595
14596 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14597 DEFAULT_MB_RDMA_LOW_WATER_5705;
14598 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14599 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
14600 tp->bufmgr_config.mbuf_high_water_jumbo =
14601 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
14602 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chanfdfec1722005-07-25 12:31:48 -070014603 tp->bufmgr_config.mbuf_read_dma_low_water =
14604 DEFAULT_MB_RDMA_LOW_WATER_5705;
14605 tp->bufmgr_config.mbuf_mac_rx_low_water =
14606 DEFAULT_MB_MACRX_LOW_WATER_5705;
14607 tp->bufmgr_config.mbuf_high_water =
14608 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070014609 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14610 tp->bufmgr_config.mbuf_mac_rx_low_water =
14611 DEFAULT_MB_MACRX_LOW_WATER_5906;
14612 tp->bufmgr_config.mbuf_high_water =
14613 DEFAULT_MB_HIGH_WATER_5906;
14614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014615
Michael Chanfdfec1722005-07-25 12:31:48 -070014616 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14617 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
14618 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14619 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
14620 tp->bufmgr_config.mbuf_high_water_jumbo =
14621 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
14622 } else {
14623 tp->bufmgr_config.mbuf_read_dma_low_water =
14624 DEFAULT_MB_RDMA_LOW_WATER;
14625 tp->bufmgr_config.mbuf_mac_rx_low_water =
14626 DEFAULT_MB_MACRX_LOW_WATER;
14627 tp->bufmgr_config.mbuf_high_water =
14628 DEFAULT_MB_HIGH_WATER;
14629
14630 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14631 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
14632 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14633 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
14634 tp->bufmgr_config.mbuf_high_water_jumbo =
14635 DEFAULT_MB_HIGH_WATER_JUMBO;
14636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014637
14638 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
14639 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
14640}
14641
14642static char * __devinit tg3_phy_string(struct tg3 *tp)
14643{
Matt Carlson79eb6902010-02-17 15:17:03 +000014644 switch (tp->phy_id & TG3_PHY_ID_MASK) {
14645 case TG3_PHY_ID_BCM5400: return "5400";
14646 case TG3_PHY_ID_BCM5401: return "5401";
14647 case TG3_PHY_ID_BCM5411: return "5411";
14648 case TG3_PHY_ID_BCM5701: return "5701";
14649 case TG3_PHY_ID_BCM5703: return "5703";
14650 case TG3_PHY_ID_BCM5704: return "5704";
14651 case TG3_PHY_ID_BCM5705: return "5705";
14652 case TG3_PHY_ID_BCM5750: return "5750";
14653 case TG3_PHY_ID_BCM5752: return "5752";
14654 case TG3_PHY_ID_BCM5714: return "5714";
14655 case TG3_PHY_ID_BCM5780: return "5780";
14656 case TG3_PHY_ID_BCM5755: return "5755";
14657 case TG3_PHY_ID_BCM5787: return "5787";
14658 case TG3_PHY_ID_BCM5784: return "5784";
14659 case TG3_PHY_ID_BCM5756: return "5722/5756";
14660 case TG3_PHY_ID_BCM5906: return "5906";
14661 case TG3_PHY_ID_BCM5761: return "5761";
14662 case TG3_PHY_ID_BCM5718C: return "5718C";
14663 case TG3_PHY_ID_BCM5718S: return "5718S";
14664 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000014665 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000014666 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000014667 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070014668 case 0: return "serdes";
14669 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070014670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014671}
14672
Michael Chanf9804dd2005-09-27 12:13:10 -070014673static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
14674{
14675 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14676 strcpy(str, "PCI Express");
14677 return str;
14678 } else if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
14679 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
14680
14681 strcpy(str, "PCIX:");
14682
14683 if ((clock_ctrl == 7) ||
14684 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
14685 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
14686 strcat(str, "133MHz");
14687 else if (clock_ctrl == 0)
14688 strcat(str, "33MHz");
14689 else if (clock_ctrl == 2)
14690 strcat(str, "50MHz");
14691 else if (clock_ctrl == 4)
14692 strcat(str, "66MHz");
14693 else if (clock_ctrl == 6)
14694 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070014695 } else {
14696 strcpy(str, "PCI:");
14697 if (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED)
14698 strcat(str, "66MHz");
14699 else
14700 strcat(str, "33MHz");
14701 }
14702 if (tp->tg3_flags & TG3_FLAG_PCI_32BIT)
14703 strcat(str, ":32-bit");
14704 else
14705 strcat(str, ":64-bit");
14706 return str;
14707}
14708
Michael Chan8c2dc7e2005-12-19 16:26:02 -080014709static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014710{
14711 struct pci_dev *peer;
14712 unsigned int func, devnr = tp->pdev->devfn & ~7;
14713
14714 for (func = 0; func < 8; func++) {
14715 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14716 if (peer && peer != tp->pdev)
14717 break;
14718 pci_dev_put(peer);
14719 }
Michael Chan16fe9d72005-12-13 21:09:54 -080014720 /* 5704 can be configured in single-port mode, set peer to
14721 * tp->pdev in that case.
14722 */
14723 if (!peer) {
14724 peer = tp->pdev;
14725 return peer;
14726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014727
14728 /*
14729 * We don't need to keep the refcount elevated; there's no way
14730 * to remove one half of this device without removing the other
14731 */
14732 pci_dev_put(peer);
14733
14734 return peer;
14735}
14736
David S. Miller15f98502005-05-18 22:49:26 -070014737static void __devinit tg3_init_coal(struct tg3 *tp)
14738{
14739 struct ethtool_coalesce *ec = &tp->coal;
14740
14741 memset(ec, 0, sizeof(*ec));
14742 ec->cmd = ETHTOOL_GCOALESCE;
14743 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
14744 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
14745 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
14746 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
14747 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
14748 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
14749 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
14750 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
14751 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
14752
14753 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
14754 HOSTCC_MODE_CLRTICK_TXBD)) {
14755 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
14756 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
14757 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
14758 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
14759 }
Michael Chand244c892005-07-05 14:42:33 -070014760
14761 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
14762 ec->rx_coalesce_usecs_irq = 0;
14763 ec->tx_coalesce_usecs_irq = 0;
14764 ec->stats_block_coalesce_usecs = 0;
14765 }
David S. Miller15f98502005-05-18 22:49:26 -070014766}
14767
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014768static const struct net_device_ops tg3_netdev_ops = {
14769 .ndo_open = tg3_open,
14770 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080014771 .ndo_start_xmit = tg3_start_xmit,
Eric Dumazet511d2222010-07-07 20:44:24 +000014772 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -080014773 .ndo_validate_addr = eth_validate_addr,
14774 .ndo_set_multicast_list = tg3_set_rx_mode,
14775 .ndo_set_mac_address = tg3_set_mac_addr,
14776 .ndo_do_ioctl = tg3_ioctl,
14777 .ndo_tx_timeout = tg3_tx_timeout,
14778 .ndo_change_mtu = tg3_change_mtu,
Michał Mirosławdc668912011-04-07 03:35:07 +000014779 .ndo_fix_features = tg3_fix_features,
Stephen Hemminger00829822008-11-20 20:14:53 -080014780#ifdef CONFIG_NET_POLL_CONTROLLER
14781 .ndo_poll_controller = tg3_poll_controller,
14782#endif
14783};
14784
14785static const struct net_device_ops tg3_netdev_ops_dma_bug = {
14786 .ndo_open = tg3_open,
14787 .ndo_stop = tg3_close,
14788 .ndo_start_xmit = tg3_start_xmit_dma_bug,
Eric Dumazet511d2222010-07-07 20:44:24 +000014789 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014790 .ndo_validate_addr = eth_validate_addr,
14791 .ndo_set_multicast_list = tg3_set_rx_mode,
14792 .ndo_set_mac_address = tg3_set_mac_addr,
14793 .ndo_do_ioctl = tg3_ioctl,
14794 .ndo_tx_timeout = tg3_tx_timeout,
14795 .ndo_change_mtu = tg3_change_mtu,
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014796#ifdef CONFIG_NET_POLL_CONTROLLER
14797 .ndo_poll_controller = tg3_poll_controller,
14798#endif
14799};
14800
Linus Torvalds1da177e2005-04-16 15:20:36 -070014801static int __devinit tg3_init_one(struct pci_dev *pdev,
14802 const struct pci_device_id *ent)
14803{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014804 struct net_device *dev;
14805 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000014806 int i, err, pm_cap;
14807 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070014808 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080014809 u64 dma_mask, persist_dma_mask;
Michał Mirosławdc668912011-04-07 03:35:07 +000014810 u32 hw_features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014811
Joe Perches05dbe002010-02-17 19:44:19 +000014812 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014813
14814 err = pci_enable_device(pdev);
14815 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014816 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014817 return err;
14818 }
14819
Linus Torvalds1da177e2005-04-16 15:20:36 -070014820 err = pci_request_regions(pdev, DRV_MODULE_NAME);
14821 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014822 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014823 goto err_out_disable_pdev;
14824 }
14825
14826 pci_set_master(pdev);
14827
14828 /* Find power-management capability. */
14829 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
14830 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000014831 dev_err(&pdev->dev,
14832 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014833 err = -EIO;
14834 goto err_out_free_res;
14835 }
14836
Matt Carlsonfe5f5782009-09-01 13:09:39 +000014837 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014838 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000014839 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014840 err = -ENOMEM;
14841 goto err_out_free_res;
14842 }
14843
Linus Torvalds1da177e2005-04-16 15:20:36 -070014844 SET_NETDEV_DEV(dev, &pdev->dev);
14845
Linus Torvalds1da177e2005-04-16 15:20:36 -070014846 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014847
14848 tp = netdev_priv(dev);
14849 tp->pdev = pdev;
14850 tp->dev = dev;
14851 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014852 tp->rx_mode = TG3_DEF_RX_MODE;
14853 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070014854
Linus Torvalds1da177e2005-04-16 15:20:36 -070014855 if (tg3_debug > 0)
14856 tp->msg_enable = tg3_debug;
14857 else
14858 tp->msg_enable = TG3_DEF_MSG_ENABLE;
14859
14860 /* The word/byte swap controls here control register access byte
14861 * swapping. DMA data byte swapping is controlled in the GRC_MODE
14862 * setting below.
14863 */
14864 tp->misc_host_ctrl =
14865 MISC_HOST_CTRL_MASK_PCI_INT |
14866 MISC_HOST_CTRL_WORD_SWAP |
14867 MISC_HOST_CTRL_INDIR_ACCESS |
14868 MISC_HOST_CTRL_PCISTATE_RW;
14869
14870 /* The NONFRM (non-frame) byte/word swap controls take effect
14871 * on descriptor entries, anything which isn't packet data.
14872 *
14873 * The StrongARM chips on the board (one for tx, one for rx)
14874 * are running in big-endian mode.
14875 */
14876 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
14877 GRC_MODE_WSWAP_NONFRM_DATA);
14878#ifdef __BIG_ENDIAN
14879 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
14880#endif
14881 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014882 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000014883 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014884
Matt Carlsond5fe4882008-11-21 17:20:32 -080014885 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010014886 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014887 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014888 err = -ENOMEM;
14889 goto err_out_free_dev;
14890 }
14891
Linus Torvalds1da177e2005-04-16 15:20:36 -070014892 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
14893 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014894
Linus Torvalds1da177e2005-04-16 15:20:36 -070014895 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014896 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014897 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014898
14899 err = tg3_get_invariants(tp);
14900 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014901 dev_err(&pdev->dev,
14902 "Problem fetching invariants of chip, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014903 goto err_out_iounmap;
14904 }
14905
Matt Carlson615774f2009-11-13 13:03:39 +000014906 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Matt Carlson0a58d662011-04-05 14:22:45 +000014907 !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
Stephen Hemminger00829822008-11-20 20:14:53 -080014908 dev->netdev_ops = &tg3_netdev_ops;
14909 else
14910 dev->netdev_ops = &tg3_netdev_ops_dma_bug;
14911
14912
Michael Chan4a29cc22006-03-19 13:21:12 -080014913 /* The EPB bridge inside 5714, 5715, and 5780 and any
14914 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080014915 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
14916 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
14917 * do DMA address check in tg3_start_xmit().
14918 */
Michael Chan4a29cc22006-03-19 13:21:12 -080014919 if (tp->tg3_flags2 & TG3_FLG2_IS_5788)
Yang Hongyang284901a2009-04-06 19:01:15 -070014920 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Michael Chan4a29cc22006-03-19 13:21:12 -080014921 else if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070014922 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080014923#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070014924 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014925#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080014926 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070014927 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014928
14929 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070014930 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080014931 err = pci_set_dma_mask(pdev, dma_mask);
14932 if (!err) {
14933 dev->features |= NETIF_F_HIGHDMA;
14934 err = pci_set_consistent_dma_mask(pdev,
14935 persist_dma_mask);
14936 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014937 dev_err(&pdev->dev, "Unable to obtain 64 bit "
14938 "DMA for consistent allocations\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080014939 goto err_out_iounmap;
14940 }
14941 }
14942 }
Yang Hongyang284901a2009-04-06 19:01:15 -070014943 if (err || dma_mask == DMA_BIT_MASK(32)) {
14944 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080014945 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014946 dev_err(&pdev->dev,
14947 "No usable DMA configuration, aborting\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080014948 goto err_out_iounmap;
14949 }
14950 }
14951
Michael Chanfdfec1722005-07-25 12:31:48 -070014952 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014953
Matt Carlson507399f2009-11-13 13:03:37 +000014954 /* Selectively allow TSO based on operating conditions */
14955 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) ||
14956 (tp->fw_needed && !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)))
14957 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
14958 else {
14959 tp->tg3_flags2 &= ~(TG3_FLG2_TSO_CAPABLE | TG3_FLG2_TSO_BUG);
14960 tp->fw_needed = NULL;
14961 }
14962
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014963 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
Matt Carlson9e9fd122009-01-19 16:57:45 -080014964 tp->fw_needed = FIRMWARE_TG3;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014965
Michael Chan4e3a7aa2006-03-20 17:47:44 -080014966 /* TSO is on by default on chips that support hardware TSO.
14967 * Firmware TSO on older chips gives lower performance, so it
14968 * is off by default, but can be enabled using ethtool.
14969 */
Matt Carlsone849cdc2009-11-13 13:03:38 +000014970 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) &&
Michał Mirosławdc668912011-04-07 03:35:07 +000014971 (dev->features & NETIF_F_IP_CSUM))
14972 hw_features |= NETIF_F_TSO;
Matt Carlsone849cdc2009-11-13 13:03:38 +000014973 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
14974 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3)) {
Michał Mirosławdc668912011-04-07 03:35:07 +000014975 if (dev->features & NETIF_F_IPV6_CSUM)
14976 hw_features |= NETIF_F_TSO6;
Matt Carlsone849cdc2009-11-13 13:03:38 +000014977 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
14978 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070014979 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14980 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014981 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000014982 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
14983 hw_features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070014984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014985
Michał Mirosławdc668912011-04-07 03:35:07 +000014986 dev->hw_features |= hw_features;
14987 dev->features |= hw_features;
14988 dev->vlan_features |= hw_features;
14989
Linus Torvalds1da177e2005-04-16 15:20:36 -070014990 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
14991 !(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
14992 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
14993 tp->tg3_flags2 |= TG3_FLG2_MAX_RXPEND_64;
14994 tp->rx_pending = 63;
14995 }
14996
Linus Torvalds1da177e2005-04-16 15:20:36 -070014997 err = tg3_get_device_address(tp);
14998 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014999 dev_err(&pdev->dev,
15000 "Could not obtain valid ethernet address, aborting\n");
Matt Carlson026a6c22009-12-03 08:36:24 +000015001 goto err_out_iounmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015002 }
15003
Matt Carlson0d3031d2007-10-10 18:02:43 -070015004 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
Matt Carlson63532392008-11-03 16:49:57 -080015005 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
Al Viro79ea13c2008-01-24 02:06:46 -080015006 if (!tp->aperegs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015007 dev_err(&pdev->dev,
15008 "Cannot map APE registers, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015009 err = -ENOMEM;
Matt Carlson026a6c22009-12-03 08:36:24 +000015010 goto err_out_iounmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015011 }
15012
15013 tg3_ape_lock_init(tp);
Matt Carlson7fd76442009-02-25 14:27:20 +000015014
15015 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
15016 tg3_read_dash_ver(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015017 }
15018
Matt Carlsonc88864d2007-11-12 21:07:01 -080015019 /*
15020 * Reset chip in case UNDI or EFI driver did not shutdown
15021 * DMA self test will enable WDMAC and we'll see (spurious)
15022 * pending DMA on the PCI bus at that point.
15023 */
15024 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15025 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15026 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15027 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15028 }
15029
15030 err = tg3_test_dma(tp);
15031 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015032 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080015033 goto err_out_apeunmap;
15034 }
15035
Matt Carlson78f90dc2009-11-13 13:03:42 +000015036 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15037 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15038 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000015039 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000015040 struct tg3_napi *tnapi = &tp->napi[i];
15041
15042 tnapi->tp = tp;
15043 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15044
15045 tnapi->int_mbox = intmbx;
15046 if (i < 4)
15047 intmbx += 0x8;
15048 else
15049 intmbx += 0x4;
15050
15051 tnapi->consmbox = rcvmbx;
15052 tnapi->prodmbox = sndmbx;
15053
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015054 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015055 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015056 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000015057 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000015058
15059 if (!(tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX))
15060 break;
15061
15062 /*
15063 * If we support MSIX, we'll be using RSS. If we're using
15064 * RSS, the first vector only handles link interrupts and the
15065 * remaining vectors handle rx and tx interrupts. Reuse the
15066 * mailbox values for the next iteration. The values we setup
15067 * above are still useful for the single vectored mode.
15068 */
15069 if (!i)
15070 continue;
15071
15072 rcvmbx += 0x8;
15073
15074 if (sndmbx & 0x4)
15075 sndmbx -= 0x4;
15076 else
15077 sndmbx += 0xc;
15078 }
15079
Matt Carlsonc88864d2007-11-12 21:07:01 -080015080 tg3_init_coal(tp);
15081
Michael Chanc49a1562006-12-17 17:07:29 -080015082 pci_set_drvdata(pdev, dev);
15083
Linus Torvalds1da177e2005-04-16 15:20:36 -070015084 err = register_netdev(dev);
15085 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015086 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015087 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015088 }
15089
Joe Perches05dbe002010-02-17 19:44:19 +000015090 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15091 tp->board_part_number,
15092 tp->pci_chip_rev_id,
15093 tg3_bus_string(tp, str),
15094 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015095
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015096 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000015097 struct phy_device *phydev;
15098 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000015099 netdev_info(dev,
15100 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015101 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015102 } else {
15103 char *ethtype;
15104
15105 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15106 ethtype = "10/100Base-TX";
15107 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15108 ethtype = "1000Base-SX";
15109 else
15110 ethtype = "10/100/1000Base-T";
15111
Matt Carlson5129c3a2010-04-05 10:19:23 +000015112 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015113 "(WireSpeed[%d])\n", tg3_phy_string(tp), ethtype,
15114 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0);
15115 }
Matt Carlsondf59c942008-11-03 16:52:56 -080015116
Joe Perches05dbe002010-02-17 19:44:19 +000015117 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000015118 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches05dbe002010-02-17 19:44:19 +000015119 (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015120 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches05dbe002010-02-17 19:44:19 +000015121 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0,
15122 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) != 0);
15123 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15124 tp->dma_rwctrl,
15125 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15126 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015127
15128 return 0;
15129
Matt Carlson0d3031d2007-10-10 18:02:43 -070015130err_out_apeunmap:
15131 if (tp->aperegs) {
15132 iounmap(tp->aperegs);
15133 tp->aperegs = NULL;
15134 }
15135
Linus Torvalds1da177e2005-04-16 15:20:36 -070015136err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070015137 if (tp->regs) {
15138 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015139 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015141
15142err_out_free_dev:
15143 free_netdev(dev);
15144
15145err_out_free_res:
15146 pci_release_regions(pdev);
15147
15148err_out_disable_pdev:
15149 pci_disable_device(pdev);
15150 pci_set_drvdata(pdev, NULL);
15151 return err;
15152}
15153
15154static void __devexit tg3_remove_one(struct pci_dev *pdev)
15155{
15156 struct net_device *dev = pci_get_drvdata(pdev);
15157
15158 if (dev) {
15159 struct tg3 *tp = netdev_priv(dev);
15160
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015161 if (tp->fw)
15162 release_firmware(tp->fw);
15163
Tejun Heo23f333a2010-12-12 16:45:14 +010015164 cancel_work_sync(&tp->reset_task);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015165
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015166 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
15167 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015168 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015169 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015170
Linus Torvalds1da177e2005-04-16 15:20:36 -070015171 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015172 if (tp->aperegs) {
15173 iounmap(tp->aperegs);
15174 tp->aperegs = NULL;
15175 }
Michael Chan68929142005-08-09 20:17:14 -070015176 if (tp->regs) {
15177 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015178 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015180 free_netdev(dev);
15181 pci_release_regions(pdev);
15182 pci_disable_device(pdev);
15183 pci_set_drvdata(pdev, NULL);
15184 }
15185}
15186
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015187#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015188static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015189{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015190 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015191 struct net_device *dev = pci_get_drvdata(pdev);
15192 struct tg3 *tp = netdev_priv(dev);
15193 int err;
15194
15195 if (!netif_running(dev))
15196 return 0;
15197
Tejun Heo23f333a2010-12-12 16:45:14 +010015198 flush_work_sync(&tp->reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015199 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015200 tg3_netif_stop(tp);
15201
15202 del_timer_sync(&tp->timer);
15203
David S. Millerf47c11e2005-06-24 20:18:35 -070015204 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015205 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015206 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015207
15208 netif_device_detach(dev);
15209
David S. Millerf47c11e2005-06-24 20:18:35 -070015210 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015211 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan6a9eba12005-12-13 21:08:58 -080015212 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
David S. Millerf47c11e2005-06-24 20:18:35 -070015213 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015214
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015215 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015216 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015217 int err2;
15218
David S. Millerf47c11e2005-06-24 20:18:35 -070015219 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015220
Michael Chan6a9eba12005-12-13 21:08:58 -080015221 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015222 err2 = tg3_restart_hw(tp, 1);
15223 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015224 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015225
15226 tp->timer.expires = jiffies + tp->timer_offset;
15227 add_timer(&tp->timer);
15228
15229 netif_device_attach(dev);
15230 tg3_netif_start(tp);
15231
Michael Chanb9ec6c12006-07-25 16:37:27 -070015232out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015233 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015234
15235 if (!err2)
15236 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015237 }
15238
15239 return err;
15240}
15241
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015242static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015243{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015244 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015245 struct net_device *dev = pci_get_drvdata(pdev);
15246 struct tg3 *tp = netdev_priv(dev);
15247 int err;
15248
15249 if (!netif_running(dev))
15250 return 0;
15251
Linus Torvalds1da177e2005-04-16 15:20:36 -070015252 netif_device_attach(dev);
15253
David S. Millerf47c11e2005-06-24 20:18:35 -070015254 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015255
Michael Chan6a9eba12005-12-13 21:08:58 -080015256 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Michael Chanb9ec6c12006-07-25 16:37:27 -070015257 err = tg3_restart_hw(tp, 1);
15258 if (err)
15259 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015260
15261 tp->timer.expires = jiffies + tp->timer_offset;
15262 add_timer(&tp->timer);
15263
Linus Torvalds1da177e2005-04-16 15:20:36 -070015264 tg3_netif_start(tp);
15265
Michael Chanb9ec6c12006-07-25 16:37:27 -070015266out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015267 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015268
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015269 if (!err)
15270 tg3_phy_start(tp);
15271
Michael Chanb9ec6c12006-07-25 16:37:27 -070015272 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015273}
15274
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015275static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015276#define TG3_PM_OPS (&tg3_pm_ops)
15277
15278#else
15279
15280#define TG3_PM_OPS NULL
15281
15282#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015283
Linus Torvalds1da177e2005-04-16 15:20:36 -070015284static struct pci_driver tg3_driver = {
15285 .name = DRV_MODULE_NAME,
15286 .id_table = tg3_pci_tbl,
15287 .probe = tg3_init_one,
15288 .remove = __devexit_p(tg3_remove_one),
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015289 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015290};
15291
15292static int __init tg3_init(void)
15293{
Jeff Garzik29917622006-08-19 17:48:59 -040015294 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015295}
15296
15297static void __exit tg3_cleanup(void)
15298{
15299 pci_unregister_driver(&tg3_driver);
15300}
15301
15302module_init(tg3_init);
15303module_exit(tg3_cleanup);