blob: f4b01c638a330d969ab32f88178bc51ab7eebf73 [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
Joe Perches63c3a662011-04-26 08:12:10 +000065/* Functions & macros to verify TG3_FLAGS types */
66
67static inline int _tg3_flag(enum TG3_FLAGS flag, unsigned long *bits)
68{
69 return test_bit(flag, bits);
70}
71
72static inline void _tg3_flag_set(enum TG3_FLAGS flag, unsigned long *bits)
73{
74 set_bit(flag, bits);
75}
76
77static inline void _tg3_flag_clear(enum TG3_FLAGS flag, unsigned long *bits)
78{
79 clear_bit(flag, bits);
80}
81
82#define tg3_flag(tp, flag) \
83 _tg3_flag(TG3_FLAG_##flag, (tp)->tg3_flags)
84#define tg3_flag_set(tp, flag) \
85 _tg3_flag_set(TG3_FLAG_##flag, (tp)->tg3_flags)
86#define tg3_flag_clear(tp, flag) \
87 _tg3_flag_clear(TG3_FLAG_##flag, (tp)->tg3_flags)
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#define DRV_MODULE_NAME "tg3"
Matt Carlson6867c842010-07-11 09:31:44 +000090#define TG3_MAJ_NUM 3
Matt Carlson43a5f002011-05-19 12:12:56 +000091#define TG3_MIN_NUM 119
Matt Carlson6867c842010-07-11 09:31:44 +000092#define DRV_MODULE_VERSION \
93 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Matt Carlson43a5f002011-05-19 12:12:56 +000094#define DRV_MODULE_RELDATE "May 18, 2011"
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96#define TG3_DEF_MAC_MODE 0
97#define TG3_DEF_RX_MODE 0
98#define TG3_DEF_TX_MODE 0
99#define TG3_DEF_MSG_ENABLE \
100 (NETIF_MSG_DRV | \
101 NETIF_MSG_PROBE | \
102 NETIF_MSG_LINK | \
103 NETIF_MSG_TIMER | \
104 NETIF_MSG_IFDOWN | \
105 NETIF_MSG_IFUP | \
106 NETIF_MSG_RX_ERR | \
107 NETIF_MSG_TX_ERR)
108
109/* length of time before we decide the hardware is borked,
110 * and dev->tx_timeout() should be called to fix the problem
111 */
Joe Perches63c3a662011-04-26 08:12:10 +0000112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#define TG3_TX_TIMEOUT (5 * HZ)
114
115/* hardware minimum and maximum for a single frame's data payload */
116#define TG3_MIN_MTU 60
117#define TG3_MAX_MTU(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000118 (tg3_flag(tp, JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120/* These numbers seem to be hard coded in the NIC firmware somehow.
121 * You can't change the ring sizes, but you can change where you place
122 * them in the NIC onboard memory.
123 */
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000124#define TG3_RX_STD_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000125 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000126 TG3_RX_STD_MAX_SIZE_5717 : TG3_RX_STD_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#define TG3_DEF_RX_RING_PENDING 200
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000128#define TG3_RX_JMB_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000129 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000130 TG3_RX_JMB_MAX_SIZE_5717 : TG3_RX_JMB_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131#define TG3_DEF_RX_JUMBO_RING_PENDING 100
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000132#define TG3_RSS_INDIR_TBL_SIZE 128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134/* Do not place this n-ring entries value into the tp struct itself,
135 * we really want to expose these constants to GCC so that modulo et
136 * al. operations are done with shifts and masks instead of with
137 * hw multiply/modulo instructions. Another solution would be to
138 * replace things like '% foo' with '& (foo - 1)'.
139 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141#define TG3_TX_RING_SIZE 512
142#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
143
Matt Carlson2c49a442010-09-30 10:34:35 +0000144#define TG3_RX_STD_RING_BYTES(tp) \
145 (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
146#define TG3_RX_JMB_RING_BYTES(tp) \
147 (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
148#define TG3_RX_RCB_RING_BYTES(tp) \
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000149 (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
151 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
153
Matt Carlson287be122009-08-28 13:58:46 +0000154#define TG3_DMA_BYTE_ENAB 64
155
156#define TG3_RX_STD_DMA_SZ 1536
157#define TG3_RX_JMB_DMA_SZ 9046
158
159#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
160
161#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
162#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Matt Carlson2c49a442010-09-30 10:34:35 +0000164#define TG3_RX_STD_BUFF_RING_SIZE(tp) \
165 (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000166
Matt Carlson2c49a442010-09-30 10:34:35 +0000167#define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
168 (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000169
Matt Carlsond2757fc2010-04-12 06:58:27 +0000170/* Due to a hardware bug, the 5701 can only DMA to memory addresses
171 * that are at least dword aligned when used in PCIX mode. The driver
172 * works around this bug by double copying the packet. This workaround
173 * is built into the normal double copy length check for efficiency.
174 *
175 * However, the double copy is only necessary on those architectures
176 * where unaligned memory accesses are inefficient. For those architectures
177 * where unaligned memory accesses incur little penalty, we can reintegrate
178 * the 5701 in the normal rx path. Doing so saves a device structure
179 * dereference by hardcoding the double copy threshold in place.
180 */
181#define TG3_RX_COPY_THRESHOLD 256
182#if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
183 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
184#else
185 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
186#endif
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000189#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Matt Carlsonad829262008-11-21 17:16:16 -0800191#define TG3_RAW_IP_ALIGN 2
192
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000193#define TG3_FW_UPDATE_TIMEOUT_SEC 5
194
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800195#define FIRMWARE_TG3 "tigon/tg3.bin"
196#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
197#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199static char version[] __devinitdata =
Joe Perches05dbe002010-02-17 19:44:19 +0000200 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
203MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
204MODULE_LICENSE("GPL");
205MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800206MODULE_FIRMWARE(FIRMWARE_TG3);
207MODULE_FIRMWARE(FIRMWARE_TG3TSO);
208MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
211module_param(tg3_debug, int, 0);
212MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
213
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000214static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700215 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
216 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
217 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
218 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
219 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
220 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
221 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
222 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
223 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
224 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
225 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
226 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
227 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
228 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
229 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
230 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
231 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
232 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
233 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901)},
234 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2)},
235 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
236 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700237 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700238 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700239 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700240 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F)},
242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F)},
247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Michael Chan676917d2006-12-07 00:20:22 -0800255 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700256 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
257 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
258 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
259 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
260 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
261 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
262 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700263 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
264 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700265 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
266 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700267 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700268 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
269 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800270 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
271 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000272 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
273 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Matt Carlson321d32a2008-11-21 17:22:19 -0800274 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
275 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
276 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790)},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000277 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000278 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
279 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000280 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
281 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
282 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
283 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
284 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791)},
285 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795)},
Matt Carlson302b5002010-06-05 17:24:38 +0000286 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Matt Carlsonba1f3c72011-04-05 14:22:50 +0000287 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700288 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
289 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
290 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
291 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
292 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
293 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
294 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
Meelis Roos1dcb14d2011-05-25 05:43:47 +0000295 {PCI_DEVICE(0x10cf, 0x11a2)}, /* Fujitsu 1000base-SX with BCM5703SKHB */
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700296 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297};
298
299MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
300
Andreas Mohr50da8592006-08-14 23:54:30 -0700301static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000303} ethtool_stats_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 { "rx_octets" },
305 { "rx_fragments" },
306 { "rx_ucast_packets" },
307 { "rx_mcast_packets" },
308 { "rx_bcast_packets" },
309 { "rx_fcs_errors" },
310 { "rx_align_errors" },
311 { "rx_xon_pause_rcvd" },
312 { "rx_xoff_pause_rcvd" },
313 { "rx_mac_ctrl_rcvd" },
314 { "rx_xoff_entered" },
315 { "rx_frame_too_long_errors" },
316 { "rx_jabbers" },
317 { "rx_undersize_packets" },
318 { "rx_in_length_errors" },
319 { "rx_out_length_errors" },
320 { "rx_64_or_less_octet_packets" },
321 { "rx_65_to_127_octet_packets" },
322 { "rx_128_to_255_octet_packets" },
323 { "rx_256_to_511_octet_packets" },
324 { "rx_512_to_1023_octet_packets" },
325 { "rx_1024_to_1522_octet_packets" },
326 { "rx_1523_to_2047_octet_packets" },
327 { "rx_2048_to_4095_octet_packets" },
328 { "rx_4096_to_8191_octet_packets" },
329 { "rx_8192_to_9022_octet_packets" },
330
331 { "tx_octets" },
332 { "tx_collisions" },
333
334 { "tx_xon_sent" },
335 { "tx_xoff_sent" },
336 { "tx_flow_control" },
337 { "tx_mac_errors" },
338 { "tx_single_collisions" },
339 { "tx_mult_collisions" },
340 { "tx_deferred" },
341 { "tx_excessive_collisions" },
342 { "tx_late_collisions" },
343 { "tx_collide_2times" },
344 { "tx_collide_3times" },
345 { "tx_collide_4times" },
346 { "tx_collide_5times" },
347 { "tx_collide_6times" },
348 { "tx_collide_7times" },
349 { "tx_collide_8times" },
350 { "tx_collide_9times" },
351 { "tx_collide_10times" },
352 { "tx_collide_11times" },
353 { "tx_collide_12times" },
354 { "tx_collide_13times" },
355 { "tx_collide_14times" },
356 { "tx_collide_15times" },
357 { "tx_ucast_packets" },
358 { "tx_mcast_packets" },
359 { "tx_bcast_packets" },
360 { "tx_carrier_sense_errors" },
361 { "tx_discards" },
362 { "tx_errors" },
363
364 { "dma_writeq_full" },
365 { "dma_write_prioq_full" },
366 { "rxbds_empty" },
367 { "rx_discards" },
368 { "rx_errors" },
369 { "rx_threshold_hit" },
370
371 { "dma_readq_full" },
372 { "dma_read_prioq_full" },
373 { "tx_comp_queue_full" },
374
375 { "ring_set_send_prod_index" },
376 { "ring_status_update" },
377 { "nic_irqs" },
378 { "nic_avoided_irqs" },
Matt Carlson4452d092011-05-19 12:12:51 +0000379 { "nic_tx_threshold_hit" },
380
381 { "mbuf_lwm_thresh_hit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382};
383
Matt Carlson48fa55a2011-04-13 11:05:06 +0000384#define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
385
386
Andreas Mohr50da8592006-08-14 23:54:30 -0700387static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700388 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000389} ethtool_test_keys[] = {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700390 { "nvram test (online) " },
391 { "link test (online) " },
392 { "register test (offline)" },
393 { "memory test (offline)" },
394 { "loopback test (offline)" },
395 { "interrupt test (offline)" },
396};
397
Matt Carlson48fa55a2011-04-13 11:05:06 +0000398#define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
399
400
Michael Chanb401e9e2005-12-19 16:27:04 -0800401static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
402{
403 writel(val, tp->regs + off);
404}
405
406static u32 tg3_read32(struct tg3 *tp, u32 off)
407{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000408 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800409}
410
Matt Carlson0d3031d2007-10-10 18:02:43 -0700411static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
412{
413 writel(val, tp->aperegs + off);
414}
415
416static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
417{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000418 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700419}
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
422{
Michael Chan68929142005-08-09 20:17:14 -0700423 unsigned long flags;
424
425 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700426 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
427 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700428 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700429}
430
431static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
432{
433 writel(val, tp->regs + off);
434 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
Michael Chan68929142005-08-09 20:17:14 -0700437static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
438{
439 unsigned long flags;
440 u32 val;
441
442 spin_lock_irqsave(&tp->indirect_lock, flags);
443 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
444 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
445 spin_unlock_irqrestore(&tp->indirect_lock, flags);
446 return val;
447}
448
449static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
450{
451 unsigned long flags;
452
453 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
454 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
455 TG3_64BIT_REG_LOW, val);
456 return;
457 }
Matt Carlson66711e62009-11-13 13:03:49 +0000458 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700459 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
460 TG3_64BIT_REG_LOW, val);
461 return;
462 }
463
464 spin_lock_irqsave(&tp->indirect_lock, flags);
465 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
466 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
467 spin_unlock_irqrestore(&tp->indirect_lock, flags);
468
469 /* In indirect mode when disabling interrupts, we also need
470 * to clear the interrupt bit in the GRC local ctrl register.
471 */
472 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
473 (val == 0x1)) {
474 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
475 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
476 }
477}
478
479static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
480{
481 unsigned long flags;
482 u32 val;
483
484 spin_lock_irqsave(&tp->indirect_lock, flags);
485 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
486 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
487 spin_unlock_irqrestore(&tp->indirect_lock, flags);
488 return val;
489}
490
Michael Chanb401e9e2005-12-19 16:27:04 -0800491/* usec_wait specifies the wait time in usec when writing to certain registers
492 * where it is unsafe to read back the register without some delay.
493 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
494 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
495 */
496static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Joe Perches63c3a662011-04-26 08:12:10 +0000498 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800499 /* Non-posted methods */
500 tp->write32(tp, off, val);
501 else {
502 /* Posted method */
503 tg3_write32(tp, off, val);
504 if (usec_wait)
505 udelay(usec_wait);
506 tp->read32(tp, off);
507 }
508 /* Wait again after the read for the posted method to guarantee that
509 * the wait time is met.
510 */
511 if (usec_wait)
512 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
Michael Chan09ee9292005-08-09 20:17:00 -0700515static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
516{
517 tp->write32_mbox(tp, off, val);
Joe Perches63c3a662011-04-26 08:12:10 +0000518 if (!tg3_flag(tp, MBOX_WRITE_REORDER) && !tg3_flag(tp, ICH_WORKAROUND))
Michael Chan68929142005-08-09 20:17:14 -0700519 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700520}
521
Michael Chan20094932005-08-09 20:16:32 -0700522static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
524 void __iomem *mbox = tp->regs + off;
525 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000526 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000528 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 readl(mbox);
530}
531
Michael Chanb5d37722006-09-27 16:06:21 -0700532static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
533{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000534 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700535}
536
537static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
538{
539 writel(val, tp->regs + off + GRCMBOX_BASE);
540}
541
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000542#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700543#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000544#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
545#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
546#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700547
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000548#define tw32(reg, val) tp->write32(tp, reg, val)
549#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
550#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
551#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
554{
Michael Chan68929142005-08-09 20:17:14 -0700555 unsigned long flags;
556
Matt Carlson6ff6f812011-05-19 12:12:54 +0000557 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700558 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
559 return;
560
Michael Chan68929142005-08-09 20:17:14 -0700561 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000562 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700563 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
564 pci_write_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 tw32_f(TG3PCI_MEM_WIN_DATA, val);
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
578static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
579{
Michael Chan68929142005-08-09 20:17:14 -0700580 unsigned long flags;
581
Matt Carlson6ff6f812011-05-19 12:12:54 +0000582 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700583 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
584 *val = 0;
585 return;
586 }
587
Michael Chan68929142005-08-09 20:17:14 -0700588 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000589 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700590 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
591 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Michael Chanbbadf502006-04-06 21:46:34 -0700593 /* Always leave this as zero. */
594 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
595 } else {
596 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
597 *val = tr32(TG3PCI_MEM_WIN_DATA);
598
599 /* Always leave this as zero. */
600 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
601 }
Michael Chan68929142005-08-09 20:17:14 -0700602 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
Matt Carlson0d3031d2007-10-10 18:02:43 -0700605static void tg3_ape_lock_init(struct tg3 *tp)
606{
607 int i;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000608 u32 regbase;
609
610 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
611 regbase = TG3_APE_LOCK_GRANT;
612 else
613 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700614
615 /* Make sure the driver hasn't any stale locks. */
616 for (i = 0; i < 8; i++)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000617 tg3_ape_write32(tp, regbase + 4 * i, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700618}
619
620static int tg3_ape_lock(struct tg3 *tp, int locknum)
621{
622 int i, off;
623 int ret = 0;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000624 u32 status, req, gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700625
Joe Perches63c3a662011-04-26 08:12:10 +0000626 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700627 return 0;
628
629 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000630 case TG3_APE_LOCK_GRC:
631 case TG3_APE_LOCK_MEM:
632 break;
633 default:
634 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700635 }
636
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000637 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
638 req = TG3_APE_LOCK_REQ;
639 gnt = TG3_APE_LOCK_GRANT;
640 } else {
641 req = TG3_APE_PER_LOCK_REQ;
642 gnt = TG3_APE_PER_LOCK_GRANT;
643 }
644
Matt Carlson0d3031d2007-10-10 18:02:43 -0700645 off = 4 * locknum;
646
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000647 tg3_ape_write32(tp, req + off, APE_LOCK_REQ_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700648
649 /* Wait for up to 1 millisecond to acquire lock. */
650 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000651 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700652 if (status == APE_LOCK_GRANT_DRIVER)
653 break;
654 udelay(10);
655 }
656
657 if (status != APE_LOCK_GRANT_DRIVER) {
658 /* Revoke the lock request. */
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000659 tg3_ape_write32(tp, gnt + off,
Matt Carlson0d3031d2007-10-10 18:02:43 -0700660 APE_LOCK_GRANT_DRIVER);
661
662 ret = -EBUSY;
663 }
664
665 return ret;
666}
667
668static void tg3_ape_unlock(struct tg3 *tp, int locknum)
669{
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000670 u32 gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700671
Joe Perches63c3a662011-04-26 08:12:10 +0000672 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700673 return;
674
675 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000676 case TG3_APE_LOCK_GRC:
677 case TG3_APE_LOCK_MEM:
678 break;
679 default:
680 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700681 }
682
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000683 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
684 gnt = TG3_APE_LOCK_GRANT;
685 else
686 gnt = TG3_APE_PER_LOCK_GRANT;
687
688 tg3_ape_write32(tp, gnt + 4 * locknum, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700689}
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691static void tg3_disable_ints(struct tg3 *tp)
692{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000693 int i;
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 tw32(TG3PCI_MISC_HOST_CTRL,
696 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000697 for (i = 0; i < tp->irq_max; i++)
698 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701static void tg3_enable_ints(struct tg3 *tp)
702{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000703 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000704
Michael Chanbbe832c2005-06-24 20:20:04 -0700705 tp->irq_sync = 0;
706 wmb();
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 tw32(TG3PCI_MISC_HOST_CTRL,
709 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000710
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000711 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000712 for (i = 0; i < tp->irq_cnt; i++) {
713 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000714
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000715 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +0000716 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000717 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
718
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000719 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000720 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000721
722 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +0000723 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000724 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
725 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
726 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000727 tw32(HOSTCC_MODE, tp->coal_now);
728
729 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
Matt Carlson17375d22009-08-28 14:02:18 +0000732static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700733{
Matt Carlson17375d22009-08-28 14:02:18 +0000734 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000735 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700736 unsigned int work_exists = 0;
737
738 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +0000739 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -0700740 if (sblk->status & SD_STATUS_LINK_CHG)
741 work_exists = 1;
742 }
743 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000744 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000745 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700746 work_exists = 1;
747
748 return work_exists;
749}
750
Matt Carlson17375d22009-08-28 14:02:18 +0000751/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700752 * similar to tg3_enable_ints, but it accurately determines whether there
753 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400754 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 */
Matt Carlson17375d22009-08-28 14:02:18 +0000756static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Matt Carlson17375d22009-08-28 14:02:18 +0000758 struct tg3 *tp = tnapi->tp;
759
Matt Carlson898a56f2009-08-28 14:02:40 +0000760 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 mmiowb();
762
David S. Millerfac9b832005-05-18 22:46:34 -0700763 /* When doing tagged status, this work check is unnecessary.
764 * The last_tag we write above tells the chip which piece of
765 * work we've completed.
766 */
Joe Perches63c3a662011-04-26 08:12:10 +0000767 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700768 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000769 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772static void tg3_switch_clocks(struct tg3 *tp)
773{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000774 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 u32 orig_clock_ctrl;
776
Joe Perches63c3a662011-04-26 08:12:10 +0000777 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700778 return;
779
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000780 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 orig_clock_ctrl = clock_ctrl;
783 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
784 CLOCK_CTRL_CLKRUN_OENABLE |
785 0x1f);
786 tp->pci_clock_ctrl = clock_ctrl;
787
Joe Perches63c3a662011-04-26 08:12:10 +0000788 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800790 tw32_wait_f(TG3PCI_CLOCK_CTRL,
791 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800794 tw32_wait_f(TG3PCI_CLOCK_CTRL,
795 clock_ctrl |
796 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
797 40);
798 tw32_wait_f(TG3PCI_CLOCK_CTRL,
799 clock_ctrl | (CLOCK_CTRL_ALTCLK),
800 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800802 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
805#define PHY_BUSY_LOOPS 5000
806
807static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
808{
809 u32 frame_val;
810 unsigned int loops;
811 int ret;
812
813 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
814 tw32_f(MAC_MI_MODE,
815 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
816 udelay(80);
817 }
818
819 *val = 0x0;
820
Matt Carlson882e9792009-09-01 13:21:36 +0000821 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 MI_COM_PHY_ADDR_MASK);
823 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
824 MI_COM_REG_ADDR_MASK);
825 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 tw32_f(MAC_MI_COM, frame_val);
828
829 loops = PHY_BUSY_LOOPS;
830 while (loops != 0) {
831 udelay(10);
832 frame_val = tr32(MAC_MI_COM);
833
834 if ((frame_val & MI_COM_BUSY) == 0) {
835 udelay(5);
836 frame_val = tr32(MAC_MI_COM);
837 break;
838 }
839 loops -= 1;
840 }
841
842 ret = -EBUSY;
843 if (loops != 0) {
844 *val = frame_val & MI_COM_DATA_MASK;
845 ret = 0;
846 }
847
848 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
849 tw32_f(MAC_MI_MODE, tp->mi_mode);
850 udelay(80);
851 }
852
853 return ret;
854}
855
856static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
857{
858 u32 frame_val;
859 unsigned int loops;
860 int ret;
861
Matt Carlsonf07e9af2010-08-02 11:26:07 +0000862 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Michael Chanb5d37722006-09-27 16:06:21 -0700863 (reg == MII_TG3_CTRL || reg == MII_TG3_AUX_CTRL))
864 return 0;
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
867 tw32_f(MAC_MI_MODE,
868 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
869 udelay(80);
870 }
871
Matt Carlson882e9792009-09-01 13:21:36 +0000872 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 MI_COM_PHY_ADDR_MASK);
874 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
875 MI_COM_REG_ADDR_MASK);
876 frame_val |= (val & MI_COM_DATA_MASK);
877 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 tw32_f(MAC_MI_COM, frame_val);
880
881 loops = PHY_BUSY_LOOPS;
882 while (loops != 0) {
883 udelay(10);
884 frame_val = tr32(MAC_MI_COM);
885 if ((frame_val & MI_COM_BUSY) == 0) {
886 udelay(5);
887 frame_val = tr32(MAC_MI_COM);
888 break;
889 }
890 loops -= 1;
891 }
892
893 ret = -EBUSY;
894 if (loops != 0)
895 ret = 0;
896
897 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
898 tw32_f(MAC_MI_MODE, tp->mi_mode);
899 udelay(80);
900 }
901
902 return ret;
903}
904
Matt Carlsonb0988c12011-04-20 07:57:39 +0000905static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
906{
907 int err;
908
909 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
910 if (err)
911 goto done;
912
913 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
914 if (err)
915 goto done;
916
917 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
918 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
919 if (err)
920 goto done;
921
922 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
923
924done:
925 return err;
926}
927
928static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
929{
930 int err;
931
932 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
933 if (err)
934 goto done;
935
936 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
937 if (err)
938 goto done;
939
940 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
941 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
942 if (err)
943 goto done;
944
945 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
946
947done:
948 return err;
949}
950
951static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
952{
953 int err;
954
955 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
956 if (!err)
957 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
958
959 return err;
960}
961
962static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
963{
964 int err;
965
966 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
967 if (!err)
968 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
969
970 return err;
971}
972
Matt Carlson15ee95c2011-04-20 07:57:40 +0000973static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
974{
975 int err;
976
977 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
978 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
979 MII_TG3_AUXCTL_SHDWSEL_MISC);
980 if (!err)
981 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
982
983 return err;
984}
985
Matt Carlsonb4bd2922011-04-20 07:57:41 +0000986static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
987{
988 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
989 set |= MII_TG3_AUXCTL_MISC_WREN;
990
991 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
992}
993
Matt Carlson1d36ba42011-04-20 07:57:42 +0000994#define TG3_PHY_AUXCTL_SMDSP_ENABLE(tp) \
995 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
996 MII_TG3_AUXCTL_ACTL_SMDSP_ENA | \
997 MII_TG3_AUXCTL_ACTL_TX_6DB)
998
999#define TG3_PHY_AUXCTL_SMDSP_DISABLE(tp) \
1000 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1001 MII_TG3_AUXCTL_ACTL_TX_6DB);
1002
Matt Carlson95e28692008-05-25 23:44:14 -07001003static int tg3_bmcr_reset(struct tg3 *tp)
1004{
1005 u32 phy_control;
1006 int limit, err;
1007
1008 /* OK, reset it, and poll the BMCR_RESET bit until it
1009 * clears or we time out.
1010 */
1011 phy_control = BMCR_RESET;
1012 err = tg3_writephy(tp, MII_BMCR, phy_control);
1013 if (err != 0)
1014 return -EBUSY;
1015
1016 limit = 5000;
1017 while (limit--) {
1018 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1019 if (err != 0)
1020 return -EBUSY;
1021
1022 if ((phy_control & BMCR_RESET) == 0) {
1023 udelay(40);
1024 break;
1025 }
1026 udelay(10);
1027 }
Roel Kluind4675b52009-02-12 16:33:27 -08001028 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001029 return -EBUSY;
1030
1031 return 0;
1032}
1033
Matt Carlson158d7ab2008-05-29 01:37:54 -07001034static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1035{
Francois Romieu3d165432009-01-19 16:56:50 -08001036 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001037 u32 val;
1038
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001039 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001040
1041 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001042 val = -EIO;
1043
1044 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001045
1046 return val;
1047}
1048
1049static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1050{
Francois Romieu3d165432009-01-19 16:56:50 -08001051 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001052 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001053
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001054 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001055
1056 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001057 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001058
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001059 spin_unlock_bh(&tp->lock);
1060
1061 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001062}
1063
1064static int tg3_mdio_reset(struct mii_bus *bp)
1065{
1066 return 0;
1067}
1068
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001069static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001070{
1071 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001072 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001073
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001074 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001075 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001076 case PHY_ID_BCM50610:
1077 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001078 val = MAC_PHYCFG2_50610_LED_MODES;
1079 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001080 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001081 val = MAC_PHYCFG2_AC131_LED_MODES;
1082 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001083 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001084 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1085 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001086 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001087 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1088 break;
1089 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001090 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001091 }
1092
1093 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1094 tw32(MAC_PHYCFG2, val);
1095
1096 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001097 val &= ~(MAC_PHYCFG1_RGMII_INT |
1098 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1099 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001100 tw32(MAC_PHYCFG1, val);
1101
1102 return;
1103 }
1104
Joe Perches63c3a662011-04-26 08:12:10 +00001105 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001106 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1107 MAC_PHYCFG2_FMODE_MASK_MASK |
1108 MAC_PHYCFG2_GMODE_MASK_MASK |
1109 MAC_PHYCFG2_ACT_MASK_MASK |
1110 MAC_PHYCFG2_QUAL_MASK_MASK |
1111 MAC_PHYCFG2_INBAND_ENABLE;
1112
1113 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001114
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001115 val = tr32(MAC_PHYCFG1);
1116 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1117 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001118 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1119 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001120 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001121 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001122 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1123 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001124 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1125 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1126 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001127
Matt Carlsona9daf362008-05-25 23:49:44 -07001128 val = tr32(MAC_EXT_RGMII_MODE);
1129 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1130 MAC_RGMII_MODE_RX_QUALITY |
1131 MAC_RGMII_MODE_RX_ACTIVITY |
1132 MAC_RGMII_MODE_RX_ENG_DET |
1133 MAC_RGMII_MODE_TX_ENABLE |
1134 MAC_RGMII_MODE_TX_LOWPWR |
1135 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001136 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1137 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001138 val |= MAC_RGMII_MODE_RX_INT_B |
1139 MAC_RGMII_MODE_RX_QUALITY |
1140 MAC_RGMII_MODE_RX_ACTIVITY |
1141 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001142 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001143 val |= MAC_RGMII_MODE_TX_ENABLE |
1144 MAC_RGMII_MODE_TX_LOWPWR |
1145 MAC_RGMII_MODE_TX_RESET;
1146 }
1147 tw32(MAC_EXT_RGMII_MODE, val);
1148}
1149
Matt Carlson158d7ab2008-05-29 01:37:54 -07001150static void tg3_mdio_start(struct tg3 *tp)
1151{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001152 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1153 tw32_f(MAC_MI_MODE, tp->mi_mode);
1154 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001155
Joe Perches63c3a662011-04-26 08:12:10 +00001156 if (tg3_flag(tp, MDIOBUS_INITED) &&
Matt Carlson9ea48182010-02-17 15:17:01 +00001157 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1158 tg3_mdio_config_5785(tp);
1159}
1160
1161static int tg3_mdio_init(struct tg3 *tp)
1162{
1163 int i;
1164 u32 reg;
1165 struct phy_device *phydev;
1166
Joe Perches63c3a662011-04-26 08:12:10 +00001167 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001168 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001169
Matt Carlson9c7df912010-06-05 17:24:36 +00001170 tp->phy_addr = PCI_FUNC(tp->pdev->devfn) + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001171
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001172 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1173 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1174 else
1175 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1176 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001177 if (is_serdes)
1178 tp->phy_addr += 7;
1179 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001180 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001181
Matt Carlson158d7ab2008-05-29 01:37:54 -07001182 tg3_mdio_start(tp);
1183
Joe Perches63c3a662011-04-26 08:12:10 +00001184 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001185 return 0;
1186
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001187 tp->mdio_bus = mdiobus_alloc();
1188 if (tp->mdio_bus == NULL)
1189 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001190
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001191 tp->mdio_bus->name = "tg3 mdio bus";
1192 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001193 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001194 tp->mdio_bus->priv = tp;
1195 tp->mdio_bus->parent = &tp->pdev->dev;
1196 tp->mdio_bus->read = &tg3_mdio_read;
1197 tp->mdio_bus->write = &tg3_mdio_write;
1198 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001199 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001200 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001201
1202 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001203 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001204
1205 /* The bus registration will look for all the PHYs on the mdio bus.
1206 * Unfortunately, it does not ensure the PHY is powered up before
1207 * accessing the PHY ID registers. A chip reset is the
1208 * quickest way to bring the device back to an operational state..
1209 */
1210 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1211 tg3_bmcr_reset(tp);
1212
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001213 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001214 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001215 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001216 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001217 return i;
1218 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001219
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001220 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001221
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001222 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001223 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001224 mdiobus_unregister(tp->mdio_bus);
1225 mdiobus_free(tp->mdio_bus);
1226 return -ENODEV;
1227 }
1228
1229 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001230 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001231 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001232 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001233 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001234 case PHY_ID_BCM50610:
1235 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001236 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001237 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001238 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001239 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001240 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001241 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001242 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001243 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001244 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001245 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001246 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001247 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001248 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001249 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001250 case PHY_ID_RTL8201E:
1251 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001252 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001253 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001254 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001255 break;
1256 }
1257
Joe Perches63c3a662011-04-26 08:12:10 +00001258 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001259
1260 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1261 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001262
1263 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001264}
1265
1266static void tg3_mdio_fini(struct tg3 *tp)
1267{
Joe Perches63c3a662011-04-26 08:12:10 +00001268 if (tg3_flag(tp, MDIOBUS_INITED)) {
1269 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001270 mdiobus_unregister(tp->mdio_bus);
1271 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001272 }
1273}
1274
Matt Carlson95e28692008-05-25 23:44:14 -07001275/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001276static inline void tg3_generate_fw_event(struct tg3 *tp)
1277{
1278 u32 val;
1279
1280 val = tr32(GRC_RX_CPU_EVENT);
1281 val |= GRC_RX_CPU_DRIVER_EVENT;
1282 tw32_f(GRC_RX_CPU_EVENT, val);
1283
1284 tp->last_event_jiffies = jiffies;
1285}
1286
1287#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1288
1289/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001290static void tg3_wait_for_event_ack(struct tg3 *tp)
1291{
1292 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001293 unsigned int delay_cnt;
1294 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001295
Matt Carlson4ba526c2008-08-15 14:10:04 -07001296 /* If enough time has passed, no wait is necessary. */
1297 time_remain = (long)(tp->last_event_jiffies + 1 +
1298 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1299 (long)jiffies;
1300 if (time_remain < 0)
1301 return;
1302
1303 /* Check if we can shorten the wait time. */
1304 delay_cnt = jiffies_to_usecs(time_remain);
1305 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1306 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1307 delay_cnt = (delay_cnt >> 3) + 1;
1308
1309 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001310 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1311 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001312 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001313 }
1314}
1315
1316/* tp->lock is held. */
1317static void tg3_ump_link_report(struct tg3 *tp)
1318{
1319 u32 reg;
1320 u32 val;
1321
Joe Perches63c3a662011-04-26 08:12:10 +00001322 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson95e28692008-05-25 23:44:14 -07001323 return;
1324
1325 tg3_wait_for_event_ack(tp);
1326
1327 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1328
1329 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1330
1331 val = 0;
1332 if (!tg3_readphy(tp, MII_BMCR, &reg))
1333 val = reg << 16;
1334 if (!tg3_readphy(tp, MII_BMSR, &reg))
1335 val |= (reg & 0xffff);
1336 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1337
1338 val = 0;
1339 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1340 val = reg << 16;
1341 if (!tg3_readphy(tp, MII_LPA, &reg))
1342 val |= (reg & 0xffff);
1343 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1344
1345 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001346 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001347 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1348 val = reg << 16;
1349 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1350 val |= (reg & 0xffff);
1351 }
1352 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1353
1354 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1355 val = reg << 16;
1356 else
1357 val = 0;
1358 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1359
Matt Carlson4ba526c2008-08-15 14:10:04 -07001360 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001361}
1362
1363static void tg3_link_report(struct tg3 *tp)
1364{
1365 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001366 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001367 tg3_ump_link_report(tp);
1368 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001369 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1370 (tp->link_config.active_speed == SPEED_1000 ?
1371 1000 :
1372 (tp->link_config.active_speed == SPEED_100 ?
1373 100 : 10)),
1374 (tp->link_config.active_duplex == DUPLEX_FULL ?
1375 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001376
Joe Perches05dbe002010-02-17 19:44:19 +00001377 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1378 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1379 "on" : "off",
1380 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1381 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001382
1383 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1384 netdev_info(tp->dev, "EEE is %s\n",
1385 tp->setlpicnt ? "enabled" : "disabled");
1386
Matt Carlson95e28692008-05-25 23:44:14 -07001387 tg3_ump_link_report(tp);
1388 }
1389}
1390
1391static u16 tg3_advert_flowctrl_1000T(u8 flow_ctrl)
1392{
1393 u16 miireg;
1394
Steve Glendinninge18ce342008-12-16 02:00:00 -08001395 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001396 miireg = ADVERTISE_PAUSE_CAP;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001397 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001398 miireg = ADVERTISE_PAUSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001399 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001400 miireg = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1401 else
1402 miireg = 0;
1403
1404 return miireg;
1405}
1406
1407static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1408{
1409 u16 miireg;
1410
Steve Glendinninge18ce342008-12-16 02:00:00 -08001411 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001412 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001413 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001414 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001415 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001416 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1417 else
1418 miireg = 0;
1419
1420 return miireg;
1421}
1422
Matt Carlson95e28692008-05-25 23:44:14 -07001423static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1424{
1425 u8 cap = 0;
1426
1427 if (lcladv & ADVERTISE_1000XPAUSE) {
1428 if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1429 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001430 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001431 else if (rmtadv & LPA_1000XPAUSE_ASYM)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001432 cap = FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001433 } else {
1434 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001435 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001436 }
1437 } else if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1438 if ((rmtadv & LPA_1000XPAUSE) && (rmtadv & LPA_1000XPAUSE_ASYM))
Steve Glendinninge18ce342008-12-16 02:00:00 -08001439 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001440 }
1441
1442 return cap;
1443}
1444
Matt Carlsonf51f3562008-05-25 23:45:08 -07001445static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001446{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001447 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001448 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001449 u32 old_rx_mode = tp->rx_mode;
1450 u32 old_tx_mode = tp->tx_mode;
1451
Joe Perches63c3a662011-04-26 08:12:10 +00001452 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001453 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001454 else
1455 autoneg = tp->link_config.autoneg;
1456
Joe Perches63c3a662011-04-26 08:12:10 +00001457 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001458 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001459 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001460 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001461 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001462 } else
1463 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001464
Matt Carlsonf51f3562008-05-25 23:45:08 -07001465 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001466
Steve Glendinninge18ce342008-12-16 02:00:00 -08001467 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001468 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1469 else
1470 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1471
Matt Carlsonf51f3562008-05-25 23:45:08 -07001472 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001473 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001474
Steve Glendinninge18ce342008-12-16 02:00:00 -08001475 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001476 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1477 else
1478 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1479
Matt Carlsonf51f3562008-05-25 23:45:08 -07001480 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001481 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001482}
1483
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001484static void tg3_adjust_link(struct net_device *dev)
1485{
1486 u8 oldflowctrl, linkmesg = 0;
1487 u32 mac_mode, lcl_adv, rmt_adv;
1488 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001489 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001490
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001491 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001492
1493 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1494 MAC_MODE_HALF_DUPLEX);
1495
1496 oldflowctrl = tp->link_config.active_flowctrl;
1497
1498 if (phydev->link) {
1499 lcl_adv = 0;
1500 rmt_adv = 0;
1501
1502 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1503 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001504 else if (phydev->speed == SPEED_1000 ||
1505 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001506 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001507 else
1508 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001509
1510 if (phydev->duplex == DUPLEX_HALF)
1511 mac_mode |= MAC_MODE_HALF_DUPLEX;
1512 else {
1513 lcl_adv = tg3_advert_flowctrl_1000T(
1514 tp->link_config.flowctrl);
1515
1516 if (phydev->pause)
1517 rmt_adv = LPA_PAUSE_CAP;
1518 if (phydev->asym_pause)
1519 rmt_adv |= LPA_PAUSE_ASYM;
1520 }
1521
1522 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1523 } else
1524 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1525
1526 if (mac_mode != tp->mac_mode) {
1527 tp->mac_mode = mac_mode;
1528 tw32_f(MAC_MODE, tp->mac_mode);
1529 udelay(40);
1530 }
1531
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001532 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1533 if (phydev->speed == SPEED_10)
1534 tw32(MAC_MI_STAT,
1535 MAC_MI_STAT_10MBPS_MODE |
1536 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1537 else
1538 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1539 }
1540
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001541 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1542 tw32(MAC_TX_LENGTHS,
1543 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1544 (6 << TX_LENGTHS_IPG_SHIFT) |
1545 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1546 else
1547 tw32(MAC_TX_LENGTHS,
1548 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1549 (6 << TX_LENGTHS_IPG_SHIFT) |
1550 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1551
1552 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1553 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1554 phydev->speed != tp->link_config.active_speed ||
1555 phydev->duplex != tp->link_config.active_duplex ||
1556 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001557 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001558
1559 tp->link_config.active_speed = phydev->speed;
1560 tp->link_config.active_duplex = phydev->duplex;
1561
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001562 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001563
1564 if (linkmesg)
1565 tg3_link_report(tp);
1566}
1567
1568static int tg3_phy_init(struct tg3 *tp)
1569{
1570 struct phy_device *phydev;
1571
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001572 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001573 return 0;
1574
1575 /* Bring the PHY back to a known state. */
1576 tg3_bmcr_reset(tp);
1577
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001578 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001579
1580 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08001581 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001582 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001583 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001584 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001585 return PTR_ERR(phydev);
1586 }
1587
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001588 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001589 switch (phydev->interface) {
1590 case PHY_INTERFACE_MODE_GMII:
1591 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001592 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08001593 phydev->supported &= (PHY_GBIT_FEATURES |
1594 SUPPORTED_Pause |
1595 SUPPORTED_Asym_Pause);
1596 break;
1597 }
1598 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001599 case PHY_INTERFACE_MODE_MII:
1600 phydev->supported &= (PHY_BASIC_FEATURES |
1601 SUPPORTED_Pause |
1602 SUPPORTED_Asym_Pause);
1603 break;
1604 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001605 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001606 return -EINVAL;
1607 }
1608
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001609 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001610
1611 phydev->advertising = phydev->supported;
1612
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001613 return 0;
1614}
1615
1616static void tg3_phy_start(struct tg3 *tp)
1617{
1618 struct phy_device *phydev;
1619
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001620 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001621 return;
1622
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001623 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001624
Matt Carlson800960682010-08-02 11:26:06 +00001625 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
1626 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001627 phydev->speed = tp->link_config.orig_speed;
1628 phydev->duplex = tp->link_config.orig_duplex;
1629 phydev->autoneg = tp->link_config.orig_autoneg;
1630 phydev->advertising = tp->link_config.orig_advertising;
1631 }
1632
1633 phy_start(phydev);
1634
1635 phy_start_aneg(phydev);
1636}
1637
1638static void tg3_phy_stop(struct tg3 *tp)
1639{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001640 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001641 return;
1642
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001643 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001644}
1645
1646static void tg3_phy_fini(struct tg3 *tp)
1647{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001648 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001649 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001650 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001651 }
1652}
1653
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001654static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1655{
1656 u32 phytest;
1657
1658 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1659 u32 phy;
1660
1661 tg3_writephy(tp, MII_TG3_FET_TEST,
1662 phytest | MII_TG3_FET_SHADOW_EN);
1663 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1664 if (enable)
1665 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1666 else
1667 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1668 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1669 }
1670 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1671 }
1672}
1673
Matt Carlson6833c042008-11-21 17:18:59 -08001674static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1675{
1676 u32 reg;
1677
Joe Perches63c3a662011-04-26 08:12:10 +00001678 if (!tg3_flag(tp, 5705_PLUS) ||
1679 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001680 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001681 return;
1682
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001683 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001684 tg3_phy_fet_toggle_apd(tp, enable);
1685 return;
1686 }
1687
Matt Carlson6833c042008-11-21 17:18:59 -08001688 reg = MII_TG3_MISC_SHDW_WREN |
1689 MII_TG3_MISC_SHDW_SCR5_SEL |
1690 MII_TG3_MISC_SHDW_SCR5_LPED |
1691 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1692 MII_TG3_MISC_SHDW_SCR5_SDTL |
1693 MII_TG3_MISC_SHDW_SCR5_C125OE;
1694 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1695 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1696
1697 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1698
1699
1700 reg = MII_TG3_MISC_SHDW_WREN |
1701 MII_TG3_MISC_SHDW_APD_SEL |
1702 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1703 if (enable)
1704 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1705
1706 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1707}
1708
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001709static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
1710{
1711 u32 phy;
1712
Joe Perches63c3a662011-04-26 08:12:10 +00001713 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001714 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001715 return;
1716
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001717 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001718 u32 ephy;
1719
Matt Carlson535ef6e2009-08-25 10:09:36 +00001720 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
1721 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
1722
1723 tg3_writephy(tp, MII_TG3_FET_TEST,
1724 ephy | MII_TG3_FET_SHADOW_EN);
1725 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001726 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00001727 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001728 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00001729 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1730 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001731 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00001732 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001733 }
1734 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00001735 int ret;
1736
1737 ret = tg3_phy_auxctl_read(tp,
1738 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
1739 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001740 if (enable)
1741 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1742 else
1743 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001744 tg3_phy_auxctl_write(tp,
1745 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001746 }
1747 }
1748}
1749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750static void tg3_phy_set_wirespeed(struct tg3 *tp)
1751{
Matt Carlson15ee95c2011-04-20 07:57:40 +00001752 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 u32 val;
1754
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001755 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 return;
1757
Matt Carlson15ee95c2011-04-20 07:57:40 +00001758 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
1759 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001760 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
1761 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762}
1763
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001764static void tg3_phy_apply_otp(struct tg3 *tp)
1765{
1766 u32 otp, phy;
1767
1768 if (!tp->phy_otp)
1769 return;
1770
1771 otp = tp->phy_otp;
1772
Matt Carlson1d36ba42011-04-20 07:57:42 +00001773 if (TG3_PHY_AUXCTL_SMDSP_ENABLE(tp))
1774 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001775
1776 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
1777 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
1778 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
1779
1780 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
1781 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
1782 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
1783
1784 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
1785 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
1786 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
1787
1788 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
1789 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
1790
1791 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
1792 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
1793
1794 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
1795 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
1796 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
1797
Matt Carlson1d36ba42011-04-20 07:57:42 +00001798 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001799}
1800
Matt Carlson52b02d02010-10-14 10:37:41 +00001801static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
1802{
1803 u32 val;
1804
1805 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
1806 return;
1807
1808 tp->setlpicnt = 0;
1809
1810 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
1811 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00001812 tp->link_config.active_duplex == DUPLEX_FULL &&
1813 (tp->link_config.active_speed == SPEED_100 ||
1814 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00001815 u32 eeectl;
1816
1817 if (tp->link_config.active_speed == SPEED_1000)
1818 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
1819 else
1820 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
1821
1822 tw32(TG3_CPMU_EEE_CTRL, eeectl);
1823
Matt Carlson3110f5f52010-12-06 08:28:50 +00001824 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
1825 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00001826
Matt Carlsonb0c59432011-05-19 12:12:48 +00001827 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
1828 val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
Matt Carlson52b02d02010-10-14 10:37:41 +00001829 tp->setlpicnt = 2;
1830 }
1831
1832 if (!tp->setlpicnt) {
1833 val = tr32(TG3_CPMU_EEE_MODE);
1834 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
1835 }
1836}
1837
Matt Carlsonb0c59432011-05-19 12:12:48 +00001838static void tg3_phy_eee_enable(struct tg3 *tp)
1839{
1840 u32 val;
1841
1842 if (tp->link_config.active_speed == SPEED_1000 &&
1843 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
1844 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
1845 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) &&
1846 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
1847 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0003);
1848 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
1849 }
1850
1851 val = tr32(TG3_CPMU_EEE_MODE);
1852 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
1853}
1854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855static int tg3_wait_macro_done(struct tg3 *tp)
1856{
1857 int limit = 100;
1858
1859 while (limit--) {
1860 u32 tmp32;
1861
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001862 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 if ((tmp32 & 0x1000) == 0)
1864 break;
1865 }
1866 }
Roel Kluind4675b52009-02-12 16:33:27 -08001867 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 return -EBUSY;
1869
1870 return 0;
1871}
1872
1873static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
1874{
1875 static const u32 test_pat[4][6] = {
1876 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
1877 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
1878 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
1879 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
1880 };
1881 int chan;
1882
1883 for (chan = 0; chan < 4; chan++) {
1884 int i;
1885
1886 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1887 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001888 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
1890 for (i = 0; i < 6; i++)
1891 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
1892 test_pat[chan][i]);
1893
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001894 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 if (tg3_wait_macro_done(tp)) {
1896 *resetp = 1;
1897 return -EBUSY;
1898 }
1899
1900 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1901 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001902 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 if (tg3_wait_macro_done(tp)) {
1904 *resetp = 1;
1905 return -EBUSY;
1906 }
1907
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001908 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 if (tg3_wait_macro_done(tp)) {
1910 *resetp = 1;
1911 return -EBUSY;
1912 }
1913
1914 for (i = 0; i < 6; i += 2) {
1915 u32 low, high;
1916
1917 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
1918 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
1919 tg3_wait_macro_done(tp)) {
1920 *resetp = 1;
1921 return -EBUSY;
1922 }
1923 low &= 0x7fff;
1924 high &= 0x000f;
1925 if (low != test_pat[chan][i] ||
1926 high != test_pat[chan][i+1]) {
1927 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
1928 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
1929 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
1930
1931 return -EBUSY;
1932 }
1933 }
1934 }
1935
1936 return 0;
1937}
1938
1939static int tg3_phy_reset_chanpat(struct tg3 *tp)
1940{
1941 int chan;
1942
1943 for (chan = 0; chan < 4; chan++) {
1944 int i;
1945
1946 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1947 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001948 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 for (i = 0; i < 6; i++)
1950 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001951 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 if (tg3_wait_macro_done(tp))
1953 return -EBUSY;
1954 }
1955
1956 return 0;
1957}
1958
1959static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
1960{
1961 u32 reg32, phy9_orig;
1962 int retries, do_phy_reset, err;
1963
1964 retries = 10;
1965 do_phy_reset = 1;
1966 do {
1967 if (do_phy_reset) {
1968 err = tg3_bmcr_reset(tp);
1969 if (err)
1970 return err;
1971 do_phy_reset = 0;
1972 }
1973
1974 /* Disable transmitter and interrupt. */
1975 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
1976 continue;
1977
1978 reg32 |= 0x3000;
1979 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1980
1981 /* Set full-duplex, 1000 mbps. */
1982 tg3_writephy(tp, MII_BMCR,
1983 BMCR_FULLDPLX | TG3_BMCR_SPEED1000);
1984
1985 /* Set to master mode. */
1986 if (tg3_readphy(tp, MII_TG3_CTRL, &phy9_orig))
1987 continue;
1988
1989 tg3_writephy(tp, MII_TG3_CTRL,
1990 (MII_TG3_CTRL_AS_MASTER |
1991 MII_TG3_CTRL_ENABLE_AS_MASTER));
1992
Matt Carlson1d36ba42011-04-20 07:57:42 +00001993 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
1994 if (err)
1995 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
1997 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00001998 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
2000 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2001 if (!err)
2002 break;
2003 } while (--retries);
2004
2005 err = tg3_phy_reset_chanpat(tp);
2006 if (err)
2007 return err;
2008
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002009 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
2011 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002012 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Matt Carlson1d36ba42011-04-20 07:57:42 +00002014 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016 tg3_writephy(tp, MII_TG3_CTRL, phy9_orig);
2017
2018 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2019 reg32 &= ~0x3000;
2020 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2021 } else if (!err)
2022 err = -EBUSY;
2023
2024 return err;
2025}
2026
2027/* This will reset the tigon3 PHY if there is no valid
2028 * link unless the FORCE argument is non-zero.
2029 */
2030static int tg3_phy_reset(struct tg3 *tp)
2031{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002032 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 int err;
2034
Michael Chan60189dd2006-12-17 17:08:07 -08002035 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002036 val = tr32(GRC_MISC_CFG);
2037 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2038 udelay(40);
2039 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002040 err = tg3_readphy(tp, MII_BMSR, &val);
2041 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 if (err != 0)
2043 return -EBUSY;
2044
Michael Chanc8e1e822006-04-29 18:55:17 -07002045 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
2046 netif_carrier_off(tp->dev);
2047 tg3_link_report(tp);
2048 }
2049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2051 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2052 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2053 err = tg3_phy_reset_5703_4_5(tp);
2054 if (err)
2055 return err;
2056 goto out;
2057 }
2058
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002059 cpmuctrl = 0;
2060 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2061 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2062 cpmuctrl = tr32(TG3_CPMU_CTRL);
2063 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2064 tw32(TG3_CPMU_CTRL,
2065 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2066 }
2067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 err = tg3_bmcr_reset(tp);
2069 if (err)
2070 return err;
2071
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002072 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002073 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2074 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002075
2076 tw32(TG3_CPMU_CTRL, cpmuctrl);
2077 }
2078
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002079 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2080 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002081 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2082 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2083 CPMU_LSPD_1000MB_MACCLK_12_5) {
2084 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2085 udelay(40);
2086 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2087 }
2088 }
2089
Joe Perches63c3a662011-04-26 08:12:10 +00002090 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002091 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002092 return 0;
2093
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002094 tg3_phy_apply_otp(tp);
2095
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002096 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002097 tg3_phy_toggle_apd(tp, true);
2098 else
2099 tg3_phy_toggle_apd(tp, false);
2100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002102 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
2103 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002104 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2105 tg3_phydsp_write(tp, 0x000a, 0x0323);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002106 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002108
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002109 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002110 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2111 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002113
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002114 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002115 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2116 tg3_phydsp_write(tp, 0x000a, 0x310b);
2117 tg3_phydsp_write(tp, 0x201f, 0x9506);
2118 tg3_phydsp_write(tp, 0x401f, 0x14e2);
2119 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2120 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002121 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002122 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2123 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2124 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2125 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2126 tg3_writephy(tp, MII_TG3_TEST1,
2127 MII_TG3_TEST1_TRIM_EN | 0x4);
2128 } else
2129 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2130
2131 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2132 }
Michael Chanc424cb22006-04-29 18:56:34 -07002133 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002134
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 /* Set Extended packet length bit (bit 14) on all chips that */
2136 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002137 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002139 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002140 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002142 err = tg3_phy_auxctl_read(tp,
2143 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2144 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002145 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2146 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 }
2148
2149 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2150 * jumbo frames transmission.
2151 */
Joe Perches63c3a662011-04-26 08:12:10 +00002152 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002153 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002154 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002155 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 }
2157
Michael Chan715116a2006-09-27 16:09:25 -07002158 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002159 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002160 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002161 }
2162
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002163 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 tg3_phy_set_wirespeed(tp);
2165 return 0;
2166}
2167
2168static void tg3_frob_aux_power(struct tg3 *tp)
2169{
Matt Carlson683644b2011-03-09 16:58:23 +00002170 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
Matt Carlson334355a2010-01-20 16:58:10 +00002172 /* The GPIOs do something completely different on 57765. */
Joe Perches63c3a662011-04-26 08:12:10 +00002173 if (!tg3_flag(tp, IS_NIC) ||
Matt Carlsona50d0792010-06-05 17:24:37 +00002174 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson334355a2010-01-20 16:58:10 +00002175 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 return;
2177
Matt Carlson683644b2011-03-09 16:58:23 +00002178 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2179 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +00002180 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2181 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) &&
Matt Carlson683644b2011-03-09 16:58:23 +00002182 tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002183 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002185 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002186
Michael Chanbc1c7562006-03-20 17:48:03 -08002187 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002188 if (dev_peer) {
2189 struct tg3 *tp_peer = netdev_priv(dev_peer);
2190
Joe Perches63c3a662011-04-26 08:12:10 +00002191 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002192 return;
2193
Joe Perches63c3a662011-04-26 08:12:10 +00002194 if (tg3_flag(tp_peer, WOL_ENABLE) ||
2195 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002196 need_vaux = true;
2197 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Joe Perches63c3a662011-04-26 08:12:10 +00002200 if (tg3_flag(tp, WOL_ENABLE) || tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002201 need_vaux = true;
2202
2203 if (need_vaux) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2205 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002206 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2207 (GRC_LCLCTRL_GPIO_OE0 |
2208 GRC_LCLCTRL_GPIO_OE1 |
2209 GRC_LCLCTRL_GPIO_OE2 |
2210 GRC_LCLCTRL_GPIO_OUTPUT0 |
2211 GRC_LCLCTRL_GPIO_OUTPUT1),
2212 100);
Matt Carlson8d519ab2009-04-20 06:58:01 +00002213 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2214 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -07002215 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2216 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2217 GRC_LCLCTRL_GPIO_OE1 |
2218 GRC_LCLCTRL_GPIO_OE2 |
2219 GRC_LCLCTRL_GPIO_OUTPUT0 |
2220 GRC_LCLCTRL_GPIO_OUTPUT1 |
2221 tp->grc_local_ctrl;
2222 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2223
2224 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2225 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2226
2227 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2228 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 } else {
2230 u32 no_gpio2;
Michael Chandc56b7d2005-12-19 16:26:28 -08002231 u32 grc_local_ctrl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
Michael Chandc56b7d2005-12-19 16:26:28 -08002233 /* Workaround to prevent overdrawing Amps. */
2234 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2235 ASIC_REV_5714) {
2236 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chanb401e9e2005-12-19 16:27:04 -08002237 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2238 grc_local_ctrl, 100);
Michael Chandc56b7d2005-12-19 16:26:28 -08002239 }
2240
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 /* On 5753 and variants, GPIO2 cannot be used. */
2242 no_gpio2 = tp->nic_sram_data_cfg &
2243 NIC_SRAM_DATA_CFG_NO_GPIO2;
2244
Michael Chandc56b7d2005-12-19 16:26:28 -08002245 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 GRC_LCLCTRL_GPIO_OE1 |
2247 GRC_LCLCTRL_GPIO_OE2 |
2248 GRC_LCLCTRL_GPIO_OUTPUT1 |
2249 GRC_LCLCTRL_GPIO_OUTPUT2;
2250 if (no_gpio2) {
2251 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2252 GRC_LCLCTRL_GPIO_OUTPUT2);
2253 }
Michael Chanb401e9e2005-12-19 16:27:04 -08002254 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2255 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
2257 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2258
Michael Chanb401e9e2005-12-19 16:27:04 -08002259 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2260 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
2262 if (!no_gpio2) {
2263 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chanb401e9e2005-12-19 16:27:04 -08002264 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2265 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 }
2267 }
2268 } else {
2269 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
2270 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002271 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2272 (GRC_LCLCTRL_GPIO_OE1 |
2273 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Michael Chanb401e9e2005-12-19 16:27:04 -08002275 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2276 GRC_LCLCTRL_GPIO_OE1, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Michael Chanb401e9e2005-12-19 16:27:04 -08002278 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2279 (GRC_LCLCTRL_GPIO_OE1 |
2280 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 }
2282 }
2283}
2284
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002285static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2286{
2287 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2288 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002289 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002290 if (speed != SPEED_10)
2291 return 1;
2292 } else if (speed == SPEED_10)
2293 return 1;
2294
2295 return 0;
2296}
2297
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298static int tg3_setup_phy(struct tg3 *, int);
2299
2300#define RESET_KIND_SHUTDOWN 0
2301#define RESET_KIND_INIT 1
2302#define RESET_KIND_SUSPEND 2
2303
2304static void tg3_write_sig_post_reset(struct tg3 *, int);
2305static int tg3_halt_cpu(struct tg3 *, u32);
2306
Matt Carlson0a459aa2008-11-03 16:54:15 -08002307static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002308{
Matt Carlsonce057f02007-11-12 21:08:03 -08002309 u32 val;
2310
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002311 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002312 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2313 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2314 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2315
2316 sg_dig_ctrl |=
2317 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2318 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2319 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2320 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002321 return;
Michael Chan51297242007-02-13 12:17:57 -08002322 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002323
Michael Chan60189dd2006-12-17 17:08:07 -08002324 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002325 tg3_bmcr_reset(tp);
2326 val = tr32(GRC_MISC_CFG);
2327 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2328 udelay(40);
2329 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002330 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002331 u32 phytest;
2332 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2333 u32 phy;
2334
2335 tg3_writephy(tp, MII_ADVERTISE, 0);
2336 tg3_writephy(tp, MII_BMCR,
2337 BMCR_ANENABLE | BMCR_ANRESTART);
2338
2339 tg3_writephy(tp, MII_TG3_FET_TEST,
2340 phytest | MII_TG3_FET_SHADOW_EN);
2341 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2342 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2343 tg3_writephy(tp,
2344 MII_TG3_FET_SHDW_AUXMODE4,
2345 phy);
2346 }
2347 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2348 }
2349 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002350 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002351 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2352 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002353
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002354 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2355 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2356 MII_TG3_AUXCTL_PCTL_VREG_11V;
2357 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07002358 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002359
Michael Chan15c3b692006-03-22 01:06:52 -08002360 /* The PHY should not be powered down on some chips because
2361 * of bugs.
2362 */
2363 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2364 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2365 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002366 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Michael Chan15c3b692006-03-22 01:06:52 -08002367 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002368
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002369 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2370 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002371 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2372 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2373 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2374 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2375 }
2376
Michael Chan15c3b692006-03-22 01:06:52 -08002377 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2378}
2379
Matt Carlson3f007892008-11-03 16:51:36 -08002380/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002381static int tg3_nvram_lock(struct tg3 *tp)
2382{
Joe Perches63c3a662011-04-26 08:12:10 +00002383 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002384 int i;
2385
2386 if (tp->nvram_lock_cnt == 0) {
2387 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2388 for (i = 0; i < 8000; i++) {
2389 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2390 break;
2391 udelay(20);
2392 }
2393 if (i == 8000) {
2394 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2395 return -ENODEV;
2396 }
2397 }
2398 tp->nvram_lock_cnt++;
2399 }
2400 return 0;
2401}
2402
2403/* tp->lock is held. */
2404static void tg3_nvram_unlock(struct tg3 *tp)
2405{
Joe Perches63c3a662011-04-26 08:12:10 +00002406 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002407 if (tp->nvram_lock_cnt > 0)
2408 tp->nvram_lock_cnt--;
2409 if (tp->nvram_lock_cnt == 0)
2410 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2411 }
2412}
2413
2414/* tp->lock is held. */
2415static void tg3_enable_nvram_access(struct tg3 *tp)
2416{
Joe Perches63c3a662011-04-26 08:12:10 +00002417 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002418 u32 nvaccess = tr32(NVRAM_ACCESS);
2419
2420 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2421 }
2422}
2423
2424/* tp->lock is held. */
2425static void tg3_disable_nvram_access(struct tg3 *tp)
2426{
Joe Perches63c3a662011-04-26 08:12:10 +00002427 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002428 u32 nvaccess = tr32(NVRAM_ACCESS);
2429
2430 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2431 }
2432}
2433
2434static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2435 u32 offset, u32 *val)
2436{
2437 u32 tmp;
2438 int i;
2439
2440 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2441 return -EINVAL;
2442
2443 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2444 EEPROM_ADDR_DEVID_MASK |
2445 EEPROM_ADDR_READ);
2446 tw32(GRC_EEPROM_ADDR,
2447 tmp |
2448 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2449 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2450 EEPROM_ADDR_ADDR_MASK) |
2451 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2452
2453 for (i = 0; i < 1000; i++) {
2454 tmp = tr32(GRC_EEPROM_ADDR);
2455
2456 if (tmp & EEPROM_ADDR_COMPLETE)
2457 break;
2458 msleep(1);
2459 }
2460 if (!(tmp & EEPROM_ADDR_COMPLETE))
2461 return -EBUSY;
2462
Matt Carlson62cedd12009-04-20 14:52:29 -07002463 tmp = tr32(GRC_EEPROM_DATA);
2464
2465 /*
2466 * The data will always be opposite the native endian
2467 * format. Perform a blind byteswap to compensate.
2468 */
2469 *val = swab32(tmp);
2470
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002471 return 0;
2472}
2473
2474#define NVRAM_CMD_TIMEOUT 10000
2475
2476static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2477{
2478 int i;
2479
2480 tw32(NVRAM_CMD, nvram_cmd);
2481 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2482 udelay(10);
2483 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2484 udelay(10);
2485 break;
2486 }
2487 }
2488
2489 if (i == NVRAM_CMD_TIMEOUT)
2490 return -EBUSY;
2491
2492 return 0;
2493}
2494
2495static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2496{
Joe Perches63c3a662011-04-26 08:12:10 +00002497 if (tg3_flag(tp, NVRAM) &&
2498 tg3_flag(tp, NVRAM_BUFFERED) &&
2499 tg3_flag(tp, FLASH) &&
2500 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002501 (tp->nvram_jedecnum == JEDEC_ATMEL))
2502
2503 addr = ((addr / tp->nvram_pagesize) <<
2504 ATMEL_AT45DB0X1B_PAGE_POS) +
2505 (addr % tp->nvram_pagesize);
2506
2507 return addr;
2508}
2509
2510static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2511{
Joe Perches63c3a662011-04-26 08:12:10 +00002512 if (tg3_flag(tp, NVRAM) &&
2513 tg3_flag(tp, NVRAM_BUFFERED) &&
2514 tg3_flag(tp, FLASH) &&
2515 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002516 (tp->nvram_jedecnum == JEDEC_ATMEL))
2517
2518 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2519 tp->nvram_pagesize) +
2520 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2521
2522 return addr;
2523}
2524
Matt Carlsone4f34112009-02-25 14:25:00 +00002525/* NOTE: Data read in from NVRAM is byteswapped according to
2526 * the byteswapping settings for all other register accesses.
2527 * tg3 devices are BE devices, so on a BE machine, the data
2528 * returned will be exactly as it is seen in NVRAM. On a LE
2529 * machine, the 32-bit value will be byteswapped.
2530 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002531static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2532{
2533 int ret;
2534
Joe Perches63c3a662011-04-26 08:12:10 +00002535 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002536 return tg3_nvram_read_using_eeprom(tp, offset, val);
2537
2538 offset = tg3_nvram_phys_addr(tp, offset);
2539
2540 if (offset > NVRAM_ADDR_MSK)
2541 return -EINVAL;
2542
2543 ret = tg3_nvram_lock(tp);
2544 if (ret)
2545 return ret;
2546
2547 tg3_enable_nvram_access(tp);
2548
2549 tw32(NVRAM_ADDR, offset);
2550 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2551 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2552
2553 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002554 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002555
2556 tg3_disable_nvram_access(tp);
2557
2558 tg3_nvram_unlock(tp);
2559
2560 return ret;
2561}
2562
Matt Carlsona9dc5292009-02-25 14:25:30 +00002563/* Ensures NVRAM data is in bytestream format. */
2564static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002565{
2566 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002567 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002568 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002569 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002570 return res;
2571}
2572
2573/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08002574static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2575{
2576 u32 addr_high, addr_low;
2577 int i;
2578
2579 addr_high = ((tp->dev->dev_addr[0] << 8) |
2580 tp->dev->dev_addr[1]);
2581 addr_low = ((tp->dev->dev_addr[2] << 24) |
2582 (tp->dev->dev_addr[3] << 16) |
2583 (tp->dev->dev_addr[4] << 8) |
2584 (tp->dev->dev_addr[5] << 0));
2585 for (i = 0; i < 4; i++) {
2586 if (i == 1 && skip_mac_1)
2587 continue;
2588 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2589 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2590 }
2591
2592 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2593 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2594 for (i = 0; i < 12; i++) {
2595 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2596 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2597 }
2598 }
2599
2600 addr_high = (tp->dev->dev_addr[0] +
2601 tp->dev->dev_addr[1] +
2602 tp->dev->dev_addr[2] +
2603 tp->dev->dev_addr[3] +
2604 tp->dev->dev_addr[4] +
2605 tp->dev->dev_addr[5]) &
2606 TX_BACKOFF_SEED_MASK;
2607 tw32(MAC_TX_BACKOFF_SEED, addr_high);
2608}
2609
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002610static void tg3_enable_register_access(struct tg3 *tp)
2611{
2612 /*
2613 * Make sure register accesses (indirect or otherwise) will function
2614 * correctly.
2615 */
2616 pci_write_config_dword(tp->pdev,
2617 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
2618}
2619
2620static int tg3_power_up(struct tg3 *tp)
2621{
2622 tg3_enable_register_access(tp);
2623
2624 pci_set_power_state(tp->pdev, PCI_D0);
2625
2626 /* Switch out of Vaux if it is a NIC */
Joe Perches63c3a662011-04-26 08:12:10 +00002627 if (tg3_flag(tp, IS_NIC))
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002628 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl, 100);
2629
2630 return 0;
2631}
2632
2633static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634{
2635 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002636 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002638 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002639
2640 /* Restore the CLKREQ setting. */
Joe Perches63c3a662011-04-26 08:12:10 +00002641 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002642 u16 lnkctl;
2643
2644 pci_read_config_word(tp->pdev,
2645 tp->pcie_cap + PCI_EXP_LNKCTL,
2646 &lnkctl);
2647 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2648 pci_write_config_word(tp->pdev,
2649 tp->pcie_cap + PCI_EXP_LNKCTL,
2650 lnkctl);
2651 }
2652
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2654 tw32(TG3PCI_MISC_HOST_CTRL,
2655 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2656
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002657 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00002658 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002659
Joe Perches63c3a662011-04-26 08:12:10 +00002660 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002661 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002662 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson800960682010-08-02 11:26:06 +00002663 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002664 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002665 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002666
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002667 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002668
Matt Carlson800960682010-08-02 11:26:06 +00002669 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002670
2671 tp->link_config.orig_speed = phydev->speed;
2672 tp->link_config.orig_duplex = phydev->duplex;
2673 tp->link_config.orig_autoneg = phydev->autoneg;
2674 tp->link_config.orig_advertising = phydev->advertising;
2675
2676 advertising = ADVERTISED_TP |
2677 ADVERTISED_Pause |
2678 ADVERTISED_Autoneg |
2679 ADVERTISED_10baseT_Half;
2680
Joe Perches63c3a662011-04-26 08:12:10 +00002681 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
2682 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002683 advertising |=
2684 ADVERTISED_100baseT_Half |
2685 ADVERTISED_100baseT_Full |
2686 ADVERTISED_10baseT_Full;
2687 else
2688 advertising |= ADVERTISED_10baseT_Full;
2689 }
2690
2691 phydev->advertising = advertising;
2692
2693 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002694
2695 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00002696 if (phyid != PHY_ID_BCMAC131) {
2697 phyid &= PHY_BCM_OUI_MASK;
2698 if (phyid == PHY_BCM_OUI_1 ||
2699 phyid == PHY_BCM_OUI_2 ||
2700 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08002701 do_low_power = true;
2702 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002703 }
Matt Carlsondd477002008-05-25 23:45:58 -07002704 } else {
Matt Carlson20232762008-12-21 20:18:56 -08002705 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002706
Matt Carlson800960682010-08-02 11:26:06 +00002707 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
2708 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07002709 tp->link_config.orig_speed = tp->link_config.speed;
2710 tp->link_config.orig_duplex = tp->link_config.duplex;
2711 tp->link_config.orig_autoneg = tp->link_config.autoneg;
2712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002714 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Matt Carlsondd477002008-05-25 23:45:58 -07002715 tp->link_config.speed = SPEED_10;
2716 tp->link_config.duplex = DUPLEX_HALF;
2717 tp->link_config.autoneg = AUTONEG_ENABLE;
2718 tg3_setup_phy(tp, 0);
2719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 }
2721
Michael Chanb5d37722006-09-27 16:06:21 -07002722 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2723 u32 val;
2724
2725 val = tr32(GRC_VCPU_EXT_CTRL);
2726 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00002727 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08002728 int i;
2729 u32 val;
2730
2731 for (i = 0; i < 200; i++) {
2732 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2733 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2734 break;
2735 msleep(1);
2736 }
2737 }
Joe Perches63c3a662011-04-26 08:12:10 +00002738 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07002739 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2740 WOL_DRV_STATE_SHUTDOWN |
2741 WOL_DRV_WOL |
2742 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08002743
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002744 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 u32 mac_mode;
2746
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002747 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002748 if (do_low_power &&
2749 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
2750 tg3_phy_auxctl_write(tp,
2751 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
2752 MII_TG3_AUXCTL_PCTL_WOL_EN |
2753 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2754 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07002755 udelay(40);
2756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002758 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07002759 mac_mode = MAC_MODE_PORT_MODE_GMII;
2760 else
2761 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002763 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2764 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2765 ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00002766 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002767 SPEED_100 : SPEED_10;
2768 if (tg3_5700_link_polarity(tp, speed))
2769 mac_mode |= MAC_MODE_LINK_POLARITY;
2770 else
2771 mac_mode &= ~MAC_MODE_LINK_POLARITY;
2772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 } else {
2774 mac_mode = MAC_MODE_PORT_MODE_TBI;
2775 }
2776
Joe Perches63c3a662011-04-26 08:12:10 +00002777 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 tw32(MAC_LED_CTRL, tp->led_ctrl);
2779
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002780 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00002781 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
2782 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002783 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
Joe Perches63c3a662011-04-26 08:12:10 +00002785 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00002786 mac_mode |= MAC_MODE_APE_TX_EN |
2787 MAC_MODE_APE_RX_EN |
2788 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07002789
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 tw32_f(MAC_MODE, mac_mode);
2791 udelay(100);
2792
2793 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2794 udelay(10);
2795 }
2796
Joe Perches63c3a662011-04-26 08:12:10 +00002797 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2799 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2800 u32 base_val;
2801
2802 base_val = tp->pci_clock_ctrl;
2803 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2804 CLOCK_CTRL_TXCLK_DISABLE);
2805
Michael Chanb401e9e2005-12-19 16:27:04 -08002806 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
2807 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00002808 } else if (tg3_flag(tp, 5780_CLASS) ||
2809 tg3_flag(tp, CPMU_PRESENT) ||
Matt Carlson6ff6f812011-05-19 12:12:54 +00002810 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07002811 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00002812 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 u32 newbits1, newbits2;
2814
2815 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2816 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2817 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
2818 CLOCK_CTRL_TXCLK_DISABLE |
2819 CLOCK_CTRL_ALTCLK);
2820 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00002821 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 newbits1 = CLOCK_CTRL_625_CORE;
2823 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
2824 } else {
2825 newbits1 = CLOCK_CTRL_ALTCLK;
2826 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2827 }
2828
Michael Chanb401e9e2005-12-19 16:27:04 -08002829 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
2830 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
Michael Chanb401e9e2005-12-19 16:27:04 -08002832 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
2833 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
Joe Perches63c3a662011-04-26 08:12:10 +00002835 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 u32 newbits3;
2837
2838 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2839 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2840 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
2841 CLOCK_CTRL_TXCLK_DISABLE |
2842 CLOCK_CTRL_44MHZ_CORE);
2843 } else {
2844 newbits3 = CLOCK_CTRL_44MHZ_CORE;
2845 }
2846
Michael Chanb401e9e2005-12-19 16:27:04 -08002847 tw32_wait_f(TG3PCI_CLOCK_CTRL,
2848 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 }
2850 }
2851
Joe Perches63c3a662011-04-26 08:12:10 +00002852 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08002853 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08002854
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 tg3_frob_aux_power(tp);
2856
2857 /* Workaround for unstable PLL clock */
2858 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
2859 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
2860 u32 val = tr32(0x7d00);
2861
2862 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
2863 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00002864 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08002865 int err;
2866
2867 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08002869 if (!err)
2870 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08002871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 }
2873
Michael Chanbbadf502006-04-06 21:46:34 -07002874 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
2875
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 return 0;
2877}
2878
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002879static void tg3_power_down(struct tg3 *tp)
2880{
2881 tg3_power_down_prepare(tp);
2882
Joe Perches63c3a662011-04-26 08:12:10 +00002883 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002884 pci_set_power_state(tp->pdev, PCI_D3hot);
2885}
2886
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
2888{
2889 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
2890 case MII_TG3_AUX_STAT_10HALF:
2891 *speed = SPEED_10;
2892 *duplex = DUPLEX_HALF;
2893 break;
2894
2895 case MII_TG3_AUX_STAT_10FULL:
2896 *speed = SPEED_10;
2897 *duplex = DUPLEX_FULL;
2898 break;
2899
2900 case MII_TG3_AUX_STAT_100HALF:
2901 *speed = SPEED_100;
2902 *duplex = DUPLEX_HALF;
2903 break;
2904
2905 case MII_TG3_AUX_STAT_100FULL:
2906 *speed = SPEED_100;
2907 *duplex = DUPLEX_FULL;
2908 break;
2909
2910 case MII_TG3_AUX_STAT_1000HALF:
2911 *speed = SPEED_1000;
2912 *duplex = DUPLEX_HALF;
2913 break;
2914
2915 case MII_TG3_AUX_STAT_1000FULL:
2916 *speed = SPEED_1000;
2917 *duplex = DUPLEX_FULL;
2918 break;
2919
2920 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002921 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07002922 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
2923 SPEED_10;
2924 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
2925 DUPLEX_HALF;
2926 break;
2927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 *speed = SPEED_INVALID;
2929 *duplex = DUPLEX_INVALID;
2930 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932}
2933
Matt Carlson42b64a42011-05-19 12:12:49 +00002934static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935{
Matt Carlson42b64a42011-05-19 12:12:49 +00002936 int err = 0;
2937 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
Matt Carlson42b64a42011-05-19 12:12:49 +00002939 new_adv = ADVERTISE_CSMA;
2940 if (advertise & ADVERTISED_10baseT_Half)
2941 new_adv |= ADVERTISE_10HALF;
2942 if (advertise & ADVERTISED_10baseT_Full)
2943 new_adv |= ADVERTISE_10FULL;
2944 if (advertise & ADVERTISED_100baseT_Half)
2945 new_adv |= ADVERTISE_100HALF;
2946 if (advertise & ADVERTISED_100baseT_Full)
2947 new_adv |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
Matt Carlson42b64a42011-05-19 12:12:49 +00002949 new_adv |= tg3_advert_flowctrl_1000T(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
Matt Carlson42b64a42011-05-19 12:12:49 +00002951 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
2952 if (err)
2953 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954
Matt Carlson42b64a42011-05-19 12:12:49 +00002955 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
2956 goto done;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002957
Matt Carlson42b64a42011-05-19 12:12:49 +00002958 new_adv = 0;
2959 if (advertise & ADVERTISED_1000baseT_Half)
2960 new_adv |= MII_TG3_CTRL_ADV_1000_HALF;
2961 if (advertise & ADVERTISED_1000baseT_Full)
2962 new_adv |= MII_TG3_CTRL_ADV_1000_FULL;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002963
Matt Carlson42b64a42011-05-19 12:12:49 +00002964 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2965 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
2966 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2967 MII_TG3_CTRL_ENABLE_AS_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968
Matt Carlson42b64a42011-05-19 12:12:49 +00002969 err = tg3_writephy(tp, MII_TG3_CTRL, new_adv);
2970 if (err)
2971 goto done;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002972
Matt Carlson42b64a42011-05-19 12:12:49 +00002973 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2974 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
Matt Carlson42b64a42011-05-19 12:12:49 +00002976 tw32(TG3_CPMU_EEE_MODE,
2977 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002978
Matt Carlson42b64a42011-05-19 12:12:49 +00002979 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
2980 if (!err) {
2981 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00002982
Matt Carlson21a00ab2011-01-25 15:58:55 +00002983 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
2984 case ASIC_REV_5717:
2985 case ASIC_REV_57765:
2986 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
2987 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
2988 MII_TG3_DSP_CH34TP2_HIBW01);
2989 /* Fall through */
2990 case ASIC_REV_5719:
2991 val = MII_TG3_DSP_TAP26_ALNOKO |
2992 MII_TG3_DSP_TAP26_RMRXSTO |
2993 MII_TG3_DSP_TAP26_OPCSINPT;
2994 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
2995 }
Matt Carlson52b02d02010-10-14 10:37:41 +00002996
Matt Carlsona6b68da2010-12-06 08:28:52 +00002997 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00002998 /* Advertise 100-BaseTX EEE ability */
2999 if (advertise & ADVERTISED_100baseT_Full)
3000 val |= MDIO_AN_EEE_ADV_100TX;
3001 /* Advertise 1000-BaseT EEE ability */
3002 if (advertise & ADVERTISED_1000baseT_Full)
3003 val |= MDIO_AN_EEE_ADV_1000T;
3004 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlson52b02d02010-10-14 10:37:41 +00003005
Matt Carlson42b64a42011-05-19 12:12:49 +00003006 err2 = TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
3007 if (!err)
3008 err = err2;
3009 }
3010
3011done:
3012 return err;
3013}
3014
3015static void tg3_phy_copper_begin(struct tg3 *tp)
3016{
3017 u32 new_adv;
3018 int i;
3019
3020 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
3021 new_adv = ADVERTISED_10baseT_Half |
3022 ADVERTISED_10baseT_Full;
3023 if (tg3_flag(tp, WOL_SPEED_100MB))
3024 new_adv |= ADVERTISED_100baseT_Half |
3025 ADVERTISED_100baseT_Full;
3026
3027 tg3_phy_autoneg_cfg(tp, new_adv,
3028 FLOW_CTRL_TX | FLOW_CTRL_RX);
3029 } else if (tp->link_config.speed == SPEED_INVALID) {
3030 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
3031 tp->link_config.advertising &=
3032 ~(ADVERTISED_1000baseT_Half |
3033 ADVERTISED_1000baseT_Full);
3034
3035 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
3036 tp->link_config.flowctrl);
3037 } else {
3038 /* Asking for a specific link mode. */
3039 if (tp->link_config.speed == SPEED_1000) {
3040 if (tp->link_config.duplex == DUPLEX_FULL)
3041 new_adv = ADVERTISED_1000baseT_Full;
3042 else
3043 new_adv = ADVERTISED_1000baseT_Half;
3044 } else if (tp->link_config.speed == SPEED_100) {
3045 if (tp->link_config.duplex == DUPLEX_FULL)
3046 new_adv = ADVERTISED_100baseT_Full;
3047 else
3048 new_adv = ADVERTISED_100baseT_Half;
3049 } else {
3050 if (tp->link_config.duplex == DUPLEX_FULL)
3051 new_adv = ADVERTISED_10baseT_Full;
3052 else
3053 new_adv = ADVERTISED_10baseT_Half;
3054 }
3055
3056 tg3_phy_autoneg_cfg(tp, new_adv,
3057 tp->link_config.flowctrl);
Matt Carlson52b02d02010-10-14 10:37:41 +00003058 }
3059
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
3061 tp->link_config.speed != SPEED_INVALID) {
3062 u32 bmcr, orig_bmcr;
3063
3064 tp->link_config.active_speed = tp->link_config.speed;
3065 tp->link_config.active_duplex = tp->link_config.duplex;
3066
3067 bmcr = 0;
3068 switch (tp->link_config.speed) {
3069 default:
3070 case SPEED_10:
3071 break;
3072
3073 case SPEED_100:
3074 bmcr |= BMCR_SPEED100;
3075 break;
3076
3077 case SPEED_1000:
3078 bmcr |= TG3_BMCR_SPEED1000;
3079 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081
3082 if (tp->link_config.duplex == DUPLEX_FULL)
3083 bmcr |= BMCR_FULLDPLX;
3084
3085 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3086 (bmcr != orig_bmcr)) {
3087 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3088 for (i = 0; i < 1500; i++) {
3089 u32 tmp;
3090
3091 udelay(10);
3092 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3093 tg3_readphy(tp, MII_BMSR, &tmp))
3094 continue;
3095 if (!(tmp & BMSR_LSTATUS)) {
3096 udelay(40);
3097 break;
3098 }
3099 }
3100 tg3_writephy(tp, MII_BMCR, bmcr);
3101 udelay(40);
3102 }
3103 } else {
3104 tg3_writephy(tp, MII_BMCR,
3105 BMCR_ANENABLE | BMCR_ANRESTART);
3106 }
3107}
3108
3109static int tg3_init_5401phy_dsp(struct tg3 *tp)
3110{
3111 int err;
3112
3113 /* Turn off tap power management. */
3114 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003115 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003117 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3118 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3119 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3120 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3121 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
3123 udelay(40);
3124
3125 return err;
3126}
3127
Michael Chan3600d912006-12-07 00:21:48 -08003128static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129{
Michael Chan3600d912006-12-07 00:21:48 -08003130 u32 adv_reg, all_mask = 0;
3131
3132 if (mask & ADVERTISED_10baseT_Half)
3133 all_mask |= ADVERTISE_10HALF;
3134 if (mask & ADVERTISED_10baseT_Full)
3135 all_mask |= ADVERTISE_10FULL;
3136 if (mask & ADVERTISED_100baseT_Half)
3137 all_mask |= ADVERTISE_100HALF;
3138 if (mask & ADVERTISED_100baseT_Full)
3139 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
3141 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
3142 return 0;
3143
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 if ((adv_reg & all_mask) != all_mask)
3145 return 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003146 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 u32 tg3_ctrl;
3148
Michael Chan3600d912006-12-07 00:21:48 -08003149 all_mask = 0;
3150 if (mask & ADVERTISED_1000baseT_Half)
3151 all_mask |= ADVERTISE_1000HALF;
3152 if (mask & ADVERTISED_1000baseT_Full)
3153 all_mask |= ADVERTISE_1000FULL;
3154
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 if (tg3_readphy(tp, MII_TG3_CTRL, &tg3_ctrl))
3156 return 0;
3157
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 if ((tg3_ctrl & all_mask) != all_mask)
3159 return 0;
3160 }
3161 return 1;
3162}
3163
Matt Carlsonef167e22007-12-20 20:10:01 -08003164static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
3165{
3166 u32 curadv, reqadv;
3167
3168 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3169 return 1;
3170
3171 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3172 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3173
3174 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3175 if (curadv != reqadv)
3176 return 0;
3177
Joe Perches63c3a662011-04-26 08:12:10 +00003178 if (tg3_flag(tp, PAUSE_AUTONEG))
Matt Carlsonef167e22007-12-20 20:10:01 -08003179 tg3_readphy(tp, MII_LPA, rmtadv);
3180 } else {
3181 /* Reprogram the advertisement register, even if it
3182 * does not affect the current link. If the link
3183 * gets renegotiated in the future, we can save an
3184 * additional renegotiation cycle by advertising
3185 * it correctly in the first place.
3186 */
3187 if (curadv != reqadv) {
3188 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3189 ADVERTISE_PAUSE_ASYM);
3190 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3191 }
3192 }
3193
3194 return 1;
3195}
3196
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3198{
3199 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003200 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08003201 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 u16 current_speed;
3203 u8 current_duplex;
3204 int i, err;
3205
3206 tw32(MAC_EVENT, 0);
3207
3208 tw32_f(MAC_STATUS,
3209 (MAC_STATUS_SYNC_CHANGED |
3210 MAC_STATUS_CFG_CHANGED |
3211 MAC_STATUS_MI_COMPLETION |
3212 MAC_STATUS_LNKSTATE_CHANGED));
3213 udelay(40);
3214
Matt Carlson8ef21422008-05-02 16:47:53 -07003215 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3216 tw32_f(MAC_MI_MODE,
3217 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3218 udelay(80);
3219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003221 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222
3223 /* Some third-party PHYs need to be reset on link going
3224 * down.
3225 */
3226 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3227 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3228 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3229 netif_carrier_ok(tp->dev)) {
3230 tg3_readphy(tp, MII_BMSR, &bmsr);
3231 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3232 !(bmsr & BMSR_LSTATUS))
3233 force_reset = 1;
3234 }
3235 if (force_reset)
3236 tg3_phy_reset(tp);
3237
Matt Carlson79eb6902010-02-17 15:17:03 +00003238 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 tg3_readphy(tp, MII_BMSR, &bmsr);
3240 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00003241 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 bmsr = 0;
3243
3244 if (!(bmsr & BMSR_LSTATUS)) {
3245 err = tg3_init_5401phy_dsp(tp);
3246 if (err)
3247 return err;
3248
3249 tg3_readphy(tp, MII_BMSR, &bmsr);
3250 for (i = 0; i < 1000; i++) {
3251 udelay(10);
3252 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3253 (bmsr & BMSR_LSTATUS)) {
3254 udelay(40);
3255 break;
3256 }
3257 }
3258
Matt Carlson79eb6902010-02-17 15:17:03 +00003259 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3260 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 !(bmsr & BMSR_LSTATUS) &&
3262 tp->link_config.active_speed == SPEED_1000) {
3263 err = tg3_phy_reset(tp);
3264 if (!err)
3265 err = tg3_init_5401phy_dsp(tp);
3266 if (err)
3267 return err;
3268 }
3269 }
3270 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3271 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3272 /* 5701 {A0,B0} CRC bug workaround */
3273 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00003274 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3275 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
3276 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 }
3278
3279 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003280 tg3_readphy(tp, MII_TG3_ISTAT, &val);
3281 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003283 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003285 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3287
3288 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3289 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3290 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3291 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3292 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3293 else
3294 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3295 }
3296
3297 current_link_up = 0;
3298 current_speed = SPEED_INVALID;
3299 current_duplex = DUPLEX_INVALID;
3300
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003301 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00003302 err = tg3_phy_auxctl_read(tp,
3303 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3304 &val);
3305 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003306 tg3_phy_auxctl_write(tp,
3307 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3308 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 goto relink;
3310 }
3311 }
3312
3313 bmsr = 0;
3314 for (i = 0; i < 100; i++) {
3315 tg3_readphy(tp, MII_BMSR, &bmsr);
3316 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3317 (bmsr & BMSR_LSTATUS))
3318 break;
3319 udelay(40);
3320 }
3321
3322 if (bmsr & BMSR_LSTATUS) {
3323 u32 aux_stat, bmcr;
3324
3325 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3326 for (i = 0; i < 2000; i++) {
3327 udelay(10);
3328 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3329 aux_stat)
3330 break;
3331 }
3332
3333 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3334 &current_speed,
3335 &current_duplex);
3336
3337 bmcr = 0;
3338 for (i = 0; i < 200; i++) {
3339 tg3_readphy(tp, MII_BMCR, &bmcr);
3340 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3341 continue;
3342 if (bmcr && bmcr != 0x7fff)
3343 break;
3344 udelay(10);
3345 }
3346
Matt Carlsonef167e22007-12-20 20:10:01 -08003347 lcl_adv = 0;
3348 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349
Matt Carlsonef167e22007-12-20 20:10:01 -08003350 tp->link_config.active_speed = current_speed;
3351 tp->link_config.active_duplex = current_duplex;
3352
3353 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3354 if ((bmcr & BMCR_ANENABLE) &&
3355 tg3_copper_is_advertising_all(tp,
3356 tp->link_config.advertising)) {
3357 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3358 &rmt_adv))
3359 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 }
3361 } else {
3362 if (!(bmcr & BMCR_ANENABLE) &&
3363 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003364 tp->link_config.duplex == current_duplex &&
3365 tp->link_config.flowctrl ==
3366 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 }
3369 }
3370
Matt Carlsonef167e22007-12-20 20:10:01 -08003371 if (current_link_up == 1 &&
3372 tp->link_config.active_duplex == DUPLEX_FULL)
3373 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 }
3375
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376relink:
Matt Carlson800960682010-08-02 11:26:06 +00003377 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 tg3_phy_copper_begin(tp);
3379
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003380 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00003381 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
3382 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 current_link_up = 1;
3384 }
3385
3386 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3387 if (current_link_up == 1) {
3388 if (tp->link_config.active_speed == SPEED_100 ||
3389 tp->link_config.active_speed == SPEED_10)
3390 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3391 else
3392 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003393 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003394 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3395 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3397
3398 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3399 if (tp->link_config.active_duplex == DUPLEX_HALF)
3400 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3401
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003403 if (current_link_up == 1 &&
3404 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003406 else
3407 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 }
3409
3410 /* ??? Without this setting Netgear GA302T PHY does not
3411 * ??? send/receive packets...
3412 */
Matt Carlson79eb6902010-02-17 15:17:03 +00003413 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3415 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3416 tw32_f(MAC_MI_MODE, tp->mi_mode);
3417 udelay(80);
3418 }
3419
3420 tw32_f(MAC_MODE, tp->mac_mode);
3421 udelay(40);
3422
Matt Carlson52b02d02010-10-14 10:37:41 +00003423 tg3_phy_eee_adjust(tp, current_link_up);
3424
Joe Perches63c3a662011-04-26 08:12:10 +00003425 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 /* Polled via timer. */
3427 tw32_f(MAC_EVENT, 0);
3428 } else {
3429 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3430 }
3431 udelay(40);
3432
3433 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3434 current_link_up == 1 &&
3435 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00003436 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 udelay(120);
3438 tw32_f(MAC_STATUS,
3439 (MAC_STATUS_SYNC_CHANGED |
3440 MAC_STATUS_CFG_CHANGED));
3441 udelay(40);
3442 tg3_write_mem(tp,
3443 NIC_SRAM_FIRMWARE_MBOX,
3444 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3445 }
3446
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003447 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00003448 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003449 u16 oldlnkctl, newlnkctl;
3450
3451 pci_read_config_word(tp->pdev,
3452 tp->pcie_cap + PCI_EXP_LNKCTL,
3453 &oldlnkctl);
3454 if (tp->link_config.active_speed == SPEED_100 ||
3455 tp->link_config.active_speed == SPEED_10)
3456 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3457 else
3458 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3459 if (newlnkctl != oldlnkctl)
3460 pci_write_config_word(tp->pdev,
3461 tp->pcie_cap + PCI_EXP_LNKCTL,
3462 newlnkctl);
3463 }
3464
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 if (current_link_up != netif_carrier_ok(tp->dev)) {
3466 if (current_link_up)
3467 netif_carrier_on(tp->dev);
3468 else
3469 netif_carrier_off(tp->dev);
3470 tg3_link_report(tp);
3471 }
3472
3473 return 0;
3474}
3475
3476struct tg3_fiber_aneginfo {
3477 int state;
3478#define ANEG_STATE_UNKNOWN 0
3479#define ANEG_STATE_AN_ENABLE 1
3480#define ANEG_STATE_RESTART_INIT 2
3481#define ANEG_STATE_RESTART 3
3482#define ANEG_STATE_DISABLE_LINK_OK 4
3483#define ANEG_STATE_ABILITY_DETECT_INIT 5
3484#define ANEG_STATE_ABILITY_DETECT 6
3485#define ANEG_STATE_ACK_DETECT_INIT 7
3486#define ANEG_STATE_ACK_DETECT 8
3487#define ANEG_STATE_COMPLETE_ACK_INIT 9
3488#define ANEG_STATE_COMPLETE_ACK 10
3489#define ANEG_STATE_IDLE_DETECT_INIT 11
3490#define ANEG_STATE_IDLE_DETECT 12
3491#define ANEG_STATE_LINK_OK 13
3492#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3493#define ANEG_STATE_NEXT_PAGE_WAIT 15
3494
3495 u32 flags;
3496#define MR_AN_ENABLE 0x00000001
3497#define MR_RESTART_AN 0x00000002
3498#define MR_AN_COMPLETE 0x00000004
3499#define MR_PAGE_RX 0x00000008
3500#define MR_NP_LOADED 0x00000010
3501#define MR_TOGGLE_TX 0x00000020
3502#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3503#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3504#define MR_LP_ADV_SYM_PAUSE 0x00000100
3505#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3506#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3507#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3508#define MR_LP_ADV_NEXT_PAGE 0x00001000
3509#define MR_TOGGLE_RX 0x00002000
3510#define MR_NP_RX 0x00004000
3511
3512#define MR_LINK_OK 0x80000000
3513
3514 unsigned long link_time, cur_time;
3515
3516 u32 ability_match_cfg;
3517 int ability_match_count;
3518
3519 char ability_match, idle_match, ack_match;
3520
3521 u32 txconfig, rxconfig;
3522#define ANEG_CFG_NP 0x00000080
3523#define ANEG_CFG_ACK 0x00000040
3524#define ANEG_CFG_RF2 0x00000020
3525#define ANEG_CFG_RF1 0x00000010
3526#define ANEG_CFG_PS2 0x00000001
3527#define ANEG_CFG_PS1 0x00008000
3528#define ANEG_CFG_HD 0x00004000
3529#define ANEG_CFG_FD 0x00002000
3530#define ANEG_CFG_INVAL 0x00001f06
3531
3532};
3533#define ANEG_OK 0
3534#define ANEG_DONE 1
3535#define ANEG_TIMER_ENAB 2
3536#define ANEG_FAILED -1
3537
3538#define ANEG_STATE_SETTLE_TIME 10000
3539
3540static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3541 struct tg3_fiber_aneginfo *ap)
3542{
Matt Carlson5be73b42007-12-20 20:09:29 -08003543 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 unsigned long delta;
3545 u32 rx_cfg_reg;
3546 int ret;
3547
3548 if (ap->state == ANEG_STATE_UNKNOWN) {
3549 ap->rxconfig = 0;
3550 ap->link_time = 0;
3551 ap->cur_time = 0;
3552 ap->ability_match_cfg = 0;
3553 ap->ability_match_count = 0;
3554 ap->ability_match = 0;
3555 ap->idle_match = 0;
3556 ap->ack_match = 0;
3557 }
3558 ap->cur_time++;
3559
3560 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3561 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3562
3563 if (rx_cfg_reg != ap->ability_match_cfg) {
3564 ap->ability_match_cfg = rx_cfg_reg;
3565 ap->ability_match = 0;
3566 ap->ability_match_count = 0;
3567 } else {
3568 if (++ap->ability_match_count > 1) {
3569 ap->ability_match = 1;
3570 ap->ability_match_cfg = rx_cfg_reg;
3571 }
3572 }
3573 if (rx_cfg_reg & ANEG_CFG_ACK)
3574 ap->ack_match = 1;
3575 else
3576 ap->ack_match = 0;
3577
3578 ap->idle_match = 0;
3579 } else {
3580 ap->idle_match = 1;
3581 ap->ability_match_cfg = 0;
3582 ap->ability_match_count = 0;
3583 ap->ability_match = 0;
3584 ap->ack_match = 0;
3585
3586 rx_cfg_reg = 0;
3587 }
3588
3589 ap->rxconfig = rx_cfg_reg;
3590 ret = ANEG_OK;
3591
Matt Carlson33f401a2010-04-05 10:19:27 +00003592 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 case ANEG_STATE_UNKNOWN:
3594 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3595 ap->state = ANEG_STATE_AN_ENABLE;
3596
3597 /* fallthru */
3598 case ANEG_STATE_AN_ENABLE:
3599 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3600 if (ap->flags & MR_AN_ENABLE) {
3601 ap->link_time = 0;
3602 ap->cur_time = 0;
3603 ap->ability_match_cfg = 0;
3604 ap->ability_match_count = 0;
3605 ap->ability_match = 0;
3606 ap->idle_match = 0;
3607 ap->ack_match = 0;
3608
3609 ap->state = ANEG_STATE_RESTART_INIT;
3610 } else {
3611 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3612 }
3613 break;
3614
3615 case ANEG_STATE_RESTART_INIT:
3616 ap->link_time = ap->cur_time;
3617 ap->flags &= ~(MR_NP_LOADED);
3618 ap->txconfig = 0;
3619 tw32(MAC_TX_AUTO_NEG, 0);
3620 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3621 tw32_f(MAC_MODE, tp->mac_mode);
3622 udelay(40);
3623
3624 ret = ANEG_TIMER_ENAB;
3625 ap->state = ANEG_STATE_RESTART;
3626
3627 /* fallthru */
3628 case ANEG_STATE_RESTART:
3629 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00003630 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00003632 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 break;
3635
3636 case ANEG_STATE_DISABLE_LINK_OK:
3637 ret = ANEG_DONE;
3638 break;
3639
3640 case ANEG_STATE_ABILITY_DETECT_INIT:
3641 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003642 ap->txconfig = ANEG_CFG_FD;
3643 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3644 if (flowctrl & ADVERTISE_1000XPAUSE)
3645 ap->txconfig |= ANEG_CFG_PS1;
3646 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3647 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3649 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3650 tw32_f(MAC_MODE, tp->mac_mode);
3651 udelay(40);
3652
3653 ap->state = ANEG_STATE_ABILITY_DETECT;
3654 break;
3655
3656 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00003657 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 break;
3660
3661 case ANEG_STATE_ACK_DETECT_INIT:
3662 ap->txconfig |= ANEG_CFG_ACK;
3663 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3664 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3665 tw32_f(MAC_MODE, tp->mac_mode);
3666 udelay(40);
3667
3668 ap->state = ANEG_STATE_ACK_DETECT;
3669
3670 /* fallthru */
3671 case ANEG_STATE_ACK_DETECT:
3672 if (ap->ack_match != 0) {
3673 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3674 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3675 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3676 } else {
3677 ap->state = ANEG_STATE_AN_ENABLE;
3678 }
3679 } else if (ap->ability_match != 0 &&
3680 ap->rxconfig == 0) {
3681 ap->state = ANEG_STATE_AN_ENABLE;
3682 }
3683 break;
3684
3685 case ANEG_STATE_COMPLETE_ACK_INIT:
3686 if (ap->rxconfig & ANEG_CFG_INVAL) {
3687 ret = ANEG_FAILED;
3688 break;
3689 }
3690 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3691 MR_LP_ADV_HALF_DUPLEX |
3692 MR_LP_ADV_SYM_PAUSE |
3693 MR_LP_ADV_ASYM_PAUSE |
3694 MR_LP_ADV_REMOTE_FAULT1 |
3695 MR_LP_ADV_REMOTE_FAULT2 |
3696 MR_LP_ADV_NEXT_PAGE |
3697 MR_TOGGLE_RX |
3698 MR_NP_RX);
3699 if (ap->rxconfig & ANEG_CFG_FD)
3700 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3701 if (ap->rxconfig & ANEG_CFG_HD)
3702 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3703 if (ap->rxconfig & ANEG_CFG_PS1)
3704 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3705 if (ap->rxconfig & ANEG_CFG_PS2)
3706 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3707 if (ap->rxconfig & ANEG_CFG_RF1)
3708 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3709 if (ap->rxconfig & ANEG_CFG_RF2)
3710 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3711 if (ap->rxconfig & ANEG_CFG_NP)
3712 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3713
3714 ap->link_time = ap->cur_time;
3715
3716 ap->flags ^= (MR_TOGGLE_TX);
3717 if (ap->rxconfig & 0x0008)
3718 ap->flags |= MR_TOGGLE_RX;
3719 if (ap->rxconfig & ANEG_CFG_NP)
3720 ap->flags |= MR_NP_RX;
3721 ap->flags |= MR_PAGE_RX;
3722
3723 ap->state = ANEG_STATE_COMPLETE_ACK;
3724 ret = ANEG_TIMER_ENAB;
3725 break;
3726
3727 case ANEG_STATE_COMPLETE_ACK:
3728 if (ap->ability_match != 0 &&
3729 ap->rxconfig == 0) {
3730 ap->state = ANEG_STATE_AN_ENABLE;
3731 break;
3732 }
3733 delta = ap->cur_time - ap->link_time;
3734 if (delta > ANEG_STATE_SETTLE_TIME) {
3735 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3736 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3737 } else {
3738 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3739 !(ap->flags & MR_NP_RX)) {
3740 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3741 } else {
3742 ret = ANEG_FAILED;
3743 }
3744 }
3745 }
3746 break;
3747
3748 case ANEG_STATE_IDLE_DETECT_INIT:
3749 ap->link_time = ap->cur_time;
3750 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3751 tw32_f(MAC_MODE, tp->mac_mode);
3752 udelay(40);
3753
3754 ap->state = ANEG_STATE_IDLE_DETECT;
3755 ret = ANEG_TIMER_ENAB;
3756 break;
3757
3758 case ANEG_STATE_IDLE_DETECT:
3759 if (ap->ability_match != 0 &&
3760 ap->rxconfig == 0) {
3761 ap->state = ANEG_STATE_AN_ENABLE;
3762 break;
3763 }
3764 delta = ap->cur_time - ap->link_time;
3765 if (delta > ANEG_STATE_SETTLE_TIME) {
3766 /* XXX another gem from the Broadcom driver :( */
3767 ap->state = ANEG_STATE_LINK_OK;
3768 }
3769 break;
3770
3771 case ANEG_STATE_LINK_OK:
3772 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3773 ret = ANEG_DONE;
3774 break;
3775
3776 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3777 /* ??? unimplemented */
3778 break;
3779
3780 case ANEG_STATE_NEXT_PAGE_WAIT:
3781 /* ??? unimplemented */
3782 break;
3783
3784 default:
3785 ret = ANEG_FAILED;
3786 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788
3789 return ret;
3790}
3791
Matt Carlson5be73b42007-12-20 20:09:29 -08003792static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793{
3794 int res = 0;
3795 struct tg3_fiber_aneginfo aninfo;
3796 int status = ANEG_FAILED;
3797 unsigned int tick;
3798 u32 tmp;
3799
3800 tw32_f(MAC_TX_AUTO_NEG, 0);
3801
3802 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3803 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3804 udelay(40);
3805
3806 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3807 udelay(40);
3808
3809 memset(&aninfo, 0, sizeof(aninfo));
3810 aninfo.flags |= MR_AN_ENABLE;
3811 aninfo.state = ANEG_STATE_UNKNOWN;
3812 aninfo.cur_time = 0;
3813 tick = 0;
3814 while (++tick < 195000) {
3815 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3816 if (status == ANEG_DONE || status == ANEG_FAILED)
3817 break;
3818
3819 udelay(1);
3820 }
3821
3822 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3823 tw32_f(MAC_MODE, tp->mac_mode);
3824 udelay(40);
3825
Matt Carlson5be73b42007-12-20 20:09:29 -08003826 *txflags = aninfo.txconfig;
3827 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828
3829 if (status == ANEG_DONE &&
3830 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3831 MR_LP_ADV_FULL_DUPLEX)))
3832 res = 1;
3833
3834 return res;
3835}
3836
3837static void tg3_init_bcm8002(struct tg3 *tp)
3838{
3839 u32 mac_status = tr32(MAC_STATUS);
3840 int i;
3841
3842 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00003843 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 !(mac_status & MAC_STATUS_PCS_SYNCED))
3845 return;
3846
3847 /* Set PLL lock range. */
3848 tg3_writephy(tp, 0x16, 0x8007);
3849
3850 /* SW reset */
3851 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
3852
3853 /* Wait for reset to complete. */
3854 /* XXX schedule_timeout() ... */
3855 for (i = 0; i < 500; i++)
3856 udelay(10);
3857
3858 /* Config mode; select PMA/Ch 1 regs. */
3859 tg3_writephy(tp, 0x10, 0x8411);
3860
3861 /* Enable auto-lock and comdet, select txclk for tx. */
3862 tg3_writephy(tp, 0x11, 0x0a10);
3863
3864 tg3_writephy(tp, 0x18, 0x00a0);
3865 tg3_writephy(tp, 0x16, 0x41ff);
3866
3867 /* Assert and deassert POR. */
3868 tg3_writephy(tp, 0x13, 0x0400);
3869 udelay(40);
3870 tg3_writephy(tp, 0x13, 0x0000);
3871
3872 tg3_writephy(tp, 0x11, 0x0a50);
3873 udelay(40);
3874 tg3_writephy(tp, 0x11, 0x0a10);
3875
3876 /* Wait for signal to stabilize */
3877 /* XXX schedule_timeout() ... */
3878 for (i = 0; i < 15000; i++)
3879 udelay(10);
3880
3881 /* Deselect the channel register so we can read the PHYID
3882 * later.
3883 */
3884 tg3_writephy(tp, 0x10, 0x8011);
3885}
3886
3887static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
3888{
Matt Carlson82cd3d12007-12-20 20:09:00 -08003889 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 u32 sg_dig_ctrl, sg_dig_status;
3891 u32 serdes_cfg, expected_sg_dig_ctrl;
3892 int workaround, port_a;
3893 int current_link_up;
3894
3895 serdes_cfg = 0;
3896 expected_sg_dig_ctrl = 0;
3897 workaround = 0;
3898 port_a = 1;
3899 current_link_up = 0;
3900
3901 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
3902 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
3903 workaround = 1;
3904 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
3905 port_a = 0;
3906
3907 /* preserve bits 0-11,13,14 for signal pre-emphasis */
3908 /* preserve bits 20-23 for voltage regulator */
3909 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
3910 }
3911
3912 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3913
3914 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003915 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 if (workaround) {
3917 u32 val = serdes_cfg;
3918
3919 if (port_a)
3920 val |= 0xc010000;
3921 else
3922 val |= 0x4010000;
3923 tw32_f(MAC_SERDES_CFG, val);
3924 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003925
3926 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 }
3928 if (mac_status & MAC_STATUS_PCS_SYNCED) {
3929 tg3_setup_flow_control(tp, 0, 0);
3930 current_link_up = 1;
3931 }
3932 goto out;
3933 }
3934
3935 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003936 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937
Matt Carlson82cd3d12007-12-20 20:09:00 -08003938 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3939 if (flowctrl & ADVERTISE_1000XPAUSE)
3940 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
3941 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3942 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943
3944 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003945 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07003946 tp->serdes_counter &&
3947 ((mac_status & (MAC_STATUS_PCS_SYNCED |
3948 MAC_STATUS_RCVD_CFG)) ==
3949 MAC_STATUS_PCS_SYNCED)) {
3950 tp->serdes_counter--;
3951 current_link_up = 1;
3952 goto out;
3953 }
3954restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 if (workaround)
3956 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003957 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 udelay(5);
3959 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
3960
Michael Chan3d3ebe72006-09-27 15:59:15 -07003961 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003962 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
3964 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003965 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 mac_status = tr32(MAC_STATUS);
3967
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003968 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08003970 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971
Matt Carlson82cd3d12007-12-20 20:09:00 -08003972 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
3973 local_adv |= ADVERTISE_1000XPAUSE;
3974 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
3975 local_adv |= ADVERTISE_1000XPSE_ASYM;
3976
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003977 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003978 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003979 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003980 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981
3982 tg3_setup_flow_control(tp, local_adv, remote_adv);
3983 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003984 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003985 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003986 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003987 if (tp->serdes_counter)
3988 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 else {
3990 if (workaround) {
3991 u32 val = serdes_cfg;
3992
3993 if (port_a)
3994 val |= 0xc010000;
3995 else
3996 val |= 0x4010000;
3997
3998 tw32_f(MAC_SERDES_CFG, val);
3999 }
4000
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004001 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 udelay(40);
4003
4004 /* Link parallel detection - link is up */
4005 /* only if we have PCS_SYNC and not */
4006 /* receiving config code words */
4007 mac_status = tr32(MAC_STATUS);
4008 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
4009 !(mac_status & MAC_STATUS_RCVD_CFG)) {
4010 tg3_setup_flow_control(tp, 0, 0);
4011 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004012 tp->phy_flags |=
4013 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004014 tp->serdes_counter =
4015 SERDES_PARALLEL_DET_TIMEOUT;
4016 } else
4017 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 }
4019 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07004020 } else {
4021 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004022 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 }
4024
4025out:
4026 return current_link_up;
4027}
4028
4029static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
4030{
4031 int current_link_up = 0;
4032
Michael Chan5cf64b8a2007-05-05 12:11:21 -07004033 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035
4036 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08004037 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004039
Matt Carlson5be73b42007-12-20 20:09:29 -08004040 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4041 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042
Matt Carlson5be73b42007-12-20 20:09:29 -08004043 if (txflags & ANEG_CFG_PS1)
4044 local_adv |= ADVERTISE_1000XPAUSE;
4045 if (txflags & ANEG_CFG_PS2)
4046 local_adv |= ADVERTISE_1000XPSE_ASYM;
4047
4048 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4049 remote_adv |= LPA_1000XPAUSE;
4050 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4051 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052
4053 tg3_setup_flow_control(tp, local_adv, remote_adv);
4054
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 current_link_up = 1;
4056 }
4057 for (i = 0; i < 30; i++) {
4058 udelay(20);
4059 tw32_f(MAC_STATUS,
4060 (MAC_STATUS_SYNC_CHANGED |
4061 MAC_STATUS_CFG_CHANGED));
4062 udelay(40);
4063 if ((tr32(MAC_STATUS) &
4064 (MAC_STATUS_SYNC_CHANGED |
4065 MAC_STATUS_CFG_CHANGED)) == 0)
4066 break;
4067 }
4068
4069 mac_status = tr32(MAC_STATUS);
4070 if (current_link_up == 0 &&
4071 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4072 !(mac_status & MAC_STATUS_RCVD_CFG))
4073 current_link_up = 1;
4074 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004075 tg3_setup_flow_control(tp, 0, 0);
4076
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 /* Forcing 1000FD link up. */
4078 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079
4080 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4081 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004082
4083 tw32_f(MAC_MODE, tp->mac_mode);
4084 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 }
4086
4087out:
4088 return current_link_up;
4089}
4090
4091static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4092{
4093 u32 orig_pause_cfg;
4094 u16 orig_active_speed;
4095 u8 orig_active_duplex;
4096 u32 mac_status;
4097 int current_link_up;
4098 int i;
4099
Matt Carlson8d018622007-12-20 20:05:44 -08004100 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 orig_active_speed = tp->link_config.active_speed;
4102 orig_active_duplex = tp->link_config.active_duplex;
4103
Joe Perches63c3a662011-04-26 08:12:10 +00004104 if (!tg3_flag(tp, HW_AUTONEG) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 netif_carrier_ok(tp->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004106 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 mac_status = tr32(MAC_STATUS);
4108 mac_status &= (MAC_STATUS_PCS_SYNCED |
4109 MAC_STATUS_SIGNAL_DET |
4110 MAC_STATUS_CFG_CHANGED |
4111 MAC_STATUS_RCVD_CFG);
4112 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4113 MAC_STATUS_SIGNAL_DET)) {
4114 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4115 MAC_STATUS_CFG_CHANGED));
4116 return 0;
4117 }
4118 }
4119
4120 tw32_f(MAC_TX_AUTO_NEG, 0);
4121
4122 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4123 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
4124 tw32_f(MAC_MODE, tp->mac_mode);
4125 udelay(40);
4126
Matt Carlson79eb6902010-02-17 15:17:03 +00004127 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 tg3_init_bcm8002(tp);
4129
4130 /* Enable link change event even when serdes polling. */
4131 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4132 udelay(40);
4133
4134 current_link_up = 0;
4135 mac_status = tr32(MAC_STATUS);
4136
Joe Perches63c3a662011-04-26 08:12:10 +00004137 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4139 else
4140 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4141
Matt Carlson898a56f2009-08-28 14:02:40 +00004142 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00004144 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145
4146 for (i = 0; i < 100; i++) {
4147 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4148 MAC_STATUS_CFG_CHANGED));
4149 udelay(5);
4150 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07004151 MAC_STATUS_CFG_CHANGED |
4152 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 break;
4154 }
4155
4156 mac_status = tr32(MAC_STATUS);
4157 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4158 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004159 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4160 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 tw32_f(MAC_MODE, (tp->mac_mode |
4162 MAC_MODE_SEND_CONFIGS));
4163 udelay(1);
4164 tw32_f(MAC_MODE, tp->mac_mode);
4165 }
4166 }
4167
4168 if (current_link_up == 1) {
4169 tp->link_config.active_speed = SPEED_1000;
4170 tp->link_config.active_duplex = DUPLEX_FULL;
4171 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4172 LED_CTRL_LNKLED_OVERRIDE |
4173 LED_CTRL_1000MBPS_ON));
4174 } else {
4175 tp->link_config.active_speed = SPEED_INVALID;
4176 tp->link_config.active_duplex = DUPLEX_INVALID;
4177 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4178 LED_CTRL_LNKLED_OVERRIDE |
4179 LED_CTRL_TRAFFIC_OVERRIDE));
4180 }
4181
4182 if (current_link_up != netif_carrier_ok(tp->dev)) {
4183 if (current_link_up)
4184 netif_carrier_on(tp->dev);
4185 else
4186 netif_carrier_off(tp->dev);
4187 tg3_link_report(tp);
4188 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004189 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 if (orig_pause_cfg != now_pause_cfg ||
4191 orig_active_speed != tp->link_config.active_speed ||
4192 orig_active_duplex != tp->link_config.active_duplex)
4193 tg3_link_report(tp);
4194 }
4195
4196 return 0;
4197}
4198
Michael Chan747e8f82005-07-25 12:33:22 -07004199static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4200{
4201 int current_link_up, err = 0;
4202 u32 bmsr, bmcr;
4203 u16 current_speed;
4204 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004205 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004206
4207 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4208 tw32_f(MAC_MODE, tp->mac_mode);
4209 udelay(40);
4210
4211 tw32(MAC_EVENT, 0);
4212
4213 tw32_f(MAC_STATUS,
4214 (MAC_STATUS_SYNC_CHANGED |
4215 MAC_STATUS_CFG_CHANGED |
4216 MAC_STATUS_MI_COMPLETION |
4217 MAC_STATUS_LNKSTATE_CHANGED));
4218 udelay(40);
4219
4220 if (force_reset)
4221 tg3_phy_reset(tp);
4222
4223 current_link_up = 0;
4224 current_speed = SPEED_INVALID;
4225 current_duplex = DUPLEX_INVALID;
4226
4227 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4228 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004229 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4230 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4231 bmsr |= BMSR_LSTATUS;
4232 else
4233 bmsr &= ~BMSR_LSTATUS;
4234 }
Michael Chan747e8f82005-07-25 12:33:22 -07004235
4236 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4237
4238 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004239 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004240 /* do nothing, just check for link up at the end */
4241 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4242 u32 adv, new_adv;
4243
4244 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4245 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4246 ADVERTISE_1000XPAUSE |
4247 ADVERTISE_1000XPSE_ASYM |
4248 ADVERTISE_SLCT);
4249
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004250 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004251
4252 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4253 new_adv |= ADVERTISE_1000XHALF;
4254 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4255 new_adv |= ADVERTISE_1000XFULL;
4256
4257 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4258 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4259 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4260 tg3_writephy(tp, MII_BMCR, bmcr);
4261
4262 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004263 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004264 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004265
4266 return err;
4267 }
4268 } else {
4269 u32 new_bmcr;
4270
4271 bmcr &= ~BMCR_SPEED1000;
4272 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4273
4274 if (tp->link_config.duplex == DUPLEX_FULL)
4275 new_bmcr |= BMCR_FULLDPLX;
4276
4277 if (new_bmcr != bmcr) {
4278 /* BMCR_SPEED1000 is a reserved bit that needs
4279 * to be set on write.
4280 */
4281 new_bmcr |= BMCR_SPEED1000;
4282
4283 /* Force a linkdown */
4284 if (netif_carrier_ok(tp->dev)) {
4285 u32 adv;
4286
4287 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4288 adv &= ~(ADVERTISE_1000XFULL |
4289 ADVERTISE_1000XHALF |
4290 ADVERTISE_SLCT);
4291 tg3_writephy(tp, MII_ADVERTISE, adv);
4292 tg3_writephy(tp, MII_BMCR, bmcr |
4293 BMCR_ANRESTART |
4294 BMCR_ANENABLE);
4295 udelay(10);
4296 netif_carrier_off(tp->dev);
4297 }
4298 tg3_writephy(tp, MII_BMCR, new_bmcr);
4299 bmcr = new_bmcr;
4300 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4301 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004302 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4303 ASIC_REV_5714) {
4304 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4305 bmsr |= BMSR_LSTATUS;
4306 else
4307 bmsr &= ~BMSR_LSTATUS;
4308 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004309 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004310 }
4311 }
4312
4313 if (bmsr & BMSR_LSTATUS) {
4314 current_speed = SPEED_1000;
4315 current_link_up = 1;
4316 if (bmcr & BMCR_FULLDPLX)
4317 current_duplex = DUPLEX_FULL;
4318 else
4319 current_duplex = DUPLEX_HALF;
4320
Matt Carlsonef167e22007-12-20 20:10:01 -08004321 local_adv = 0;
4322 remote_adv = 0;
4323
Michael Chan747e8f82005-07-25 12:33:22 -07004324 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004325 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004326
4327 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4328 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4329 common = local_adv & remote_adv;
4330 if (common & (ADVERTISE_1000XHALF |
4331 ADVERTISE_1000XFULL)) {
4332 if (common & ADVERTISE_1000XFULL)
4333 current_duplex = DUPLEX_FULL;
4334 else
4335 current_duplex = DUPLEX_HALF;
Joe Perches63c3a662011-04-26 08:12:10 +00004336 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00004337 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00004338 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07004339 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00004340 }
Michael Chan747e8f82005-07-25 12:33:22 -07004341 }
4342 }
4343
Matt Carlsonef167e22007-12-20 20:10:01 -08004344 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4345 tg3_setup_flow_control(tp, local_adv, remote_adv);
4346
Michael Chan747e8f82005-07-25 12:33:22 -07004347 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4348 if (tp->link_config.active_duplex == DUPLEX_HALF)
4349 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4350
4351 tw32_f(MAC_MODE, tp->mac_mode);
4352 udelay(40);
4353
4354 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4355
4356 tp->link_config.active_speed = current_speed;
4357 tp->link_config.active_duplex = current_duplex;
4358
4359 if (current_link_up != netif_carrier_ok(tp->dev)) {
4360 if (current_link_up)
4361 netif_carrier_on(tp->dev);
4362 else {
4363 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004364 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004365 }
4366 tg3_link_report(tp);
4367 }
4368 return err;
4369}
4370
4371static void tg3_serdes_parallel_detect(struct tg3 *tp)
4372{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004373 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004374 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004375 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004376 return;
4377 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004378
Michael Chan747e8f82005-07-25 12:33:22 -07004379 if (!netif_carrier_ok(tp->dev) &&
4380 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4381 u32 bmcr;
4382
4383 tg3_readphy(tp, MII_BMCR, &bmcr);
4384 if (bmcr & BMCR_ANENABLE) {
4385 u32 phy1, phy2;
4386
4387 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004388 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
4389 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07004390
4391 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004392 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4393 MII_TG3_DSP_EXP1_INT_STAT);
4394 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
4395 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004396
4397 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4398 /* We have signal detect and not receiving
4399 * config code words, link is up by parallel
4400 * detection.
4401 */
4402
4403 bmcr &= ~BMCR_ANENABLE;
4404 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4405 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004406 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004407 }
4408 }
Matt Carlson859a588792010-04-05 10:19:28 +00004409 } else if (netif_carrier_ok(tp->dev) &&
4410 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004411 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004412 u32 phy2;
4413
4414 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004415 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4416 MII_TG3_DSP_EXP1_INT_STAT);
4417 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004418 if (phy2 & 0x20) {
4419 u32 bmcr;
4420
4421 /* Config code words received, turn on autoneg. */
4422 tg3_readphy(tp, MII_BMCR, &bmcr);
4423 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4424
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004425 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004426
4427 }
4428 }
4429}
4430
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4432{
Matt Carlsonf2096f92011-04-05 14:22:48 +00004433 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 int err;
4435
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004436 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004438 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07004439 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00004440 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442
Matt Carlsonbcb37f62008-11-03 16:52:09 -08004443 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00004444 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08004445
4446 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4447 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4448 scale = 65;
4449 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4450 scale = 6;
4451 else
4452 scale = 12;
4453
4454 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4455 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4456 tw32(GRC_MISC_CFG, val);
4457 }
4458
Matt Carlsonf2096f92011-04-05 14:22:48 +00004459 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4460 (6 << TX_LENGTHS_IPG_SHIFT);
4461 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
4462 val |= tr32(MAC_TX_LENGTHS) &
4463 (TX_LENGTHS_JMB_FRM_LEN_MSK |
4464 TX_LENGTHS_CNT_DWN_VAL_MSK);
4465
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 if (tp->link_config.active_speed == SPEED_1000 &&
4467 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00004468 tw32(MAC_TX_LENGTHS, val |
4469 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00004471 tw32(MAC_TX_LENGTHS, val |
4472 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473
Joe Perches63c3a662011-04-26 08:12:10 +00004474 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 if (netif_carrier_ok(tp->dev)) {
4476 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07004477 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 } else {
4479 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4480 }
4481 }
4482
Joe Perches63c3a662011-04-26 08:12:10 +00004483 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00004484 val = tr32(PCIE_PWR_MGMT_THRESH);
Matt Carlson8ed5d972007-05-07 00:25:49 -07004485 if (!netif_carrier_ok(tp->dev))
4486 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4487 tp->pwrmgmt_thresh;
4488 else
4489 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4490 tw32(PCIE_PWR_MGMT_THRESH, val);
4491 }
4492
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 return err;
4494}
4495
Matt Carlson66cfd1b2010-09-30 10:34:30 +00004496static inline int tg3_irq_sync(struct tg3 *tp)
4497{
4498 return tp->irq_sync;
4499}
4500
Matt Carlson97bd8e42011-04-13 11:05:04 +00004501static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
4502{
4503 int i;
4504
4505 dst = (u32 *)((u8 *)dst + off);
4506 for (i = 0; i < len; i += sizeof(u32))
4507 *dst++ = tr32(off + i);
4508}
4509
4510static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
4511{
4512 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
4513 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
4514 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
4515 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
4516 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
4517 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
4518 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
4519 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
4520 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
4521 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
4522 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
4523 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
4524 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
4525 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
4526 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
4527 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
4528 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
4529 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
4530 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
4531
Joe Perches63c3a662011-04-26 08:12:10 +00004532 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00004533 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
4534
4535 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
4536 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
4537 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
4538 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
4539 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
4540 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
4541 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
4542 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
4543
Joe Perches63c3a662011-04-26 08:12:10 +00004544 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00004545 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
4546 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
4547 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
4548 }
4549
4550 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
4551 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
4552 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
4553 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
4554 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
4555
Joe Perches63c3a662011-04-26 08:12:10 +00004556 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00004557 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
4558}
4559
4560static void tg3_dump_state(struct tg3 *tp)
4561{
4562 int i;
4563 u32 *regs;
4564
4565 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
4566 if (!regs) {
4567 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
4568 return;
4569 }
4570
Joe Perches63c3a662011-04-26 08:12:10 +00004571 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00004572 /* Read up to but not including private PCI registers */
4573 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
4574 regs[i / sizeof(u32)] = tr32(i);
4575 } else
4576 tg3_dump_legacy_regs(tp, regs);
4577
4578 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
4579 if (!regs[i + 0] && !regs[i + 1] &&
4580 !regs[i + 2] && !regs[i + 3])
4581 continue;
4582
4583 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
4584 i * 4,
4585 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
4586 }
4587
4588 kfree(regs);
4589
4590 for (i = 0; i < tp->irq_cnt; i++) {
4591 struct tg3_napi *tnapi = &tp->napi[i];
4592
4593 /* SW status block */
4594 netdev_err(tp->dev,
4595 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
4596 i,
4597 tnapi->hw_status->status,
4598 tnapi->hw_status->status_tag,
4599 tnapi->hw_status->rx_jumbo_consumer,
4600 tnapi->hw_status->rx_consumer,
4601 tnapi->hw_status->rx_mini_consumer,
4602 tnapi->hw_status->idx[0].rx_producer,
4603 tnapi->hw_status->idx[0].tx_consumer);
4604
4605 netdev_err(tp->dev,
4606 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
4607 i,
4608 tnapi->last_tag, tnapi->last_irq_tag,
4609 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
4610 tnapi->rx_rcb_ptr,
4611 tnapi->prodring.rx_std_prod_idx,
4612 tnapi->prodring.rx_std_cons_idx,
4613 tnapi->prodring.rx_jmb_prod_idx,
4614 tnapi->prodring.rx_jmb_cons_idx);
4615 }
4616}
4617
Michael Chandf3e6542006-05-26 17:48:07 -07004618/* This is called whenever we suspect that the system chipset is re-
4619 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4620 * is bogus tx completions. We try to recover by setting the
4621 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4622 * in the workqueue.
4623 */
4624static void tg3_tx_recover(struct tg3 *tp)
4625{
Joe Perches63c3a662011-04-26 08:12:10 +00004626 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07004627 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4628
Matt Carlson5129c3a2010-04-05 10:19:23 +00004629 netdev_warn(tp->dev,
4630 "The system may be re-ordering memory-mapped I/O "
4631 "cycles to the network device, attempting to recover. "
4632 "Please report the problem to the driver maintainer "
4633 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07004634
4635 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00004636 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07004637 spin_unlock(&tp->lock);
4638}
4639
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004640static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07004641{
Matt Carlsonf65aac12010-08-02 11:26:03 +00004642 /* Tell compiler to fetch tx indices from memory. */
4643 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004644 return tnapi->tx_pending -
4645 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07004646}
4647
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648/* Tigon3 never reports partial packet sends. So we do not
4649 * need special logic to handle SKBs that have not had all
4650 * of their frags sent yet, like SunGEM does.
4651 */
Matt Carlson17375d22009-08-28 14:02:18 +00004652static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653{
Matt Carlson17375d22009-08-28 14:02:18 +00004654 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004655 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004656 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004657 struct netdev_queue *txq;
4658 int index = tnapi - tp->napi;
4659
Joe Perches63c3a662011-04-26 08:12:10 +00004660 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004661 index--;
4662
4663 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664
4665 while (sw_idx != hw_idx) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00004666 struct ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004668 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669
Michael Chandf3e6542006-05-26 17:48:07 -07004670 if (unlikely(skb == NULL)) {
4671 tg3_tx_recover(tp);
4672 return;
4673 }
4674
Alexander Duyckf4188d82009-12-02 16:48:38 +00004675 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004676 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004677 skb_headlen(skb),
4678 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679
4680 ri->skb = NULL;
4681
4682 sw_idx = NEXT_TX(sw_idx);
4683
4684 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004685 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004686 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4687 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00004688
4689 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004690 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004691 skb_shinfo(skb)->frags[i].size,
4692 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 sw_idx = NEXT_TX(sw_idx);
4694 }
4695
David S. Millerf47c11e2005-06-24 20:18:35 -07004696 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004697
4698 if (unlikely(tx_bug)) {
4699 tg3_tx_recover(tp);
4700 return;
4701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 }
4703
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004704 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705
Michael Chan1b2a7202006-08-07 21:46:02 -07004706 /* Need to make the tx_cons update visible to tg3_start_xmit()
4707 * before checking for netif_queue_stopped(). Without the
4708 * memory barrier, there is a small possibility that tg3_start_xmit()
4709 * will miss it and cause the queue to be stopped forever.
4710 */
4711 smp_mb();
4712
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004713 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004714 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004715 __netif_tx_lock(txq, smp_processor_id());
4716 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004717 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004718 netif_tx_wake_queue(txq);
4719 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07004720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721}
4722
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004723static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
4724{
4725 if (!ri->skb)
4726 return;
4727
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004728 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004729 map_sz, PCI_DMA_FROMDEVICE);
4730 dev_kfree_skb_any(ri->skb);
4731 ri->skb = NULL;
4732}
4733
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734/* Returns size of skb allocated or < 0 on error.
4735 *
4736 * We only need to fill in the address because the other members
4737 * of the RX descriptor are invariant, see tg3_init_rings.
4738 *
4739 * Note the purposeful assymetry of cpu vs. chip accesses. For
4740 * posting buffers we only dirty the first cache line of the RX
4741 * descriptor (containing the address). Whereas for the RX status
4742 * buffers the cpu only reads the last cacheline of the RX descriptor
4743 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4744 */
Matt Carlson86b21e52009-11-13 13:03:45 +00004745static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00004746 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747{
4748 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00004749 struct ring_info *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 struct sk_buff *skb;
4751 dma_addr_t mapping;
4752 int skb_size, dest_idx;
4753
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 switch (opaque_key) {
4755 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004756 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00004757 desc = &tpr->rx_std[dest_idx];
4758 map = &tpr->rx_std_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004759 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 break;
4761
4762 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004763 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004764 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004765 map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004766 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 break;
4768
4769 default:
4770 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772
4773 /* Do not overwrite any of the map or rp information
4774 * until we are sure we can commit to a new buffer.
4775 *
4776 * Callers depend upon this behavior and assume that
4777 * we leave everything unchanged if we fail.
4778 */
Matt Carlson287be122009-08-28 13:58:46 +00004779 skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 if (skb == NULL)
4781 return -ENOMEM;
4782
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 skb_reserve(skb, tp->rx_offset);
4784
Matt Carlson287be122009-08-28 13:58:46 +00004785 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00004787 if (pci_dma_mapping_error(tp->pdev, mapping)) {
4788 dev_kfree_skb(skb);
4789 return -EIO;
4790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791
4792 map->skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004793 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 desc->addr_hi = ((u64)mapping >> 32);
4796 desc->addr_lo = ((u64)mapping & 0xffffffff);
4797
4798 return skb_size;
4799}
4800
4801/* We only need to move over in the address because the other
4802 * members of the RX descriptor are invariant. See notes above
4803 * tg3_alloc_rx_skb for full details.
4804 */
Matt Carlsona3896162009-11-13 13:03:44 +00004805static void tg3_recycle_rx(struct tg3_napi *tnapi,
4806 struct tg3_rx_prodring_set *dpr,
4807 u32 opaque_key, int src_idx,
4808 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809{
Matt Carlson17375d22009-08-28 14:02:18 +00004810 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4812 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004813 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004814 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815
4816 switch (opaque_key) {
4817 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004818 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004819 dest_desc = &dpr->rx_std[dest_idx];
4820 dest_map = &dpr->rx_std_buffers[dest_idx];
4821 src_desc = &spr->rx_std[src_idx];
4822 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 break;
4824
4825 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004826 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004827 dest_desc = &dpr->rx_jmb[dest_idx].std;
4828 dest_map = &dpr->rx_jmb_buffers[dest_idx];
4829 src_desc = &spr->rx_jmb[src_idx].std;
4830 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 break;
4832
4833 default:
4834 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836
4837 dest_map->skb = src_map->skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004838 dma_unmap_addr_set(dest_map, mapping,
4839 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 dest_desc->addr_hi = src_desc->addr_hi;
4841 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00004842
4843 /* Ensure that the update to the skb happens after the physical
4844 * addresses have been transferred to the new BD location.
4845 */
4846 smp_wmb();
4847
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 src_map->skb = NULL;
4849}
4850
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851/* The RX ring scheme is composed of multiple rings which post fresh
4852 * buffers to the chip, and one special ring the chip uses to report
4853 * status back to the host.
4854 *
4855 * The special ring reports the status of received packets to the
4856 * host. The chip does not write into the original descriptor the
4857 * RX buffer was obtained from. The chip simply takes the original
4858 * descriptor as provided by the host, updates the status and length
4859 * field, then writes this into the next status ring entry.
4860 *
4861 * Each ring the host uses to post buffers to the chip is described
4862 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
4863 * it is first placed into the on-chip ram. When the packet's length
4864 * is known, it walks down the TG3_BDINFO entries to select the ring.
4865 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
4866 * which is within the range of the new packet's length is chosen.
4867 *
4868 * The "separate ring for rx status" scheme may sound queer, but it makes
4869 * sense from a cache coherency perspective. If only the host writes
4870 * to the buffer post rings, and only the chip writes to the rx status
4871 * rings, then cache lines never move beyond shared-modified state.
4872 * If both the host and chip were to write into the same ring, cache line
4873 * eviction could occur since both entities want it in an exclusive state.
4874 */
Matt Carlson17375d22009-08-28 14:02:18 +00004875static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876{
Matt Carlson17375d22009-08-28 14:02:18 +00004877 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07004878 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004879 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00004880 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07004881 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004883 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004885 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 /*
4887 * We need to order the read of hw_idx and the read of
4888 * the opaque cookie.
4889 */
4890 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 work_mask = 0;
4892 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004893 std_prod_idx = tpr->rx_std_prod_idx;
4894 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00004896 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00004897 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 unsigned int len;
4899 struct sk_buff *skb;
4900 dma_addr_t dma_addr;
4901 u32 opaque_key, desc_idx, *post_ptr;
4902
4903 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
4904 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
4905 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004906 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004907 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004908 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004909 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07004910 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004912 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004913 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004914 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004915 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00004916 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918
4919 work_mask |= opaque_key;
4920
4921 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
4922 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
4923 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00004924 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 desc_idx, *post_ptr);
4926 drop_it_no_recycle:
4927 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00004928 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 goto next_pkt;
4930 }
4931
Matt Carlsonad829262008-11-21 17:16:16 -08004932 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
4933 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934
Matt Carlsond2757fc2010-04-12 06:58:27 +00004935 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 int skb_size;
4937
Matt Carlson86b21e52009-11-13 13:03:45 +00004938 skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00004939 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 if (skb_size < 0)
4941 goto drop_it;
4942
Matt Carlson287be122009-08-28 13:58:46 +00004943 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 PCI_DMA_FROMDEVICE);
4945
Matt Carlson61e800c2010-02-17 15:16:54 +00004946 /* Ensure that the update to the skb happens
4947 * after the usage of the old DMA mapping.
4948 */
4949 smp_wmb();
4950
4951 ri->skb = NULL;
4952
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 skb_put(skb, len);
4954 } else {
4955 struct sk_buff *copy_skb;
4956
Matt Carlsona3896162009-11-13 13:03:44 +00004957 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 desc_idx, *post_ptr);
4959
Matt Carlsonbf933c82011-01-25 15:58:49 +00004960 copy_skb = netdev_alloc_skb(tp->dev, len +
Matt Carlson9dc7a112010-04-12 06:58:28 +00004961 TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 if (copy_skb == NULL)
4963 goto drop_it_no_recycle;
4964
Matt Carlsonbf933c82011-01-25 15:58:49 +00004965 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 skb_put(copy_skb, len);
4967 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03004968 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
4970
4971 /* We'll reuse the original ring buffer. */
4972 skb = copy_skb;
4973 }
4974
Michał Mirosławdc668912011-04-07 03:35:07 +00004975 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
4977 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
4978 >> RXD_TCPCSUM_SHIFT) == 0xffff))
4979 skb->ip_summed = CHECKSUM_UNNECESSARY;
4980 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07004981 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982
4983 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004984
4985 if (len > (tp->dev->mtu + ETH_HLEN) &&
4986 skb->protocol != htons(ETH_P_8021Q)) {
4987 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00004988 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004989 }
4990
Matt Carlson9dc7a112010-04-12 06:58:28 +00004991 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00004992 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
4993 __vlan_hwaccel_put_tag(skb,
4994 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00004995
Matt Carlsonbf933c82011-01-25 15:58:49 +00004996 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 received++;
4999 budget--;
5000
5001next_pkt:
5002 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07005003
5004 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005005 tpr->rx_std_prod_idx = std_prod_idx &
5006 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00005007 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5008 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07005009 work_mask &= ~RXD_OPAQUE_RING_STD;
5010 rx_std_posted = 0;
5011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07005013 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00005014 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07005015
5016 /* Refresh hw_idx to see if there is new work */
5017 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005018 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07005019 rmb();
5020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 }
5022
5023 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00005024 tnapi->rx_rcb_ptr = sw_idx;
5025 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026
5027 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00005028 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005029 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005030 tpr->rx_std_prod_idx = std_prod_idx &
5031 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005032 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5033 tpr->rx_std_prod_idx);
5034 }
5035 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005036 tpr->rx_jmb_prod_idx = jmb_prod_idx &
5037 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005038 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5039 tpr->rx_jmb_prod_idx);
5040 }
5041 mmiowb();
5042 } else if (work_mask) {
5043 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
5044 * updated before the producer indices can be updated.
5045 */
5046 smp_wmb();
5047
Matt Carlson2c49a442010-09-30 10:34:35 +00005048 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
5049 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005050
Matt Carlsone4af1af2010-02-12 14:47:05 +00005051 if (tnapi != &tp->napi[1])
5052 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054
5055 return received;
5056}
5057
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005058static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00005061 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005062 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
5063
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 if (sblk->status & SD_STATUS_LINK_CHG) {
5065 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005066 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07005067 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005068 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07005069 tw32_f(MAC_STATUS,
5070 (MAC_STATUS_SYNC_CHANGED |
5071 MAC_STATUS_CFG_CHANGED |
5072 MAC_STATUS_MI_COMPLETION |
5073 MAC_STATUS_LNKSTATE_CHANGED));
5074 udelay(40);
5075 } else
5076 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07005077 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 }
5079 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005080}
5081
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005082static int tg3_rx_prodring_xfer(struct tg3 *tp,
5083 struct tg3_rx_prodring_set *dpr,
5084 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005085{
5086 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005087 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005088
5089 while (1) {
5090 src_prod_idx = spr->rx_std_prod_idx;
5091
5092 /* Make sure updates to the rx_std_buffers[] entries and the
5093 * standard producer index are seen in the correct order.
5094 */
5095 smp_rmb();
5096
5097 if (spr->rx_std_cons_idx == src_prod_idx)
5098 break;
5099
5100 if (spr->rx_std_cons_idx < src_prod_idx)
5101 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
5102 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005103 cpycnt = tp->rx_std_ring_mask + 1 -
5104 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005105
Matt Carlson2c49a442010-09-30 10:34:35 +00005106 cpycnt = min(cpycnt,
5107 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005108
5109 si = spr->rx_std_cons_idx;
5110 di = dpr->rx_std_prod_idx;
5111
Matt Carlsone92967b2010-02-12 14:47:06 +00005112 for (i = di; i < di + cpycnt; i++) {
5113 if (dpr->rx_std_buffers[i].skb) {
5114 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005115 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005116 break;
5117 }
5118 }
5119
5120 if (!cpycnt)
5121 break;
5122
5123 /* Ensure that updates to the rx_std_buffers ring and the
5124 * shadowed hardware producer ring from tg3_recycle_skb() are
5125 * ordered correctly WRT the skb check above.
5126 */
5127 smp_rmb();
5128
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005129 memcpy(&dpr->rx_std_buffers[di],
5130 &spr->rx_std_buffers[si],
5131 cpycnt * sizeof(struct ring_info));
5132
5133 for (i = 0; i < cpycnt; i++, di++, si++) {
5134 struct tg3_rx_buffer_desc *sbd, *dbd;
5135 sbd = &spr->rx_std[si];
5136 dbd = &dpr->rx_std[di];
5137 dbd->addr_hi = sbd->addr_hi;
5138 dbd->addr_lo = sbd->addr_lo;
5139 }
5140
Matt Carlson2c49a442010-09-30 10:34:35 +00005141 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
5142 tp->rx_std_ring_mask;
5143 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
5144 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005145 }
5146
5147 while (1) {
5148 src_prod_idx = spr->rx_jmb_prod_idx;
5149
5150 /* Make sure updates to the rx_jmb_buffers[] entries and
5151 * the jumbo producer index are seen in the correct order.
5152 */
5153 smp_rmb();
5154
5155 if (spr->rx_jmb_cons_idx == src_prod_idx)
5156 break;
5157
5158 if (spr->rx_jmb_cons_idx < src_prod_idx)
5159 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
5160 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005161 cpycnt = tp->rx_jmb_ring_mask + 1 -
5162 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005163
5164 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00005165 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005166
5167 si = spr->rx_jmb_cons_idx;
5168 di = dpr->rx_jmb_prod_idx;
5169
Matt Carlsone92967b2010-02-12 14:47:06 +00005170 for (i = di; i < di + cpycnt; i++) {
5171 if (dpr->rx_jmb_buffers[i].skb) {
5172 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005173 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005174 break;
5175 }
5176 }
5177
5178 if (!cpycnt)
5179 break;
5180
5181 /* Ensure that updates to the rx_jmb_buffers ring and the
5182 * shadowed hardware producer ring from tg3_recycle_skb() are
5183 * ordered correctly WRT the skb check above.
5184 */
5185 smp_rmb();
5186
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005187 memcpy(&dpr->rx_jmb_buffers[di],
5188 &spr->rx_jmb_buffers[si],
5189 cpycnt * sizeof(struct ring_info));
5190
5191 for (i = 0; i < cpycnt; i++, di++, si++) {
5192 struct tg3_rx_buffer_desc *sbd, *dbd;
5193 sbd = &spr->rx_jmb[si].std;
5194 dbd = &dpr->rx_jmb[di].std;
5195 dbd->addr_hi = sbd->addr_hi;
5196 dbd->addr_lo = sbd->addr_lo;
5197 }
5198
Matt Carlson2c49a442010-09-30 10:34:35 +00005199 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
5200 tp->rx_jmb_ring_mask;
5201 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
5202 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005203 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005204
5205 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005206}
5207
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005208static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
5209{
5210 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211
5212 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005213 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00005214 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00005215 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07005216 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 }
5218
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 /* run RX thread, within the bounds set by NAPI.
5220 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005221 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005223 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00005224 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225
Joe Perches63c3a662011-04-26 08:12:10 +00005226 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005227 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005228 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00005229 u32 std_prod_idx = dpr->rx_std_prod_idx;
5230 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005231
Matt Carlsone4af1af2010-02-12 14:47:05 +00005232 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005233 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00005234 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005235
5236 wmb();
5237
Matt Carlsone4af1af2010-02-12 14:47:05 +00005238 if (std_prod_idx != dpr->rx_std_prod_idx)
5239 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5240 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005241
Matt Carlsone4af1af2010-02-12 14:47:05 +00005242 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5243 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5244 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005245
5246 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005247
5248 if (err)
5249 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005250 }
5251
David S. Miller6f535762007-10-11 18:08:29 -07005252 return work_done;
5253}
David S. Millerf7383c22005-05-18 22:50:53 -07005254
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005255static int tg3_poll_msix(struct napi_struct *napi, int budget)
5256{
5257 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5258 struct tg3 *tp = tnapi->tp;
5259 int work_done = 0;
5260 struct tg3_hw_status *sblk = tnapi->hw_status;
5261
5262 while (1) {
5263 work_done = tg3_poll_work(tnapi, work_done, budget);
5264
Joe Perches63c3a662011-04-26 08:12:10 +00005265 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005266 goto tx_recovery;
5267
5268 if (unlikely(work_done >= budget))
5269 break;
5270
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005271 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005272 * to tell the hw how much work has been processed,
5273 * so we must read it before checking for more work.
5274 */
5275 tnapi->last_tag = sblk->status_tag;
5276 tnapi->last_irq_tag = tnapi->last_tag;
5277 rmb();
5278
5279 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005280 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5281 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005282 napi_complete(napi);
5283 /* Reenable interrupts. */
5284 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5285 mmiowb();
5286 break;
5287 }
5288 }
5289
5290 return work_done;
5291
5292tx_recovery:
5293 /* work_done is guaranteed to be less than budget. */
5294 napi_complete(napi);
5295 schedule_work(&tp->reset_task);
5296 return work_done;
5297}
5298
Matt Carlsone64de4e2011-04-13 11:05:05 +00005299static void tg3_process_error(struct tg3 *tp)
5300{
5301 u32 val;
5302 bool real_error = false;
5303
Joe Perches63c3a662011-04-26 08:12:10 +00005304 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00005305 return;
5306
5307 /* Check Flow Attention register */
5308 val = tr32(HOSTCC_FLOW_ATTN);
5309 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
5310 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
5311 real_error = true;
5312 }
5313
5314 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
5315 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
5316 real_error = true;
5317 }
5318
5319 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
5320 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
5321 real_error = true;
5322 }
5323
5324 if (!real_error)
5325 return;
5326
5327 tg3_dump_state(tp);
5328
Joe Perches63c3a662011-04-26 08:12:10 +00005329 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsone64de4e2011-04-13 11:05:05 +00005330 schedule_work(&tp->reset_task);
5331}
5332
David S. Miller6f535762007-10-11 18:08:29 -07005333static int tg3_poll(struct napi_struct *napi, int budget)
5334{
Matt Carlson8ef04422009-08-28 14:01:37 +00005335 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5336 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07005337 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00005338 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07005339
5340 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00005341 if (sblk->status & SD_STATUS_ERROR)
5342 tg3_process_error(tp);
5343
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005344 tg3_poll_link(tp);
5345
Matt Carlson17375d22009-08-28 14:02:18 +00005346 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07005347
Joe Perches63c3a662011-04-26 08:12:10 +00005348 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07005349 goto tx_recovery;
5350
5351 if (unlikely(work_done >= budget))
5352 break;
5353
Joe Perches63c3a662011-04-26 08:12:10 +00005354 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00005355 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07005356 * to tell the hw how much work has been processed,
5357 * so we must read it before checking for more work.
5358 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005359 tnapi->last_tag = sblk->status_tag;
5360 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07005361 rmb();
5362 } else
5363 sblk->status &= ~SD_STATUS_UPDATED;
5364
Matt Carlson17375d22009-08-28 14:02:18 +00005365 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005366 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00005367 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005368 break;
5369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 }
5371
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005372 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07005373
5374tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07005375 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08005376 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07005377 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07005378 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379}
5380
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005381static void tg3_napi_disable(struct tg3 *tp)
5382{
5383 int i;
5384
5385 for (i = tp->irq_cnt - 1; i >= 0; i--)
5386 napi_disable(&tp->napi[i].napi);
5387}
5388
5389static void tg3_napi_enable(struct tg3 *tp)
5390{
5391 int i;
5392
5393 for (i = 0; i < tp->irq_cnt; i++)
5394 napi_enable(&tp->napi[i].napi);
5395}
5396
5397static void tg3_napi_init(struct tg3 *tp)
5398{
5399 int i;
5400
5401 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
5402 for (i = 1; i < tp->irq_cnt; i++)
5403 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
5404}
5405
5406static void tg3_napi_fini(struct tg3 *tp)
5407{
5408 int i;
5409
5410 for (i = 0; i < tp->irq_cnt; i++)
5411 netif_napi_del(&tp->napi[i].napi);
5412}
5413
5414static inline void tg3_netif_stop(struct tg3 *tp)
5415{
5416 tp->dev->trans_start = jiffies; /* prevent tx timeout */
5417 tg3_napi_disable(tp);
5418 netif_tx_disable(tp->dev);
5419}
5420
5421static inline void tg3_netif_start(struct tg3 *tp)
5422{
5423 /* NOTE: unconditional netif_tx_wake_all_queues is only
5424 * appropriate so long as all callers are assured to
5425 * have free tx slots (such as after tg3_init_hw)
5426 */
5427 netif_tx_wake_all_queues(tp->dev);
5428
5429 tg3_napi_enable(tp);
5430 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
5431 tg3_enable_ints(tp);
5432}
5433
David S. Millerf47c11e2005-06-24 20:18:35 -07005434static void tg3_irq_quiesce(struct tg3 *tp)
5435{
Matt Carlson4f125f42009-09-01 12:55:02 +00005436 int i;
5437
David S. Millerf47c11e2005-06-24 20:18:35 -07005438 BUG_ON(tp->irq_sync);
5439
5440 tp->irq_sync = 1;
5441 smp_mb();
5442
Matt Carlson4f125f42009-09-01 12:55:02 +00005443 for (i = 0; i < tp->irq_cnt; i++)
5444 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07005445}
5446
David S. Millerf47c11e2005-06-24 20:18:35 -07005447/* Fully shutdown all tg3 driver activity elsewhere in the system.
5448 * If irq_sync is non-zero, then the IRQ handler must be synchronized
5449 * with as well. Most of the time, this is not necessary except when
5450 * shutting down the device.
5451 */
5452static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
5453{
Michael Chan46966542007-07-11 19:47:19 -07005454 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07005455 if (irq_sync)
5456 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005457}
5458
5459static inline void tg3_full_unlock(struct tg3 *tp)
5460{
David S. Millerf47c11e2005-06-24 20:18:35 -07005461 spin_unlock_bh(&tp->lock);
5462}
5463
Michael Chanfcfa0a32006-03-20 22:28:41 -08005464/* One-shot MSI handler - Chip automatically disables interrupt
5465 * after sending MSI so driver doesn't have to do it.
5466 */
David Howells7d12e782006-10-05 14:55:46 +01005467static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08005468{
Matt Carlson09943a12009-08-28 14:01:57 +00005469 struct tg3_napi *tnapi = dev_id;
5470 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08005471
Matt Carlson898a56f2009-08-28 14:02:40 +00005472 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005473 if (tnapi->rx_rcb)
5474 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005475
5476 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005477 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005478
5479 return IRQ_HANDLED;
5480}
5481
Michael Chan88b06bc22005-04-21 17:13:25 -07005482/* MSI ISR - No need to check for interrupt sharing and no need to
5483 * flush status block and interrupt mailbox. PCI ordering rules
5484 * guarantee that MSI will arrive after the status block.
5485 */
David Howells7d12e782006-10-05 14:55:46 +01005486static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07005487{
Matt Carlson09943a12009-08-28 14:01:57 +00005488 struct tg3_napi *tnapi = dev_id;
5489 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07005490
Matt Carlson898a56f2009-08-28 14:02:40 +00005491 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005492 if (tnapi->rx_rcb)
5493 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07005494 /*
David S. Millerfac9b832005-05-18 22:46:34 -07005495 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07005496 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07005497 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07005498 * NIC to stop sending us irqs, engaging "in-intr-handler"
5499 * event coalescing.
5500 */
5501 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07005502 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005503 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07005504
Michael Chan88b06bc22005-04-21 17:13:25 -07005505 return IRQ_RETVAL(1);
5506}
5507
David Howells7d12e782006-10-05 14:55:46 +01005508static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509{
Matt Carlson09943a12009-08-28 14:01:57 +00005510 struct tg3_napi *tnapi = dev_id;
5511 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005512 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 unsigned int handled = 1;
5514
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 /* In INTx mode, it is possible for the interrupt to arrive at
5516 * the CPU before the status block posted prior to the interrupt.
5517 * Reading the PCI State register will confirm whether the
5518 * interrupt is ours and will flush the status block.
5519 */
Michael Chand18edcb2007-03-24 20:57:11 -07005520 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00005521 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07005522 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5523 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005524 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07005525 }
Michael Chand18edcb2007-03-24 20:57:11 -07005526 }
5527
5528 /*
5529 * Writing any value to intr-mbox-0 clears PCI INTA# and
5530 * chip-internal interrupt pending events.
5531 * Writing non-zero to intr-mbox-0 additional tells the
5532 * NIC to stop sending us irqs, engaging "in-intr-handler"
5533 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005534 *
5535 * Flush the mailbox to de-assert the IRQ immediately to prevent
5536 * spurious interrupts. The flush impacts performance but
5537 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005538 */
Michael Chanc04cb342007-05-07 00:26:15 -07005539 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07005540 if (tg3_irq_sync(tp))
5541 goto out;
5542 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00005543 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00005544 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00005545 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07005546 } else {
5547 /* No work, shared interrupt perhaps? re-enable
5548 * interrupts, and flush that PCI write
5549 */
5550 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
5551 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07005552 }
David S. Millerf47c11e2005-06-24 20:18:35 -07005553out:
David S. Millerfac9b832005-05-18 22:46:34 -07005554 return IRQ_RETVAL(handled);
5555}
5556
David Howells7d12e782006-10-05 14:55:46 +01005557static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07005558{
Matt Carlson09943a12009-08-28 14:01:57 +00005559 struct tg3_napi *tnapi = dev_id;
5560 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005561 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07005562 unsigned int handled = 1;
5563
David S. Millerfac9b832005-05-18 22:46:34 -07005564 /* In INTx mode, it is possible for the interrupt to arrive at
5565 * the CPU before the status block posted prior to the interrupt.
5566 * Reading the PCI State register will confirm whether the
5567 * interrupt is ours and will flush the status block.
5568 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005569 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00005570 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07005571 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5572 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005573 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 }
Michael Chand18edcb2007-03-24 20:57:11 -07005575 }
5576
5577 /*
5578 * writing any value to intr-mbox-0 clears PCI INTA# and
5579 * chip-internal interrupt pending events.
5580 * writing non-zero to intr-mbox-0 additional tells the
5581 * NIC to stop sending us irqs, engaging "in-intr-handler"
5582 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005583 *
5584 * Flush the mailbox to de-assert the IRQ immediately to prevent
5585 * spurious interrupts. The flush impacts performance but
5586 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005587 */
Michael Chanc04cb342007-05-07 00:26:15 -07005588 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00005589
5590 /*
5591 * In a shared interrupt configuration, sometimes other devices'
5592 * interrupts will scream. We record the current status tag here
5593 * so that the above check can report that the screaming interrupts
5594 * are unhandled. Eventually they will be silenced.
5595 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005596 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00005597
Michael Chand18edcb2007-03-24 20:57:11 -07005598 if (tg3_irq_sync(tp))
5599 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00005600
Matt Carlson72334482009-08-28 14:03:01 +00005601 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00005602
Matt Carlson09943a12009-08-28 14:01:57 +00005603 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00005604
David S. Millerf47c11e2005-06-24 20:18:35 -07005605out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606 return IRQ_RETVAL(handled);
5607}
5608
Michael Chan79381092005-04-21 17:13:59 -07005609/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01005610static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07005611{
Matt Carlson09943a12009-08-28 14:01:57 +00005612 struct tg3_napi *tnapi = dev_id;
5613 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005614 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07005615
Michael Chanf9804dd2005-09-27 12:13:10 -07005616 if ((sblk->status & SD_STATUS_UPDATED) ||
5617 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07005618 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07005619 return IRQ_RETVAL(1);
5620 }
5621 return IRQ_RETVAL(0);
5622}
5623
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07005624static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07005625static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626
Michael Chanb9ec6c12006-07-25 16:37:27 -07005627/* Restart hardware after configuration changes, self-test, etc.
5628 * Invoked with tp->lock held.
5629 */
5630static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07005631 __releases(tp->lock)
5632 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005633{
5634 int err;
5635
5636 err = tg3_init_hw(tp, reset_phy);
5637 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00005638 netdev_err(tp->dev,
5639 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07005640 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
5641 tg3_full_unlock(tp);
5642 del_timer_sync(&tp->timer);
5643 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00005644 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005645 dev_close(tp->dev);
5646 tg3_full_lock(tp, 0);
5647 }
5648 return err;
5649}
5650
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651#ifdef CONFIG_NET_POLL_CONTROLLER
5652static void tg3_poll_controller(struct net_device *dev)
5653{
Matt Carlson4f125f42009-09-01 12:55:02 +00005654 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07005655 struct tg3 *tp = netdev_priv(dev);
5656
Matt Carlson4f125f42009-09-01 12:55:02 +00005657 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00005658 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659}
5660#endif
5661
David Howellsc4028952006-11-22 14:57:56 +00005662static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663{
David Howellsc4028952006-11-22 14:57:56 +00005664 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005665 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 unsigned int restart_timer;
5667
Michael Chan7faa0062006-02-02 17:29:28 -08005668 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08005669
5670 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08005671 tg3_full_unlock(tp);
5672 return;
5673 }
5674
5675 tg3_full_unlock(tp);
5676
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005677 tg3_phy_stop(tp);
5678
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 tg3_netif_stop(tp);
5680
David S. Millerf47c11e2005-06-24 20:18:35 -07005681 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682
Joe Perches63c3a662011-04-26 08:12:10 +00005683 restart_timer = tg3_flag(tp, RESTART_TIMER);
5684 tg3_flag_clear(tp, RESTART_TIMER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685
Joe Perches63c3a662011-04-26 08:12:10 +00005686 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
Michael Chandf3e6542006-05-26 17:48:07 -07005687 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5688 tp->write32_rx_mbox = tg3_write_flush_reg32;
Joe Perches63c3a662011-04-26 08:12:10 +00005689 tg3_flag_set(tp, MBOX_WRITE_REORDER);
5690 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005691 }
5692
Michael Chan944d9802005-05-29 14:57:48 -07005693 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005694 err = tg3_init_hw(tp, 1);
5695 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005696 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697
5698 tg3_netif_start(tp);
5699
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 if (restart_timer)
5701 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08005702
Michael Chanb9ec6c12006-07-25 16:37:27 -07005703out:
Michael Chan7faa0062006-02-02 17:29:28 -08005704 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005705
5706 if (!err)
5707 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708}
5709
5710static void tg3_tx_timeout(struct net_device *dev)
5711{
5712 struct tg3 *tp = netdev_priv(dev);
5713
Michael Chanb0408752007-02-13 12:18:30 -08005714 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00005715 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00005716 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08005717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718
5719 schedule_work(&tp->reset_task);
5720}
5721
Michael Chanc58ec932005-09-17 00:46:27 -07005722/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5723static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5724{
5725 u32 base = (u32) mapping & 0xffffffff;
5726
Eric Dumazet807540b2010-09-23 05:40:09 +00005727 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07005728}
5729
Michael Chan72f2afb2006-03-06 19:28:35 -08005730/* Test for DMA addresses > 40-bit */
5731static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5732 int len)
5733{
5734#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00005735 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00005736 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08005737 return 0;
5738#else
5739 return 0;
5740#endif
5741}
5742
Matt Carlson2ffcc982011-05-19 12:12:44 +00005743static void tg3_set_txd(struct tg3_napi *tnapi, int entry,
5744 dma_addr_t mapping, int len, u32 flags,
5745 u32 mss_and_is_end)
5746{
5747 struct tg3_tx_buffer_desc *txd = &tnapi->tx_ring[entry];
5748 int is_end = (mss_and_is_end & 0x1);
5749 u32 mss = (mss_and_is_end >> 1);
5750 u32 vlan_tag = 0;
5751
5752 if (is_end)
5753 flags |= TXD_FLAG_END;
5754 if (flags & TXD_FLAG_VLAN) {
5755 vlan_tag = flags >> 16;
5756 flags &= 0xffff;
5757 }
5758 vlan_tag |= (mss << TXD_MSS_SHIFT);
5759
5760 txd->addr_hi = ((u64) mapping >> 32);
5761 txd->addr_lo = ((u64) mapping & 0xffffffff);
5762 txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
5763 txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
5764}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765
Matt Carlson432aa7e2011-05-19 12:12:45 +00005766static void tg3_skb_error_unmap(struct tg3_napi *tnapi,
5767 struct sk_buff *skb, int last)
5768{
5769 int i;
5770 u32 entry = tnapi->tx_prod;
5771 struct ring_info *txb = &tnapi->tx_buffers[entry];
5772
5773 pci_unmap_single(tnapi->tp->pdev,
5774 dma_unmap_addr(txb, mapping),
5775 skb_headlen(skb),
5776 PCI_DMA_TODEVICE);
5777 for (i = 0; i <= last; i++) {
5778 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5779
5780 entry = NEXT_TX(entry);
5781 txb = &tnapi->tx_buffers[entry];
5782
5783 pci_unmap_page(tnapi->tp->pdev,
5784 dma_unmap_addr(txb, mapping),
5785 frag->size, PCI_DMA_TODEVICE);
5786 }
5787}
5788
Michael Chan72f2afb2006-03-06 19:28:35 -08005789/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005790static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
Matt Carlson432aa7e2011-05-19 12:12:45 +00005791 struct sk_buff *skb,
5792 u32 base_flags, u32 mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793{
Matt Carlson24f4efd2009-11-13 13:03:35 +00005794 struct tg3 *tp = tnapi->tp;
Matt Carlson41588ba2008-04-19 18:12:33 -07005795 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07005796 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00005797 u32 entry = tnapi->tx_prod;
5798 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799
Matt Carlson41588ba2008-04-19 18:12:33 -07005800 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
5801 new_skb = skb_copy(skb, GFP_ATOMIC);
5802 else {
5803 int more_headroom = 4 - ((unsigned long)skb->data & 3);
5804
5805 new_skb = skb_copy_expand(skb,
5806 skb_headroom(skb) + more_headroom,
5807 skb_tailroom(skb), GFP_ATOMIC);
5808 }
5809
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07005811 ret = -1;
5812 } else {
5813 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00005814 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
5815 PCI_DMA_TODEVICE);
5816 /* Make sure the mapping succeeded */
5817 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
5818 ret = -1;
5819 dev_kfree_skb(new_skb);
David S. Miller90079ce2008-09-11 04:52:51 -07005820
Michael Chanc58ec932005-09-17 00:46:27 -07005821 /* Make sure new skb does not cross any 4G boundaries.
5822 * Drop the packet if it does.
5823 */
Joe Perches63c3a662011-04-26 08:12:10 +00005824 } else if (tg3_flag(tp, 4G_DMA_BNDRY_BUG) &&
5825 tg3_4g_overflow_test(new_addr, new_skb->len)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00005826 pci_unmap_single(tp->pdev, new_addr, new_skb->len,
5827 PCI_DMA_TODEVICE);
Michael Chanc58ec932005-09-17 00:46:27 -07005828 ret = -1;
5829 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07005830 } else {
Matt Carlson432aa7e2011-05-19 12:12:45 +00005831 tnapi->tx_buffers[entry].skb = new_skb;
5832 dma_unmap_addr_set(&tnapi->tx_buffers[entry],
5833 mapping, new_addr);
5834
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005835 tg3_set_txd(tnapi, entry, new_addr, new_skb->len,
Michael Chanc58ec932005-09-17 00:46:27 -07005836 base_flags, 1 | (mss << 1));
Michael Chanc58ec932005-09-17 00:46:27 -07005837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838 }
5839
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 dev_kfree_skb(skb);
5841
Michael Chanc58ec932005-09-17 00:46:27 -07005842 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843}
5844
Matt Carlson2ffcc982011-05-19 12:12:44 +00005845static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07005846
5847/* Use GSO to workaround a rare TSO bug that may be triggered when the
5848 * TSO header is greater than 80 bytes.
5849 */
5850static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
5851{
5852 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005853 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07005854
5855 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005856 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07005857 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00005858
5859 /* netif_tx_stop_queue() must be done before checking
5860 * checking tx index in tg3_tx_avail() below, because in
5861 * tg3_tx(), we update tx index before checking for
5862 * netif_tx_queue_stopped().
5863 */
5864 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005865 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08005866 return NETDEV_TX_BUSY;
5867
5868 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005869 }
5870
5871 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07005872 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07005873 goto tg3_tso_bug_end;
5874
5875 do {
5876 nskb = segs;
5877 segs = segs->next;
5878 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00005879 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005880 } while (segs);
5881
5882tg3_tso_bug_end:
5883 dev_kfree_skb(skb);
5884
5885 return NETDEV_TX_OK;
5886}
Michael Chan52c0fd82006-06-29 20:15:54 -07005887
Michael Chan5a6f3072006-03-20 22:28:05 -08005888/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00005889 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08005890 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00005891static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08005892{
5893 struct tg3 *tp = netdev_priv(dev);
Michael Chan5a6f3072006-03-20 22:28:05 -08005894 u32 len, entry, base_flags, mss;
Matt Carlson432aa7e2011-05-19 12:12:45 +00005895 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07005896 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005897 struct tg3_napi *tnapi;
5898 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00005899 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005900
Matt Carlson24f4efd2009-11-13 13:03:35 +00005901 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5902 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00005903 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00005904 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905
Michael Chan00b70502006-06-17 21:58:45 -07005906 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005907 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07005908 * interrupt. Furthermore, IRQ processing runs lockless so we have
5909 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005911 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00005912 if (!netif_tx_queue_stopped(txq)) {
5913 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005914
5915 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00005916 netdev_err(dev,
5917 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919 return NETDEV_TX_BUSY;
5920 }
5921
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005922 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07005924 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005926
Matt Carlsonbe98da62010-07-11 09:31:46 +00005927 mss = skb_shinfo(skb)->gso_size;
5928 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005929 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00005930 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931
5932 if (skb_header_cloned(skb) &&
5933 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5934 dev_kfree_skb(skb);
5935 goto out_unlock;
5936 }
5937
Matt Carlson34195c32010-07-11 09:31:42 +00005938 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005939 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940
Matt Carlson02e96082010-09-15 08:59:59 +00005941 if (skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00005942 hdr_len = skb_headlen(skb) - ETH_HLEN;
5943 } else {
5944 u32 ip_tcp_len;
5945
5946 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
5947 hdr_len = ip_tcp_len + tcp_opt_len;
5948
5949 iph->check = 0;
5950 iph->tot_len = htons(mss + hdr_len);
5951 }
5952
Michael Chan52c0fd82006-06-29 20:15:54 -07005953 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00005954 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00005955 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07005956
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5958 TXD_FLAG_CPU_POST_DMA);
5959
Joe Perches63c3a662011-04-26 08:12:10 +00005960 if (tg3_flag(tp, HW_TSO_1) ||
5961 tg3_flag(tp, HW_TSO_2) ||
5962 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005963 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005965 } else
5966 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
5967 iph->daddr, 0,
5968 IPPROTO_TCP,
5969 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970
Joe Perches63c3a662011-04-26 08:12:10 +00005971 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00005972 mss |= (hdr_len & 0xc) << 12;
5973 if (hdr_len & 0x10)
5974 base_flags |= 0x00000010;
5975 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00005976 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005977 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00005978 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005979 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005980 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981 int tsflags;
5982
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005983 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984 mss |= (tsflags << 11);
5985 }
5986 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005987 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988 int tsflags;
5989
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005990 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 base_flags |= tsflags << 12;
5992 }
5993 }
5994 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00005995
Jesse Grosseab6d182010-10-20 13:56:03 +00005996 if (vlan_tx_tag_present(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997 base_flags |= (TXD_FLAG_VLAN |
5998 (vlan_tx_tag_get(skb) << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999
Joe Perches63c3a662011-04-26 08:12:10 +00006000 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
Matt Carlson8fc2f992010-12-06 08:28:49 +00006001 !mss && skb->len > VLAN_ETH_FRAME_LEN)
Matt Carlson615774f2009-11-13 13:03:39 +00006002 base_flags |= TXD_FLAG_JMB_PKT;
6003
Alexander Duyckf4188d82009-12-02 16:48:38 +00006004 len = skb_headlen(skb);
6005
6006 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
6007 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07006008 dev_kfree_skb(skb);
6009 goto out_unlock;
6010 }
6011
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006012 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006013 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014
6015 would_hit_hwbug = 0;
6016
Joe Perches63c3a662011-04-26 08:12:10 +00006017 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006018 would_hit_hwbug = 1;
6019
Joe Perches63c3a662011-04-26 08:12:10 +00006020 if (tg3_flag(tp, 4G_DMA_BNDRY_BUG) &&
Matt Carlson0e1406d2009-11-02 12:33:33 +00006021 tg3_4g_overflow_test(mapping, len))
Matt Carlson41588ba2008-04-19 18:12:33 -07006022 would_hit_hwbug = 1;
Matt Carlson0e1406d2009-11-02 12:33:33 +00006023
Joe Perches63c3a662011-04-26 08:12:10 +00006024 if (tg3_flag(tp, 40BIT_DMA_LIMIT_BUG) &&
Matt Carlson0e1406d2009-11-02 12:33:33 +00006025 tg3_40bit_overflow_test(tp, mapping, len))
6026 would_hit_hwbug = 1;
6027
Joe Perches63c3a662011-04-26 08:12:10 +00006028 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07006029 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006031 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
6033
6034 entry = NEXT_TX(entry);
6035
6036 /* Now loop through additional data fragments, and queue them. */
6037 if (skb_shinfo(skb)->nr_frags > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038 last = skb_shinfo(skb)->nr_frags - 1;
6039 for (i = 0; i <= last; i++) {
6040 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6041
6042 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006043 mapping = pci_map_page(tp->pdev,
6044 frag->page,
6045 frag->page_offset,
6046 len, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006048 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006049 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006050 mapping);
6051 if (pci_dma_mapping_error(tp->pdev, mapping))
6052 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053
Joe Perches63c3a662011-04-26 08:12:10 +00006054 if (tg3_flag(tp, SHORT_DMA_BUG) &&
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006055 len <= 8)
6056 would_hit_hwbug = 1;
6057
Joe Perches63c3a662011-04-26 08:12:10 +00006058 if (tg3_flag(tp, 4G_DMA_BNDRY_BUG) &&
Matt Carlson0e1406d2009-11-02 12:33:33 +00006059 tg3_4g_overflow_test(mapping, len))
Michael Chanc58ec932005-09-17 00:46:27 -07006060 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061
Joe Perches63c3a662011-04-26 08:12:10 +00006062 if (tg3_flag(tp, 40BIT_DMA_LIMIT_BUG) &&
Matt Carlson0e1406d2009-11-02 12:33:33 +00006063 tg3_40bit_overflow_test(tp, mapping, len))
Michael Chan72f2afb2006-03-06 19:28:35 -08006064 would_hit_hwbug = 1;
6065
Joe Perches63c3a662011-04-26 08:12:10 +00006066 if (tg3_flag(tp, HW_TSO_1) ||
6067 tg3_flag(tp, HW_TSO_2) ||
6068 tg3_flag(tp, HW_TSO_3))
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006069 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070 base_flags, (i == last)|(mss << 1));
6071 else
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006072 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073 base_flags, (i == last));
6074
6075 entry = NEXT_TX(entry);
6076 }
6077 }
6078
6079 if (would_hit_hwbug) {
Matt Carlson432aa7e2011-05-19 12:12:45 +00006080 tg3_skb_error_unmap(tnapi, skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081
6082 /* If the workaround fails due to memory/mapping
6083 * failure, silently drop this packet.
6084 */
Matt Carlson432aa7e2011-05-19 12:12:45 +00006085 if (tigon3_dma_hwbug_workaround(tnapi, skb, base_flags, mss))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086 goto out_unlock;
6087
Matt Carlson432aa7e2011-05-19 12:12:45 +00006088 entry = NEXT_TX(tnapi->tx_prod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089 }
6090
6091 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006092 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006094 tnapi->tx_prod = entry;
6095 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006096 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006097
6098 /* netif_tx_stop_queue() must be done before checking
6099 * checking tx index in tg3_tx_avail() below, because in
6100 * tg3_tx(), we update tx index before checking for
6101 * netif_tx_queue_stopped().
6102 */
6103 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006104 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006105 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107
6108out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00006109 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110
6111 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006112
6113dma_error:
Matt Carlson432aa7e2011-05-19 12:12:45 +00006114 tg3_skb_error_unmap(tnapi, skb, i);
Alexander Duyckf4188d82009-12-02 16:48:38 +00006115 dev_kfree_skb(skb);
Matt Carlson432aa7e2011-05-19 12:12:45 +00006116 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006117 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118}
6119
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006120static void tg3_set_loopback(struct net_device *dev, u32 features)
6121{
6122 struct tg3 *tp = netdev_priv(dev);
6123
6124 if (features & NETIF_F_LOOPBACK) {
6125 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
6126 return;
6127
6128 /*
6129 * Clear MAC_MODE_HALF_DUPLEX or you won't get packets back in
6130 * loopback mode if Half-Duplex mode was negotiated earlier.
6131 */
6132 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
6133
6134 /* Enable internal MAC loopback mode */
6135 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
6136 spin_lock_bh(&tp->lock);
6137 tw32(MAC_MODE, tp->mac_mode);
6138 netif_carrier_on(tp->dev);
6139 spin_unlock_bh(&tp->lock);
6140 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
6141 } else {
6142 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
6143 return;
6144
6145 /* Disable internal MAC loopback mode */
6146 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
6147 spin_lock_bh(&tp->lock);
6148 tw32(MAC_MODE, tp->mac_mode);
6149 /* Force link status check */
6150 tg3_setup_phy(tp, 1);
6151 spin_unlock_bh(&tp->lock);
6152 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
6153 }
6154}
6155
Michał Mirosławdc668912011-04-07 03:35:07 +00006156static u32 tg3_fix_features(struct net_device *dev, u32 features)
6157{
6158 struct tg3 *tp = netdev_priv(dev);
6159
Joe Perches63c3a662011-04-26 08:12:10 +00006160 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00006161 features &= ~NETIF_F_ALL_TSO;
6162
6163 return features;
6164}
6165
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006166static int tg3_set_features(struct net_device *dev, u32 features)
6167{
6168 u32 changed = dev->features ^ features;
6169
6170 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
6171 tg3_set_loopback(dev, features);
6172
6173 return 0;
6174}
6175
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
6177 int new_mtu)
6178{
6179 dev->mtu = new_mtu;
6180
Michael Chanef7f5ec2005-07-25 12:32:25 -07006181 if (new_mtu > ETH_DATA_LEN) {
Joe Perches63c3a662011-04-26 08:12:10 +00006182 if (tg3_flag(tp, 5780_CLASS)) {
Michał Mirosławdc668912011-04-07 03:35:07 +00006183 netdev_update_features(dev);
Joe Perches63c3a662011-04-26 08:12:10 +00006184 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson859a588792010-04-05 10:19:28 +00006185 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00006186 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Matt Carlson859a588792010-04-05 10:19:28 +00006187 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07006188 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00006189 if (tg3_flag(tp, 5780_CLASS)) {
6190 tg3_flag_set(tp, TSO_CAPABLE);
Michał Mirosławdc668912011-04-07 03:35:07 +00006191 netdev_update_features(dev);
6192 }
Joe Perches63c3a662011-04-26 08:12:10 +00006193 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
Michael Chanef7f5ec2005-07-25 12:32:25 -07006194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195}
6196
6197static int tg3_change_mtu(struct net_device *dev, int new_mtu)
6198{
6199 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07006200 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201
6202 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
6203 return -EINVAL;
6204
6205 if (!netif_running(dev)) {
6206 /* We'll just catch it later when the
6207 * device is up'd.
6208 */
6209 tg3_set_mtu(dev, tp, new_mtu);
6210 return 0;
6211 }
6212
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006213 tg3_phy_stop(tp);
6214
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006216
6217 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218
Michael Chan944d9802005-05-29 14:57:48 -07006219 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220
6221 tg3_set_mtu(dev, tp, new_mtu);
6222
Michael Chanb9ec6c12006-07-25 16:37:27 -07006223 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224
Michael Chanb9ec6c12006-07-25 16:37:27 -07006225 if (!err)
6226 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227
David S. Millerf47c11e2005-06-24 20:18:35 -07006228 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006230 if (!err)
6231 tg3_phy_start(tp);
6232
Michael Chanb9ec6c12006-07-25 16:37:27 -07006233 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234}
6235
Matt Carlson21f581a2009-08-28 14:00:25 +00006236static void tg3_rx_prodring_free(struct tg3 *tp,
6237 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239 int i;
6240
Matt Carlson8fea32b2010-09-15 08:59:58 +00006241 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006242 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006243 i = (i + 1) & tp->rx_std_ring_mask)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006244 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6245 tp->rx_pkt_map_sz);
6246
Joe Perches63c3a662011-04-26 08:12:10 +00006247 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006248 for (i = tpr->rx_jmb_cons_idx;
6249 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006250 i = (i + 1) & tp->rx_jmb_ring_mask) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006251 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6252 TG3_RX_JMB_MAP_SZ);
6253 }
6254 }
6255
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006256 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258
Matt Carlson2c49a442010-09-30 10:34:35 +00006259 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006260 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6261 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262
Joe Perches63c3a662011-04-26 08:12:10 +00006263 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006264 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006265 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6266 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267 }
6268}
6269
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006270/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 *
6272 * The chip has been shut down and the driver detached from
6273 * the networking, so no interrupts or new tx packets will
6274 * end up in the driver. tp->{tx,}lock are held and thus
6275 * we may not sleep.
6276 */
Matt Carlson21f581a2009-08-28 14:00:25 +00006277static int tg3_rx_prodring_alloc(struct tg3 *tp,
6278 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279{
Matt Carlson287be122009-08-28 13:58:46 +00006280 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006282 tpr->rx_std_cons_idx = 0;
6283 tpr->rx_std_prod_idx = 0;
6284 tpr->rx_jmb_cons_idx = 0;
6285 tpr->rx_jmb_prod_idx = 0;
6286
Matt Carlson8fea32b2010-09-15 08:59:58 +00006287 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006288 memset(&tpr->rx_std_buffers[0], 0,
6289 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00006290 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006291 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00006292 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006293 goto done;
6294 }
6295
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00006297 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298
Matt Carlson287be122009-08-28 13:58:46 +00006299 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00006300 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00006301 tp->dev->mtu > ETH_DATA_LEN)
6302 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
6303 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07006304
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305 /* Initialize invariants of the rings, we only set this
6306 * stuff once. This works because the card does not
6307 * write into the rx buffer posting rings.
6308 */
Matt Carlson2c49a442010-09-30 10:34:35 +00006309 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006310 struct tg3_rx_buffer_desc *rxd;
6311
Matt Carlson21f581a2009-08-28 14:00:25 +00006312 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00006313 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
6315 rxd->opaque = (RXD_OPAQUE_RING_STD |
6316 (i << RXD_OPAQUE_INDEX_SHIFT));
6317 }
6318
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006319 /* Now allocate fresh SKBs for each rx ring. */
6320 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson86b21e52009-11-13 13:03:45 +00006321 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006322 netdev_warn(tp->dev,
6323 "Using a smaller RX standard ring. Only "
6324 "%d out of %d buffers were allocated "
6325 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006326 if (i == 0)
6327 goto initfail;
6328 tp->rx_pending = i;
6329 break;
6330 }
6331 }
6332
Joe Perches63c3a662011-04-26 08:12:10 +00006333 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006334 goto done;
6335
Matt Carlson2c49a442010-09-30 10:34:35 +00006336 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006337
Joe Perches63c3a662011-04-26 08:12:10 +00006338 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00006339 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340
Matt Carlson2c49a442010-09-30 10:34:35 +00006341 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00006342 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343
Matt Carlson0d86df82010-02-17 15:17:00 +00006344 rxd = &tpr->rx_jmb[i].std;
6345 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
6346 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
6347 RXD_FLAG_JUMBO;
6348 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
6349 (i << RXD_OPAQUE_INDEX_SHIFT));
6350 }
6351
6352 for (i = 0; i < tp->rx_jumbo_pending; i++) {
6353 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006354 netdev_warn(tp->dev,
6355 "Using a smaller RX jumbo ring. Only %d "
6356 "out of %d buffers were allocated "
6357 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00006358 if (i == 0)
6359 goto initfail;
6360 tp->rx_jumbo_pending = i;
6361 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362 }
6363 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006364
6365done:
Michael Chan32d8c572006-07-25 16:38:29 -07006366 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006367
6368initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00006369 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006370 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371}
6372
Matt Carlson21f581a2009-08-28 14:00:25 +00006373static void tg3_rx_prodring_fini(struct tg3 *tp,
6374 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375{
Matt Carlson21f581a2009-08-28 14:00:25 +00006376 kfree(tpr->rx_std_buffers);
6377 tpr->rx_std_buffers = NULL;
6378 kfree(tpr->rx_jmb_buffers);
6379 tpr->rx_jmb_buffers = NULL;
6380 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006381 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
6382 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006383 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384 }
Matt Carlson21f581a2009-08-28 14:00:25 +00006385 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006386 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
6387 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006388 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006389 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006390}
6391
Matt Carlson21f581a2009-08-28 14:00:25 +00006392static int tg3_rx_prodring_init(struct tg3 *tp,
6393 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006394{
Matt Carlson2c49a442010-09-30 10:34:35 +00006395 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
6396 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006397 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006398 return -ENOMEM;
6399
Matt Carlson4bae65c2010-11-24 08:31:52 +00006400 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
6401 TG3_RX_STD_RING_BYTES(tp),
6402 &tpr->rx_std_mapping,
6403 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006404 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006405 goto err_out;
6406
Joe Perches63c3a662011-04-26 08:12:10 +00006407 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006408 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00006409 GFP_KERNEL);
6410 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006411 goto err_out;
6412
Matt Carlson4bae65c2010-11-24 08:31:52 +00006413 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
6414 TG3_RX_JMB_RING_BYTES(tp),
6415 &tpr->rx_jmb_mapping,
6416 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006417 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006418 goto err_out;
6419 }
6420
6421 return 0;
6422
6423err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00006424 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006425 return -ENOMEM;
6426}
6427
6428/* Free up pending packets in all rx/tx rings.
6429 *
6430 * The chip has been shut down and the driver detached from
6431 * the networking, so no interrupts or new tx packets will
6432 * end up in the driver. tp->{tx,}lock is not held and we are not
6433 * in an interrupt context and thus may sleep.
6434 */
6435static void tg3_free_rings(struct tg3 *tp)
6436{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006437 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006438
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006439 for (j = 0; j < tp->irq_cnt; j++) {
6440 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006441
Matt Carlson8fea32b2010-09-15 08:59:58 +00006442 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00006443
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006444 if (!tnapi->tx_buffers)
6445 continue;
6446
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006447 for (i = 0; i < TG3_TX_RING_SIZE; ) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006448 struct ring_info *txp;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006449 struct sk_buff *skb;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006450 unsigned int k;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006451
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006452 txp = &tnapi->tx_buffers[i];
6453 skb = txp->skb;
6454
6455 if (skb == NULL) {
6456 i++;
6457 continue;
6458 }
6459
Alexander Duyckf4188d82009-12-02 16:48:38 +00006460 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006461 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006462 skb_headlen(skb),
6463 PCI_DMA_TODEVICE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006464 txp->skb = NULL;
6465
Alexander Duyckf4188d82009-12-02 16:48:38 +00006466 i++;
6467
6468 for (k = 0; k < skb_shinfo(skb)->nr_frags; k++) {
6469 txp = &tnapi->tx_buffers[i & (TG3_TX_RING_SIZE - 1)];
6470 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006471 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006472 skb_shinfo(skb)->frags[k].size,
6473 PCI_DMA_TODEVICE);
6474 i++;
6475 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006476
6477 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006478 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006479 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006480}
6481
6482/* Initialize tx/rx rings for packet processing.
6483 *
6484 * The chip has been shut down and the driver detached from
6485 * the networking, so no interrupts or new tx packets will
6486 * end up in the driver. tp->{tx,}lock are held and thus
6487 * we may not sleep.
6488 */
6489static int tg3_init_rings(struct tg3 *tp)
6490{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006491 int i;
Matt Carlson72334482009-08-28 14:03:01 +00006492
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006493 /* Free up all the SKBs. */
6494 tg3_free_rings(tp);
6495
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006496 for (i = 0; i < tp->irq_cnt; i++) {
6497 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006498
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006499 tnapi->last_tag = 0;
6500 tnapi->last_irq_tag = 0;
6501 tnapi->hw_status->status = 0;
6502 tnapi->hw_status->status_tag = 0;
6503 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6504
6505 tnapi->tx_prod = 0;
6506 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006507 if (tnapi->tx_ring)
6508 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006509
6510 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006511 if (tnapi->rx_rcb)
6512 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006513
Matt Carlson8fea32b2010-09-15 08:59:58 +00006514 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00006515 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006516 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006517 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006518 }
Matt Carlson72334482009-08-28 14:03:01 +00006519
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006520 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006521}
6522
6523/*
6524 * Must not be invoked with interrupt sources disabled and
6525 * the hardware shutdown down.
6526 */
6527static void tg3_free_consistent(struct tg3 *tp)
6528{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006529 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006530
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006531 for (i = 0; i < tp->irq_cnt; i++) {
6532 struct tg3_napi *tnapi = &tp->napi[i];
6533
6534 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006535 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006536 tnapi->tx_ring, tnapi->tx_desc_mapping);
6537 tnapi->tx_ring = NULL;
6538 }
6539
6540 kfree(tnapi->tx_buffers);
6541 tnapi->tx_buffers = NULL;
6542
6543 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006544 dma_free_coherent(&tp->pdev->dev,
6545 TG3_RX_RCB_RING_BYTES(tp),
6546 tnapi->rx_rcb,
6547 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006548 tnapi->rx_rcb = NULL;
6549 }
6550
Matt Carlson8fea32b2010-09-15 08:59:58 +00006551 tg3_rx_prodring_fini(tp, &tnapi->prodring);
6552
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006553 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006554 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
6555 tnapi->hw_status,
6556 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006557 tnapi->hw_status = NULL;
6558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006559 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006560
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006562 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
6563 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006564 tp->hw_stats = NULL;
6565 }
6566}
6567
6568/*
6569 * Must not be invoked with interrupt sources disabled and
6570 * the hardware shutdown down. Can sleep.
6571 */
6572static int tg3_alloc_consistent(struct tg3 *tp)
6573{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006574 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006575
Matt Carlson4bae65c2010-11-24 08:31:52 +00006576 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
6577 sizeof(struct tg3_hw_stats),
6578 &tp->stats_mapping,
6579 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580 if (!tp->hw_stats)
6581 goto err_out;
6582
Linus Torvalds1da177e2005-04-16 15:20:36 -07006583 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6584
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006585 for (i = 0; i < tp->irq_cnt; i++) {
6586 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006587 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006588
Matt Carlson4bae65c2010-11-24 08:31:52 +00006589 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
6590 TG3_HW_STATUS_SIZE,
6591 &tnapi->status_mapping,
6592 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006593 if (!tnapi->hw_status)
6594 goto err_out;
6595
6596 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006597 sblk = tnapi->hw_status;
6598
Matt Carlson8fea32b2010-09-15 08:59:58 +00006599 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
6600 goto err_out;
6601
Matt Carlson19cfaec2009-12-03 08:36:20 +00006602 /* If multivector TSS is enabled, vector 0 does not handle
6603 * tx interrupts. Don't allocate any resources for it.
6604 */
Joe Perches63c3a662011-04-26 08:12:10 +00006605 if ((!i && !tg3_flag(tp, ENABLE_TSS)) ||
6606 (i && tg3_flag(tp, ENABLE_TSS))) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00006607 tnapi->tx_buffers = kzalloc(sizeof(struct ring_info) *
6608 TG3_TX_RING_SIZE,
6609 GFP_KERNEL);
6610 if (!tnapi->tx_buffers)
6611 goto err_out;
6612
Matt Carlson4bae65c2010-11-24 08:31:52 +00006613 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
6614 TG3_TX_RING_BYTES,
6615 &tnapi->tx_desc_mapping,
6616 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00006617 if (!tnapi->tx_ring)
6618 goto err_out;
6619 }
6620
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006621 /*
6622 * When RSS is enabled, the status block format changes
6623 * slightly. The "rx_jumbo_consumer", "reserved",
6624 * and "rx_mini_consumer" members get mapped to the
6625 * other three rx return ring producer indexes.
6626 */
6627 switch (i) {
6628 default:
6629 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
6630 break;
6631 case 2:
6632 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
6633 break;
6634 case 3:
6635 tnapi->rx_rcb_prod_idx = &sblk->reserved;
6636 break;
6637 case 4:
6638 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
6639 break;
6640 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006641
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006642 /*
6643 * If multivector RSS is enabled, vector 0 does not handle
6644 * rx or tx interrupts. Don't allocate any resources for it.
6645 */
Joe Perches63c3a662011-04-26 08:12:10 +00006646 if (!i && tg3_flag(tp, ENABLE_RSS))
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006647 continue;
6648
Matt Carlson4bae65c2010-11-24 08:31:52 +00006649 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
6650 TG3_RX_RCB_RING_BYTES(tp),
6651 &tnapi->rx_rcb_mapping,
6652 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006653 if (!tnapi->rx_rcb)
6654 goto err_out;
6655
6656 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006657 }
6658
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659 return 0;
6660
6661err_out:
6662 tg3_free_consistent(tp);
6663 return -ENOMEM;
6664}
6665
6666#define MAX_WAIT_CNT 1000
6667
6668/* To stop a block, clear the enable bit and poll till it
6669 * clears. tp->lock is held.
6670 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006671static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672{
6673 unsigned int i;
6674 u32 val;
6675
Joe Perches63c3a662011-04-26 08:12:10 +00006676 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006677 switch (ofs) {
6678 case RCVLSC_MODE:
6679 case DMAC_MODE:
6680 case MBFREE_MODE:
6681 case BUFMGR_MODE:
6682 case MEMARB_MODE:
6683 /* We can't enable/disable these bits of the
6684 * 5705/5750, just say success.
6685 */
6686 return 0;
6687
6688 default:
6689 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006691 }
6692
6693 val = tr32(ofs);
6694 val &= ~enable_bit;
6695 tw32_f(ofs, val);
6696
6697 for (i = 0; i < MAX_WAIT_CNT; i++) {
6698 udelay(100);
6699 val = tr32(ofs);
6700 if ((val & enable_bit) == 0)
6701 break;
6702 }
6703
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006704 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00006705 dev_err(&tp->pdev->dev,
6706 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
6707 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708 return -ENODEV;
6709 }
6710
6711 return 0;
6712}
6713
6714/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006715static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716{
6717 int i, err;
6718
6719 tg3_disable_ints(tp);
6720
6721 tp->rx_mode &= ~RX_MODE_ENABLE;
6722 tw32_f(MAC_RX_MODE, tp->rx_mode);
6723 udelay(10);
6724
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006725 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
6726 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
6727 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
6728 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
6729 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
6730 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006731
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006732 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
6733 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
6734 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
6735 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
6736 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
6737 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
6738 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006739
6740 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
6741 tw32_f(MAC_MODE, tp->mac_mode);
6742 udelay(40);
6743
6744 tp->tx_mode &= ~TX_MODE_ENABLE;
6745 tw32_f(MAC_TX_MODE, tp->tx_mode);
6746
6747 for (i = 0; i < MAX_WAIT_CNT; i++) {
6748 udelay(100);
6749 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
6750 break;
6751 }
6752 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00006753 dev_err(&tp->pdev->dev,
6754 "%s timed out, TX_MODE_ENABLE will not clear "
6755 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07006756 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006757 }
6758
Michael Chane6de8ad2005-05-05 14:42:41 -07006759 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006760 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
6761 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762
6763 tw32(FTQ_RESET, 0xffffffff);
6764 tw32(FTQ_RESET, 0x00000000);
6765
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006766 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
6767 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006769 for (i = 0; i < tp->irq_cnt; i++) {
6770 struct tg3_napi *tnapi = &tp->napi[i];
6771 if (tnapi->hw_status)
6772 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774 if (tp->hw_stats)
6775 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6776
Linus Torvalds1da177e2005-04-16 15:20:36 -07006777 return err;
6778}
6779
Matt Carlson0d3031d2007-10-10 18:02:43 -07006780static void tg3_ape_send_event(struct tg3 *tp, u32 event)
6781{
6782 int i;
6783 u32 apedata;
6784
Matt Carlsondc6d0742010-09-15 08:59:55 +00006785 /* NCSI does not support APE events */
Joe Perches63c3a662011-04-26 08:12:10 +00006786 if (tg3_flag(tp, APE_HAS_NCSI))
Matt Carlsondc6d0742010-09-15 08:59:55 +00006787 return;
6788
Matt Carlson0d3031d2007-10-10 18:02:43 -07006789 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
6790 if (apedata != APE_SEG_SIG_MAGIC)
6791 return;
6792
6793 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07006794 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006795 return;
6796
6797 /* Wait for up to 1 millisecond for APE to service previous event. */
6798 for (i = 0; i < 10; i++) {
6799 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
6800 return;
6801
6802 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
6803
6804 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6805 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
6806 event | APE_EVENT_STATUS_EVENT_PENDING);
6807
6808 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
6809
6810 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6811 break;
6812
6813 udelay(100);
6814 }
6815
6816 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6817 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
6818}
6819
6820static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
6821{
6822 u32 event;
6823 u32 apedata;
6824
Joe Perches63c3a662011-04-26 08:12:10 +00006825 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006826 return;
6827
6828 switch (kind) {
Matt Carlson33f401a2010-04-05 10:19:27 +00006829 case RESET_KIND_INIT:
6830 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
6831 APE_HOST_SEG_SIG_MAGIC);
6832 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
6833 APE_HOST_SEG_LEN_MAGIC);
6834 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
6835 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
6836 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
Matt Carlson6867c842010-07-11 09:31:44 +00006837 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
Matt Carlson33f401a2010-04-05 10:19:27 +00006838 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
6839 APE_HOST_BEHAV_NO_PHYLOCK);
Matt Carlsondc6d0742010-09-15 08:59:55 +00006840 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
6841 TG3_APE_HOST_DRVR_STATE_START);
Matt Carlson0d3031d2007-10-10 18:02:43 -07006842
Matt Carlson33f401a2010-04-05 10:19:27 +00006843 event = APE_EVENT_STATUS_STATE_START;
6844 break;
6845 case RESET_KIND_SHUTDOWN:
6846 /* With the interface we are currently using,
6847 * APE does not track driver state. Wiping
6848 * out the HOST SEGMENT SIGNATURE forces
6849 * the APE to assume OS absent status.
6850 */
6851 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
Matt Carlsonb2aee152008-11-03 16:51:11 -08006852
Matt Carlsondc6d0742010-09-15 08:59:55 +00006853 if (device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00006854 tg3_flag(tp, WOL_ENABLE)) {
Matt Carlsondc6d0742010-09-15 08:59:55 +00006855 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
6856 TG3_APE_HOST_WOL_SPEED_AUTO);
6857 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
6858 } else
6859 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
6860
6861 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
6862
Matt Carlson33f401a2010-04-05 10:19:27 +00006863 event = APE_EVENT_STATUS_STATE_UNLOAD;
6864 break;
6865 case RESET_KIND_SUSPEND:
6866 event = APE_EVENT_STATUS_STATE_SUSPEND;
6867 break;
6868 default:
6869 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006870 }
6871
6872 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
6873
6874 tg3_ape_send_event(tp, event);
6875}
6876
Michael Chane6af3012005-04-21 17:12:05 -07006877/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
6879{
David S. Millerf49639e2006-06-09 11:58:36 -07006880 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
6881 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006882
Joe Perches63c3a662011-04-26 08:12:10 +00006883 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006884 switch (kind) {
6885 case RESET_KIND_INIT:
6886 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6887 DRV_STATE_START);
6888 break;
6889
6890 case RESET_KIND_SHUTDOWN:
6891 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6892 DRV_STATE_UNLOAD);
6893 break;
6894
6895 case RESET_KIND_SUSPEND:
6896 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6897 DRV_STATE_SUSPEND);
6898 break;
6899
6900 default:
6901 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006904
6905 if (kind == RESET_KIND_INIT ||
6906 kind == RESET_KIND_SUSPEND)
6907 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908}
6909
6910/* tp->lock is held. */
6911static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
6912{
Joe Perches63c3a662011-04-26 08:12:10 +00006913 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914 switch (kind) {
6915 case RESET_KIND_INIT:
6916 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6917 DRV_STATE_START_DONE);
6918 break;
6919
6920 case RESET_KIND_SHUTDOWN:
6921 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6922 DRV_STATE_UNLOAD_DONE);
6923 break;
6924
6925 default:
6926 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006928 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006929
6930 if (kind == RESET_KIND_SHUTDOWN)
6931 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006932}
6933
6934/* tp->lock is held. */
6935static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
6936{
Joe Perches63c3a662011-04-26 08:12:10 +00006937 if (tg3_flag(tp, ENABLE_ASF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006938 switch (kind) {
6939 case RESET_KIND_INIT:
6940 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6941 DRV_STATE_START);
6942 break;
6943
6944 case RESET_KIND_SHUTDOWN:
6945 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6946 DRV_STATE_UNLOAD);
6947 break;
6948
6949 case RESET_KIND_SUSPEND:
6950 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6951 DRV_STATE_SUSPEND);
6952 break;
6953
6954 default:
6955 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006957 }
6958}
6959
Michael Chan7a6f4362006-09-27 16:03:31 -07006960static int tg3_poll_fw(struct tg3 *tp)
6961{
6962 int i;
6963 u32 val;
6964
Michael Chanb5d37722006-09-27 16:06:21 -07006965 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08006966 /* Wait up to 20ms for init done. */
6967 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07006968 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
6969 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08006970 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07006971 }
6972 return -ENODEV;
6973 }
6974
Michael Chan7a6f4362006-09-27 16:03:31 -07006975 /* Wait for firmware initialization to complete. */
6976 for (i = 0; i < 100000; i++) {
6977 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
6978 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
6979 break;
6980 udelay(10);
6981 }
6982
6983 /* Chip might not be fitted with firmware. Some Sun onboard
6984 * parts are configured like that. So don't signal the timeout
6985 * of the above loop as an error, but do report the lack of
6986 * running firmware once.
6987 */
Joe Perches63c3a662011-04-26 08:12:10 +00006988 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
6989 tg3_flag_set(tp, NO_FWARE_REPORTED);
Michael Chan7a6f4362006-09-27 16:03:31 -07006990
Joe Perches05dbe002010-02-17 19:44:19 +00006991 netdev_info(tp->dev, "No firmware running\n");
Michael Chan7a6f4362006-09-27 16:03:31 -07006992 }
6993
Matt Carlson6b10c162010-02-12 14:47:08 +00006994 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
6995 /* The 57765 A0 needs a little more
6996 * time to do some important work.
6997 */
6998 mdelay(10);
6999 }
7000
Michael Chan7a6f4362006-09-27 16:03:31 -07007001 return 0;
7002}
7003
Michael Chanee6a99b2007-07-18 21:49:10 -07007004/* Save PCI command register before chip reset */
7005static void tg3_save_pci_state(struct tg3 *tp)
7006{
Matt Carlson8a6eac92007-10-21 16:17:55 -07007007 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007008}
7009
7010/* Restore PCI state after chip reset */
7011static void tg3_restore_pci_state(struct tg3 *tp)
7012{
7013 u32 val;
7014
7015 /* Re-enable indirect register accesses. */
7016 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7017 tp->misc_host_ctrl);
7018
7019 /* Set MAX PCI retry to zero. */
7020 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7021 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007022 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07007023 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007024 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00007025 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007026 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00007027 PCISTATE_ALLOW_APE_SHMEM_WR |
7028 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07007029 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7030
Matt Carlson8a6eac92007-10-21 16:17:55 -07007031 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007032
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007033 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
Joe Perches63c3a662011-04-26 08:12:10 +00007034 if (tg3_flag(tp, PCI_EXPRESS))
Matt Carlsoncf790032010-11-24 08:31:48 +00007035 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007036 else {
7037 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7038 tp->pci_cacheline_sz);
7039 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7040 tp->pci_lat_timer);
7041 }
Michael Chan114342f2007-10-15 02:12:26 -07007042 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08007043
Michael Chanee6a99b2007-07-18 21:49:10 -07007044 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00007045 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07007046 u16 pcix_cmd;
7047
7048 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7049 &pcix_cmd);
7050 pcix_cmd &= ~PCI_X_CMD_ERO;
7051 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7052 pcix_cmd);
7053 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007054
Joe Perches63c3a662011-04-26 08:12:10 +00007055 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007056
7057 /* Chip reset on 5780 will reset MSI enable bit,
7058 * so need to restore it.
7059 */
Joe Perches63c3a662011-04-26 08:12:10 +00007060 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007061 u16 ctrl;
7062
7063 pci_read_config_word(tp->pdev,
7064 tp->msi_cap + PCI_MSI_FLAGS,
7065 &ctrl);
7066 pci_write_config_word(tp->pdev,
7067 tp->msi_cap + PCI_MSI_FLAGS,
7068 ctrl | PCI_MSI_FLAGS_ENABLE);
7069 val = tr32(MSGINT_MODE);
7070 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7071 }
7072 }
7073}
7074
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075static void tg3_stop_fw(struct tg3 *);
7076
7077/* tp->lock is held. */
7078static int tg3_chip_reset(struct tg3 *tp)
7079{
7080 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007081 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007082 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007083
David S. Millerf49639e2006-06-09 11:58:36 -07007084 tg3_nvram_lock(tp);
7085
Matt Carlson77b483f2008-08-15 14:07:24 -07007086 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7087
David S. Millerf49639e2006-06-09 11:58:36 -07007088 /* No matching tg3_nvram_unlock() after this because
7089 * chip reset below will undo the nvram lock.
7090 */
7091 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092
Michael Chanee6a99b2007-07-18 21:49:10 -07007093 /* GRC_MISC_CFG core clock reset will clear the memory
7094 * enable bit in PCI register 4 and the MSI enable bit
7095 * on some chips, so we save relevant registers here.
7096 */
7097 tg3_save_pci_state(tp);
7098
Michael Chand9ab5ad2006-03-20 22:27:35 -08007099 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00007100 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08007101 tw32(GRC_FASTBOOT_PC, 0);
7102
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103 /*
7104 * We must avoid the readl() that normally takes place.
7105 * It locks machines, causes machine checks, and other
7106 * fun things. So, temporarily disable the 5701
7107 * hardware workaround, while we do the reset.
7108 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007109 write_op = tp->write32;
7110 if (write_op == tg3_write_flush_reg32)
7111 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007112
Michael Chand18edcb2007-03-24 20:57:11 -07007113 /* Prevent the irq handler from reading or writing PCI registers
7114 * during chip reset when the memory enable bit in the PCI command
7115 * register may be cleared. The chip does not generate interrupt
7116 * at this time, but the irq handler may still be called due to irq
7117 * sharing or irqpoll.
7118 */
Joe Perches63c3a662011-04-26 08:12:10 +00007119 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007120 for (i = 0; i < tp->irq_cnt; i++) {
7121 struct tg3_napi *tnapi = &tp->napi[i];
7122 if (tnapi->hw_status) {
7123 tnapi->hw_status->status = 0;
7124 tnapi->hw_status->status_tag = 0;
7125 }
7126 tnapi->last_tag = 0;
7127 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007128 }
Michael Chand18edcb2007-03-24 20:57:11 -07007129 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007130
7131 for (i = 0; i < tp->irq_cnt; i++)
7132 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007133
Matt Carlson255ca312009-08-25 10:07:27 +00007134 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7135 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7136 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7137 }
7138
Linus Torvalds1da177e2005-04-16 15:20:36 -07007139 /* do the reset */
7140 val = GRC_MISC_CFG_CORECLK_RESET;
7141
Joe Perches63c3a662011-04-26 08:12:10 +00007142 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00007143 /* Force PCIe 1.0a mode */
7144 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007145 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00007146 tr32(TG3_PCIE_PHY_TSTCTL) ==
7147 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7148 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7149
Linus Torvalds1da177e2005-04-16 15:20:36 -07007150 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7151 tw32(GRC_MISC_CFG, (1 << 29));
7152 val |= (1 << 29);
7153 }
7154 }
7155
Michael Chanb5d37722006-09-27 16:06:21 -07007156 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7157 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7158 tw32(GRC_VCPU_EXT_CTRL,
7159 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7160 }
7161
Matt Carlsonf37500d2010-08-02 11:25:59 +00007162 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00007163 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007164 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007165
Linus Torvalds1da177e2005-04-16 15:20:36 -07007166 tw32(GRC_MISC_CFG, val);
7167
Michael Chan1ee582d2005-08-09 20:16:46 -07007168 /* restore 5701 hardware bug workaround write method */
7169 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007170
7171 /* Unfortunately, we have to delay before the PCI read back.
7172 * Some 575X chips even will not respond to a PCI cfg access
7173 * when the reset command is given to the chip.
7174 *
7175 * How do these hardware designers expect things to work
7176 * properly if the PCI write is posted for a long period
7177 * of time? It is always necessary to have some method by
7178 * which a register read back can occur to push the write
7179 * out which does the reset.
7180 *
7181 * For most tg3 variants the trick below was working.
7182 * Ho hum...
7183 */
7184 udelay(120);
7185
7186 /* Flush PCI posted writes. The normal MMIO registers
7187 * are inaccessible at this time so this is the only
7188 * way to make this reliably (actually, this is no longer
7189 * the case, see above). I tried to use indirect
7190 * register read/write but this upset some 5701 variants.
7191 */
7192 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7193
7194 udelay(120);
7195
Joe Perches63c3a662011-04-26 08:12:10 +00007196 if (tg3_flag(tp, PCI_EXPRESS) && tp->pcie_cap) {
Matt Carlsone7126992009-08-25 10:08:16 +00007197 u16 val16;
7198
Linus Torvalds1da177e2005-04-16 15:20:36 -07007199 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7200 int i;
7201 u32 cfg_val;
7202
7203 /* Wait for link training to complete. */
7204 for (i = 0; i < 5000; i++)
7205 udelay(100);
7206
7207 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7208 pci_write_config_dword(tp->pdev, 0xc4,
7209 cfg_val | (1 << 15));
7210 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007211
Matt Carlsone7126992009-08-25 10:08:16 +00007212 /* Clear the "no snoop" and "relaxed ordering" bits. */
7213 pci_read_config_word(tp->pdev,
7214 tp->pcie_cap + PCI_EXP_DEVCTL,
7215 &val16);
7216 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7217 PCI_EXP_DEVCTL_NOSNOOP_EN);
7218 /*
7219 * Older PCIe devices only support the 128 byte
7220 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007221 */
Joe Perches63c3a662011-04-26 08:12:10 +00007222 if (!tg3_flag(tp, CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007223 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007224 pci_write_config_word(tp->pdev,
7225 tp->pcie_cap + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007226 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007227
Matt Carlsoncf790032010-11-24 08:31:48 +00007228 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007229
7230 /* Clear error status */
7231 pci_write_config_word(tp->pdev,
7232 tp->pcie_cap + PCI_EXP_DEVSTA,
7233 PCI_EXP_DEVSTA_CED |
7234 PCI_EXP_DEVSTA_NFED |
7235 PCI_EXP_DEVSTA_FED |
7236 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007237 }
7238
Michael Chanee6a99b2007-07-18 21:49:10 -07007239 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007240
Joe Perches63c3a662011-04-26 08:12:10 +00007241 tg3_flag_clear(tp, CHIP_RESETTING);
7242 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07007243
Michael Chanee6a99b2007-07-18 21:49:10 -07007244 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007245 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07007246 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007247 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007248
7249 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7250 tg3_stop_fw(tp);
7251 tw32(0x5000, 0x400);
7252 }
7253
7254 tw32(GRC_MODE, tp->grc_mode);
7255
7256 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007257 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007258
7259 tw32(0xc4, val | (1 << 15));
7260 }
7261
7262 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7263 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7264 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7265 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7266 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7267 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7268 }
7269
Joe Perches63c3a662011-04-26 08:12:10 +00007270 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007271 tp->mac_mode = MAC_MODE_APE_TX_EN |
7272 MAC_MODE_APE_RX_EN |
7273 MAC_MODE_TDE_ENABLE;
7274
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007275 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007276 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
7277 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007278 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007279 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7280 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007281 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007282 val = 0;
7283
7284 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007285 udelay(40);
7286
Matt Carlson77b483f2008-08-15 14:07:24 -07007287 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7288
Michael Chan7a6f4362006-09-27 16:03:31 -07007289 err = tg3_poll_fw(tp);
7290 if (err)
7291 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007292
Matt Carlson0a9140c2009-08-28 12:27:50 +00007293 tg3_mdio_start(tp);
7294
Joe Perches63c3a662011-04-26 08:12:10 +00007295 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007296 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7297 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007298 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007299 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007300
7301 tw32(0x7c00, val | (1 << 25));
7302 }
7303
Matt Carlsond78b59f2011-04-05 14:22:46 +00007304 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
7305 val = tr32(TG3_CPMU_CLCK_ORIDE);
7306 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
7307 }
7308
Linus Torvalds1da177e2005-04-16 15:20:36 -07007309 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00007310 tg3_flag_clear(tp, ENABLE_ASF);
7311 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007312 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7313 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7314 u32 nic_cfg;
7315
7316 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7317 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00007318 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007319 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00007320 if (tg3_flag(tp, 5750_PLUS))
7321 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007322 }
7323 }
7324
7325 return 0;
7326}
7327
7328/* tp->lock is held. */
7329static void tg3_stop_fw(struct tg3 *tp)
7330{
Joe Perches63c3a662011-04-26 08:12:10 +00007331 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07007332 /* Wait for RX cpu to ACK the previous event. */
7333 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007334
7335 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007336
7337 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007338
Matt Carlson7c5026a2008-05-02 16:49:29 -07007339 /* Wait for RX cpu to ACK this event. */
7340 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007341 }
7342}
7343
7344/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007345static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007346{
7347 int err;
7348
7349 tg3_stop_fw(tp);
7350
Michael Chan944d9802005-05-29 14:57:48 -07007351 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007352
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007353 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007354 err = tg3_chip_reset(tp);
7355
Matt Carlsondaba2a62009-04-20 06:58:52 +00007356 __tg3_set_mac_addr(tp, 0);
7357
Michael Chan944d9802005-05-29 14:57:48 -07007358 tg3_write_sig_legacy(tp, kind);
7359 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007360
7361 if (err)
7362 return err;
7363
7364 return 0;
7365}
7366
Linus Torvalds1da177e2005-04-16 15:20:36 -07007367#define RX_CPU_SCRATCH_BASE 0x30000
7368#define RX_CPU_SCRATCH_SIZE 0x04000
7369#define TX_CPU_SCRATCH_BASE 0x34000
7370#define TX_CPU_SCRATCH_SIZE 0x04000
7371
7372/* tp->lock is held. */
7373static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
7374{
7375 int i;
7376
Joe Perches63c3a662011-04-26 08:12:10 +00007377 BUG_ON(offset == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007378
Michael Chanb5d37722006-09-27 16:06:21 -07007379 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7380 u32 val = tr32(GRC_VCPU_EXT_CTRL);
7381
7382 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
7383 return 0;
7384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007385 if (offset == RX_CPU_BASE) {
7386 for (i = 0; i < 10000; i++) {
7387 tw32(offset + CPU_STATE, 0xffffffff);
7388 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7389 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7390 break;
7391 }
7392
7393 tw32(offset + CPU_STATE, 0xffffffff);
7394 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
7395 udelay(10);
7396 } else {
7397 for (i = 0; i < 10000; i++) {
7398 tw32(offset + CPU_STATE, 0xffffffff);
7399 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7400 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7401 break;
7402 }
7403 }
7404
7405 if (i >= 10000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007406 netdev_err(tp->dev, "%s timed out, %s CPU\n",
7407 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007408 return -ENODEV;
7409 }
Michael Chanec41c7d2006-01-17 02:40:55 -08007410
7411 /* Clear firmware's nvram arbitration. */
Joe Perches63c3a662011-04-26 08:12:10 +00007412 if (tg3_flag(tp, NVRAM))
Michael Chanec41c7d2006-01-17 02:40:55 -08007413 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007414 return 0;
7415}
7416
7417struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007418 unsigned int fw_base;
7419 unsigned int fw_len;
7420 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007421};
7422
7423/* tp->lock is held. */
7424static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
7425 int cpu_scratch_size, struct fw_info *info)
7426{
Michael Chanec41c7d2006-01-17 02:40:55 -08007427 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007428 void (*write_op)(struct tg3 *, u32, u32);
7429
Joe Perches63c3a662011-04-26 08:12:10 +00007430 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007431 netdev_err(tp->dev,
7432 "%s: Trying to load TX cpu firmware which is 5705\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007433 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007434 return -EINVAL;
7435 }
7436
Joe Perches63c3a662011-04-26 08:12:10 +00007437 if (tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007438 write_op = tg3_write_mem;
7439 else
7440 write_op = tg3_write_indirect_reg32;
7441
Michael Chan1b628152005-05-29 14:59:49 -07007442 /* It is possible that bootcode is still loading at this point.
7443 * Get the nvram lock first before halting the cpu.
7444 */
Michael Chanec41c7d2006-01-17 02:40:55 -08007445 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007446 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08007447 if (!lock_err)
7448 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007449 if (err)
7450 goto out;
7451
7452 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
7453 write_op(tp, cpu_scratch_base + i, 0);
7454 tw32(cpu_base + CPU_STATE, 0xffffffff);
7455 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007456 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007457 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007458 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07007459 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007460 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007461
7462 err = 0;
7463
7464out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007465 return err;
7466}
7467
7468/* tp->lock is held. */
7469static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
7470{
7471 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007472 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007473 int err, i;
7474
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007475 fw_data = (void *)tp->fw->data;
7476
7477 /* Firmware blob starts with version numbers, followed by
7478 start address and length. We are setting complete length.
7479 length = end_address_of_bss - start_address_of_text.
7480 Remainder is the blob to be loaded contiguously
7481 from start address. */
7482
7483 info.fw_base = be32_to_cpu(fw_data[1]);
7484 info.fw_len = tp->fw->size - 12;
7485 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007486
7487 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
7488 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
7489 &info);
7490 if (err)
7491 return err;
7492
7493 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
7494 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
7495 &info);
7496 if (err)
7497 return err;
7498
7499 /* Now startup only the RX cpu. */
7500 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007501 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007502
7503 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007504 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007505 break;
7506 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7507 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007508 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007509 udelay(1000);
7510 }
7511 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007512 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
7513 "should be %08x\n", __func__,
Joe Perches05dbe002010-02-17 19:44:19 +00007514 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007515 return -ENODEV;
7516 }
7517 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7518 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
7519
7520 return 0;
7521}
7522
Linus Torvalds1da177e2005-04-16 15:20:36 -07007523/* tp->lock is held. */
7524static int tg3_load_tso_firmware(struct tg3 *tp)
7525{
7526 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007527 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007528 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
7529 int err, i;
7530
Joe Perches63c3a662011-04-26 08:12:10 +00007531 if (tg3_flag(tp, HW_TSO_1) ||
7532 tg3_flag(tp, HW_TSO_2) ||
7533 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007534 return 0;
7535
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007536 fw_data = (void *)tp->fw->data;
7537
7538 /* Firmware blob starts with version numbers, followed by
7539 start address and length. We are setting complete length.
7540 length = end_address_of_bss - start_address_of_text.
7541 Remainder is the blob to be loaded contiguously
7542 from start address. */
7543
7544 info.fw_base = be32_to_cpu(fw_data[1]);
7545 cpu_scratch_size = tp->fw_len;
7546 info.fw_len = tp->fw->size - 12;
7547 info.fw_data = &fw_data[3];
7548
Linus Torvalds1da177e2005-04-16 15:20:36 -07007549 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007550 cpu_base = RX_CPU_BASE;
7551 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007552 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007553 cpu_base = TX_CPU_BASE;
7554 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
7555 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
7556 }
7557
7558 err = tg3_load_firmware_cpu(tp, cpu_base,
7559 cpu_scratch_base, cpu_scratch_size,
7560 &info);
7561 if (err)
7562 return err;
7563
7564 /* Now startup the cpu. */
7565 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007566 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007567
7568 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007569 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007570 break;
7571 tw32(cpu_base + CPU_STATE, 0xffffffff);
7572 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007573 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007574 udelay(1000);
7575 }
7576 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007577 netdev_err(tp->dev,
7578 "%s fails to set CPU PC, is %08x should be %08x\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007579 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007580 return -ENODEV;
7581 }
7582 tw32(cpu_base + CPU_STATE, 0xffffffff);
7583 tw32_f(cpu_base + CPU_MODE, 0x00000000);
7584 return 0;
7585}
7586
Linus Torvalds1da177e2005-04-16 15:20:36 -07007587
Linus Torvalds1da177e2005-04-16 15:20:36 -07007588static int tg3_set_mac_addr(struct net_device *dev, void *p)
7589{
7590 struct tg3 *tp = netdev_priv(dev);
7591 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007592 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007593
Michael Chanf9804dd2005-09-27 12:13:10 -07007594 if (!is_valid_ether_addr(addr->sa_data))
7595 return -EINVAL;
7596
Linus Torvalds1da177e2005-04-16 15:20:36 -07007597 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7598
Michael Chane75f7c92006-03-20 21:33:26 -08007599 if (!netif_running(dev))
7600 return 0;
7601
Joe Perches63c3a662011-04-26 08:12:10 +00007602 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007603 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007604
Michael Chan986e0ae2007-05-05 12:10:20 -07007605 addr0_high = tr32(MAC_ADDR_0_HIGH);
7606 addr0_low = tr32(MAC_ADDR_0_LOW);
7607 addr1_high = tr32(MAC_ADDR_1_HIGH);
7608 addr1_low = tr32(MAC_ADDR_1_LOW);
7609
7610 /* Skip MAC addr 1 if ASF is using it. */
7611 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7612 !(addr1_high == 0 && addr1_low == 0))
7613 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007614 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007615 spin_lock_bh(&tp->lock);
7616 __tg3_set_mac_addr(tp, skip_mac_1);
7617 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007618
Michael Chanb9ec6c12006-07-25 16:37:27 -07007619 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007620}
7621
7622/* tp->lock is held. */
7623static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7624 dma_addr_t mapping, u32 maxlen_flags,
7625 u32 nic_addr)
7626{
7627 tg3_write_mem(tp,
7628 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7629 ((u64) mapping >> 32));
7630 tg3_write_mem(tp,
7631 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7632 ((u64) mapping & 0xffffffff));
7633 tg3_write_mem(tp,
7634 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7635 maxlen_flags);
7636
Joe Perches63c3a662011-04-26 08:12:10 +00007637 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007638 tg3_write_mem(tp,
7639 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7640 nic_addr);
7641}
7642
7643static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007644static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007645{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007646 int i;
7647
Joe Perches63c3a662011-04-26 08:12:10 +00007648 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007649 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7650 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7651 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007652 } else {
7653 tw32(HOSTCC_TXCOL_TICKS, 0);
7654 tw32(HOSTCC_TXMAX_FRAMES, 0);
7655 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007656 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007657
Joe Perches63c3a662011-04-26 08:12:10 +00007658 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007659 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
7660 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
7661 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
7662 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007663 tw32(HOSTCC_RXCOL_TICKS, 0);
7664 tw32(HOSTCC_RXMAX_FRAMES, 0);
7665 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07007666 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007667
Joe Perches63c3a662011-04-26 08:12:10 +00007668 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07007669 u32 val = ec->stats_block_coalesce_usecs;
7670
Matt Carlsonb6080e12009-09-01 13:12:00 +00007671 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
7672 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
7673
David S. Miller15f98502005-05-18 22:49:26 -07007674 if (!netif_carrier_ok(tp->dev))
7675 val = 0;
7676
7677 tw32(HOSTCC_STAT_COAL_TICKS, val);
7678 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007679
7680 for (i = 0; i < tp->irq_cnt - 1; i++) {
7681 u32 reg;
7682
7683 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
7684 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007685 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
7686 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007687 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
7688 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007689
Joe Perches63c3a662011-04-26 08:12:10 +00007690 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007691 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
7692 tw32(reg, ec->tx_coalesce_usecs);
7693 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
7694 tw32(reg, ec->tx_max_coalesced_frames);
7695 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
7696 tw32(reg, ec->tx_max_coalesced_frames_irq);
7697 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007698 }
7699
7700 for (; i < tp->irq_max - 1; i++) {
7701 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007702 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007703 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007704
Joe Perches63c3a662011-04-26 08:12:10 +00007705 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007706 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
7707 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
7708 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7709 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007710 }
David S. Miller15f98502005-05-18 22:49:26 -07007711}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007712
7713/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00007714static void tg3_rings_reset(struct tg3 *tp)
7715{
7716 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007717 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007718 struct tg3_napi *tnapi = &tp->napi[0];
7719
7720 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00007721 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007722 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00007723 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00007724 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlsonb703df62009-12-03 08:36:21 +00007725 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
7726 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007727 else
7728 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7729
7730 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7731 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
7732 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
7733 BDINFO_FLAGS_DISABLED);
7734
7735
7736 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00007737 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007738 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00007739 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007740 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007741 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7742 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00007743 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
7744 else
7745 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7746
7747 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7748 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
7749 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
7750 BDINFO_FLAGS_DISABLED);
7751
7752 /* Disable interrupts */
7753 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
7754
7755 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00007756 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00007757 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007758 tp->napi[i].tx_prod = 0;
7759 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007760 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00007761 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007762 tw32_rx_mbox(tp->napi[i].consmbox, 0);
7763 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
7764 }
Joe Perches63c3a662011-04-26 08:12:10 +00007765 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00007766 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007767 } else {
7768 tp->napi[0].tx_prod = 0;
7769 tp->napi[0].tx_cons = 0;
7770 tw32_mailbox(tp->napi[0].prodmbox, 0);
7771 tw32_rx_mbox(tp->napi[0].consmbox, 0);
7772 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007773
7774 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00007775 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00007776 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
7777 for (i = 0; i < 16; i++)
7778 tw32_tx_mbox(mbox + i * 8, 0);
7779 }
7780
7781 txrcb = NIC_SRAM_SEND_RCB;
7782 rxrcb = NIC_SRAM_RCV_RET_RCB;
7783
7784 /* Clear status block in ram. */
7785 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7786
7787 /* Set status block DMA address */
7788 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7789 ((u64) tnapi->status_mapping >> 32));
7790 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7791 ((u64) tnapi->status_mapping & 0xffffffff));
7792
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007793 if (tnapi->tx_ring) {
7794 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7795 (TG3_TX_RING_SIZE <<
7796 BDINFO_FLAGS_MAXLEN_SHIFT),
7797 NIC_SRAM_TX_BUFFER_DESC);
7798 txrcb += TG3_BDINFO_SIZE;
7799 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007800
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007801 if (tnapi->rx_rcb) {
7802 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007803 (tp->rx_ret_ring_mask + 1) <<
7804 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007805 rxrcb += TG3_BDINFO_SIZE;
7806 }
7807
7808 stblk = HOSTCC_STATBLCK_RING1;
7809
7810 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
7811 u64 mapping = (u64)tnapi->status_mapping;
7812 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
7813 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
7814
7815 /* Clear status block in ram. */
7816 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7817
Matt Carlson19cfaec2009-12-03 08:36:20 +00007818 if (tnapi->tx_ring) {
7819 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7820 (TG3_TX_RING_SIZE <<
7821 BDINFO_FLAGS_MAXLEN_SHIFT),
7822 NIC_SRAM_TX_BUFFER_DESC);
7823 txrcb += TG3_BDINFO_SIZE;
7824 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007825
7826 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007827 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007828 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7829
7830 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007831 rxrcb += TG3_BDINFO_SIZE;
7832 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007833}
7834
Matt Carlsoneb07a942011-04-20 07:57:36 +00007835static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
7836{
7837 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
7838
Joe Perches63c3a662011-04-26 08:12:10 +00007839 if (!tg3_flag(tp, 5750_PLUS) ||
7840 tg3_flag(tp, 5780_CLASS) ||
Matt Carlsoneb07a942011-04-20 07:57:36 +00007841 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
7842 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
7843 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
7844 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7845 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
7846 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
7847 else
7848 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
7849
7850 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
7851 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
7852
7853 val = min(nic_rep_thresh, host_rep_thresh);
7854 tw32(RCVBDI_STD_THRESH, val);
7855
Joe Perches63c3a662011-04-26 08:12:10 +00007856 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00007857 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
7858
Joe Perches63c3a662011-04-26 08:12:10 +00007859 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00007860 return;
7861
Joe Perches63c3a662011-04-26 08:12:10 +00007862 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00007863 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
7864 else
7865 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5717;
7866
7867 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
7868
7869 val = min(bdcache_maxcnt / 2, host_rep_thresh);
7870 tw32(RCVBDI_JUMBO_THRESH, val);
7871
Joe Perches63c3a662011-04-26 08:12:10 +00007872 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00007873 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
7874}
7875
Matt Carlson2d31eca2009-09-01 12:53:31 +00007876/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007877static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007878{
7879 u32 val, rdmac_mode;
7880 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00007881 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007882
7883 tg3_disable_ints(tp);
7884
7885 tg3_stop_fw(tp);
7886
7887 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
7888
Joe Perches63c3a662011-04-26 08:12:10 +00007889 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07007890 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007891
Matt Carlson699c0192010-12-06 08:28:51 +00007892 /* Enable MAC control of LPI */
7893 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
7894 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
7895 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
7896 TG3_CPMU_EEE_LNKIDL_UART_IDL);
7897
7898 tw32_f(TG3_CPMU_EEE_CTRL,
7899 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
7900
Matt Carlsona386b902010-12-06 08:28:53 +00007901 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
7902 TG3_CPMU_EEEMD_LPI_IN_TX |
7903 TG3_CPMU_EEEMD_LPI_IN_RX |
7904 TG3_CPMU_EEEMD_EEE_ENABLE;
7905
7906 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
7907 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
7908
Joe Perches63c3a662011-04-26 08:12:10 +00007909 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00007910 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
7911
7912 tw32_f(TG3_CPMU_EEE_MODE, val);
7913
7914 tw32_f(TG3_CPMU_EEE_DBTMR1,
7915 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
7916 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
7917
7918 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00007919 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00007920 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00007921 }
7922
Matt Carlson603f1172010-02-12 14:47:10 +00007923 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08007924 tg3_phy_reset(tp);
7925
Linus Torvalds1da177e2005-04-16 15:20:36 -07007926 err = tg3_chip_reset(tp);
7927 if (err)
7928 return err;
7929
7930 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
7931
Matt Carlsonbcb37f62008-11-03 16:52:09 -08007932 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007933 val = tr32(TG3_CPMU_CTRL);
7934 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
7935 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08007936
7937 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7938 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7939 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7940 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
7941
7942 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
7943 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
7944 val |= CPMU_LNK_AWARE_MACCLK_6_25;
7945 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
7946
7947 val = tr32(TG3_CPMU_HST_ACC);
7948 val &= ~CPMU_HST_ACC_MACCLK_MASK;
7949 val |= CPMU_HST_ACC_MACCLK_6_25;
7950 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07007951 }
7952
Matt Carlson33466d92009-04-20 06:57:41 +00007953 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7954 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
7955 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
7956 PCIE_PWR_MGMT_L1_THRESH_4MS;
7957 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00007958
7959 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
7960 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
7961
7962 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00007963
Matt Carlsonf40386c2009-11-02 14:24:02 +00007964 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7965 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00007966 }
7967
Joe Perches63c3a662011-04-26 08:12:10 +00007968 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00007969 u32 grc_mode = tr32(GRC_MODE);
7970
7971 /* Access the lower 1K of PL PCIE block registers. */
7972 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7973 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
7974
7975 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
7976 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
7977 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
7978
7979 tw32(GRC_MODE, grc_mode);
7980 }
7981
Matt Carlson5093eed2010-11-24 08:31:45 +00007982 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
7983 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
7984 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00007985
Matt Carlson5093eed2010-11-24 08:31:45 +00007986 /* Access the lower 1K of PL PCIE block registers. */
7987 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7988 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00007989
Matt Carlson5093eed2010-11-24 08:31:45 +00007990 val = tr32(TG3_PCIE_TLDLPL_PORT +
7991 TG3_PCIE_PL_LO_PHYCTL5);
7992 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
7993 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00007994
Matt Carlson5093eed2010-11-24 08:31:45 +00007995 tw32(GRC_MODE, grc_mode);
7996 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00007997
Matt Carlson1ff30a52011-05-19 12:12:46 +00007998 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
7999 u32 grc_mode = tr32(GRC_MODE);
8000
8001 /* Access the lower 1K of DL PCIE block registers. */
8002 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8003 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
8004
8005 val = tr32(TG3_PCIE_TLDLPL_PORT +
8006 TG3_PCIE_DL_LO_FTSMAX);
8007 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
8008 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
8009 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
8010
8011 tw32(GRC_MODE, grc_mode);
8012 }
8013
Matt Carlsona977dbe2010-04-12 06:58:26 +00008014 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8015 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8016 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8017 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008018 }
8019
Linus Torvalds1da177e2005-04-16 15:20:36 -07008020 /* This works around an issue with Athlon chipsets on
8021 * B3 tigon3 silicon. This bit has no effect on any
8022 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008023 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008024 */
Joe Perches63c3a662011-04-26 08:12:10 +00008025 if (!tg3_flag(tp, CPMU_PRESENT)) {
8026 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07008027 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8028 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008030
8031 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008032 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008033 val = tr32(TG3PCI_PCISTATE);
8034 val |= PCISTATE_RETRY_SAME_DMA;
8035 tw32(TG3PCI_PCISTATE, val);
8036 }
8037
Joe Perches63c3a662011-04-26 08:12:10 +00008038 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07008039 /* Allow reads and writes to the
8040 * APE register and memory space.
8041 */
8042 val = tr32(TG3PCI_PCISTATE);
8043 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008044 PCISTATE_ALLOW_APE_SHMEM_WR |
8045 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008046 tw32(TG3PCI_PCISTATE, val);
8047 }
8048
Linus Torvalds1da177e2005-04-16 15:20:36 -07008049 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8050 /* Enable some hw fixes. */
8051 val = tr32(TG3PCI_MSI_DATA);
8052 val |= (1 << 26) | (1 << 28) | (1 << 29);
8053 tw32(TG3PCI_MSI_DATA, val);
8054 }
8055
8056 /* Descriptor ring init may make accesses to the
8057 * NIC SRAM area to setup the TX descriptors, so we
8058 * can only do this after the hardware has been
8059 * successfully reset.
8060 */
Michael Chan32d8c572006-07-25 16:38:29 -07008061 err = tg3_init_rings(tp);
8062 if (err)
8063 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008064
Joe Perches63c3a662011-04-26 08:12:10 +00008065 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008066 val = tr32(TG3PCI_DMA_RW_CTRL) &
8067 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008068 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8069 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson0aebff42011-04-25 12:42:45 +00008070 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765 &&
8071 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8072 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008073 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8074 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8075 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008076 /* This value is determined during the probe time DMA
8077 * engine test, tg3_test_dma.
8078 */
8079 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008081
8082 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8083 GRC_MODE_4X_NIC_SEND_RINGS |
8084 GRC_MODE_NO_TX_PHDR_CSUM |
8085 GRC_MODE_NO_RX_PHDR_CSUM);
8086 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07008087
8088 /* Pseudo-header checksum is done by hardware logic and not
8089 * the offload processers, so make the chip do the pseudo-
8090 * header checksums on receive. For transmit it is more
8091 * convenient to do the pseudo-header checksum in software
8092 * as Linux does that on transmit for us in all cases.
8093 */
8094 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008095
8096 tw32(GRC_MODE,
8097 tp->grc_mode |
8098 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8099
8100 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8101 val = tr32(GRC_MISC_CFG);
8102 val &= ~0xff;
8103 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8104 tw32(GRC_MISC_CFG, val);
8105
8106 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00008107 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008108 /* Do nothing. */
8109 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8110 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8111 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8112 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8113 else
8114 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8115 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8116 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00008117 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008118 int fw_len;
8119
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008120 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008121 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8122 tw32(BUFMGR_MB_POOL_ADDR,
8123 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8124 tw32(BUFMGR_MB_POOL_SIZE,
8125 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008127
Michael Chan0f893dc2005-07-25 12:30:38 -07008128 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008129 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8130 tp->bufmgr_config.mbuf_read_dma_low_water);
8131 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8132 tp->bufmgr_config.mbuf_mac_rx_low_water);
8133 tw32(BUFMGR_MB_HIGH_WATER,
8134 tp->bufmgr_config.mbuf_high_water);
8135 } else {
8136 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8137 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8138 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8139 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8140 tw32(BUFMGR_MB_HIGH_WATER,
8141 tp->bufmgr_config.mbuf_high_water_jumbo);
8142 }
8143 tw32(BUFMGR_DMA_LOW_WATER,
8144 tp->bufmgr_config.dma_low_water);
8145 tw32(BUFMGR_DMA_HIGH_WATER,
8146 tp->bufmgr_config.dma_high_water);
8147
Matt Carlsond309a462010-09-30 10:34:31 +00008148 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8149 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8150 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00008151 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8152 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
8153 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
8154 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00008155 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008156 for (i = 0; i < 2000; i++) {
8157 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8158 break;
8159 udelay(10);
8160 }
8161 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008162 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008163 return -ENODEV;
8164 }
8165
Matt Carlsoneb07a942011-04-20 07:57:36 +00008166 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8167 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07008168
Matt Carlsoneb07a942011-04-20 07:57:36 +00008169 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008170
8171 /* Initialize TG3_BDINFO's at:
8172 * RCVDBDI_STD_BD: standard eth size rx ring
8173 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8174 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8175 *
8176 * like so:
8177 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8178 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8179 * ring attribute flags
8180 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8181 *
8182 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8183 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8184 *
8185 * The size of each ring is fixed in the firmware, but the location is
8186 * configurable.
8187 */
8188 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008189 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008190 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008191 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00008192 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00008193 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8194 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008195
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008196 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00008197 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008198 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8199 BDINFO_FLAGS_DISABLED);
8200
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008201 /* Program the jumbo buffer descriptor ring control
8202 * blocks on those devices that have them.
8203 */
Matt Carlsonbb18bb92011-03-09 16:58:19 +00008204 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008205 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008206
Joe Perches63c3a662011-04-26 08:12:10 +00008207 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008208 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008209 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008210 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008211 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008212 val = TG3_RX_JMB_RING_SIZE(tp) <<
8213 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008214 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008215 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00008216 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Matt Carlsona50d0792010-06-05 17:24:37 +00008217 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson87668d32009-11-13 13:03:34 +00008218 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8219 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008220 } else {
8221 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8222 BDINFO_FLAGS_DISABLED);
8223 }
8224
Joe Perches63c3a662011-04-26 08:12:10 +00008225 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008226 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsonde9f5232011-04-05 14:22:43 +00008227 val = TG3_RX_STD_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008228 else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008229 val = TG3_RX_STD_MAX_SIZE_5717;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008230 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8231 val |= (TG3_RX_STD_DMA_SZ << 2);
8232 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008233 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008234 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008235 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008236
8237 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008238
Matt Carlson411da642009-11-13 13:03:46 +00008239 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00008240 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008241
Joe Perches63c3a662011-04-26 08:12:10 +00008242 tpr->rx_jmb_prod_idx =
8243 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00008244 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008245
Matt Carlson2d31eca2009-09-01 12:53:31 +00008246 tg3_rings_reset(tp);
8247
Linus Torvalds1da177e2005-04-16 15:20:36 -07008248 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008249 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008250
8251 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008252 tw32(MAC_RX_MTU_SIZE,
8253 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008254
8255 /* The slot time is changed by tg3_setup_phy if we
8256 * run at gigabit with half duplex.
8257 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00008258 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8259 (6 << TX_LENGTHS_IPG_SHIFT) |
8260 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8261
8262 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8263 val |= tr32(MAC_TX_LENGTHS) &
8264 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8265 TX_LENGTHS_CNT_DWN_VAL_MSK);
8266
8267 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008268
8269 /* Receive rules. */
8270 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8271 tw32(RCVLPC_CONFIG, 0x0181);
8272
8273 /* Calculate RDMAC_MODE setting early, we need it to determine
8274 * the RCVLPC_STATE_ENABLE mask.
8275 */
8276 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8277 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8278 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8279 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8280 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008281
Matt Carlsondeabaac2010-11-24 08:31:50 +00008282 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008283 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8284
Matt Carlson57e69832008-05-25 23:48:31 -07008285 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008286 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8287 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008288 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8289 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8290 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8291
Matt Carlsonc5908932011-03-09 16:58:25 +00008292 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8293 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008294 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008295 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008296 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8297 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008298 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008299 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8300 }
8301 }
8302
Joe Perches63c3a662011-04-26 08:12:10 +00008303 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07008304 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8305
Joe Perches63c3a662011-04-26 08:12:10 +00008306 if (tg3_flag(tp, HW_TSO_1) ||
8307 tg3_flag(tp, HW_TSO_2) ||
8308 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08008309 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8310
Matt Carlson108a6c12011-05-19 12:12:47 +00008311 if (tg3_flag(tp, 57765_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00008312 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008313 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8314 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008315
Matt Carlsonf2096f92011-04-05 14:22:48 +00008316 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8317 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8318
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008319 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8320 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8321 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8322 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008323 tg3_flag(tp, 57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008324 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00008325 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8326 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008327 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8328 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8329 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8330 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8331 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8332 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008333 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008334 tw32(TG3_RDMA_RSRVCTRL_REG,
8335 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8336 }
8337
Matt Carlsond78b59f2011-04-05 14:22:46 +00008338 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8339 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00008340 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8341 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8342 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8343 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8344 }
8345
Linus Torvalds1da177e2005-04-16 15:20:36 -07008346 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00008347 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07008348 val = tr32(RCVLPC_STATS_ENABLE);
8349 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8350 tw32(RCVLPC_STATS_ENABLE, val);
8351 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008352 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008353 val = tr32(RCVLPC_STATS_ENABLE);
8354 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8355 tw32(RCVLPC_STATS_ENABLE, val);
8356 } else {
8357 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8358 }
8359 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8360 tw32(SNDDATAI_STATSENAB, 0xffffff);
8361 tw32(SNDDATAI_STATSCTRL,
8362 (SNDDATAI_SCTRL_ENABLE |
8363 SNDDATAI_SCTRL_FASTUPD));
8364
8365 /* Setup host coalescing engine. */
8366 tw32(HOSTCC_MODE, 0);
8367 for (i = 0; i < 2000; i++) {
8368 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8369 break;
8370 udelay(10);
8371 }
8372
Michael Chand244c892005-07-05 14:42:33 -07008373 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008374
Joe Perches63c3a662011-04-26 08:12:10 +00008375 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008376 /* Status/statistics block address. See tg3_timer,
8377 * the tg3_periodic_fetch_stats call there, and
8378 * tg3_get_stats to see how this works for 5705/5750 chips.
8379 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008380 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8381 ((u64) tp->stats_mapping >> 32));
8382 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8383 ((u64) tp->stats_mapping & 0xffffffff));
8384 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008385
Linus Torvalds1da177e2005-04-16 15:20:36 -07008386 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008387
8388 /* Clear statistics and status block memory areas */
8389 for (i = NIC_SRAM_STATS_BLK;
8390 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8391 i += sizeof(u32)) {
8392 tg3_write_mem(tp, i, 0);
8393 udelay(40);
8394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008395 }
8396
8397 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8398
8399 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8400 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008401 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008402 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8403
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008404 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8405 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07008406 /* reset to prevent losing 1st rx packet intermittently */
8407 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8408 udelay(10);
8409 }
8410
Joe Perches63c3a662011-04-26 08:12:10 +00008411 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008412 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -07008413 else
8414 tp->mac_mode = 0;
8415 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07008416 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008417 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008418 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008419 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8420 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008421 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8422 udelay(40);
8423
Michael Chan314fba32005-04-21 17:07:04 -07008424 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00008425 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008426 * register to preserve the GPIO settings for LOMs. The GPIOs,
8427 * whether used as inputs or outputs, are set by boot code after
8428 * reset.
8429 */
Joe Perches63c3a662011-04-26 08:12:10 +00008430 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008431 u32 gpio_mask;
8432
Michael Chan9d26e212006-12-07 00:21:14 -08008433 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8434 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8435 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008436
8437 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8438 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8439 GRC_LCLCTRL_GPIO_OUTPUT3;
8440
Michael Chanaf36e6b2006-03-23 01:28:06 -08008441 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8442 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8443
Gary Zambranoaaf84462007-05-05 11:51:45 -07008444 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008445 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8446
8447 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00008448 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08008449 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8450 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008452 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8453 udelay(100);
8454
Joe Perches63c3a662011-04-26 08:12:10 +00008455 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008456 val = tr32(MSGINT_MODE);
8457 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
8458 tw32(MSGINT_MODE, val);
8459 }
8460
Joe Perches63c3a662011-04-26 08:12:10 +00008461 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008462 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8463 udelay(40);
8464 }
8465
8466 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8467 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8468 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8469 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8470 WDMAC_MODE_LNGREAD_ENAB);
8471
Matt Carlsonc5908932011-03-09 16:58:25 +00008472 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8473 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008474 if (tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008475 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8476 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8477 /* nothing */
8478 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008479 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008480 val |= WDMAC_MODE_RX_ACCEL;
8481 }
8482 }
8483
Michael Chand9ab5ad2006-03-20 22:27:35 -08008484 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00008485 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07008486 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -08008487
Matt Carlson788a0352009-11-02 14:26:03 +00008488 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8489 val |= WDMAC_MODE_BURST_ALL_DATA;
8490
Linus Torvalds1da177e2005-04-16 15:20:36 -07008491 tw32_f(WDMAC_MODE, val);
8492 udelay(40);
8493
Joe Perches63c3a662011-04-26 08:12:10 +00008494 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008495 u16 pcix_cmd;
8496
8497 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8498 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008499 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008500 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8501 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008502 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008503 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8504 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008505 }
Matt Carlson9974a352007-10-07 23:27:28 -07008506 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8507 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008508 }
8509
8510 tw32_f(RDMAC_MODE, rdmac_mode);
8511 udelay(40);
8512
8513 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008514 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008515 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008516
8517 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8518 tw32(SNDDATAC_MODE,
8519 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8520 else
8521 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8522
Linus Torvalds1da177e2005-04-16 15:20:36 -07008523 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8524 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008525 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00008526 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008527 val |= RCVDBDI_MODE_LRG_RING_SZ;
8528 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008529 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008530 if (tg3_flag(tp, HW_TSO_1) ||
8531 tg3_flag(tp, HW_TSO_2) ||
8532 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008533 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008534 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008535 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008536 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8537 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008538 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8539
8540 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8541 err = tg3_load_5701_a0_firmware_fix(tp);
8542 if (err)
8543 return err;
8544 }
8545
Joe Perches63c3a662011-04-26 08:12:10 +00008546 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008547 err = tg3_load_tso_firmware(tp);
8548 if (err)
8549 return err;
8550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008551
8552 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008553
Joe Perches63c3a662011-04-26 08:12:10 +00008554 if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsonb1d05212010-06-05 17:24:31 +00008555 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8556 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008557
8558 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8559 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
8560 tp->tx_mode &= ~val;
8561 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
8562 }
8563
Linus Torvalds1da177e2005-04-16 15:20:36 -07008564 tw32_f(MAC_TX_MODE, tp->tx_mode);
8565 udelay(100);
8566
Joe Perches63c3a662011-04-26 08:12:10 +00008567 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008568 u32 reg = MAC_RSS_INDIR_TBL_0;
8569 u8 *ent = (u8 *)&val;
8570
8571 /* Setup the indirection table */
8572 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8573 int idx = i % sizeof(val);
8574
Matt Carlson5efeeea2010-07-11 09:31:40 +00008575 ent[idx] = i % (tp->irq_cnt - 1);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008576 if (idx == sizeof(val) - 1) {
8577 tw32(reg, val);
8578 reg += 4;
8579 }
8580 }
8581
8582 /* Setup the "secret" hash key. */
8583 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8584 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8585 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8586 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8587 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8588 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8589 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8590 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8591 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8592 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8593 }
8594
Linus Torvalds1da177e2005-04-16 15:20:36 -07008595 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008596 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08008597 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8598
Joe Perches63c3a662011-04-26 08:12:10 +00008599 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008600 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8601 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8602 RX_MODE_RSS_IPV6_HASH_EN |
8603 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8604 RX_MODE_RSS_IPV4_HASH_EN |
8605 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8606
Linus Torvalds1da177e2005-04-16 15:20:36 -07008607 tw32_f(MAC_RX_MODE, tp->rx_mode);
8608 udelay(10);
8609
Linus Torvalds1da177e2005-04-16 15:20:36 -07008610 tw32(MAC_LED_CTRL, tp->led_ctrl);
8611
8612 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008613 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008614 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8615 udelay(10);
8616 }
8617 tw32_f(MAC_RX_MODE, tp->rx_mode);
8618 udelay(10);
8619
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008620 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008621 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008622 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008623 /* Set drive transmission level to 1.2V */
8624 /* only if the signal pre-emphasis bit is not set */
8625 val = tr32(MAC_SERDES_CFG);
8626 val &= 0xfffff000;
8627 val |= 0x880;
8628 tw32(MAC_SERDES_CFG, val);
8629 }
8630 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8631 tw32(MAC_SERDES_CFG, 0x616000);
8632 }
8633
8634 /* Prevent chip from dropping frames when flow control
8635 * is enabled.
8636 */
Matt Carlson666bc832010-01-20 16:58:03 +00008637 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8638 val = 1;
8639 else
8640 val = 2;
8641 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008642
8643 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008644 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008645 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00008646 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008647 }
8648
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008649 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +00008650 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08008651 u32 tmp;
8652
8653 tmp = tr32(SERDES_RX_CTRL);
8654 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
8655 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
8656 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
8657 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8658 }
8659
Joe Perches63c3a662011-04-26 08:12:10 +00008660 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson800960682010-08-02 11:26:06 +00008661 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
8662 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07008663 tp->link_config.speed = tp->link_config.orig_speed;
8664 tp->link_config.duplex = tp->link_config.orig_duplex;
8665 tp->link_config.autoneg = tp->link_config.orig_autoneg;
8666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008667
Matt Carlsondd477002008-05-25 23:45:58 -07008668 err = tg3_setup_phy(tp, 0);
8669 if (err)
8670 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008671
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008672 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
8673 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07008674 u32 tmp;
8675
8676 /* Clear CRC stats. */
8677 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
8678 tg3_writephy(tp, MII_TG3_TEST1,
8679 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00008680 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07008681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008682 }
8683 }
8684
8685 __tg3_set_rx_mode(tp->dev);
8686
8687 /* Initialize receive rules. */
8688 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
8689 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
8690 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
8691 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
8692
Joe Perches63c3a662011-04-26 08:12:10 +00008693 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008694 limit = 8;
8695 else
8696 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008697 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008698 limit -= 4;
8699 switch (limit) {
8700 case 16:
8701 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
8702 case 15:
8703 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
8704 case 14:
8705 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
8706 case 13:
8707 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
8708 case 12:
8709 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
8710 case 11:
8711 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
8712 case 10:
8713 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
8714 case 9:
8715 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
8716 case 8:
8717 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
8718 case 7:
8719 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
8720 case 6:
8721 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
8722 case 5:
8723 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
8724 case 4:
8725 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
8726 case 3:
8727 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
8728 case 2:
8729 case 1:
8730
8731 default:
8732 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008734
Joe Perches63c3a662011-04-26 08:12:10 +00008735 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07008736 /* Write our heartbeat update interval to APE. */
8737 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
8738 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07008739
Linus Torvalds1da177e2005-04-16 15:20:36 -07008740 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
8741
Linus Torvalds1da177e2005-04-16 15:20:36 -07008742 return 0;
8743}
8744
8745/* Called at device open time to get the chip ready for
8746 * packet processing. Invoked with tp->lock held.
8747 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008748static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008749{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008750 tg3_switch_clocks(tp);
8751
8752 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
8753
Matt Carlson2f751b62008-08-04 23:17:34 -07008754 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008755}
8756
8757#define TG3_STAT_ADD32(PSTAT, REG) \
8758do { u32 __val = tr32(REG); \
8759 (PSTAT)->low += __val; \
8760 if ((PSTAT)->low < __val) \
8761 (PSTAT)->high += 1; \
8762} while (0)
8763
8764static void tg3_periodic_fetch_stats(struct tg3 *tp)
8765{
8766 struct tg3_hw_stats *sp = tp->hw_stats;
8767
8768 if (!netif_carrier_ok(tp->dev))
8769 return;
8770
8771 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
8772 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
8773 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
8774 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
8775 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
8776 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
8777 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
8778 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
8779 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
8780 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
8781 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
8782 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
8783 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
8784
8785 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
8786 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
8787 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
8788 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
8789 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
8790 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
8791 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
8792 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
8793 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
8794 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
8795 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
8796 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
8797 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
8798 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07008799
8800 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson310050f2011-05-19 12:12:55 +00008801 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
8802 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0 &&
8803 tp->pci_chip_rev_id != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +00008804 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
8805 } else {
8806 u32 val = tr32(HOSTCC_FLOW_ATTN);
8807 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
8808 if (val) {
8809 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
8810 sp->rx_discards.low += val;
8811 if (sp->rx_discards.low < val)
8812 sp->rx_discards.high += 1;
8813 }
8814 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
8815 }
Michael Chan463d3052006-05-22 16:36:27 -07008816 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008817}
8818
8819static void tg3_timer(unsigned long __opaque)
8820{
8821 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008822
Michael Chanf475f162006-03-27 23:20:14 -08008823 if (tp->irq_sync)
8824 goto restart_timer;
8825
David S. Millerf47c11e2005-06-24 20:18:35 -07008826 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008827
Joe Perches63c3a662011-04-26 08:12:10 +00008828 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -07008829 /* All of this garbage is because when using non-tagged
8830 * IRQ status the mailbox/status_block protocol the chip
8831 * uses with the cpu is race prone.
8832 */
Matt Carlson898a56f2009-08-28 14:02:40 +00008833 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07008834 tw32(GRC_LOCAL_CTRL,
8835 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
8836 } else {
8837 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008838 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07008839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008840
David S. Millerfac9b832005-05-18 22:46:34 -07008841 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +00008842 tg3_flag_set(tp, RESTART_TIMER);
David S. Millerf47c11e2005-06-24 20:18:35 -07008843 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07008844 schedule_work(&tp->reset_task);
8845 return;
8846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008847 }
8848
Linus Torvalds1da177e2005-04-16 15:20:36 -07008849 /* This part only runs once per second. */
8850 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00008851 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -07008852 tg3_periodic_fetch_stats(tp);
8853
Matt Carlsonb0c59432011-05-19 12:12:48 +00008854 if (tp->setlpicnt && !--tp->setlpicnt)
8855 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +00008856
Joe Perches63c3a662011-04-26 08:12:10 +00008857 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008858 u32 mac_stat;
8859 int phy_event;
8860
8861 mac_stat = tr32(MAC_STATUS);
8862
8863 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008864 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008865 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
8866 phy_event = 1;
8867 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
8868 phy_event = 1;
8869
8870 if (phy_event)
8871 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +00008872 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008873 u32 mac_stat = tr32(MAC_STATUS);
8874 int need_setup = 0;
8875
8876 if (netif_carrier_ok(tp->dev) &&
8877 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
8878 need_setup = 1;
8879 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00008880 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008881 (mac_stat & (MAC_STATUS_PCS_SYNCED |
8882 MAC_STATUS_SIGNAL_DET))) {
8883 need_setup = 1;
8884 }
8885 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07008886 if (!tp->serdes_counter) {
8887 tw32_f(MAC_MODE,
8888 (tp->mac_mode &
8889 ~MAC_MODE_PORT_MODE_MASK));
8890 udelay(40);
8891 tw32_f(MAC_MODE, tp->mac_mode);
8892 udelay(40);
8893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008894 tg3_setup_phy(tp, 0);
8895 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008896 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008897 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07008898 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00008899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008900
8901 tp->timer_counter = tp->timer_multiplier;
8902 }
8903
Michael Chan130b8e42006-09-27 16:00:40 -07008904 /* Heartbeat is only sent once every 2 seconds.
8905 *
8906 * The heartbeat is to tell the ASF firmware that the host
8907 * driver is still alive. In the event that the OS crashes,
8908 * ASF needs to reset the hardware to free up the FIFO space
8909 * that may be filled with rx packets destined for the host.
8910 * If the FIFO is full, ASF will no longer function properly.
8911 *
8912 * Unintended resets have been reported on real time kernels
8913 * where the timer doesn't run on time. Netpoll will also have
8914 * same problem.
8915 *
8916 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
8917 * to check the ring condition when the heartbeat is expiring
8918 * before doing the reset. This will prevent most unintended
8919 * resets.
8920 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008921 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00008922 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07008923 tg3_wait_for_event_ack(tp);
8924
Michael Chanbbadf502006-04-06 21:46:34 -07008925 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07008926 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07008927 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00008928 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
8929 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008930
8931 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008932 }
8933 tp->asf_counter = tp->asf_multiplier;
8934 }
8935
David S. Millerf47c11e2005-06-24 20:18:35 -07008936 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008937
Michael Chanf475f162006-03-27 23:20:14 -08008938restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07008939 tp->timer.expires = jiffies + tp->timer_offset;
8940 add_timer(&tp->timer);
8941}
8942
Matt Carlson4f125f42009-09-01 12:55:02 +00008943static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08008944{
David Howells7d12e782006-10-05 14:55:46 +01008945 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008946 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00008947 char *name;
8948 struct tg3_napi *tnapi = &tp->napi[irq_num];
8949
8950 if (tp->irq_cnt == 1)
8951 name = tp->dev->name;
8952 else {
8953 name = &tnapi->irq_lbl[0];
8954 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
8955 name[IFNAMSIZ-1] = 0;
8956 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008957
Joe Perches63c3a662011-04-26 08:12:10 +00008958 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08008959 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +00008960 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -08008961 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00008962 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008963 } else {
8964 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +00008965 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -08008966 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00008967 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008968 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008969
8970 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008971}
8972
Michael Chan79381092005-04-21 17:13:59 -07008973static int tg3_test_interrupt(struct tg3 *tp)
8974{
Matt Carlson09943a12009-08-28 14:01:57 +00008975 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07008976 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07008977 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008978 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07008979
Michael Chand4bc3922005-05-29 14:59:20 -07008980 if (!netif_running(dev))
8981 return -ENODEV;
8982
Michael Chan79381092005-04-21 17:13:59 -07008983 tg3_disable_ints(tp);
8984
Matt Carlson4f125f42009-09-01 12:55:02 +00008985 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008986
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008987 /*
8988 * Turn off MSI one shot mode. Otherwise this test has no
8989 * observable way to know whether the interrupt was delivered.
8990 */
Joe Perches63c3a662011-04-26 08:12:10 +00008991 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008992 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
8993 tw32(MSGINT_MODE, val);
8994 }
8995
Matt Carlson4f125f42009-09-01 12:55:02 +00008996 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00008997 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008998 if (err)
8999 return err;
9000
Matt Carlson898a56f2009-08-28 14:02:40 +00009001 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07009002 tg3_enable_ints(tp);
9003
9004 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009005 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07009006
9007 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07009008 u32 int_mbox, misc_host_ctrl;
9009
Matt Carlson898a56f2009-08-28 14:02:40 +00009010 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07009011 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
9012
9013 if ((int_mbox != 0) ||
9014 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
9015 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07009016 break;
Michael Chanb16250e2006-09-27 16:10:14 -07009017 }
9018
Michael Chan79381092005-04-21 17:13:59 -07009019 msleep(10);
9020 }
9021
9022 tg3_disable_ints(tp);
9023
Matt Carlson4f125f42009-09-01 12:55:02 +00009024 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009025
Matt Carlson4f125f42009-09-01 12:55:02 +00009026 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009027
9028 if (err)
9029 return err;
9030
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009031 if (intr_ok) {
9032 /* Reenable MSI one shot mode. */
Joe Perches63c3a662011-04-26 08:12:10 +00009033 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009034 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
9035 tw32(MSGINT_MODE, val);
9036 }
Michael Chan79381092005-04-21 17:13:59 -07009037 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009038 }
Michael Chan79381092005-04-21 17:13:59 -07009039
9040 return -EIO;
9041}
9042
9043/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
9044 * successfully restored
9045 */
9046static int tg3_test_msi(struct tg3 *tp)
9047{
Michael Chan79381092005-04-21 17:13:59 -07009048 int err;
9049 u16 pci_cmd;
9050
Joe Perches63c3a662011-04-26 08:12:10 +00009051 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -07009052 return 0;
9053
9054 /* Turn off SERR reporting in case MSI terminates with Master
9055 * Abort.
9056 */
9057 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
9058 pci_write_config_word(tp->pdev, PCI_COMMAND,
9059 pci_cmd & ~PCI_COMMAND_SERR);
9060
9061 err = tg3_test_interrupt(tp);
9062
9063 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
9064
9065 if (!err)
9066 return 0;
9067
9068 /* other failures */
9069 if (err != -EIO)
9070 return err;
9071
9072 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009073 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
9074 "to INTx mode. Please report this failure to the PCI "
9075 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07009076
Matt Carlson4f125f42009-09-01 12:55:02 +00009077 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00009078
Michael Chan79381092005-04-21 17:13:59 -07009079 pci_disable_msi(tp->pdev);
9080
Joe Perches63c3a662011-04-26 08:12:10 +00009081 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +00009082 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07009083
Matt Carlson4f125f42009-09-01 12:55:02 +00009084 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009085 if (err)
9086 return err;
9087
9088 /* Need to reset the chip because the MSI cycle may have terminated
9089 * with Master Abort.
9090 */
David S. Millerf47c11e2005-06-24 20:18:35 -07009091 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009092
Michael Chan944d9802005-05-29 14:57:48 -07009093 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009094 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009095
David S. Millerf47c11e2005-06-24 20:18:35 -07009096 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009097
9098 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00009099 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07009100
9101 return err;
9102}
9103
Matt Carlson9e9fd122009-01-19 16:57:45 -08009104static int tg3_request_firmware(struct tg3 *tp)
9105{
9106 const __be32 *fw_data;
9107
9108 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009109 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
9110 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009111 return -ENOENT;
9112 }
9113
9114 fw_data = (void *)tp->fw->data;
9115
9116 /* Firmware blob starts with version numbers, followed by
9117 * start address and _full_ length including BSS sections
9118 * (which must be longer than the actual data, of course
9119 */
9120
9121 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9122 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009123 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9124 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009125 release_firmware(tp->fw);
9126 tp->fw = NULL;
9127 return -EINVAL;
9128 }
9129
9130 /* We no longer need firmware; we have it. */
9131 tp->fw_needed = NULL;
9132 return 0;
9133}
9134
Matt Carlson679563f2009-09-01 12:55:46 +00009135static bool tg3_enable_msix(struct tg3 *tp)
9136{
9137 int i, rc, cpus = num_online_cpus();
9138 struct msix_entry msix_ent[tp->irq_max];
9139
9140 if (cpus == 1)
9141 /* Just fallback to the simpler MSI mode. */
9142 return false;
9143
9144 /*
9145 * We want as many rx rings enabled as there are cpus.
9146 * The first MSIX vector only deals with link interrupts, etc,
9147 * so we add one to the number of vectors we are requesting.
9148 */
9149 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
9150
9151 for (i = 0; i < tp->irq_max; i++) {
9152 msix_ent[i].entry = i;
9153 msix_ent[i].vector = 0;
9154 }
9155
9156 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009157 if (rc < 0) {
9158 return false;
9159 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009160 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9161 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009162 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9163 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009164 tp->irq_cnt = rc;
9165 }
9166
9167 for (i = 0; i < tp->irq_max; i++)
9168 tp->napi[i].irq_vec = msix_ent[i].vector;
9169
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009170 netif_set_real_num_tx_queues(tp->dev, 1);
9171 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9172 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9173 pci_disable_msix(tp->pdev);
9174 return false;
9175 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009176
9177 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +00009178 tg3_flag_set(tp, ENABLE_RSS);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009179
9180 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9181 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Joe Perches63c3a662011-04-26 08:12:10 +00009182 tg3_flag_set(tp, ENABLE_TSS);
Matt Carlsonb92b9042010-11-24 08:31:51 +00009183 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9184 }
9185 }
Matt Carlson2430b032010-06-05 17:24:34 +00009186
Matt Carlson679563f2009-09-01 12:55:46 +00009187 return true;
9188}
9189
Matt Carlson07b01732009-08-28 14:01:15 +00009190static void tg3_ints_init(struct tg3 *tp)
9191{
Joe Perches63c3a662011-04-26 08:12:10 +00009192 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
9193 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009194 /* All MSI supporting chips should support tagged
9195 * status. Assert that this is the case.
9196 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009197 netdev_warn(tp->dev,
9198 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009199 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009200 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009201
Joe Perches63c3a662011-04-26 08:12:10 +00009202 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
9203 tg3_flag_set(tp, USING_MSIX);
9204 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
9205 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +00009206
Joe Perches63c3a662011-04-26 08:12:10 +00009207 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009208 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +00009209 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009210 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson679563f2009-09-01 12:55:46 +00009211 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9212 }
9213defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +00009214 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009215 tp->irq_cnt = 1;
9216 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009217 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009218 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009219 }
Matt Carlson07b01732009-08-28 14:01:15 +00009220}
9221
9222static void tg3_ints_fini(struct tg3 *tp)
9223{
Joe Perches63c3a662011-04-26 08:12:10 +00009224 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +00009225 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009226 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +00009227 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009228 tg3_flag_clear(tp, USING_MSI);
9229 tg3_flag_clear(tp, USING_MSIX);
9230 tg3_flag_clear(tp, ENABLE_RSS);
9231 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009232}
9233
Linus Torvalds1da177e2005-04-16 15:20:36 -07009234static int tg3_open(struct net_device *dev)
9235{
9236 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009237 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009238
Matt Carlson9e9fd122009-01-19 16:57:45 -08009239 if (tp->fw_needed) {
9240 err = tg3_request_firmware(tp);
9241 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9242 if (err)
9243 return err;
9244 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009245 netdev_warn(tp->dev, "TSO capability disabled\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009246 tg3_flag_clear(tp, TSO_CAPABLE);
9247 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009248 netdev_notice(tp->dev, "TSO capability restored\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009249 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009250 }
9251 }
9252
Michael Chanc49a1562006-12-17 17:07:29 -08009253 netif_carrier_off(tp->dev);
9254
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009255 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -07009256 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009257 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009258
9259 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009260
Linus Torvalds1da177e2005-04-16 15:20:36 -07009261 tg3_disable_ints(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009262 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009263
David S. Millerf47c11e2005-06-24 20:18:35 -07009264 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009265
Matt Carlson679563f2009-09-01 12:55:46 +00009266 /*
9267 * Setup interrupts first so we know how
9268 * many NAPI resources to allocate
9269 */
9270 tg3_ints_init(tp);
9271
Linus Torvalds1da177e2005-04-16 15:20:36 -07009272 /* The placement of this call is tied
9273 * to the setup and use of Host TX descriptors.
9274 */
9275 err = tg3_alloc_consistent(tp);
9276 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009277 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009278
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009279 tg3_napi_init(tp);
9280
Matt Carlsonfed97812009-09-01 13:10:19 +00009281 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07009282
Matt Carlson4f125f42009-09-01 12:55:02 +00009283 for (i = 0; i < tp->irq_cnt; i++) {
9284 struct tg3_napi *tnapi = &tp->napi[i];
9285 err = tg3_request_irq(tp, i);
9286 if (err) {
9287 for (i--; i >= 0; i--)
9288 free_irq(tnapi->irq_vec, tnapi);
9289 break;
9290 }
9291 }
Matt Carlson07b01732009-08-28 14:01:15 +00009292
9293 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009294 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00009295
David S. Millerf47c11e2005-06-24 20:18:35 -07009296 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009297
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009298 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009299 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07009300 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009301 tg3_free_rings(tp);
9302 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00009303 if (tg3_flag(tp, TAGGED_STATUS))
David S. Millerfac9b832005-05-18 22:46:34 -07009304 tp->timer_offset = HZ;
9305 else
9306 tp->timer_offset = HZ / 10;
9307
9308 BUG_ON(tp->timer_offset > HZ);
9309 tp->timer_counter = tp->timer_multiplier =
9310 (HZ / tp->timer_offset);
9311 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07009312 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009313
9314 init_timer(&tp->timer);
9315 tp->timer.expires = jiffies + tp->timer_offset;
9316 tp->timer.data = (unsigned long) tp;
9317 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009318 }
9319
David S. Millerf47c11e2005-06-24 20:18:35 -07009320 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009321
Matt Carlson07b01732009-08-28 14:01:15 +00009322 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009323 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009324
Joe Perches63c3a662011-04-26 08:12:10 +00009325 if (tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -07009326 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07009327
Michael Chan79381092005-04-21 17:13:59 -07009328 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009329 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07009330 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07009331 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07009332 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009333
Matt Carlson679563f2009-09-01 12:55:46 +00009334 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07009335 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009336
Joe Perches63c3a662011-04-26 08:12:10 +00009337 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009338 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009339
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009340 tw32(PCIE_TRANSACTION_CFG,
9341 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009342 }
Michael Chan79381092005-04-21 17:13:59 -07009343 }
9344
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009345 tg3_phy_start(tp);
9346
David S. Millerf47c11e2005-06-24 20:18:35 -07009347 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009348
Michael Chan79381092005-04-21 17:13:59 -07009349 add_timer(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +00009350 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009351 tg3_enable_ints(tp);
9352
David S. Millerf47c11e2005-06-24 20:18:35 -07009353 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009354
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009355 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009356
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00009357 /*
9358 * Reset loopback feature if it was turned on while the device was down
9359 * make sure that it's installed properly now.
9360 */
9361 if (dev->features & NETIF_F_LOOPBACK)
9362 tg3_set_loopback(dev, dev->features);
9363
Linus Torvalds1da177e2005-04-16 15:20:36 -07009364 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00009365
Matt Carlson679563f2009-09-01 12:55:46 +00009366err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00009367 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9368 struct tg3_napi *tnapi = &tp->napi[i];
9369 free_irq(tnapi->irq_vec, tnapi);
9370 }
Matt Carlson07b01732009-08-28 14:01:15 +00009371
Matt Carlson679563f2009-09-01 12:55:46 +00009372err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00009373 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009374 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009375 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00009376
9377err_out1:
9378 tg3_ints_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009379 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009380}
9381
Eric Dumazet511d2222010-07-07 20:44:24 +00009382static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
9383 struct rtnl_link_stats64 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009384static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
9385
9386static int tg3_close(struct net_device *dev)
9387{
Matt Carlson4f125f42009-09-01 12:55:02 +00009388 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009389 struct tg3 *tp = netdev_priv(dev);
9390
Matt Carlsonfed97812009-09-01 13:10:19 +00009391 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07009392 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08009393
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009394 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009395
9396 del_timer_sync(&tp->timer);
9397
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009398 tg3_phy_stop(tp);
9399
David S. Millerf47c11e2005-06-24 20:18:35 -07009400 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009401
9402 tg3_disable_ints(tp);
9403
Michael Chan944d9802005-05-29 14:57:48 -07009404 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009405 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009406 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009407
David S. Millerf47c11e2005-06-24 20:18:35 -07009408 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009409
Matt Carlson4f125f42009-09-01 12:55:02 +00009410 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9411 struct tg3_napi *tnapi = &tp->napi[i];
9412 free_irq(tnapi->irq_vec, tnapi);
9413 }
Matt Carlson07b01732009-08-28 14:01:15 +00009414
9415 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009416
Eric Dumazet511d2222010-07-07 20:44:24 +00009417 tg3_get_stats64(tp->dev, &tp->net_stats_prev);
9418
Linus Torvalds1da177e2005-04-16 15:20:36 -07009419 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9420 sizeof(tp->estats_prev));
9421
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009422 tg3_napi_fini(tp);
9423
Linus Torvalds1da177e2005-04-16 15:20:36 -07009424 tg3_free_consistent(tp);
9425
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009426 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -08009427
9428 netif_carrier_off(tp->dev);
9429
Linus Torvalds1da177e2005-04-16 15:20:36 -07009430 return 0;
9431}
9432
Eric Dumazet511d2222010-07-07 20:44:24 +00009433static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -07009434{
9435 return ((u64)val->high << 32) | ((u64)val->low);
9436}
9437
Eric Dumazet511d2222010-07-07 20:44:24 +00009438static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009439{
9440 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9441
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009442 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009443 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9444 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009445 u32 val;
9446
David S. Millerf47c11e2005-06-24 20:18:35 -07009447 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009448 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9449 tg3_writephy(tp, MII_TG3_TEST1,
9450 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009451 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009452 } else
9453 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009454 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009455
9456 tp->phy_crc_errors += val;
9457
9458 return tp->phy_crc_errors;
9459 }
9460
9461 return get_stat64(&hw_stats->rx_fcs_errors);
9462}
9463
9464#define ESTAT_ADD(member) \
9465 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +00009466 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009467
9468static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9469{
9470 struct tg3_ethtool_stats *estats = &tp->estats;
9471 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9472 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9473
9474 if (!hw_stats)
9475 return old_estats;
9476
9477 ESTAT_ADD(rx_octets);
9478 ESTAT_ADD(rx_fragments);
9479 ESTAT_ADD(rx_ucast_packets);
9480 ESTAT_ADD(rx_mcast_packets);
9481 ESTAT_ADD(rx_bcast_packets);
9482 ESTAT_ADD(rx_fcs_errors);
9483 ESTAT_ADD(rx_align_errors);
9484 ESTAT_ADD(rx_xon_pause_rcvd);
9485 ESTAT_ADD(rx_xoff_pause_rcvd);
9486 ESTAT_ADD(rx_mac_ctrl_rcvd);
9487 ESTAT_ADD(rx_xoff_entered);
9488 ESTAT_ADD(rx_frame_too_long_errors);
9489 ESTAT_ADD(rx_jabbers);
9490 ESTAT_ADD(rx_undersize_packets);
9491 ESTAT_ADD(rx_in_length_errors);
9492 ESTAT_ADD(rx_out_length_errors);
9493 ESTAT_ADD(rx_64_or_less_octet_packets);
9494 ESTAT_ADD(rx_65_to_127_octet_packets);
9495 ESTAT_ADD(rx_128_to_255_octet_packets);
9496 ESTAT_ADD(rx_256_to_511_octet_packets);
9497 ESTAT_ADD(rx_512_to_1023_octet_packets);
9498 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9499 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9500 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9501 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9502 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9503
9504 ESTAT_ADD(tx_octets);
9505 ESTAT_ADD(tx_collisions);
9506 ESTAT_ADD(tx_xon_sent);
9507 ESTAT_ADD(tx_xoff_sent);
9508 ESTAT_ADD(tx_flow_control);
9509 ESTAT_ADD(tx_mac_errors);
9510 ESTAT_ADD(tx_single_collisions);
9511 ESTAT_ADD(tx_mult_collisions);
9512 ESTAT_ADD(tx_deferred);
9513 ESTAT_ADD(tx_excessive_collisions);
9514 ESTAT_ADD(tx_late_collisions);
9515 ESTAT_ADD(tx_collide_2times);
9516 ESTAT_ADD(tx_collide_3times);
9517 ESTAT_ADD(tx_collide_4times);
9518 ESTAT_ADD(tx_collide_5times);
9519 ESTAT_ADD(tx_collide_6times);
9520 ESTAT_ADD(tx_collide_7times);
9521 ESTAT_ADD(tx_collide_8times);
9522 ESTAT_ADD(tx_collide_9times);
9523 ESTAT_ADD(tx_collide_10times);
9524 ESTAT_ADD(tx_collide_11times);
9525 ESTAT_ADD(tx_collide_12times);
9526 ESTAT_ADD(tx_collide_13times);
9527 ESTAT_ADD(tx_collide_14times);
9528 ESTAT_ADD(tx_collide_15times);
9529 ESTAT_ADD(tx_ucast_packets);
9530 ESTAT_ADD(tx_mcast_packets);
9531 ESTAT_ADD(tx_bcast_packets);
9532 ESTAT_ADD(tx_carrier_sense_errors);
9533 ESTAT_ADD(tx_discards);
9534 ESTAT_ADD(tx_errors);
9535
9536 ESTAT_ADD(dma_writeq_full);
9537 ESTAT_ADD(dma_write_prioq_full);
9538 ESTAT_ADD(rxbds_empty);
9539 ESTAT_ADD(rx_discards);
9540 ESTAT_ADD(rx_errors);
9541 ESTAT_ADD(rx_threshold_hit);
9542
9543 ESTAT_ADD(dma_readq_full);
9544 ESTAT_ADD(dma_read_prioq_full);
9545 ESTAT_ADD(tx_comp_queue_full);
9546
9547 ESTAT_ADD(ring_set_send_prod_index);
9548 ESTAT_ADD(ring_status_update);
9549 ESTAT_ADD(nic_irqs);
9550 ESTAT_ADD(nic_avoided_irqs);
9551 ESTAT_ADD(nic_tx_threshold_hit);
9552
Matt Carlson4452d092011-05-19 12:12:51 +00009553 ESTAT_ADD(mbuf_lwm_thresh_hit);
9554
Linus Torvalds1da177e2005-04-16 15:20:36 -07009555 return estats;
9556}
9557
Eric Dumazet511d2222010-07-07 20:44:24 +00009558static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9559 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009560{
9561 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +00009562 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009563 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9564
9565 if (!hw_stats)
9566 return old_stats;
9567
9568 stats->rx_packets = old_stats->rx_packets +
9569 get_stat64(&hw_stats->rx_ucast_packets) +
9570 get_stat64(&hw_stats->rx_mcast_packets) +
9571 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009572
Linus Torvalds1da177e2005-04-16 15:20:36 -07009573 stats->tx_packets = old_stats->tx_packets +
9574 get_stat64(&hw_stats->tx_ucast_packets) +
9575 get_stat64(&hw_stats->tx_mcast_packets) +
9576 get_stat64(&hw_stats->tx_bcast_packets);
9577
9578 stats->rx_bytes = old_stats->rx_bytes +
9579 get_stat64(&hw_stats->rx_octets);
9580 stats->tx_bytes = old_stats->tx_bytes +
9581 get_stat64(&hw_stats->tx_octets);
9582
9583 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009584 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009585 stats->tx_errors = old_stats->tx_errors +
9586 get_stat64(&hw_stats->tx_errors) +
9587 get_stat64(&hw_stats->tx_mac_errors) +
9588 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9589 get_stat64(&hw_stats->tx_discards);
9590
9591 stats->multicast = old_stats->multicast +
9592 get_stat64(&hw_stats->rx_mcast_packets);
9593 stats->collisions = old_stats->collisions +
9594 get_stat64(&hw_stats->tx_collisions);
9595
9596 stats->rx_length_errors = old_stats->rx_length_errors +
9597 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9598 get_stat64(&hw_stats->rx_undersize_packets);
9599
9600 stats->rx_over_errors = old_stats->rx_over_errors +
9601 get_stat64(&hw_stats->rxbds_empty);
9602 stats->rx_frame_errors = old_stats->rx_frame_errors +
9603 get_stat64(&hw_stats->rx_align_errors);
9604 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9605 get_stat64(&hw_stats->tx_discards);
9606 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
9607 get_stat64(&hw_stats->tx_carrier_sense_errors);
9608
9609 stats->rx_crc_errors = old_stats->rx_crc_errors +
9610 calc_crc_errors(tp);
9611
John W. Linville4f63b872005-09-12 14:43:18 -07009612 stats->rx_missed_errors = old_stats->rx_missed_errors +
9613 get_stat64(&hw_stats->rx_discards);
9614
Eric Dumazetb0057c52010-10-10 19:55:52 +00009615 stats->rx_dropped = tp->rx_dropped;
9616
Linus Torvalds1da177e2005-04-16 15:20:36 -07009617 return stats;
9618}
9619
9620static inline u32 calc_crc(unsigned char *buf, int len)
9621{
9622 u32 reg;
9623 u32 tmp;
9624 int j, k;
9625
9626 reg = 0xffffffff;
9627
9628 for (j = 0; j < len; j++) {
9629 reg ^= buf[j];
9630
9631 for (k = 0; k < 8; k++) {
9632 tmp = reg & 0x01;
9633
9634 reg >>= 1;
9635
Matt Carlson859a588792010-04-05 10:19:28 +00009636 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009637 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009638 }
9639 }
9640
9641 return ~reg;
9642}
9643
9644static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9645{
9646 /* accept or reject all multicast frames */
9647 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9648 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9649 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9650 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9651}
9652
9653static void __tg3_set_rx_mode(struct net_device *dev)
9654{
9655 struct tg3 *tp = netdev_priv(dev);
9656 u32 rx_mode;
9657
9658 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9659 RX_MODE_KEEP_VLAN_TAG);
9660
Matt Carlsonbf933c82011-01-25 15:58:49 +00009661#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009662 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9663 * flag clear.
9664 */
Joe Perches63c3a662011-04-26 08:12:10 +00009665 if (!tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009666 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9667#endif
9668
9669 if (dev->flags & IFF_PROMISC) {
9670 /* Promiscuous mode. */
9671 rx_mode |= RX_MODE_PROMISC;
9672 } else if (dev->flags & IFF_ALLMULTI) {
9673 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009674 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00009675 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009676 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009677 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009678 } else {
9679 /* Accept one or more multicast(s). */
Jiri Pirko22bedad32010-04-01 21:22:57 +00009680 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009681 u32 mc_filter[4] = { 0, };
9682 u32 regidx;
9683 u32 bit;
9684 u32 crc;
9685
Jiri Pirko22bedad32010-04-01 21:22:57 +00009686 netdev_for_each_mc_addr(ha, dev) {
9687 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009688 bit = ~crc & 0x7f;
9689 regidx = (bit & 0x60) >> 5;
9690 bit &= 0x1f;
9691 mc_filter[regidx] |= (1 << bit);
9692 }
9693
9694 tw32(MAC_HASH_REG_0, mc_filter[0]);
9695 tw32(MAC_HASH_REG_1, mc_filter[1]);
9696 tw32(MAC_HASH_REG_2, mc_filter[2]);
9697 tw32(MAC_HASH_REG_3, mc_filter[3]);
9698 }
9699
9700 if (rx_mode != tp->rx_mode) {
9701 tp->rx_mode = rx_mode;
9702 tw32_f(MAC_RX_MODE, rx_mode);
9703 udelay(10);
9704 }
9705}
9706
9707static void tg3_set_rx_mode(struct net_device *dev)
9708{
9709 struct tg3 *tp = netdev_priv(dev);
9710
Michael Chane75f7c92006-03-20 21:33:26 -08009711 if (!netif_running(dev))
9712 return;
9713
David S. Millerf47c11e2005-06-24 20:18:35 -07009714 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009715 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -07009716 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009717}
9718
Linus Torvalds1da177e2005-04-16 15:20:36 -07009719static int tg3_get_regs_len(struct net_device *dev)
9720{
Matt Carlson97bd8e42011-04-13 11:05:04 +00009721 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009722}
9723
9724static void tg3_get_regs(struct net_device *dev,
9725 struct ethtool_regs *regs, void *_p)
9726{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009727 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009728
9729 regs->version = 0;
9730
Matt Carlson97bd8e42011-04-13 11:05:04 +00009731 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009732
Matt Carlson800960682010-08-02 11:26:06 +00009733 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009734 return;
9735
David S. Millerf47c11e2005-06-24 20:18:35 -07009736 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009737
Matt Carlson97bd8e42011-04-13 11:05:04 +00009738 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009739
David S. Millerf47c11e2005-06-24 20:18:35 -07009740 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009741}
9742
9743static int tg3_get_eeprom_len(struct net_device *dev)
9744{
9745 struct tg3 *tp = netdev_priv(dev);
9746
9747 return tp->nvram_size;
9748}
9749
Linus Torvalds1da177e2005-04-16 15:20:36 -07009750static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9751{
9752 struct tg3 *tp = netdev_priv(dev);
9753 int ret;
9754 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -08009755 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009756 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009757
Joe Perches63c3a662011-04-26 08:12:10 +00009758 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +00009759 return -EINVAL;
9760
Matt Carlson800960682010-08-02 11:26:06 +00009761 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009762 return -EAGAIN;
9763
Linus Torvalds1da177e2005-04-16 15:20:36 -07009764 offset = eeprom->offset;
9765 len = eeprom->len;
9766 eeprom->len = 0;
9767
9768 eeprom->magic = TG3_EEPROM_MAGIC;
9769
9770 if (offset & 3) {
9771 /* adjustments to start on required 4 byte boundary */
9772 b_offset = offset & 3;
9773 b_count = 4 - b_offset;
9774 if (b_count > len) {
9775 /* i.e. offset=1 len=2 */
9776 b_count = len;
9777 }
Matt Carlsona9dc5292009-02-25 14:25:30 +00009778 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009779 if (ret)
9780 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +00009781 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009782 len -= b_count;
9783 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009784 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009785 }
9786
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009787 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009788 pd = &data[eeprom->len];
9789 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00009790 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009791 if (ret) {
9792 eeprom->len += i;
9793 return ret;
9794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009795 memcpy(pd + i, &val, 4);
9796 }
9797 eeprom->len += i;
9798
9799 if (len & 3) {
9800 /* read last bytes not ending on 4 byte boundary */
9801 pd = &data[eeprom->len];
9802 b_count = len & 3;
9803 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009804 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009805 if (ret)
9806 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009807 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009808 eeprom->len += b_count;
9809 }
9810 return 0;
9811}
9812
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009813static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009814
9815static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9816{
9817 struct tg3 *tp = netdev_priv(dev);
9818 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009819 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009820 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009821 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009822
Matt Carlson800960682010-08-02 11:26:06 +00009823 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009824 return -EAGAIN;
9825
Joe Perches63c3a662011-04-26 08:12:10 +00009826 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +00009827 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009828 return -EINVAL;
9829
9830 offset = eeprom->offset;
9831 len = eeprom->len;
9832
9833 if ((b_offset = (offset & 3))) {
9834 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +00009835 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009836 if (ret)
9837 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009838 len += b_offset;
9839 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -07009840 if (len < 4)
9841 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009842 }
9843
9844 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -07009845 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009846 /* adjustments to end on required 4 byte boundary */
9847 odd_len = 1;
9848 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009849 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009850 if (ret)
9851 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009852 }
9853
9854 buf = data;
9855 if (b_offset || odd_len) {
9856 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009857 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009858 return -ENOMEM;
9859 if (b_offset)
9860 memcpy(buf, &start, 4);
9861 if (odd_len)
9862 memcpy(buf+len-4, &end, 4);
9863 memcpy(buf + b_offset, data, eeprom->len);
9864 }
9865
9866 ret = tg3_nvram_write_block(tp, offset, len, buf);
9867
9868 if (buf != data)
9869 kfree(buf);
9870
9871 return ret;
9872}
9873
9874static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9875{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009876 struct tg3 *tp = netdev_priv(dev);
9877
Joe Perches63c3a662011-04-26 08:12:10 +00009878 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009879 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009880 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009881 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009882 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9883 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009884 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009885
Linus Torvalds1da177e2005-04-16 15:20:36 -07009886 cmd->supported = (SUPPORTED_Autoneg);
9887
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009888 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009889 cmd->supported |= (SUPPORTED_1000baseT_Half |
9890 SUPPORTED_1000baseT_Full);
9891
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009892 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009893 cmd->supported |= (SUPPORTED_100baseT_Half |
9894 SUPPORTED_100baseT_Full |
9895 SUPPORTED_10baseT_Half |
9896 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -08009897 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -07009898 cmd->port = PORT_TP;
9899 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009900 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -07009901 cmd->port = PORT_FIBRE;
9902 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009903
Linus Torvalds1da177e2005-04-16 15:20:36 -07009904 cmd->advertising = tp->link_config.advertising;
9905 if (netif_running(dev)) {
David Decotigny70739492011-04-27 18:32:40 +00009906 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009907 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson64c22182010-10-14 10:37:44 +00009908 } else {
David Decotigny70739492011-04-27 18:32:40 +00009909 ethtool_cmd_speed_set(cmd, SPEED_INVALID);
Matt Carlson64c22182010-10-14 10:37:44 +00009910 cmd->duplex = DUPLEX_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009911 }
Matt Carlson882e9792009-09-01 13:21:36 +00009912 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009913 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009914 cmd->autoneg = tp->link_config.autoneg;
9915 cmd->maxtxpkt = 0;
9916 cmd->maxrxpkt = 0;
9917 return 0;
9918}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009919
Linus Torvalds1da177e2005-04-16 15:20:36 -07009920static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9921{
9922 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +00009923 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009924
Joe Perches63c3a662011-04-26 08:12:10 +00009925 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009926 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009927 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009928 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009929 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9930 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009931 }
9932
Matt Carlson7e5856b2009-02-25 14:23:01 +00009933 if (cmd->autoneg != AUTONEG_ENABLE &&
9934 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -07009935 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009936
9937 if (cmd->autoneg == AUTONEG_DISABLE &&
9938 cmd->duplex != DUPLEX_FULL &&
9939 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -07009940 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009941
Matt Carlson7e5856b2009-02-25 14:23:01 +00009942 if (cmd->autoneg == AUTONEG_ENABLE) {
9943 u32 mask = ADVERTISED_Autoneg |
9944 ADVERTISED_Pause |
9945 ADVERTISED_Asym_Pause;
9946
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009947 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +00009948 mask |= ADVERTISED_1000baseT_Half |
9949 ADVERTISED_1000baseT_Full;
9950
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009951 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +00009952 mask |= ADVERTISED_100baseT_Half |
9953 ADVERTISED_100baseT_Full |
9954 ADVERTISED_10baseT_Half |
9955 ADVERTISED_10baseT_Full |
9956 ADVERTISED_TP;
9957 else
9958 mask |= ADVERTISED_FIBRE;
9959
9960 if (cmd->advertising & ~mask)
9961 return -EINVAL;
9962
9963 mask &= (ADVERTISED_1000baseT_Half |
9964 ADVERTISED_1000baseT_Full |
9965 ADVERTISED_100baseT_Half |
9966 ADVERTISED_100baseT_Full |
9967 ADVERTISED_10baseT_Half |
9968 ADVERTISED_10baseT_Full);
9969
9970 cmd->advertising &= mask;
9971 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009972 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +00009973 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +00009974 return -EINVAL;
9975
9976 if (cmd->duplex != DUPLEX_FULL)
9977 return -EINVAL;
9978 } else {
David Decotigny25db0332011-04-27 18:32:39 +00009979 if (speed != SPEED_100 &&
9980 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +00009981 return -EINVAL;
9982 }
9983 }
9984
David S. Millerf47c11e2005-06-24 20:18:35 -07009985 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009986
9987 tp->link_config.autoneg = cmd->autoneg;
9988 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -07009989 tp->link_config.advertising = (cmd->advertising |
9990 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009991 tp->link_config.speed = SPEED_INVALID;
9992 tp->link_config.duplex = DUPLEX_INVALID;
9993 } else {
9994 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +00009995 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009996 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009997 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009998
Michael Chan24fcad62006-12-17 17:06:46 -08009999 tp->link_config.orig_speed = tp->link_config.speed;
10000 tp->link_config.orig_duplex = tp->link_config.duplex;
10001 tp->link_config.orig_autoneg = tp->link_config.autoneg;
10002
Linus Torvalds1da177e2005-04-16 15:20:36 -070010003 if (netif_running(dev))
10004 tg3_setup_phy(tp, 1);
10005
David S. Millerf47c11e2005-06-24 20:18:35 -070010006 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010007
Linus Torvalds1da177e2005-04-16 15:20:36 -070010008 return 0;
10009}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010010
Linus Torvalds1da177e2005-04-16 15:20:36 -070010011static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10012{
10013 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010014
Linus Torvalds1da177e2005-04-16 15:20:36 -070010015 strcpy(info->driver, DRV_MODULE_NAME);
10016 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -080010017 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010018 strcpy(info->bus_info, pci_name(tp->pdev));
10019}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010020
Linus Torvalds1da177e2005-04-16 15:20:36 -070010021static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10022{
10023 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010024
Joe Perches63c3a662011-04-26 08:12:10 +000010025 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070010026 wol->supported = WAKE_MAGIC;
10027 else
10028 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010029 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010030 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010031 wol->wolopts = WAKE_MAGIC;
10032 memset(&wol->sopass, 0, sizeof(wol->sopass));
10033}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010034
Linus Torvalds1da177e2005-04-16 15:20:36 -070010035static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10036{
10037 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010038 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010039
Linus Torvalds1da177e2005-04-16 15:20:36 -070010040 if (wol->wolopts & ~WAKE_MAGIC)
10041 return -EINVAL;
10042 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010043 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010044 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010045
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010046 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
10047
David S. Millerf47c11e2005-06-24 20:18:35 -070010048 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010049 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000010050 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010051 else
Joe Perches63c3a662011-04-26 08:12:10 +000010052 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070010053 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010054
Linus Torvalds1da177e2005-04-16 15:20:36 -070010055 return 0;
10056}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010057
Linus Torvalds1da177e2005-04-16 15:20:36 -070010058static u32 tg3_get_msglevel(struct net_device *dev)
10059{
10060 struct tg3 *tp = netdev_priv(dev);
10061 return tp->msg_enable;
10062}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010063
Linus Torvalds1da177e2005-04-16 15:20:36 -070010064static void tg3_set_msglevel(struct net_device *dev, u32 value)
10065{
10066 struct tg3 *tp = netdev_priv(dev);
10067 tp->msg_enable = value;
10068}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010069
Linus Torvalds1da177e2005-04-16 15:20:36 -070010070static int tg3_nway_reset(struct net_device *dev)
10071{
10072 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010073 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010074
Linus Torvalds1da177e2005-04-16 15:20:36 -070010075 if (!netif_running(dev))
10076 return -EAGAIN;
10077
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010078 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010079 return -EINVAL;
10080
Joe Perches63c3a662011-04-26 08:12:10 +000010081 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010082 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010083 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010084 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010085 } else {
10086 u32 bmcr;
10087
10088 spin_lock_bh(&tp->lock);
10089 r = -EINVAL;
10090 tg3_readphy(tp, MII_BMCR, &bmcr);
10091 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10092 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010093 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010094 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10095 BMCR_ANENABLE);
10096 r = 0;
10097 }
10098 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010099 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010100
Linus Torvalds1da177e2005-04-16 15:20:36 -070010101 return r;
10102}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010103
Linus Torvalds1da177e2005-04-16 15:20:36 -070010104static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10105{
10106 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010107
Matt Carlson2c49a442010-09-30 10:34:35 +000010108 ering->rx_max_pending = tp->rx_std_ring_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010109 ering->rx_mini_max_pending = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010110 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000010111 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010112 else
10113 ering->rx_jumbo_max_pending = 0;
10114
10115 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010116
10117 ering->rx_pending = tp->rx_pending;
10118 ering->rx_mini_pending = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010119 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080010120 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10121 else
10122 ering->rx_jumbo_pending = 0;
10123
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010124 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010125}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010126
Linus Torvalds1da177e2005-04-16 15:20:36 -070010127static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10128{
10129 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010130 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010131
Matt Carlson2c49a442010-09-30 10:34:35 +000010132 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10133 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010134 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10135 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000010136 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010137 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010138 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010139
Michael Chanbbe832c2005-06-24 20:20:04 -070010140 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010141 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010142 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010143 irq_sync = 1;
10144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010145
Michael Chanbbe832c2005-06-24 20:20:04 -070010146 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010147
Linus Torvalds1da177e2005-04-16 15:20:36 -070010148 tp->rx_pending = ering->rx_pending;
10149
Joe Perches63c3a662011-04-26 08:12:10 +000010150 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010151 tp->rx_pending > 63)
10152 tp->rx_pending = 63;
10153 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010154
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010155 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010156 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010157
10158 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010159 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010160 err = tg3_restart_hw(tp, 1);
10161 if (!err)
10162 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010163 }
10164
David S. Millerf47c11e2005-06-24 20:18:35 -070010165 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010166
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010167 if (irq_sync && !err)
10168 tg3_phy_start(tp);
10169
Michael Chanb9ec6c12006-07-25 16:37:27 -070010170 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010171}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010172
Linus Torvalds1da177e2005-04-16 15:20:36 -070010173static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10174{
10175 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010176
Joe Perches63c3a662011-04-26 08:12:10 +000010177 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080010178
Steve Glendinninge18ce342008-12-16 02:00:00 -080010179 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010180 epause->rx_pause = 1;
10181 else
10182 epause->rx_pause = 0;
10183
Steve Glendinninge18ce342008-12-16 02:00:00 -080010184 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010185 epause->tx_pause = 1;
10186 else
10187 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010188}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010189
Linus Torvalds1da177e2005-04-16 15:20:36 -070010190static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10191{
10192 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010193 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010194
Joe Perches63c3a662011-04-26 08:12:10 +000010195 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000010196 u32 newadv;
10197 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010198
Matt Carlson27121682010-02-17 15:16:57 +000010199 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010200
Matt Carlson27121682010-02-17 15:16:57 +000010201 if (!(phydev->supported & SUPPORTED_Pause) ||
10202 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010203 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010204 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010205
Matt Carlson27121682010-02-17 15:16:57 +000010206 tp->link_config.flowctrl = 0;
10207 if (epause->rx_pause) {
10208 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010209
Matt Carlson27121682010-02-17 15:16:57 +000010210 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010211 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010212 newadv = ADVERTISED_Pause;
10213 } else
10214 newadv = ADVERTISED_Pause |
10215 ADVERTISED_Asym_Pause;
10216 } else if (epause->tx_pause) {
10217 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10218 newadv = ADVERTISED_Asym_Pause;
10219 } else
10220 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010221
Matt Carlson27121682010-02-17 15:16:57 +000010222 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010223 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010224 else
Joe Perches63c3a662011-04-26 08:12:10 +000010225 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010226
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010227 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010228 u32 oldadv = phydev->advertising &
10229 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10230 if (oldadv != newadv) {
10231 phydev->advertising &=
10232 ~(ADVERTISED_Pause |
10233 ADVERTISED_Asym_Pause);
10234 phydev->advertising |= newadv;
10235 if (phydev->autoneg) {
10236 /*
10237 * Always renegotiate the link to
10238 * inform our link partner of our
10239 * flow control settings, even if the
10240 * flow control is forced. Let
10241 * tg3_adjust_link() do the final
10242 * flow control setup.
10243 */
10244 return phy_start_aneg(phydev);
10245 }
10246 }
10247
10248 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010249 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010250 } else {
10251 tp->link_config.orig_advertising &=
10252 ~(ADVERTISED_Pause |
10253 ADVERTISED_Asym_Pause);
10254 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010255 }
10256 } else {
10257 int irq_sync = 0;
10258
10259 if (netif_running(dev)) {
10260 tg3_netif_stop(tp);
10261 irq_sync = 1;
10262 }
10263
10264 tg3_full_lock(tp, irq_sync);
10265
10266 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010267 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010268 else
Joe Perches63c3a662011-04-26 08:12:10 +000010269 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010270 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010271 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010272 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010273 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010274 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010275 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010276 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010277 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010278
10279 if (netif_running(dev)) {
10280 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10281 err = tg3_restart_hw(tp, 1);
10282 if (!err)
10283 tg3_netif_start(tp);
10284 }
10285
10286 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010288
Michael Chanb9ec6c12006-07-25 16:37:27 -070010289 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010290}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010291
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010292static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010293{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010294 switch (sset) {
10295 case ETH_SS_TEST:
10296 return TG3_NUM_TEST;
10297 case ETH_SS_STATS:
10298 return TG3_NUM_STATS;
10299 default:
10300 return -EOPNOTSUPP;
10301 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010302}
10303
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010304static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010305{
10306 switch (stringset) {
10307 case ETH_SS_STATS:
10308 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10309 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010310 case ETH_SS_TEST:
10311 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10312 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010313 default:
10314 WARN_ON(1); /* we need a WARN() */
10315 break;
10316 }
10317}
10318
stephen hemminger81b87092011-04-04 08:43:50 +000010319static int tg3_set_phys_id(struct net_device *dev,
10320 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070010321{
10322 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070010323
10324 if (!netif_running(tp->dev))
10325 return -EAGAIN;
10326
stephen hemminger81b87092011-04-04 08:43:50 +000010327 switch (state) {
10328 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000010329 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070010330
stephen hemminger81b87092011-04-04 08:43:50 +000010331 case ETHTOOL_ID_ON:
10332 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10333 LED_CTRL_1000MBPS_ON |
10334 LED_CTRL_100MBPS_ON |
10335 LED_CTRL_10MBPS_ON |
10336 LED_CTRL_TRAFFIC_OVERRIDE |
10337 LED_CTRL_TRAFFIC_BLINK |
10338 LED_CTRL_TRAFFIC_LED);
10339 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010340
stephen hemminger81b87092011-04-04 08:43:50 +000010341 case ETHTOOL_ID_OFF:
10342 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10343 LED_CTRL_TRAFFIC_OVERRIDE);
10344 break;
Michael Chan4009a932005-09-05 17:52:54 -070010345
stephen hemminger81b87092011-04-04 08:43:50 +000010346 case ETHTOOL_ID_INACTIVE:
10347 tw32(MAC_LED_CTRL, tp->led_ctrl);
10348 break;
Michael Chan4009a932005-09-05 17:52:54 -070010349 }
stephen hemminger81b87092011-04-04 08:43:50 +000010350
Michael Chan4009a932005-09-05 17:52:54 -070010351 return 0;
10352}
10353
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010354static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010355 struct ethtool_stats *estats, u64 *tmp_stats)
10356{
10357 struct tg3 *tp = netdev_priv(dev);
10358 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10359}
10360
Matt Carlsonc3e94502011-04-13 11:05:08 +000010361static __be32 * tg3_vpd_readblock(struct tg3 *tp)
10362{
10363 int i;
10364 __be32 *buf;
10365 u32 offset = 0, len = 0;
10366 u32 magic, val;
10367
Joe Perches63c3a662011-04-26 08:12:10 +000010368 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000010369 return NULL;
10370
10371 if (magic == TG3_EEPROM_MAGIC) {
10372 for (offset = TG3_NVM_DIR_START;
10373 offset < TG3_NVM_DIR_END;
10374 offset += TG3_NVM_DIRENT_SIZE) {
10375 if (tg3_nvram_read(tp, offset, &val))
10376 return NULL;
10377
10378 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
10379 TG3_NVM_DIRTYPE_EXTVPD)
10380 break;
10381 }
10382
10383 if (offset != TG3_NVM_DIR_END) {
10384 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
10385 if (tg3_nvram_read(tp, offset + 4, &offset))
10386 return NULL;
10387
10388 offset = tg3_nvram_logical_addr(tp, offset);
10389 }
10390 }
10391
10392 if (!offset || !len) {
10393 offset = TG3_NVM_VPD_OFF;
10394 len = TG3_NVM_VPD_LEN;
10395 }
10396
10397 buf = kmalloc(len, GFP_KERNEL);
10398 if (buf == NULL)
10399 return NULL;
10400
10401 if (magic == TG3_EEPROM_MAGIC) {
10402 for (i = 0; i < len; i += 4) {
10403 /* The data is in little-endian format in NVRAM.
10404 * Use the big-endian read routines to preserve
10405 * the byte order as it exists in NVRAM.
10406 */
10407 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
10408 goto error;
10409 }
10410 } else {
10411 u8 *ptr;
10412 ssize_t cnt;
10413 unsigned int pos = 0;
10414
10415 ptr = (u8 *)&buf[0];
10416 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
10417 cnt = pci_read_vpd(tp->pdev, pos,
10418 len - pos, ptr);
10419 if (cnt == -ETIMEDOUT || cnt == -EINTR)
10420 cnt = 0;
10421 else if (cnt < 0)
10422 goto error;
10423 }
10424 if (pos != len)
10425 goto error;
10426 }
10427
10428 return buf;
10429
10430error:
10431 kfree(buf);
10432 return NULL;
10433}
10434
Michael Chan566f86a2005-05-29 14:56:58 -070010435#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010436#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10437#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10438#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Michael Chanb16250e2006-09-27 16:10:14 -070010439#define NVRAM_SELFBOOT_HW_SIZE 0x20
10440#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010441
10442static int tg3_test_nvram(struct tg3 *tp)
10443{
Al Virob9fc7dc2007-12-17 22:59:57 -080010444 u32 csum, magic;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010445 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010446 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010447
Joe Perches63c3a662011-04-26 08:12:10 +000010448 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010449 return 0;
10450
Matt Carlsone4f34112009-02-25 14:25:00 +000010451 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010452 return -EIO;
10453
Michael Chan1b277772006-03-20 22:27:48 -080010454 if (magic == TG3_EEPROM_MAGIC)
10455 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010456 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010457 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10458 TG3_EEPROM_SB_FORMAT_1) {
10459 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10460 case TG3_EEPROM_SB_REVISION_0:
10461 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10462 break;
10463 case TG3_EEPROM_SB_REVISION_2:
10464 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10465 break;
10466 case TG3_EEPROM_SB_REVISION_3:
10467 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10468 break;
10469 default:
10470 return 0;
10471 }
10472 } else
Michael Chan1b277772006-03-20 22:27:48 -080010473 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010474 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10475 size = NVRAM_SELFBOOT_HW_SIZE;
10476 else
Michael Chan1b277772006-03-20 22:27:48 -080010477 return -EIO;
10478
10479 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010480 if (buf == NULL)
10481 return -ENOMEM;
10482
Michael Chan1b277772006-03-20 22:27:48 -080010483 err = -EIO;
10484 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010485 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10486 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010487 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010488 }
Michael Chan1b277772006-03-20 22:27:48 -080010489 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010490 goto out;
10491
Michael Chan1b277772006-03-20 22:27:48 -080010492 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010493 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010494 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010495 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010496 u8 *buf8 = (u8 *) buf, csum8 = 0;
10497
Al Virob9fc7dc2007-12-17 22:59:57 -080010498 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010499 TG3_EEPROM_SB_REVISION_2) {
10500 /* For rev 2, the csum doesn't include the MBA. */
10501 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10502 csum8 += buf8[i];
10503 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10504 csum8 += buf8[i];
10505 } else {
10506 for (i = 0; i < size; i++)
10507 csum8 += buf8[i];
10508 }
Michael Chan1b277772006-03-20 22:27:48 -080010509
Adrian Bunkad96b482006-04-05 22:21:04 -070010510 if (csum8 == 0) {
10511 err = 0;
10512 goto out;
10513 }
10514
10515 err = -EIO;
10516 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010517 }
Michael Chan566f86a2005-05-29 14:56:58 -070010518
Al Virob9fc7dc2007-12-17 22:59:57 -080010519 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010520 TG3_EEPROM_MAGIC_HW) {
10521 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010522 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010523 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010524
10525 /* Separate the parity bits and the data bytes. */
10526 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10527 if ((i == 0) || (i == 8)) {
10528 int l;
10529 u8 msk;
10530
10531 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10532 parity[k++] = buf8[i] & msk;
10533 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000010534 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010535 int l;
10536 u8 msk;
10537
10538 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10539 parity[k++] = buf8[i] & msk;
10540 i++;
10541
10542 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10543 parity[k++] = buf8[i] & msk;
10544 i++;
10545 }
10546 data[j++] = buf8[i];
10547 }
10548
10549 err = -EIO;
10550 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10551 u8 hw8 = hweight8(data[i]);
10552
10553 if ((hw8 & 0x1) && parity[i])
10554 goto out;
10555 else if (!(hw8 & 0x1) && !parity[i])
10556 goto out;
10557 }
10558 err = 0;
10559 goto out;
10560 }
10561
Matt Carlson01c3a392011-03-09 16:58:20 +000010562 err = -EIO;
10563
Michael Chan566f86a2005-05-29 14:56:58 -070010564 /* Bootstrap checksum at offset 0x10 */
10565 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000010566 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010567 goto out;
10568
10569 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10570 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000010571 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000010572 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010573
Matt Carlsonc3e94502011-04-13 11:05:08 +000010574 kfree(buf);
10575
10576 buf = tg3_vpd_readblock(tp);
10577 if (!buf)
10578 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000010579
10580 i = pci_vpd_find_tag((u8 *)buf, 0, TG3_NVM_VPD_LEN,
10581 PCI_VPD_LRDT_RO_DATA);
10582 if (i > 0) {
10583 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
10584 if (j < 0)
10585 goto out;
10586
10587 if (i + PCI_VPD_LRDT_TAG_SIZE + j > TG3_NVM_VPD_LEN)
10588 goto out;
10589
10590 i += PCI_VPD_LRDT_TAG_SIZE;
10591 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
10592 PCI_VPD_RO_KEYWORD_CHKSUM);
10593 if (j > 0) {
10594 u8 csum8 = 0;
10595
10596 j += PCI_VPD_INFO_FLD_HDR_SIZE;
10597
10598 for (i = 0; i <= j; i++)
10599 csum8 += ((u8 *)buf)[i];
10600
10601 if (csum8)
10602 goto out;
10603 }
10604 }
10605
Michael Chan566f86a2005-05-29 14:56:58 -070010606 err = 0;
10607
10608out:
10609 kfree(buf);
10610 return err;
10611}
10612
Michael Chanca430072005-05-29 14:57:23 -070010613#define TG3_SERDES_TIMEOUT_SEC 2
10614#define TG3_COPPER_TIMEOUT_SEC 6
10615
10616static int tg3_test_link(struct tg3 *tp)
10617{
10618 int i, max;
10619
10620 if (!netif_running(tp->dev))
10621 return -ENODEV;
10622
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010623 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070010624 max = TG3_SERDES_TIMEOUT_SEC;
10625 else
10626 max = TG3_COPPER_TIMEOUT_SEC;
10627
10628 for (i = 0; i < max; i++) {
10629 if (netif_carrier_ok(tp->dev))
10630 return 0;
10631
10632 if (msleep_interruptible(1000))
10633 break;
10634 }
10635
10636 return -EIO;
10637}
10638
Michael Chana71116d2005-05-29 14:58:11 -070010639/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080010640static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070010641{
Michael Chanb16250e2006-09-27 16:10:14 -070010642 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070010643 u32 offset, read_mask, write_mask, val, save_val, read_val;
10644 static struct {
10645 u16 offset;
10646 u16 flags;
10647#define TG3_FL_5705 0x1
10648#define TG3_FL_NOT_5705 0x2
10649#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070010650#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070010651 u32 read_mask;
10652 u32 write_mask;
10653 } reg_tbl[] = {
10654 /* MAC Control Registers */
10655 { MAC_MODE, TG3_FL_NOT_5705,
10656 0x00000000, 0x00ef6f8c },
10657 { MAC_MODE, TG3_FL_5705,
10658 0x00000000, 0x01ef6b8c },
10659 { MAC_STATUS, TG3_FL_NOT_5705,
10660 0x03800107, 0x00000000 },
10661 { MAC_STATUS, TG3_FL_5705,
10662 0x03800100, 0x00000000 },
10663 { MAC_ADDR_0_HIGH, 0x0000,
10664 0x00000000, 0x0000ffff },
10665 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010666 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070010667 { MAC_RX_MTU_SIZE, 0x0000,
10668 0x00000000, 0x0000ffff },
10669 { MAC_TX_MODE, 0x0000,
10670 0x00000000, 0x00000070 },
10671 { MAC_TX_LENGTHS, 0x0000,
10672 0x00000000, 0x00003fff },
10673 { MAC_RX_MODE, TG3_FL_NOT_5705,
10674 0x00000000, 0x000007fc },
10675 { MAC_RX_MODE, TG3_FL_5705,
10676 0x00000000, 0x000007dc },
10677 { MAC_HASH_REG_0, 0x0000,
10678 0x00000000, 0xffffffff },
10679 { MAC_HASH_REG_1, 0x0000,
10680 0x00000000, 0xffffffff },
10681 { MAC_HASH_REG_2, 0x0000,
10682 0x00000000, 0xffffffff },
10683 { MAC_HASH_REG_3, 0x0000,
10684 0x00000000, 0xffffffff },
10685
10686 /* Receive Data and Receive BD Initiator Control Registers. */
10687 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
10688 0x00000000, 0xffffffff },
10689 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
10690 0x00000000, 0xffffffff },
10691 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
10692 0x00000000, 0x00000003 },
10693 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
10694 0x00000000, 0xffffffff },
10695 { RCVDBDI_STD_BD+0, 0x0000,
10696 0x00000000, 0xffffffff },
10697 { RCVDBDI_STD_BD+4, 0x0000,
10698 0x00000000, 0xffffffff },
10699 { RCVDBDI_STD_BD+8, 0x0000,
10700 0x00000000, 0xffff0002 },
10701 { RCVDBDI_STD_BD+0xc, 0x0000,
10702 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010703
Michael Chana71116d2005-05-29 14:58:11 -070010704 /* Receive BD Initiator Control Registers. */
10705 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
10706 0x00000000, 0xffffffff },
10707 { RCVBDI_STD_THRESH, TG3_FL_5705,
10708 0x00000000, 0x000003ff },
10709 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
10710 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010711
Michael Chana71116d2005-05-29 14:58:11 -070010712 /* Host Coalescing Control Registers. */
10713 { HOSTCC_MODE, TG3_FL_NOT_5705,
10714 0x00000000, 0x00000004 },
10715 { HOSTCC_MODE, TG3_FL_5705,
10716 0x00000000, 0x000000f6 },
10717 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
10718 0x00000000, 0xffffffff },
10719 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
10720 0x00000000, 0x000003ff },
10721 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
10722 0x00000000, 0xffffffff },
10723 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
10724 0x00000000, 0x000003ff },
10725 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
10726 0x00000000, 0xffffffff },
10727 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10728 0x00000000, 0x000000ff },
10729 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
10730 0x00000000, 0xffffffff },
10731 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10732 0x00000000, 0x000000ff },
10733 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
10734 0x00000000, 0xffffffff },
10735 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
10736 0x00000000, 0xffffffff },
10737 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10738 0x00000000, 0xffffffff },
10739 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10740 0x00000000, 0x000000ff },
10741 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10742 0x00000000, 0xffffffff },
10743 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10744 0x00000000, 0x000000ff },
10745 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
10746 0x00000000, 0xffffffff },
10747 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
10748 0x00000000, 0xffffffff },
10749 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
10750 0x00000000, 0xffffffff },
10751 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
10752 0x00000000, 0xffffffff },
10753 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
10754 0x00000000, 0xffffffff },
10755 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
10756 0xffffffff, 0x00000000 },
10757 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
10758 0xffffffff, 0x00000000 },
10759
10760 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070010761 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010762 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070010763 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010764 0x00000000, 0x007fffff },
10765 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
10766 0x00000000, 0x0000003f },
10767 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
10768 0x00000000, 0x000001ff },
10769 { BUFMGR_MB_HIGH_WATER, 0x0000,
10770 0x00000000, 0x000001ff },
10771 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
10772 0xffffffff, 0x00000000 },
10773 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
10774 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010775
Michael Chana71116d2005-05-29 14:58:11 -070010776 /* Mailbox Registers */
10777 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
10778 0x00000000, 0x000001ff },
10779 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
10780 0x00000000, 0x000001ff },
10781 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
10782 0x00000000, 0x000007ff },
10783 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
10784 0x00000000, 0x000001ff },
10785
10786 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
10787 };
10788
Michael Chanb16250e2006-09-27 16:10:14 -070010789 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010790 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070010791 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000010792 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070010793 is_5750 = 1;
10794 }
Michael Chana71116d2005-05-29 14:58:11 -070010795
10796 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
10797 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
10798 continue;
10799
10800 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
10801 continue;
10802
Joe Perches63c3a662011-04-26 08:12:10 +000010803 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070010804 (reg_tbl[i].flags & TG3_FL_NOT_5788))
10805 continue;
10806
Michael Chanb16250e2006-09-27 16:10:14 -070010807 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
10808 continue;
10809
Michael Chana71116d2005-05-29 14:58:11 -070010810 offset = (u32) reg_tbl[i].offset;
10811 read_mask = reg_tbl[i].read_mask;
10812 write_mask = reg_tbl[i].write_mask;
10813
10814 /* Save the original register content */
10815 save_val = tr32(offset);
10816
10817 /* Determine the read-only value. */
10818 read_val = save_val & read_mask;
10819
10820 /* Write zero to the register, then make sure the read-only bits
10821 * are not changed and the read/write bits are all zeros.
10822 */
10823 tw32(offset, 0);
10824
10825 val = tr32(offset);
10826
10827 /* Test the read-only and read/write bits. */
10828 if (((val & read_mask) != read_val) || (val & write_mask))
10829 goto out;
10830
10831 /* Write ones to all the bits defined by RdMask and WrMask, then
10832 * make sure the read-only bits are not changed and the
10833 * read/write bits are all ones.
10834 */
10835 tw32(offset, read_mask | write_mask);
10836
10837 val = tr32(offset);
10838
10839 /* Test the read-only bits. */
10840 if ((val & read_mask) != read_val)
10841 goto out;
10842
10843 /* Test the read/write bits. */
10844 if ((val & write_mask) != write_mask)
10845 goto out;
10846
10847 tw32(offset, save_val);
10848 }
10849
10850 return 0;
10851
10852out:
Michael Chan9f88f292006-12-07 00:22:54 -080010853 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000010854 netdev_err(tp->dev,
10855 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070010856 tw32(offset, save_val);
10857 return -EIO;
10858}
10859
Michael Chan7942e1d2005-05-29 14:58:36 -070010860static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
10861{
Arjan van de Venf71e1302006-03-03 21:33:57 -050010862 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070010863 int i;
10864 u32 j;
10865
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020010866 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070010867 for (j = 0; j < len; j += 4) {
10868 u32 val;
10869
10870 tg3_write_mem(tp, offset + j, test_pattern[i]);
10871 tg3_read_mem(tp, offset + j, &val);
10872 if (val != test_pattern[i])
10873 return -EIO;
10874 }
10875 }
10876 return 0;
10877}
10878
10879static int tg3_test_memory(struct tg3 *tp)
10880{
10881 static struct mem_entry {
10882 u32 offset;
10883 u32 len;
10884 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080010885 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070010886 { 0x00002000, 0x1c000},
10887 { 0xffffffff, 0x00000}
10888 }, mem_tbl_5705[] = {
10889 { 0x00000100, 0x0000c},
10890 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070010891 { 0x00004000, 0x00800},
10892 { 0x00006000, 0x01000},
10893 { 0x00008000, 0x02000},
10894 { 0x00010000, 0x0e000},
10895 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080010896 }, mem_tbl_5755[] = {
10897 { 0x00000200, 0x00008},
10898 { 0x00004000, 0x00800},
10899 { 0x00006000, 0x00800},
10900 { 0x00008000, 0x02000},
10901 { 0x00010000, 0x0c000},
10902 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070010903 }, mem_tbl_5906[] = {
10904 { 0x00000200, 0x00008},
10905 { 0x00004000, 0x00400},
10906 { 0x00006000, 0x00400},
10907 { 0x00008000, 0x01000},
10908 { 0x00010000, 0x01000},
10909 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010910 }, mem_tbl_5717[] = {
10911 { 0x00000200, 0x00008},
10912 { 0x00010000, 0x0a000},
10913 { 0x00020000, 0x13c00},
10914 { 0xffffffff, 0x00000}
10915 }, mem_tbl_57765[] = {
10916 { 0x00000200, 0x00008},
10917 { 0x00004000, 0x00800},
10918 { 0x00006000, 0x09800},
10919 { 0x00010000, 0x0a000},
10920 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070010921 };
10922 struct mem_entry *mem_tbl;
10923 int err = 0;
10924 int i;
10925
Joe Perches63c3a662011-04-26 08:12:10 +000010926 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010927 mem_tbl = mem_tbl_5717;
10928 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
10929 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000010930 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080010931 mem_tbl = mem_tbl_5755;
10932 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
10933 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000010934 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080010935 mem_tbl = mem_tbl_5705;
10936 else
Michael Chan7942e1d2005-05-29 14:58:36 -070010937 mem_tbl = mem_tbl_570x;
10938
10939 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000010940 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
10941 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070010942 break;
10943 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010944
Michael Chan7942e1d2005-05-29 14:58:36 -070010945 return err;
10946}
10947
Michael Chan9f40dea2005-09-05 17:53:06 -070010948#define TG3_MAC_LOOPBACK 0
10949#define TG3_PHY_LOOPBACK 1
Matt Carlsonbb158d62011-04-25 12:42:47 +000010950#define TG3_TSO_LOOPBACK 2
10951
10952#define TG3_TSO_MSS 500
10953
10954#define TG3_TSO_IP_HDR_LEN 20
10955#define TG3_TSO_TCP_HDR_LEN 20
10956#define TG3_TSO_TCP_OPT_LEN 12
10957
10958static const u8 tg3_tso_header[] = {
109590x08, 0x00,
109600x45, 0x00, 0x00, 0x00,
109610x00, 0x00, 0x40, 0x00,
109620x40, 0x06, 0x00, 0x00,
109630x0a, 0x00, 0x00, 0x01,
109640x0a, 0x00, 0x00, 0x02,
109650x0d, 0x00, 0xe0, 0x00,
109660x00, 0x00, 0x01, 0x00,
109670x00, 0x00, 0x02, 0x00,
109680x80, 0x10, 0x10, 0x00,
109690x14, 0x09, 0x00, 0x00,
109700x01, 0x01, 0x08, 0x0a,
109710x11, 0x11, 0x11, 0x11,
109720x11, 0x11, 0x11, 0x11,
10973};
Michael Chan9f40dea2005-09-05 17:53:06 -070010974
Matt Carlson4852a862011-04-13 11:05:07 +000010975static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, int loopback_mode)
Michael Chanc76949a2005-05-29 14:58:59 -070010976{
Michael Chan9f40dea2005-09-05 17:53:06 -070010977 u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000010978 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Michael Chanc76949a2005-05-29 14:58:59 -070010979 struct sk_buff *skb, *rx_skb;
10980 u8 *tx_data;
10981 dma_addr_t map;
10982 int num_pkts, tx_len, rx_len, i, err;
10983 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000010984 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000010985 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070010986
Matt Carlsonc8873402010-02-12 14:47:11 +000010987 tnapi = &tp->napi[0];
10988 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010989 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000010990 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000010991 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000010992 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000010993 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010994 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010995 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000010996
Michael Chan9f40dea2005-09-05 17:53:06 -070010997 if (loopback_mode == TG3_MAC_LOOPBACK) {
Michael Chanc94e3942005-09-27 12:12:42 -070010998 /* HW errata - mac loopback fails in some cases on 5780.
10999 * Normal traffic and PHY loopback are not affected by
Matt Carlsonaba49f22011-01-25 15:58:53 +000011000 * errata. Also, the MAC loopback test is deprecated for
11001 * all newer ASIC revisions.
Michael Chanc94e3942005-09-27 12:12:42 -070011002 */
Matt Carlsonaba49f22011-01-25 15:58:53 +000011003 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000011004 tg3_flag(tp, CPMU_PRESENT))
Michael Chanc94e3942005-09-27 12:12:42 -070011005 return 0;
11006
Matt Carlson49692ca2011-01-25 15:58:52 +000011007 mac_mode = tp->mac_mode &
11008 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
11009 mac_mode |= MAC_MODE_PORT_INT_LPBACK;
Joe Perches63c3a662011-04-26 08:12:10 +000011010 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011011 mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011012 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
Michael Chan3f7045c2006-09-27 16:02:29 -070011013 mac_mode |= MAC_MODE_PORT_MODE_MII;
11014 else
11015 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chan9f40dea2005-09-05 17:53:06 -070011016 tw32(MAC_MODE, mac_mode);
Matt Carlsonbb158d62011-04-25 12:42:47 +000011017 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011018 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +000011019 tg3_phy_fet_toggle_apd(tp, false);
Michael Chan5d64ad32006-12-07 00:19:40 -080011020 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
11021 } else
11022 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
Michael Chan3f7045c2006-09-27 16:02:29 -070011023
Matt Carlson9ef8ca92007-07-11 19:48:29 -070011024 tg3_phy_toggle_automdix(tp, 0);
11025
Michael Chan3f7045c2006-09-27 16:02:29 -070011026 tg3_writephy(tp, MII_BMCR, val);
Michael Chanc94e3942005-09-27 12:12:42 -070011027 udelay(40);
Michael Chan5d64ad32006-12-07 00:19:40 -080011028
Matt Carlson49692ca2011-01-25 15:58:52 +000011029 mac_mode = tp->mac_mode &
11030 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011031 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson1061b7c2010-02-12 14:47:12 +000011032 tg3_writephy(tp, MII_TG3_FET_PTEST,
11033 MII_TG3_FET_PTEST_FRC_TX_LINK |
11034 MII_TG3_FET_PTEST_FRC_TX_LOCK);
11035 /* The write needs to be flushed for the AC131 */
11036 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
11037 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
Michael Chan5d64ad32006-12-07 00:19:40 -080011038 mac_mode |= MAC_MODE_PORT_MODE_MII;
11039 } else
11040 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chanb16250e2006-09-27 16:10:14 -070011041
Michael Chanc94e3942005-09-27 12:12:42 -070011042 /* reset to prevent losing 1st rx packet intermittently */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011043 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Michael Chanc94e3942005-09-27 12:12:42 -070011044 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
11045 udelay(10);
11046 tw32_f(MAC_RX_MODE, tp->rx_mode);
11047 }
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011048 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlson79eb6902010-02-17 15:17:03 +000011049 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
11050 if (masked_phy_id == TG3_PHY_ID_BCM5401)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011051 mac_mode &= ~MAC_MODE_LINK_POLARITY;
Matt Carlson79eb6902010-02-17 15:17:03 +000011052 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011053 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chanff18ff02006-03-27 23:17:27 -080011054 tg3_writephy(tp, MII_TG3_EXT_CTRL,
11055 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
11056 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011057 tw32(MAC_MODE, mac_mode);
Matt Carlson49692ca2011-01-25 15:58:52 +000011058
11059 /* Wait for link */
11060 for (i = 0; i < 100; i++) {
11061 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
11062 break;
11063 mdelay(1);
11064 }
Matt Carlson859a588792010-04-05 10:19:28 +000011065 }
Michael Chanc76949a2005-05-29 14:58:59 -070011066
11067 err = -EIO;
11068
Matt Carlson4852a862011-04-13 11:05:07 +000011069 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070011070 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070011071 if (!skb)
11072 return -ENOMEM;
11073
Michael Chanc76949a2005-05-29 14:58:59 -070011074 tx_data = skb_put(skb, tx_len);
11075 memcpy(tx_data, tp->dev->dev_addr, 6);
11076 memset(tx_data + 6, 0x0, 8);
11077
Matt Carlson4852a862011-04-13 11:05:07 +000011078 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070011079
Matt Carlsonbb158d62011-04-25 12:42:47 +000011080 if (loopback_mode == TG3_TSO_LOOPBACK) {
11081 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
11082
11083 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
11084 TG3_TSO_TCP_OPT_LEN;
11085
11086 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
11087 sizeof(tg3_tso_header));
11088 mss = TG3_TSO_MSS;
11089
11090 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
11091 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
11092
11093 /* Set the total length field in the IP header */
11094 iph->tot_len = htons((u16)(mss + hdr_len));
11095
11096 base_flags = (TXD_FLAG_CPU_PRE_DMA |
11097 TXD_FLAG_CPU_POST_DMA);
11098
Joe Perches63c3a662011-04-26 08:12:10 +000011099 if (tg3_flag(tp, HW_TSO_1) ||
11100 tg3_flag(tp, HW_TSO_2) ||
11101 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011102 struct tcphdr *th;
11103 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
11104 th = (struct tcphdr *)&tx_data[val];
11105 th->check = 0;
11106 } else
11107 base_flags |= TXD_FLAG_TCPUDP_CSUM;
11108
Joe Perches63c3a662011-04-26 08:12:10 +000011109 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011110 mss |= (hdr_len & 0xc) << 12;
11111 if (hdr_len & 0x10)
11112 base_flags |= 0x00000010;
11113 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000011114 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011115 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000011116 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlsonbb158d62011-04-25 12:42:47 +000011117 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
11118 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
11119 } else {
11120 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
11121 }
11122
11123 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
11124 } else {
11125 num_pkts = 1;
11126 data_off = ETH_HLEN;
11127 }
11128
11129 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070011130 tx_data[i] = (u8) (i & 0xff);
11131
Alexander Duyckf4188d82009-12-02 16:48:38 +000011132 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
11133 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000011134 dev_kfree_skb(skb);
11135 return -EIO;
11136 }
Michael Chanc76949a2005-05-29 14:58:59 -070011137
11138 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011139 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011140
11141 udelay(10);
11142
Matt Carlson898a56f2009-08-28 14:02:40 +000011143 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011144
Matt Carlsonbb158d62011-04-25 12:42:47 +000011145 tg3_set_txd(tnapi, tnapi->tx_prod, map, tx_len,
11146 base_flags, (mss << 1) | 1);
Michael Chanc76949a2005-05-29 14:58:59 -070011147
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011148 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011149
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011150 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11151 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011152
11153 udelay(10);
11154
Matt Carlson303fc922009-11-02 14:27:34 +000011155 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11156 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011157 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011158 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011159
11160 udelay(10);
11161
Matt Carlson898a56f2009-08-28 14:02:40 +000011162 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11163 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011164 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011165 (rx_idx == (rx_start_idx + num_pkts)))
11166 break;
11167 }
11168
Alexander Duyckf4188d82009-12-02 16:48:38 +000011169 pci_unmap_single(tp->pdev, map, tx_len, PCI_DMA_TODEVICE);
Michael Chanc76949a2005-05-29 14:58:59 -070011170 dev_kfree_skb(skb);
11171
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011172 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011173 goto out;
11174
11175 if (rx_idx != rx_start_idx + num_pkts)
11176 goto out;
11177
Matt Carlsonbb158d62011-04-25 12:42:47 +000011178 val = data_off;
11179 while (rx_idx != rx_start_idx) {
11180 desc = &rnapi->rx_rcb[rx_start_idx++];
11181 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11182 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070011183
Matt Carlsonbb158d62011-04-25 12:42:47 +000011184 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11185 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000011186 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070011187
Matt Carlsonbb158d62011-04-25 12:42:47 +000011188 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
11189 - ETH_FCS_LEN;
11190
11191 if (loopback_mode != TG3_TSO_LOOPBACK) {
11192 if (rx_len != tx_len)
11193 goto out;
11194
11195 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
11196 if (opaque_key != RXD_OPAQUE_RING_STD)
11197 goto out;
11198 } else {
11199 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
11200 goto out;
11201 }
11202 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
11203 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000011204 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011205 goto out;
11206 }
11207
11208 if (opaque_key == RXD_OPAQUE_RING_STD) {
11209 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
11210 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
11211 mapping);
11212 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
11213 rx_skb = tpr->rx_jmb_buffers[desc_idx].skb;
11214 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
11215 mapping);
11216 } else
Matt Carlson4852a862011-04-13 11:05:07 +000011217 goto out;
11218
Matt Carlsonbb158d62011-04-25 12:42:47 +000011219 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
11220 PCI_DMA_FROMDEVICE);
11221
11222 for (i = data_off; i < rx_len; i++, val++) {
11223 if (*(rx_skb->data + i) != (u8) (val & 0xff))
11224 goto out;
11225 }
Matt Carlson4852a862011-04-13 11:05:07 +000011226 }
11227
Michael Chanc76949a2005-05-29 14:58:59 -070011228 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011229
Michael Chanc76949a2005-05-29 14:58:59 -070011230 /* tg3_free_rings will unmap and free the rx_skb */
11231out:
11232 return err;
11233}
11234
Matt Carlson00c266b2011-04-25 12:42:46 +000011235#define TG3_STD_LOOPBACK_FAILED 1
11236#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000011237#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson00c266b2011-04-25 12:42:46 +000011238
11239#define TG3_MAC_LOOPBACK_SHIFT 0
11240#define TG3_PHY_LOOPBACK_SHIFT 4
Matt Carlsonbb158d62011-04-25 12:42:47 +000011241#define TG3_LOOPBACK_FAILED 0x00000077
Michael Chan9f40dea2005-09-05 17:53:06 -070011242
11243static int tg3_test_loopback(struct tg3 *tp)
11244{
11245 int err = 0;
Matt Carlsonab789042011-01-25 15:58:54 +000011246 u32 eee_cap, cpmuctrl = 0;
Michael Chan9f40dea2005-09-05 17:53:06 -070011247
11248 if (!netif_running(tp->dev))
11249 return TG3_LOOPBACK_FAILED;
11250
Matt Carlsonab789042011-01-25 15:58:54 +000011251 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11252 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11253
Michael Chanb9ec6c12006-07-25 16:37:27 -070011254 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011255 if (err) {
11256 err = TG3_LOOPBACK_FAILED;
11257 goto done;
11258 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011259
Joe Perches63c3a662011-04-26 08:12:10 +000011260 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000011261 int i;
11262
11263 /* Reroute all rx packets to the 1st queue */
11264 for (i = MAC_RSS_INDIR_TBL_0;
11265 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
11266 tw32(i, 0x0);
11267 }
11268
Matt Carlson6833c042008-11-21 17:18:59 -080011269 /* Turn off gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011270 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080011271 tg3_phy_toggle_apd(tp, false);
11272
Joe Perches63c3a662011-04-26 08:12:10 +000011273 if (tg3_flag(tp, CPMU_PRESENT)) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070011274 int i;
11275 u32 status;
11276
11277 tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
11278
11279 /* Wait for up to 40 microseconds to acquire lock. */
11280 for (i = 0; i < 4; i++) {
11281 status = tr32(TG3_CPMU_MUTEX_GNT);
11282 if (status == CPMU_MUTEX_GNT_DRIVER)
11283 break;
11284 udelay(10);
11285 }
11286
Matt Carlsonab789042011-01-25 15:58:54 +000011287 if (status != CPMU_MUTEX_GNT_DRIVER) {
11288 err = TG3_LOOPBACK_FAILED;
11289 goto done;
11290 }
Matt Carlson9936bcf2007-10-10 18:03:07 -070011291
Matt Carlsonb2a5c192008-04-03 21:44:44 -070011292 /* Turn off link-based power management. */
Matt Carlsone8750932007-11-12 21:11:51 -080011293 cpmuctrl = tr32(TG3_CPMU_CTRL);
Matt Carlson109115e2008-05-02 16:48:59 -070011294 tw32(TG3_CPMU_CTRL,
11295 cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
11296 CPMU_CTRL_LINK_AWARE_MODE));
Matt Carlson9936bcf2007-10-10 18:03:07 -070011297 }
11298
Matt Carlson4852a862011-04-13 11:05:07 +000011299 if (tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_MAC_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011300 err |= TG3_STD_LOOPBACK_FAILED << TG3_MAC_LOOPBACK_SHIFT;
Matt Carlson9936bcf2007-10-10 18:03:07 -070011301
Joe Perches63c3a662011-04-26 08:12:10 +000011302 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson4852a862011-04-13 11:05:07 +000011303 tg3_run_loopback(tp, 9000 + ETH_HLEN, TG3_MAC_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011304 err |= TG3_JMB_LOOPBACK_FAILED << TG3_MAC_LOOPBACK_SHIFT;
Matt Carlson4852a862011-04-13 11:05:07 +000011305
Joe Perches63c3a662011-04-26 08:12:10 +000011306 if (tg3_flag(tp, CPMU_PRESENT)) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070011307 tw32(TG3_CPMU_CTRL, cpmuctrl);
11308
11309 /* Release the mutex */
11310 tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
11311 }
11312
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011313 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011314 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson4852a862011-04-13 11:05:07 +000011315 if (tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_PHY_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011316 err |= TG3_STD_LOOPBACK_FAILED <<
11317 TG3_PHY_LOOPBACK_SHIFT;
Joe Perches63c3a662011-04-26 08:12:10 +000011318 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonbb158d62011-04-25 12:42:47 +000011319 tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_TSO_LOOPBACK))
11320 err |= TG3_TSO_LOOPBACK_FAILED <<
11321 TG3_PHY_LOOPBACK_SHIFT;
Joe Perches63c3a662011-04-26 08:12:10 +000011322 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson4852a862011-04-13 11:05:07 +000011323 tg3_run_loopback(tp, 9000 + ETH_HLEN, TG3_PHY_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011324 err |= TG3_JMB_LOOPBACK_FAILED <<
11325 TG3_PHY_LOOPBACK_SHIFT;
Michael Chan9f40dea2005-09-05 17:53:06 -070011326 }
11327
Matt Carlson6833c042008-11-21 17:18:59 -080011328 /* Re-enable gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011329 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080011330 tg3_phy_toggle_apd(tp, true);
11331
Matt Carlsonab789042011-01-25 15:58:54 +000011332done:
11333 tp->phy_flags |= eee_cap;
11334
Michael Chan9f40dea2005-09-05 17:53:06 -070011335 return err;
11336}
11337
Michael Chan4cafd3f2005-05-29 14:56:34 -070011338static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11339 u64 *data)
11340{
Michael Chan566f86a2005-05-29 14:56:58 -070011341 struct tg3 *tp = netdev_priv(dev);
11342
Matt Carlson800960682010-08-02 11:26:06 +000011343 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011344 tg3_power_up(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011345
Michael Chan566f86a2005-05-29 14:56:58 -070011346 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
11347
11348 if (tg3_test_nvram(tp) != 0) {
11349 etest->flags |= ETH_TEST_FL_FAILED;
11350 data[0] = 1;
11351 }
Michael Chanca430072005-05-29 14:57:23 -070011352 if (tg3_test_link(tp) != 0) {
11353 etest->flags |= ETH_TEST_FL_FAILED;
11354 data[1] = 1;
11355 }
Michael Chana71116d2005-05-29 14:58:11 -070011356 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011357 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070011358
Michael Chanbbe832c2005-06-24 20:20:04 -070011359 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011360 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011361 tg3_netif_stop(tp);
11362 irq_sync = 1;
11363 }
11364
11365 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070011366
11367 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080011368 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011369 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000011370 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070011371 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080011372 if (!err)
11373 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011374
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011375 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad2006-03-20 22:27:35 -080011376 tg3_phy_reset(tp);
11377
Michael Chana71116d2005-05-29 14:58:11 -070011378 if (tg3_test_registers(tp) != 0) {
11379 etest->flags |= ETH_TEST_FL_FAILED;
11380 data[2] = 1;
11381 }
Michael Chan7942e1d2005-05-29 14:58:36 -070011382 if (tg3_test_memory(tp) != 0) {
11383 etest->flags |= ETH_TEST_FL_FAILED;
11384 data[3] = 1;
11385 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011386 if ((data[4] = tg3_test_loopback(tp)) != 0)
Michael Chanc76949a2005-05-29 14:58:59 -070011387 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070011388
David S. Millerf47c11e2005-06-24 20:18:35 -070011389 tg3_full_unlock(tp);
11390
Michael Chand4bc3922005-05-29 14:59:20 -070011391 if (tg3_test_interrupt(tp) != 0) {
11392 etest->flags |= ETH_TEST_FL_FAILED;
11393 data[5] = 1;
11394 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011395
11396 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070011397
Michael Chana71116d2005-05-29 14:58:11 -070011398 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11399 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011400 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011401 err2 = tg3_restart_hw(tp, 1);
11402 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070011403 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011404 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011405
11406 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011407
11408 if (irq_sync && !err2)
11409 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011410 }
Matt Carlson800960682010-08-02 11:26:06 +000011411 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011412 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011413
Michael Chan4cafd3f2005-05-29 14:56:34 -070011414}
11415
Linus Torvalds1da177e2005-04-16 15:20:36 -070011416static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
11417{
11418 struct mii_ioctl_data *data = if_mii(ifr);
11419 struct tg3 *tp = netdev_priv(dev);
11420 int err;
11421
Joe Perches63c3a662011-04-26 08:12:10 +000011422 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011423 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011424 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011425 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011426 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000011427 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011428 }
11429
Matt Carlson33f401a2010-04-05 10:19:27 +000011430 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011431 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000011432 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011433
11434 /* fallthru */
11435 case SIOCGMIIREG: {
11436 u32 mii_regval;
11437
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011438 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011439 break; /* We have no PHY */
11440
Matt Carlson34eea5a2011-04-20 07:57:38 +000011441 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011442 return -EAGAIN;
11443
David S. Millerf47c11e2005-06-24 20:18:35 -070011444 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011445 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070011446 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011447
11448 data->val_out = mii_regval;
11449
11450 return err;
11451 }
11452
11453 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011454 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011455 break; /* We have no PHY */
11456
Matt Carlson34eea5a2011-04-20 07:57:38 +000011457 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011458 return -EAGAIN;
11459
David S. Millerf47c11e2005-06-24 20:18:35 -070011460 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011461 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070011462 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011463
11464 return err;
11465
11466 default:
11467 /* do nothing */
11468 break;
11469 }
11470 return -EOPNOTSUPP;
11471}
11472
David S. Miller15f98502005-05-18 22:49:26 -070011473static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11474{
11475 struct tg3 *tp = netdev_priv(dev);
11476
11477 memcpy(ec, &tp->coal, sizeof(*ec));
11478 return 0;
11479}
11480
Michael Chand244c892005-07-05 14:42:33 -070011481static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11482{
11483 struct tg3 *tp = netdev_priv(dev);
11484 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11485 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11486
Joe Perches63c3a662011-04-26 08:12:10 +000011487 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070011488 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11489 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11490 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11491 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11492 }
11493
11494 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11495 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11496 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11497 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11498 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11499 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11500 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11501 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11502 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11503 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11504 return -EINVAL;
11505
11506 /* No rx interrupts will be generated if both are zero */
11507 if ((ec->rx_coalesce_usecs == 0) &&
11508 (ec->rx_max_coalesced_frames == 0))
11509 return -EINVAL;
11510
11511 /* No tx interrupts will be generated if both are zero */
11512 if ((ec->tx_coalesce_usecs == 0) &&
11513 (ec->tx_max_coalesced_frames == 0))
11514 return -EINVAL;
11515
11516 /* Only copy relevant parameters, ignore all others. */
11517 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11518 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11519 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11520 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11521 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11522 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11523 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11524 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11525 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11526
11527 if (netif_running(dev)) {
11528 tg3_full_lock(tp, 0);
11529 __tg3_set_coalesce(tp, &tp->coal);
11530 tg3_full_unlock(tp);
11531 }
11532 return 0;
11533}
11534
Jeff Garzik7282d492006-09-13 14:30:00 -040011535static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011536 .get_settings = tg3_get_settings,
11537 .set_settings = tg3_set_settings,
11538 .get_drvinfo = tg3_get_drvinfo,
11539 .get_regs_len = tg3_get_regs_len,
11540 .get_regs = tg3_get_regs,
11541 .get_wol = tg3_get_wol,
11542 .set_wol = tg3_set_wol,
11543 .get_msglevel = tg3_get_msglevel,
11544 .set_msglevel = tg3_set_msglevel,
11545 .nway_reset = tg3_nway_reset,
11546 .get_link = ethtool_op_get_link,
11547 .get_eeprom_len = tg3_get_eeprom_len,
11548 .get_eeprom = tg3_get_eeprom,
11549 .set_eeprom = tg3_set_eeprom,
11550 .get_ringparam = tg3_get_ringparam,
11551 .set_ringparam = tg3_set_ringparam,
11552 .get_pauseparam = tg3_get_pauseparam,
11553 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011554 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011555 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000011556 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011557 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011558 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011559 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011560 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011561};
11562
11563static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11564{
Michael Chan1b277772006-03-20 22:27:48 -080011565 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011566
11567 tp->nvram_size = EEPROM_CHIP_SIZE;
11568
Matt Carlsone4f34112009-02-25 14:25:00 +000011569 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011570 return;
11571
Michael Chanb16250e2006-09-27 16:10:14 -070011572 if ((magic != TG3_EEPROM_MAGIC) &&
11573 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11574 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011575 return;
11576
11577 /*
11578 * Size the chip by reading offsets at increasing powers of two.
11579 * When we encounter our validation signature, we know the addressing
11580 * has wrapped around, and thus have our chip size.
11581 */
Michael Chan1b277772006-03-20 22:27:48 -080011582 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011583
11584 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011585 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011586 return;
11587
Michael Chan18201802006-03-20 22:29:15 -080011588 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011589 break;
11590
11591 cursize <<= 1;
11592 }
11593
11594 tp->nvram_size = cursize;
11595}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011596
Linus Torvalds1da177e2005-04-16 15:20:36 -070011597static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11598{
11599 u32 val;
11600
Joe Perches63c3a662011-04-26 08:12:10 +000011601 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011602 return;
11603
11604 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011605 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011606 tg3_get_eeprom_size(tp);
11607 return;
11608 }
11609
Matt Carlson6d348f22009-02-25 14:25:52 +000011610 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011611 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011612 /* This is confusing. We want to operate on the
11613 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11614 * call will read from NVRAM and byteswap the data
11615 * according to the byteswapping settings for all
11616 * other register accesses. This ensures the data we
11617 * want will always reside in the lower 16-bits.
11618 * However, the data in NVRAM is in LE format, which
11619 * means the data from the NVRAM read will always be
11620 * opposite the endianness of the CPU. The 16-bit
11621 * byteswap then brings the data to CPU endianness.
11622 */
11623 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011624 return;
11625 }
11626 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070011627 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011628}
11629
11630static void __devinit tg3_get_nvram_info(struct tg3 *tp)
11631{
11632 u32 nvcfg1;
11633
11634 nvcfg1 = tr32(NVRAM_CFG1);
11635 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000011636 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011637 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011638 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11639 tw32(NVRAM_CFG1, nvcfg1);
11640 }
11641
Matt Carlson6ff6f812011-05-19 12:12:54 +000011642 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000011643 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011644 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011645 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
11646 tp->nvram_jedecnum = JEDEC_ATMEL;
11647 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011648 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011649 break;
11650 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
11651 tp->nvram_jedecnum = JEDEC_ATMEL;
11652 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
11653 break;
11654 case FLASH_VENDOR_ATMEL_EEPROM:
11655 tp->nvram_jedecnum = JEDEC_ATMEL;
11656 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011657 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011658 break;
11659 case FLASH_VENDOR_ST:
11660 tp->nvram_jedecnum = JEDEC_ST;
11661 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011662 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011663 break;
11664 case FLASH_VENDOR_SAIFUN:
11665 tp->nvram_jedecnum = JEDEC_SAIFUN;
11666 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
11667 break;
11668 case FLASH_VENDOR_SST_SMALL:
11669 case FLASH_VENDOR_SST_LARGE:
11670 tp->nvram_jedecnum = JEDEC_SST;
11671 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
11672 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011673 }
Matt Carlson8590a602009-08-28 12:29:16 +000011674 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011675 tp->nvram_jedecnum = JEDEC_ATMEL;
11676 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011677 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011678 }
11679}
11680
Matt Carlsona1b950d2009-09-01 13:20:17 +000011681static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
11682{
11683 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
11684 case FLASH_5752PAGE_SIZE_256:
11685 tp->nvram_pagesize = 256;
11686 break;
11687 case FLASH_5752PAGE_SIZE_512:
11688 tp->nvram_pagesize = 512;
11689 break;
11690 case FLASH_5752PAGE_SIZE_1K:
11691 tp->nvram_pagesize = 1024;
11692 break;
11693 case FLASH_5752PAGE_SIZE_2K:
11694 tp->nvram_pagesize = 2048;
11695 break;
11696 case FLASH_5752PAGE_SIZE_4K:
11697 tp->nvram_pagesize = 4096;
11698 break;
11699 case FLASH_5752PAGE_SIZE_264:
11700 tp->nvram_pagesize = 264;
11701 break;
11702 case FLASH_5752PAGE_SIZE_528:
11703 tp->nvram_pagesize = 528;
11704 break;
11705 }
11706}
11707
Michael Chan361b4ac2005-04-21 17:11:21 -070011708static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
11709{
11710 u32 nvcfg1;
11711
11712 nvcfg1 = tr32(NVRAM_CFG1);
11713
Michael Chane6af3012005-04-21 17:12:05 -070011714 /* NVRAM protection for TPM */
11715 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000011716 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070011717
Michael Chan361b4ac2005-04-21 17:11:21 -070011718 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011719 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
11720 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
11721 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011722 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011723 break;
11724 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11725 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011726 tg3_flag_set(tp, NVRAM_BUFFERED);
11727 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011728 break;
11729 case FLASH_5752VENDOR_ST_M45PE10:
11730 case FLASH_5752VENDOR_ST_M45PE20:
11731 case FLASH_5752VENDOR_ST_M45PE40:
11732 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011733 tg3_flag_set(tp, NVRAM_BUFFERED);
11734 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011735 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070011736 }
11737
Joe Perches63c3a662011-04-26 08:12:10 +000011738 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000011739 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000011740 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070011741 /* For eeprom, set pagesize to maximum eeprom size */
11742 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11743
11744 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11745 tw32(NVRAM_CFG1, nvcfg1);
11746 }
11747}
11748
Michael Chand3c7b882006-03-23 01:28:25 -080011749static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
11750{
Matt Carlson989a9d22007-05-05 11:51:05 -070011751 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080011752
11753 nvcfg1 = tr32(NVRAM_CFG1);
11754
11755 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070011756 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011757 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070011758 protect = 1;
11759 }
Michael Chand3c7b882006-03-23 01:28:25 -080011760
Matt Carlson989a9d22007-05-05 11:51:05 -070011761 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11762 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011763 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11764 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11765 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11766 case FLASH_5755VENDOR_ATMEL_FLASH_5:
11767 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011768 tg3_flag_set(tp, NVRAM_BUFFERED);
11769 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011770 tp->nvram_pagesize = 264;
11771 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
11772 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
11773 tp->nvram_size = (protect ? 0x3e200 :
11774 TG3_NVRAM_SIZE_512KB);
11775 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
11776 tp->nvram_size = (protect ? 0x1f200 :
11777 TG3_NVRAM_SIZE_256KB);
11778 else
11779 tp->nvram_size = (protect ? 0x1f200 :
11780 TG3_NVRAM_SIZE_128KB);
11781 break;
11782 case FLASH_5752VENDOR_ST_M45PE10:
11783 case FLASH_5752VENDOR_ST_M45PE20:
11784 case FLASH_5752VENDOR_ST_M45PE40:
11785 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011786 tg3_flag_set(tp, NVRAM_BUFFERED);
11787 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011788 tp->nvram_pagesize = 256;
11789 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
11790 tp->nvram_size = (protect ?
11791 TG3_NVRAM_SIZE_64KB :
11792 TG3_NVRAM_SIZE_128KB);
11793 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
11794 tp->nvram_size = (protect ?
11795 TG3_NVRAM_SIZE_64KB :
11796 TG3_NVRAM_SIZE_256KB);
11797 else
11798 tp->nvram_size = (protect ?
11799 TG3_NVRAM_SIZE_128KB :
11800 TG3_NVRAM_SIZE_512KB);
11801 break;
Michael Chand3c7b882006-03-23 01:28:25 -080011802 }
11803}
11804
Michael Chan1b277772006-03-20 22:27:48 -080011805static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
11806{
11807 u32 nvcfg1;
11808
11809 nvcfg1 = tr32(NVRAM_CFG1);
11810
11811 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011812 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
11813 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11814 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
11815 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11816 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011817 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011818 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080011819
Matt Carlson8590a602009-08-28 12:29:16 +000011820 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11821 tw32(NVRAM_CFG1, nvcfg1);
11822 break;
11823 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11824 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11825 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11826 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11827 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011828 tg3_flag_set(tp, NVRAM_BUFFERED);
11829 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011830 tp->nvram_pagesize = 264;
11831 break;
11832 case FLASH_5752VENDOR_ST_M45PE10:
11833 case FLASH_5752VENDOR_ST_M45PE20:
11834 case FLASH_5752VENDOR_ST_M45PE40:
11835 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011836 tg3_flag_set(tp, NVRAM_BUFFERED);
11837 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011838 tp->nvram_pagesize = 256;
11839 break;
Michael Chan1b277772006-03-20 22:27:48 -080011840 }
11841}
11842
Matt Carlson6b91fa02007-10-10 18:01:09 -070011843static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
11844{
11845 u32 nvcfg1, protect = 0;
11846
11847 nvcfg1 = tr32(NVRAM_CFG1);
11848
11849 /* NVRAM protection for TPM */
11850 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011851 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070011852 protect = 1;
11853 }
11854
11855 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11856 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011857 case FLASH_5761VENDOR_ATMEL_ADB021D:
11858 case FLASH_5761VENDOR_ATMEL_ADB041D:
11859 case FLASH_5761VENDOR_ATMEL_ADB081D:
11860 case FLASH_5761VENDOR_ATMEL_ADB161D:
11861 case FLASH_5761VENDOR_ATMEL_MDB021D:
11862 case FLASH_5761VENDOR_ATMEL_MDB041D:
11863 case FLASH_5761VENDOR_ATMEL_MDB081D:
11864 case FLASH_5761VENDOR_ATMEL_MDB161D:
11865 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011866 tg3_flag_set(tp, NVRAM_BUFFERED);
11867 tg3_flag_set(tp, FLASH);
11868 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000011869 tp->nvram_pagesize = 256;
11870 break;
11871 case FLASH_5761VENDOR_ST_A_M45PE20:
11872 case FLASH_5761VENDOR_ST_A_M45PE40:
11873 case FLASH_5761VENDOR_ST_A_M45PE80:
11874 case FLASH_5761VENDOR_ST_A_M45PE16:
11875 case FLASH_5761VENDOR_ST_M_M45PE20:
11876 case FLASH_5761VENDOR_ST_M_M45PE40:
11877 case FLASH_5761VENDOR_ST_M_M45PE80:
11878 case FLASH_5761VENDOR_ST_M_M45PE16:
11879 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011880 tg3_flag_set(tp, NVRAM_BUFFERED);
11881 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011882 tp->nvram_pagesize = 256;
11883 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011884 }
11885
11886 if (protect) {
11887 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
11888 } else {
11889 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011890 case FLASH_5761VENDOR_ATMEL_ADB161D:
11891 case FLASH_5761VENDOR_ATMEL_MDB161D:
11892 case FLASH_5761VENDOR_ST_A_M45PE16:
11893 case FLASH_5761VENDOR_ST_M_M45PE16:
11894 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
11895 break;
11896 case FLASH_5761VENDOR_ATMEL_ADB081D:
11897 case FLASH_5761VENDOR_ATMEL_MDB081D:
11898 case FLASH_5761VENDOR_ST_A_M45PE80:
11899 case FLASH_5761VENDOR_ST_M_M45PE80:
11900 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
11901 break;
11902 case FLASH_5761VENDOR_ATMEL_ADB041D:
11903 case FLASH_5761VENDOR_ATMEL_MDB041D:
11904 case FLASH_5761VENDOR_ST_A_M45PE40:
11905 case FLASH_5761VENDOR_ST_M_M45PE40:
11906 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11907 break;
11908 case FLASH_5761VENDOR_ATMEL_ADB021D:
11909 case FLASH_5761VENDOR_ATMEL_MDB021D:
11910 case FLASH_5761VENDOR_ST_A_M45PE20:
11911 case FLASH_5761VENDOR_ST_M_M45PE20:
11912 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11913 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011914 }
11915 }
11916}
11917
Michael Chanb5d37722006-09-27 16:06:21 -070011918static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
11919{
11920 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011921 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070011922 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11923}
11924
Matt Carlson321d32a2008-11-21 17:22:19 -080011925static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
11926{
11927 u32 nvcfg1;
11928
11929 nvcfg1 = tr32(NVRAM_CFG1);
11930
11931 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11932 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11933 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11934 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011935 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080011936 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11937
11938 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11939 tw32(NVRAM_CFG1, nvcfg1);
11940 return;
11941 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11942 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11943 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11944 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11945 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11946 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11947 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11948 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011949 tg3_flag_set(tp, NVRAM_BUFFERED);
11950 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080011951
11952 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11953 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11954 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11955 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11956 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11957 break;
11958 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11959 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11960 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11961 break;
11962 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11963 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11964 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11965 break;
11966 }
11967 break;
11968 case FLASH_5752VENDOR_ST_M45PE10:
11969 case FLASH_5752VENDOR_ST_M45PE20:
11970 case FLASH_5752VENDOR_ST_M45PE40:
11971 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011972 tg3_flag_set(tp, NVRAM_BUFFERED);
11973 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080011974
11975 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11976 case FLASH_5752VENDOR_ST_M45PE10:
11977 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11978 break;
11979 case FLASH_5752VENDOR_ST_M45PE20:
11980 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11981 break;
11982 case FLASH_5752VENDOR_ST_M45PE40:
11983 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11984 break;
11985 }
11986 break;
11987 default:
Joe Perches63c3a662011-04-26 08:12:10 +000011988 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080011989 return;
11990 }
11991
Matt Carlsona1b950d2009-09-01 13:20:17 +000011992 tg3_nvram_get_pagesize(tp, nvcfg1);
11993 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000011994 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000011995}
11996
11997
11998static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
11999{
12000 u32 nvcfg1;
12001
12002 nvcfg1 = tr32(NVRAM_CFG1);
12003
12004 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12005 case FLASH_5717VENDOR_ATMEL_EEPROM:
12006 case FLASH_5717VENDOR_MICRO_EEPROM:
12007 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012008 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012009 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12010
12011 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12012 tw32(NVRAM_CFG1, nvcfg1);
12013 return;
12014 case FLASH_5717VENDOR_ATMEL_MDB011D:
12015 case FLASH_5717VENDOR_ATMEL_ADB011B:
12016 case FLASH_5717VENDOR_ATMEL_ADB011D:
12017 case FLASH_5717VENDOR_ATMEL_MDB021D:
12018 case FLASH_5717VENDOR_ATMEL_ADB021B:
12019 case FLASH_5717VENDOR_ATMEL_ADB021D:
12020 case FLASH_5717VENDOR_ATMEL_45USPT:
12021 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012022 tg3_flag_set(tp, NVRAM_BUFFERED);
12023 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012024
12025 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12026 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012027 /* Detect size with tg3_nvram_get_size() */
12028 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012029 case FLASH_5717VENDOR_ATMEL_ADB021B:
12030 case FLASH_5717VENDOR_ATMEL_ADB021D:
12031 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12032 break;
12033 default:
12034 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12035 break;
12036 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012037 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012038 case FLASH_5717VENDOR_ST_M_M25PE10:
12039 case FLASH_5717VENDOR_ST_A_M25PE10:
12040 case FLASH_5717VENDOR_ST_M_M45PE10:
12041 case FLASH_5717VENDOR_ST_A_M45PE10:
12042 case FLASH_5717VENDOR_ST_M_M25PE20:
12043 case FLASH_5717VENDOR_ST_A_M25PE20:
12044 case FLASH_5717VENDOR_ST_M_M45PE20:
12045 case FLASH_5717VENDOR_ST_A_M45PE20:
12046 case FLASH_5717VENDOR_ST_25USPT:
12047 case FLASH_5717VENDOR_ST_45USPT:
12048 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012049 tg3_flag_set(tp, NVRAM_BUFFERED);
12050 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012051
12052 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12053 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012054 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012055 /* Detect size with tg3_nvram_get_size() */
12056 break;
12057 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012058 case FLASH_5717VENDOR_ST_A_M45PE20:
12059 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12060 break;
12061 default:
12062 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12063 break;
12064 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012065 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012066 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012067 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012068 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080012069 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000012070
12071 tg3_nvram_get_pagesize(tp, nvcfg1);
12072 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012073 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080012074}
12075
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012076static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
12077{
12078 u32 nvcfg1, nvmpinstrp;
12079
12080 nvcfg1 = tr32(NVRAM_CFG1);
12081 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
12082
12083 switch (nvmpinstrp) {
12084 case FLASH_5720_EEPROM_HD:
12085 case FLASH_5720_EEPROM_LD:
12086 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012087 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012088
12089 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12090 tw32(NVRAM_CFG1, nvcfg1);
12091 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
12092 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12093 else
12094 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
12095 return;
12096 case FLASH_5720VENDOR_M_ATMEL_DB011D:
12097 case FLASH_5720VENDOR_A_ATMEL_DB011B:
12098 case FLASH_5720VENDOR_A_ATMEL_DB011D:
12099 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12100 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12101 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12102 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12103 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12104 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12105 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12106 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12107 case FLASH_5720VENDOR_ATMEL_45USPT:
12108 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012109 tg3_flag_set(tp, NVRAM_BUFFERED);
12110 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012111
12112 switch (nvmpinstrp) {
12113 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12114 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12115 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12116 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12117 break;
12118 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12119 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12120 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12121 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12122 break;
12123 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12124 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12125 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12126 break;
12127 default:
12128 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12129 break;
12130 }
12131 break;
12132 case FLASH_5720VENDOR_M_ST_M25PE10:
12133 case FLASH_5720VENDOR_M_ST_M45PE10:
12134 case FLASH_5720VENDOR_A_ST_M25PE10:
12135 case FLASH_5720VENDOR_A_ST_M45PE10:
12136 case FLASH_5720VENDOR_M_ST_M25PE20:
12137 case FLASH_5720VENDOR_M_ST_M45PE20:
12138 case FLASH_5720VENDOR_A_ST_M25PE20:
12139 case FLASH_5720VENDOR_A_ST_M45PE20:
12140 case FLASH_5720VENDOR_M_ST_M25PE40:
12141 case FLASH_5720VENDOR_M_ST_M45PE40:
12142 case FLASH_5720VENDOR_A_ST_M25PE40:
12143 case FLASH_5720VENDOR_A_ST_M45PE40:
12144 case FLASH_5720VENDOR_M_ST_M25PE80:
12145 case FLASH_5720VENDOR_M_ST_M45PE80:
12146 case FLASH_5720VENDOR_A_ST_M25PE80:
12147 case FLASH_5720VENDOR_A_ST_M45PE80:
12148 case FLASH_5720VENDOR_ST_25USPT:
12149 case FLASH_5720VENDOR_ST_45USPT:
12150 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012151 tg3_flag_set(tp, NVRAM_BUFFERED);
12152 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012153
12154 switch (nvmpinstrp) {
12155 case FLASH_5720VENDOR_M_ST_M25PE20:
12156 case FLASH_5720VENDOR_M_ST_M45PE20:
12157 case FLASH_5720VENDOR_A_ST_M25PE20:
12158 case FLASH_5720VENDOR_A_ST_M45PE20:
12159 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12160 break;
12161 case FLASH_5720VENDOR_M_ST_M25PE40:
12162 case FLASH_5720VENDOR_M_ST_M45PE40:
12163 case FLASH_5720VENDOR_A_ST_M25PE40:
12164 case FLASH_5720VENDOR_A_ST_M45PE40:
12165 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12166 break;
12167 case FLASH_5720VENDOR_M_ST_M25PE80:
12168 case FLASH_5720VENDOR_M_ST_M45PE80:
12169 case FLASH_5720VENDOR_A_ST_M25PE80:
12170 case FLASH_5720VENDOR_A_ST_M45PE80:
12171 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12172 break;
12173 default:
12174 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12175 break;
12176 }
12177 break;
12178 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012179 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012180 return;
12181 }
12182
12183 tg3_nvram_get_pagesize(tp, nvcfg1);
12184 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012185 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012186}
12187
Linus Torvalds1da177e2005-04-16 15:20:36 -070012188/* Chips other than 5700/5701 use the NVRAM for fetching info. */
12189static void __devinit tg3_nvram_init(struct tg3 *tp)
12190{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012191 tw32_f(GRC_EEPROM_ADDR,
12192 (EEPROM_ADDR_FSM_RESET |
12193 (EEPROM_DEFAULT_CLOCK_PERIOD <<
12194 EEPROM_ADDR_CLKPERD_SHIFT)));
12195
Michael Chan9d57f012006-12-07 00:23:25 -080012196 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012197
12198 /* Enable seeprom accesses. */
12199 tw32_f(GRC_LOCAL_CTRL,
12200 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
12201 udelay(100);
12202
12203 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12204 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000012205 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012206
Michael Chanec41c7d2006-01-17 02:40:55 -080012207 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000012208 netdev_warn(tp->dev,
12209 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000012210 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080012211 return;
12212 }
Michael Chane6af3012005-04-21 17:12:05 -070012213 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012214
Matt Carlson989a9d22007-05-05 11:51:05 -070012215 tp->nvram_size = 0;
12216
Michael Chan361b4ac2005-04-21 17:11:21 -070012217 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12218 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080012219 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
12220 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070012221 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012222 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12223 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080012224 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012225 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
12226 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070012227 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12228 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000012229 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
12230 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080012231 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012232 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12233 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000012234 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012235 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
12236 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070012237 else
12238 tg3_get_nvram_info(tp);
12239
Matt Carlson989a9d22007-05-05 11:51:05 -070012240 if (tp->nvram_size == 0)
12241 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012242
Michael Chane6af3012005-04-21 17:12:05 -070012243 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080012244 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012245
12246 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000012247 tg3_flag_clear(tp, NVRAM);
12248 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012249
12250 tg3_get_eeprom_size(tp);
12251 }
12252}
12253
Linus Torvalds1da177e2005-04-16 15:20:36 -070012254static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
12255 u32 offset, u32 len, u8 *buf)
12256{
12257 int i, j, rc = 0;
12258 u32 val;
12259
12260 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012261 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012262 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012263
12264 addr = offset + i;
12265
12266 memcpy(&data, buf + i, 4);
12267
Matt Carlson62cedd12009-04-20 14:52:29 -070012268 /*
12269 * The SEEPROM interface expects the data to always be opposite
12270 * the native endian format. We accomplish this by reversing
12271 * all the operations that would have been performed on the
12272 * data from a call to tg3_nvram_read_be32().
12273 */
12274 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012275
12276 val = tr32(GRC_EEPROM_ADDR);
12277 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
12278
12279 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
12280 EEPROM_ADDR_READ);
12281 tw32(GRC_EEPROM_ADDR, val |
12282 (0 << EEPROM_ADDR_DEVID_SHIFT) |
12283 (addr & EEPROM_ADDR_ADDR_MASK) |
12284 EEPROM_ADDR_START |
12285 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012286
Michael Chan9d57f012006-12-07 00:23:25 -080012287 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012288 val = tr32(GRC_EEPROM_ADDR);
12289
12290 if (val & EEPROM_ADDR_COMPLETE)
12291 break;
Michael Chan9d57f012006-12-07 00:23:25 -080012292 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012293 }
12294 if (!(val & EEPROM_ADDR_COMPLETE)) {
12295 rc = -EBUSY;
12296 break;
12297 }
12298 }
12299
12300 return rc;
12301}
12302
12303/* offset and length are dword aligned */
12304static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
12305 u8 *buf)
12306{
12307 int ret = 0;
12308 u32 pagesize = tp->nvram_pagesize;
12309 u32 pagemask = pagesize - 1;
12310 u32 nvram_cmd;
12311 u8 *tmp;
12312
12313 tmp = kmalloc(pagesize, GFP_KERNEL);
12314 if (tmp == NULL)
12315 return -ENOMEM;
12316
12317 while (len) {
12318 int j;
Michael Chane6af3012005-04-21 17:12:05 -070012319 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012320
12321 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012322
Linus Torvalds1da177e2005-04-16 15:20:36 -070012323 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012324 ret = tg3_nvram_read_be32(tp, phy_addr + j,
12325 (__be32 *) (tmp + j));
12326 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012327 break;
12328 }
12329 if (ret)
12330 break;
12331
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012332 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012333 size = pagesize;
12334 if (len < size)
12335 size = len;
12336
12337 len -= size;
12338
12339 memcpy(tmp + page_off, buf, size);
12340
12341 offset = offset + (pagesize - page_off);
12342
Michael Chane6af3012005-04-21 17:12:05 -070012343 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012344
12345 /*
12346 * Before we can erase the flash page, we need
12347 * to issue a special "write enable" command.
12348 */
12349 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12350
12351 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12352 break;
12353
12354 /* Erase the target page */
12355 tw32(NVRAM_ADDR, phy_addr);
12356
12357 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
12358 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
12359
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012360 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012361 break;
12362
12363 /* Issue another write enable to start the write. */
12364 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12365
12366 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12367 break;
12368
12369 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012370 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012371
Al Virob9fc7dc2007-12-17 22:59:57 -080012372 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000012373
Al Virob9fc7dc2007-12-17 22:59:57 -080012374 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012375
12376 tw32(NVRAM_ADDR, phy_addr + j);
12377
12378 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
12379 NVRAM_CMD_WR;
12380
12381 if (j == 0)
12382 nvram_cmd |= NVRAM_CMD_FIRST;
12383 else if (j == (pagesize - 4))
12384 nvram_cmd |= NVRAM_CMD_LAST;
12385
12386 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12387 break;
12388 }
12389 if (ret)
12390 break;
12391 }
12392
12393 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12394 tg3_nvram_exec_cmd(tp, nvram_cmd);
12395
12396 kfree(tmp);
12397
12398 return ret;
12399}
12400
12401/* offset and length are dword aligned */
12402static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
12403 u8 *buf)
12404{
12405 int i, ret = 0;
12406
12407 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012408 u32 page_off, phy_addr, nvram_cmd;
12409 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012410
12411 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080012412 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012413
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012414 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012415
Michael Chan18201802006-03-20 22:29:15 -080012416 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012417
12418 tw32(NVRAM_ADDR, phy_addr);
12419
12420 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
12421
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012422 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012423 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070012424 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012425 nvram_cmd |= NVRAM_CMD_LAST;
12426
12427 if (i == (len - 4))
12428 nvram_cmd |= NVRAM_CMD_LAST;
12429
Matt Carlson321d32a2008-11-21 17:22:19 -080012430 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
Joe Perches63c3a662011-04-26 08:12:10 +000012431 !tg3_flag(tp, 5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070012432 (tp->nvram_jedecnum == JEDEC_ST) &&
12433 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012434
12435 if ((ret = tg3_nvram_exec_cmd(tp,
12436 NVRAM_CMD_WREN | NVRAM_CMD_GO |
12437 NVRAM_CMD_DONE)))
12438
12439 break;
12440 }
Joe Perches63c3a662011-04-26 08:12:10 +000012441 if (!tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012442 /* We always do complete word writes to eeprom. */
12443 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
12444 }
12445
12446 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12447 break;
12448 }
12449 return ret;
12450}
12451
12452/* offset and length are dword aligned */
12453static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
12454{
12455 int ret;
12456
Joe Perches63c3a662011-04-26 08:12:10 +000012457 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012458 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
12459 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012460 udelay(40);
12461 }
12462
Joe Perches63c3a662011-04-26 08:12:10 +000012463 if (!tg3_flag(tp, NVRAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012464 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a588792010-04-05 10:19:28 +000012465 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012466 u32 grc_mode;
12467
Michael Chanec41c7d2006-01-17 02:40:55 -080012468 ret = tg3_nvram_lock(tp);
12469 if (ret)
12470 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012471
Michael Chane6af3012005-04-21 17:12:05 -070012472 tg3_enable_nvram_access(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000012473 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012474 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012475
12476 grc_mode = tr32(GRC_MODE);
12477 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
12478
Joe Perches63c3a662011-04-26 08:12:10 +000012479 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012480 ret = tg3_nvram_write_block_buffered(tp, offset, len,
12481 buf);
Matt Carlson859a588792010-04-05 10:19:28 +000012482 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012483 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
12484 buf);
12485 }
12486
12487 grc_mode = tr32(GRC_MODE);
12488 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
12489
Michael Chane6af3012005-04-21 17:12:05 -070012490 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012491 tg3_nvram_unlock(tp);
12492 }
12493
Joe Perches63c3a662011-04-26 08:12:10 +000012494 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012495 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012496 udelay(40);
12497 }
12498
12499 return ret;
12500}
12501
12502struct subsys_tbl_ent {
12503 u16 subsys_vendor, subsys_devid;
12504 u32 phy_id;
12505};
12506
Matt Carlson24daf2b2010-02-17 15:17:02 +000012507static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012508 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012509 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012510 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012511 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012512 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012513 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012514 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012515 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12516 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
12517 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012518 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012519 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012520 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012521 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12522 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
12523 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012524 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012525 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012526 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012527 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012528 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012529 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012530 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012531
12532 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012533 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012534 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012535 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012536 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012537 { TG3PCI_SUBVENDOR_ID_3COM,
12538 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
12539 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012540 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012541 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012542 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012543
12544 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012545 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012546 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012547 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012548 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012549 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012550 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012551 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012552 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012553
12554 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012555 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012556 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012557 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012558 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012559 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12560 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
12561 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012562 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012563 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012564 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012565
12566 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012567 { TG3PCI_SUBVENDOR_ID_IBM,
12568 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012569};
12570
Matt Carlson24daf2b2010-02-17 15:17:02 +000012571static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012572{
12573 int i;
12574
12575 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
12576 if ((subsys_id_to_phy_id[i].subsys_vendor ==
12577 tp->pdev->subsystem_vendor) &&
12578 (subsys_id_to_phy_id[i].subsys_devid ==
12579 tp->pdev->subsystem_device))
12580 return &subsys_id_to_phy_id[i];
12581 }
12582 return NULL;
12583}
12584
Michael Chan7d0c41e2005-04-21 17:06:20 -070012585static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012586{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012587 u32 val;
Michael Chancaf636c72006-03-22 01:05:31 -080012588 u16 pmcsr;
12589
12590 /* On some early chips the SRAM cannot be accessed in D3hot state,
12591 * so need make sure we're in D0.
12592 */
12593 pci_read_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, &pmcsr);
12594 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
12595 pci_write_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, pmcsr);
12596 msleep(1);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012597
12598 /* Make sure register accesses (indirect or otherwise)
12599 * will function correctly.
12600 */
12601 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12602 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012603
David S. Millerf49639e2006-06-09 11:58:36 -070012604 /* The memory arbiter has to be enabled in order for SRAM accesses
12605 * to succeed. Normally on powerup the tg3 chip firmware will make
12606 * sure it is enabled, but other entities such as system netboot
12607 * code might disable it.
12608 */
12609 val = tr32(MEMARB_MODE);
12610 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
12611
Matt Carlson79eb6902010-02-17 15:17:03 +000012612 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012613 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12614
Gary Zambranoa85feb82007-05-05 11:52:19 -070012615 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000012616 tg3_flag_set(tp, EEPROM_WRITE_PROT);
12617 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080012618
Michael Chanb5d37722006-09-27 16:06:21 -070012619 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012620 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012621 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
12622 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080012623 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012624 val = tr32(VCPU_CFGSHDW);
12625 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000012626 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070012627 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012628 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012629 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012630 device_set_wakeup_enable(&tp->pdev->dev, true);
12631 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012632 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070012633 }
12634
Linus Torvalds1da177e2005-04-16 15:20:36 -070012635 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12636 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12637 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012638 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012639 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012640
12641 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12642 tp->nic_sram_data_cfg = nic_cfg;
12643
12644 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
12645 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Matt Carlson6ff6f812011-05-19 12:12:54 +000012646 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12647 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
12648 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012649 (ver > 0) && (ver < 0x100))
12650 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
12651
Matt Carlsona9daf362008-05-25 23:49:44 -070012652 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
12653 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
12654
Linus Torvalds1da177e2005-04-16 15:20:36 -070012655 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
12656 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
12657 eeprom_phy_serdes = 1;
12658
12659 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
12660 if (nic_phy_id != 0) {
12661 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
12662 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
12663
12664 eeprom_phy_id = (id1 >> 16) << 10;
12665 eeprom_phy_id |= (id2 & 0xfc00) << 16;
12666 eeprom_phy_id |= (id2 & 0x03ff) << 0;
12667 } else
12668 eeprom_phy_id = 0;
12669
Michael Chan7d0c41e2005-04-21 17:06:20 -070012670 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070012671 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000012672 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012673 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000012674 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012675 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070012676 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070012677
Joe Perches63c3a662011-04-26 08:12:10 +000012678 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012679 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
12680 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070012681 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070012682 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
12683
12684 switch (led_cfg) {
12685 default:
12686 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
12687 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12688 break;
12689
12690 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
12691 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12692 break;
12693
12694 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
12695 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070012696
12697 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
12698 * read on some older 5700/5701 bootcode.
12699 */
12700 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
12701 ASIC_REV_5700 ||
12702 GET_ASIC_REV(tp->pci_chip_rev_id) ==
12703 ASIC_REV_5701)
12704 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12705
Linus Torvalds1da177e2005-04-16 15:20:36 -070012706 break;
12707
12708 case SHASTA_EXT_LED_SHARED:
12709 tp->led_ctrl = LED_CTRL_MODE_SHARED;
12710 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
12711 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
12712 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12713 LED_CTRL_MODE_PHY_2);
12714 break;
12715
12716 case SHASTA_EXT_LED_MAC:
12717 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
12718 break;
12719
12720 case SHASTA_EXT_LED_COMBO:
12721 tp->led_ctrl = LED_CTRL_MODE_COMBO;
12722 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
12723 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12724 LED_CTRL_MODE_PHY_2);
12725 break;
12726
Stephen Hemminger855e1112008-04-16 16:37:28 -070012727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012728
12729 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12730 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
12731 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
12732 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12733
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012734 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
12735 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080012736
Michael Chan9d26e212006-12-07 00:21:14 -080012737 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000012738 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080012739 if ((tp->pdev->subsystem_vendor ==
12740 PCI_VENDOR_ID_ARIMA) &&
12741 (tp->pdev->subsystem_device == 0x205a ||
12742 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000012743 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080012744 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000012745 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
12746 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080012747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012748
12749 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000012750 tg3_flag_set(tp, ENABLE_ASF);
12751 if (tg3_flag(tp, 5750_PLUS))
12752 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012753 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012754
12755 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000012756 tg3_flag(tp, 5750_PLUS))
12757 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012758
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012759 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070012760 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000012761 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012762
Joe Perches63c3a662011-04-26 08:12:10 +000012763 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012764 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012765 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012766 device_set_wakeup_enable(&tp->pdev->dev, true);
12767 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012768
Linus Torvalds1da177e2005-04-16 15:20:36 -070012769 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012770 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012771
12772 /* serdes signal pre-emphasis in register 0x590 set by */
12773 /* bootcode if bit 18 is set */
12774 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012775 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070012776
Joe Perches63c3a662011-04-26 08:12:10 +000012777 if ((tg3_flag(tp, 57765_PLUS) ||
12778 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
12779 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080012780 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012781 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080012782
Joe Perches63c3a662011-04-26 08:12:10 +000012783 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson8c69b1e2010-08-02 11:26:00 +000012784 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000012785 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070012786 u32 cfg3;
12787
12788 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
12789 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000012790 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070012791 }
Matt Carlsona9daf362008-05-25 23:49:44 -070012792
Matt Carlson14417062010-02-17 15:16:59 +000012793 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000012794 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070012795 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000012796 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070012797 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000012798 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012799 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012800done:
Joe Perches63c3a662011-04-26 08:12:10 +000012801 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000012802 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000012803 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000012804 else
12805 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012806}
12807
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012808static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
12809{
12810 int i;
12811 u32 val;
12812
12813 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
12814 tw32(OTP_CTRL, cmd);
12815
12816 /* Wait for up to 1 ms for command to execute. */
12817 for (i = 0; i < 100; i++) {
12818 val = tr32(OTP_STATUS);
12819 if (val & OTP_STATUS_CMD_DONE)
12820 break;
12821 udelay(10);
12822 }
12823
12824 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
12825}
12826
12827/* Read the gphy configuration from the OTP region of the chip. The gphy
12828 * configuration is a 32-bit value that straddles the alignment boundary.
12829 * We do two 32-bit reads and then shift and merge the results.
12830 */
12831static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
12832{
12833 u32 bhalf_otp, thalf_otp;
12834
12835 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
12836
12837 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
12838 return 0;
12839
12840 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
12841
12842 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12843 return 0;
12844
12845 thalf_otp = tr32(OTP_READ_DATA);
12846
12847 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
12848
12849 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12850 return 0;
12851
12852 bhalf_otp = tr32(OTP_READ_DATA);
12853
12854 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
12855}
12856
Matt Carlsone256f8a2011-03-09 16:58:24 +000012857static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
12858{
12859 u32 adv = ADVERTISED_Autoneg |
12860 ADVERTISED_Pause;
12861
12862 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
12863 adv |= ADVERTISED_1000baseT_Half |
12864 ADVERTISED_1000baseT_Full;
12865
12866 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
12867 adv |= ADVERTISED_100baseT_Half |
12868 ADVERTISED_100baseT_Full |
12869 ADVERTISED_10baseT_Half |
12870 ADVERTISED_10baseT_Full |
12871 ADVERTISED_TP;
12872 else
12873 adv |= ADVERTISED_FIBRE;
12874
12875 tp->link_config.advertising = adv;
12876 tp->link_config.speed = SPEED_INVALID;
12877 tp->link_config.duplex = DUPLEX_INVALID;
12878 tp->link_config.autoneg = AUTONEG_ENABLE;
12879 tp->link_config.active_speed = SPEED_INVALID;
12880 tp->link_config.active_duplex = DUPLEX_INVALID;
12881 tp->link_config.orig_speed = SPEED_INVALID;
12882 tp->link_config.orig_duplex = DUPLEX_INVALID;
12883 tp->link_config.orig_autoneg = AUTONEG_INVALID;
12884}
12885
Michael Chan7d0c41e2005-04-21 17:06:20 -070012886static int __devinit tg3_phy_probe(struct tg3 *tp)
12887{
12888 u32 hw_phy_id_1, hw_phy_id_2;
12889 u32 hw_phy_id, hw_phy_id_masked;
12890 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012891
Matt Carlsone256f8a2011-03-09 16:58:24 +000012892 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000012893 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000012894 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
12895
Joe Perches63c3a662011-04-26 08:12:10 +000012896 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012897 return tg3_phy_init(tp);
12898
Linus Torvalds1da177e2005-04-16 15:20:36 -070012899 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010012900 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012901 */
12902 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000012903 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000012904 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012905 } else {
12906 /* Now read the physical PHY_ID from the chip and verify
12907 * that it is sane. If it doesn't look good, we fall back
12908 * to either the hard-coded table based PHY_ID and failing
12909 * that the value found in the eeprom area.
12910 */
12911 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
12912 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
12913
12914 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
12915 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
12916 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
12917
Matt Carlson79eb6902010-02-17 15:17:03 +000012918 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012919 }
12920
Matt Carlson79eb6902010-02-17 15:17:03 +000012921 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012922 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000012923 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012924 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070012925 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012926 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012927 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000012928 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070012929 /* Do nothing, phy ID already set up in
12930 * tg3_get_eeprom_hw_cfg().
12931 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012932 } else {
12933 struct subsys_tbl_ent *p;
12934
12935 /* No eeprom signature? Try the hardcoded
12936 * subsys device table.
12937 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012938 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012939 if (!p)
12940 return -ENODEV;
12941
12942 tp->phy_id = p->phy_id;
12943 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000012944 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012945 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012946 }
12947 }
12948
Matt Carlsona6b68da2010-12-06 08:28:52 +000012949 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
12950 ((tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
12951 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
12952 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12953 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000012954 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
12955
Matt Carlsone256f8a2011-03-09 16:58:24 +000012956 tg3_phy_init_link_config(tp);
12957
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012958 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000012959 !tg3_flag(tp, ENABLE_APE) &&
12960 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000012961 u32 bmsr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012962
12963 tg3_readphy(tp, MII_BMSR, &bmsr);
12964 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
12965 (bmsr & BMSR_LSTATUS))
12966 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012967
Linus Torvalds1da177e2005-04-16 15:20:36 -070012968 err = tg3_phy_reset(tp);
12969 if (err)
12970 return err;
12971
Matt Carlson42b64a42011-05-19 12:12:49 +000012972 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012973
Michael Chan3600d912006-12-07 00:21:48 -080012974 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
12975 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
12976 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
12977 if (!tg3_copper_is_advertising_all(tp, mask)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000012978 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
12979 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012980
12981 tg3_writephy(tp, MII_BMCR,
12982 BMCR_ANENABLE | BMCR_ANRESTART);
12983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012984 }
12985
12986skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000012987 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012988 err = tg3_init_5401phy_dsp(tp);
12989 if (err)
12990 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012991
Linus Torvalds1da177e2005-04-16 15:20:36 -070012992 err = tg3_init_5401phy_dsp(tp);
12993 }
12994
Linus Torvalds1da177e2005-04-16 15:20:36 -070012995 return err;
12996}
12997
Matt Carlson184b8902010-04-05 10:19:25 +000012998static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012999{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013000 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013001 unsigned int block_end, rosize, len;
Matt Carlson184b8902010-04-05 10:19:25 +000013002 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013003
Matt Carlsonc3e94502011-04-13 11:05:08 +000013004 vpd_data = (u8 *)tg3_vpd_readblock(tp);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013005 if (!vpd_data)
13006 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013007
Matt Carlson4181b2c2010-02-26 14:04:45 +000013008 i = pci_vpd_find_tag(vpd_data, 0, TG3_NVM_VPD_LEN,
13009 PCI_VPD_LRDT_RO_DATA);
13010 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013011 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013012
13013 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
13014 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
13015 i += PCI_VPD_LRDT_TAG_SIZE;
13016
13017 if (block_end > TG3_NVM_VPD_LEN)
13018 goto out_not_found;
13019
Matt Carlson184b8902010-04-05 10:19:25 +000013020 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13021 PCI_VPD_RO_KEYWORD_MFR_ID);
13022 if (j > 0) {
13023 len = pci_vpd_info_field_size(&vpd_data[j]);
13024
13025 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13026 if (j + len > block_end || len != 4 ||
13027 memcmp(&vpd_data[j], "1028", 4))
13028 goto partno;
13029
13030 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13031 PCI_VPD_RO_KEYWORD_VENDOR0);
13032 if (j < 0)
13033 goto partno;
13034
13035 len = pci_vpd_info_field_size(&vpd_data[j]);
13036
13037 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13038 if (j + len > block_end)
13039 goto partno;
13040
13041 memcpy(tp->fw_ver, &vpd_data[j], len);
13042 strncat(tp->fw_ver, " bc ", TG3_NVM_VPD_LEN - len - 1);
13043 }
13044
13045partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000013046 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13047 PCI_VPD_RO_KEYWORD_PARTNO);
13048 if (i < 0)
13049 goto out_not_found;
13050
13051 len = pci_vpd_info_field_size(&vpd_data[i]);
13052
13053 i += PCI_VPD_INFO_FLD_HDR_SIZE;
13054 if (len > TG3_BPN_SIZE ||
13055 (len + i) > TG3_NVM_VPD_LEN)
13056 goto out_not_found;
13057
13058 memcpy(tp->board_part_number, &vpd_data[i], len);
13059
Linus Torvalds1da177e2005-04-16 15:20:36 -070013060out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013061 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000013062 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013063 return;
13064
13065out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000013066 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13067 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
13068 strcpy(tp->board_part_number, "BCM5717");
13069 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
13070 strcpy(tp->board_part_number, "BCM5718");
13071 else
13072 goto nomatch;
13073 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
13074 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
13075 strcpy(tp->board_part_number, "BCM57780");
13076 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
13077 strcpy(tp->board_part_number, "BCM57760");
13078 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
13079 strcpy(tp->board_part_number, "BCM57790");
13080 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
13081 strcpy(tp->board_part_number, "BCM57788");
13082 else
13083 goto nomatch;
13084 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
13085 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
13086 strcpy(tp->board_part_number, "BCM57761");
13087 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
13088 strcpy(tp->board_part_number, "BCM57765");
13089 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
13090 strcpy(tp->board_part_number, "BCM57781");
13091 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
13092 strcpy(tp->board_part_number, "BCM57785");
13093 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
13094 strcpy(tp->board_part_number, "BCM57791");
13095 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13096 strcpy(tp->board_part_number, "BCM57795");
13097 else
13098 goto nomatch;
13099 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070013100 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000013101 } else {
13102nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070013103 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000013104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013105}
13106
Matt Carlson9c8a6202007-10-21 16:16:08 -070013107static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
13108{
13109 u32 val;
13110
Matt Carlsone4f34112009-02-25 14:25:00 +000013111 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013112 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013113 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013114 val != 0)
13115 return 0;
13116
13117 return 1;
13118}
13119
Matt Carlsonacd9c112009-02-25 14:26:33 +000013120static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13121{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013122 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000013123 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013124 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013125
13126 if (tg3_nvram_read(tp, 0xc, &offset) ||
13127 tg3_nvram_read(tp, 0x4, &start))
13128 return;
13129
13130 offset = tg3_nvram_logical_addr(tp, offset);
13131
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013132 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013133 return;
13134
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013135 if ((val & 0xfc000000) == 0x0c000000) {
13136 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013137 return;
13138
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013139 if (val == 0)
13140 newver = true;
13141 }
13142
Matt Carlson75f99362010-04-05 10:19:24 +000013143 dst_off = strlen(tp->fw_ver);
13144
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013145 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013146 if (TG3_VER_SIZE - dst_off < 16 ||
13147 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013148 return;
13149
13150 offset = offset + ver_offset - start;
13151 for (i = 0; i < 16; i += 4) {
13152 __be32 v;
13153 if (tg3_nvram_read_be32(tp, offset + i, &v))
13154 return;
13155
Matt Carlson75f99362010-04-05 10:19:24 +000013156 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013157 }
13158 } else {
13159 u32 major, minor;
13160
13161 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13162 return;
13163
13164 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13165 TG3_NVM_BCVER_MAJSFT;
13166 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013167 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13168 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013169 }
13170}
13171
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013172static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13173{
13174 u32 val, major, minor;
13175
13176 /* Use native endian representation */
13177 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13178 return;
13179
13180 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13181 TG3_NVM_HWSB_CFG1_MAJSFT;
13182 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13183 TG3_NVM_HWSB_CFG1_MINSFT;
13184
13185 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13186}
13187
Matt Carlsondfe00d72008-11-21 17:19:41 -080013188static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13189{
13190 u32 offset, major, minor, build;
13191
Matt Carlson75f99362010-04-05 10:19:24 +000013192 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013193
13194 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13195 return;
13196
13197 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13198 case TG3_EEPROM_SB_REVISION_0:
13199 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13200 break;
13201 case TG3_EEPROM_SB_REVISION_2:
13202 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13203 break;
13204 case TG3_EEPROM_SB_REVISION_3:
13205 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13206 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013207 case TG3_EEPROM_SB_REVISION_4:
13208 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13209 break;
13210 case TG3_EEPROM_SB_REVISION_5:
13211 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13212 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013213 case TG3_EEPROM_SB_REVISION_6:
13214 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13215 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013216 default:
13217 return;
13218 }
13219
Matt Carlsone4f34112009-02-25 14:25:00 +000013220 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013221 return;
13222
13223 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13224 TG3_EEPROM_SB_EDH_BLD_SHFT;
13225 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13226 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13227 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13228
13229 if (minor > 99 || build > 26)
13230 return;
13231
Matt Carlson75f99362010-04-05 10:19:24 +000013232 offset = strlen(tp->fw_ver);
13233 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13234 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013235
13236 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013237 offset = strlen(tp->fw_ver);
13238 if (offset < TG3_VER_SIZE - 1)
13239 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013240 }
13241}
13242
Matt Carlsonacd9c112009-02-25 14:26:33 +000013243static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013244{
13245 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013246 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013247
13248 for (offset = TG3_NVM_DIR_START;
13249 offset < TG3_NVM_DIR_END;
13250 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013251 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013252 return;
13253
13254 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13255 break;
13256 }
13257
13258 if (offset == TG3_NVM_DIR_END)
13259 return;
13260
Joe Perches63c3a662011-04-26 08:12:10 +000013261 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013262 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013263 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013264 return;
13265
Matt Carlsone4f34112009-02-25 14:25:00 +000013266 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013267 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013268 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013269 return;
13270
13271 offset += val - start;
13272
Matt Carlsonacd9c112009-02-25 14:26:33 +000013273 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013274
Matt Carlsonacd9c112009-02-25 14:26:33 +000013275 tp->fw_ver[vlen++] = ',';
13276 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013277
13278 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013279 __be32 v;
13280 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013281 return;
13282
Al Virob9fc7dc2007-12-17 22:59:57 -080013283 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013284
Matt Carlsonacd9c112009-02-25 14:26:33 +000013285 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13286 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013287 break;
13288 }
13289
Matt Carlsonacd9c112009-02-25 14:26:33 +000013290 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13291 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013292 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013293}
13294
Matt Carlson7fd76442009-02-25 14:27:20 +000013295static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13296{
13297 int vlen;
13298 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013299 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013300
Joe Perches63c3a662011-04-26 08:12:10 +000013301 if (!tg3_flag(tp, ENABLE_APE) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000013302 return;
13303
13304 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13305 if (apedata != APE_SEG_SIG_MAGIC)
13306 return;
13307
13308 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13309 if (!(apedata & APE_FW_STATUS_READY))
13310 return;
13311
13312 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13313
Matt Carlsondc6d0742010-09-15 08:59:55 +000013314 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
Joe Perches63c3a662011-04-26 08:12:10 +000013315 tg3_flag_set(tp, APE_HAS_NCSI);
Matt Carlsonecc79642010-08-02 11:26:01 +000013316 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013317 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013318 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013319 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013320
Matt Carlson7fd76442009-02-25 14:27:20 +000013321 vlen = strlen(tp->fw_ver);
13322
Matt Carlsonecc79642010-08-02 11:26:01 +000013323 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13324 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013325 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13326 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13327 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13328 (apedata & APE_FW_VERSION_BLDMSK));
13329}
13330
Matt Carlsonacd9c112009-02-25 14:26:33 +000013331static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13332{
13333 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013334 bool vpd_vers = false;
13335
13336 if (tp->fw_ver[0] != 0)
13337 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013338
Joe Perches63c3a662011-04-26 08:12:10 +000013339 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000013340 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013341 return;
13342 }
13343
Matt Carlsonacd9c112009-02-25 14:26:33 +000013344 if (tg3_nvram_read(tp, 0, &val))
13345 return;
13346
13347 if (val == TG3_EEPROM_MAGIC)
13348 tg3_read_bc_ver(tp);
13349 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13350 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013351 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13352 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013353 else
13354 return;
13355
Joe Perches63c3a662011-04-26 08:12:10 +000013356 if (!tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || vpd_vers)
Matt Carlson75f99362010-04-05 10:19:24 +000013357 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013358
13359 tg3_read_mgmtfw_ver(tp);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013360
Matt Carlson75f99362010-04-05 10:19:24 +000013361done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013362 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013363}
13364
Michael Chan7544b092007-05-05 13:08:32 -070013365static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
13366
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013367static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13368{
Joe Perches63c3a662011-04-26 08:12:10 +000013369 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013370 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000013371 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013372 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013373 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013374 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013375}
13376
Matt Carlson41434702011-03-09 16:58:22 +000013377static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013378 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13379 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13380 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13381 { },
13382};
13383
Linus Torvalds1da177e2005-04-16 15:20:36 -070013384static int __devinit tg3_get_invariants(struct tg3 *tp)
13385{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013386 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013387 u32 pci_state_reg, grc_misc_cfg;
13388 u32 val;
13389 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013390 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013391
Linus Torvalds1da177e2005-04-16 15:20:36 -070013392 /* Force memory write invalidate off. If we leave it on,
13393 * then on 5700_BX chips we have to enable a workaround.
13394 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13395 * to match the cacheline size. The Broadcom driver have this
13396 * workaround but turns MWI off all the times so never uses
13397 * it. This seems to suggest that the workaround is insufficient.
13398 */
13399 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13400 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13401 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13402
13403 /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
13404 * has the register indirect write enable bit set before
13405 * we try to access any of the MMIO registers. It is also
13406 * critical that the PCI-X hw workaround situation is decided
13407 * before that as well.
13408 */
13409 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13410 &misc_ctrl_reg);
13411
13412 tp->pci_chip_rev_id = (misc_ctrl_reg >>
13413 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070013414 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13415 u32 prod_id_asic_rev;
13416
Matt Carlson5001e2f2009-11-13 13:03:51 +000013417 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13418 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013419 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
13420 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013421 pci_read_config_dword(tp->pdev,
13422 TG3PCI_GEN2_PRODID_ASICREV,
13423 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000013424 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13425 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13426 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13427 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13428 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13429 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13430 pci_read_config_dword(tp->pdev,
13431 TG3PCI_GEN15_PRODID_ASICREV,
13432 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013433 else
13434 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
13435 &prod_id_asic_rev);
13436
Matt Carlson321d32a2008-11-21 17:22:19 -080013437 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070013438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013439
Michael Chanff645be2005-04-21 17:09:53 -070013440 /* Wrong chip ID in 5752 A0. This code can be removed later
13441 * as A0 is not in production.
13442 */
13443 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13444 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13445
Michael Chan68929142005-08-09 20:17:14 -070013446 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
13447 * we need to disable memory and use config. cycles
13448 * only to access all registers. The 5702/03 chips
13449 * can mistakenly decode the special cycles from the
13450 * ICH chipsets as memory write cycles, causing corruption
13451 * of register and memory space. Only certain ICH bridges
13452 * will drive special cycles with non-zero data during the
13453 * address phase which can fall within the 5703's address
13454 * range. This is not an ICH bug as the PCI spec allows
13455 * non-zero address during special cycles. However, only
13456 * these ICH bridges are known to drive non-zero addresses
13457 * during special cycles.
13458 *
13459 * Since special cycles do not cross PCI bridges, we only
13460 * enable this workaround if the 5703 is on the secondary
13461 * bus of these ICH bridges.
13462 */
13463 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
13464 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
13465 static struct tg3_dev_id {
13466 u32 vendor;
13467 u32 device;
13468 u32 rev;
13469 } ich_chipsets[] = {
13470 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
13471 PCI_ANY_ID },
13472 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
13473 PCI_ANY_ID },
13474 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
13475 0xa },
13476 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
13477 PCI_ANY_ID },
13478 { },
13479 };
13480 struct tg3_dev_id *pci_id = &ich_chipsets[0];
13481 struct pci_dev *bridge = NULL;
13482
13483 while (pci_id->vendor != 0) {
13484 bridge = pci_get_device(pci_id->vendor, pci_id->device,
13485 bridge);
13486 if (!bridge) {
13487 pci_id++;
13488 continue;
13489 }
13490 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070013491 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070013492 continue;
13493 }
13494 if (bridge->subordinate &&
13495 (bridge->subordinate->number ==
13496 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013497 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070013498 pci_dev_put(bridge);
13499 break;
13500 }
13501 }
13502 }
13503
Matt Carlson6ff6f812011-05-19 12:12:54 +000013504 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070013505 static struct tg3_dev_id {
13506 u32 vendor;
13507 u32 device;
13508 } bridge_chipsets[] = {
13509 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
13510 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
13511 { },
13512 };
13513 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
13514 struct pci_dev *bridge = NULL;
13515
13516 while (pci_id->vendor != 0) {
13517 bridge = pci_get_device(pci_id->vendor,
13518 pci_id->device,
13519 bridge);
13520 if (!bridge) {
13521 pci_id++;
13522 continue;
13523 }
13524 if (bridge->subordinate &&
13525 (bridge->subordinate->number <=
13526 tp->pdev->bus->number) &&
13527 (bridge->subordinate->subordinate >=
13528 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013529 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070013530 pci_dev_put(bridge);
13531 break;
13532 }
13533 }
13534 }
13535
Michael Chan4a29cc22006-03-19 13:21:12 -080013536 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
13537 * DMA addresses > 40-bit. This bridge may have other additional
13538 * 57xx devices behind it in some 4-port NIC designs for example.
13539 * Any tg3 device found behind the bridge will also need the 40-bit
13540 * DMA workaround.
13541 */
Michael Chana4e2b342005-10-26 15:46:52 -070013542 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
13543 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Joe Perches63c3a662011-04-26 08:12:10 +000013544 tg3_flag_set(tp, 5780_CLASS);
13545 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070013546 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000013547 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080013548 struct pci_dev *bridge = NULL;
13549
13550 do {
13551 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
13552 PCI_DEVICE_ID_SERVERWORKS_EPB,
13553 bridge);
13554 if (bridge && bridge->subordinate &&
13555 (bridge->subordinate->number <=
13556 tp->pdev->bus->number) &&
13557 (bridge->subordinate->subordinate >=
13558 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013559 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080013560 pci_dev_put(bridge);
13561 break;
13562 }
13563 } while (bridge);
13564 }
Michael Chan4cf78e42005-07-25 12:29:19 -070013565
Linus Torvalds1da177e2005-04-16 15:20:36 -070013566 /* Initialize misc host control in PCI block. */
13567 tp->misc_host_ctrl |= (misc_ctrl_reg &
13568 MISC_HOST_CTRL_CHIPREV);
13569 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13570 tp->misc_host_ctrl);
13571
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013572 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
13573 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013574 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13575 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Michael Chan7544b092007-05-05 13:08:32 -070013576 tp->pdev_peer = tg3_find_peer(tp);
13577
Matt Carlsonc885e822010-08-02 11:25:57 +000013578 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013579 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13580 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000013581 tg3_flag_set(tp, 5717_PLUS);
Matt Carlson0a58d662011-04-05 14:22:45 +000013582
13583 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013584 tg3_flag(tp, 5717_PLUS))
13585 tg3_flag_set(tp, 57765_PLUS);
Matt Carlsonc885e822010-08-02 11:25:57 +000013586
Matt Carlson321d32a2008-11-21 17:22:19 -080013587 /* Intentionally exclude ASIC_REV_5906 */
13588 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad2006-03-20 22:27:35 -080013589 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013590 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013591 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013592 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013593 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013594 tg3_flag(tp, 57765_PLUS))
13595 tg3_flag_set(tp, 5755_PLUS);
Matt Carlson321d32a2008-11-21 17:22:19 -080013596
13597 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13598 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070013599 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013600 tg3_flag(tp, 5755_PLUS) ||
13601 tg3_flag(tp, 5780_CLASS))
13602 tg3_flag_set(tp, 5750_PLUS);
John W. Linville6708e5c2005-04-21 17:00:52 -070013603
Matt Carlson6ff6f812011-05-19 12:12:54 +000013604 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013605 tg3_flag(tp, 5750_PLUS))
13606 tg3_flag_set(tp, 5705_PLUS);
John W. Linville1b440c562005-04-21 17:03:18 -070013607
Matt Carlson507399f2009-11-13 13:03:37 +000013608 /* Determine TSO capabilities */
Matt Carlson2866d952011-02-10 20:06:46 -080013609 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlson4d163b72011-01-25 15:58:48 +000013610 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000013611 else if (tg3_flag(tp, 57765_PLUS))
13612 tg3_flag_set(tp, HW_TSO_3);
13613 else if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000013614 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000013615 tg3_flag_set(tp, HW_TSO_2);
13616 else if (tg3_flag(tp, 5750_PLUS)) {
13617 tg3_flag_set(tp, HW_TSO_1);
13618 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013619 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13620 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000013621 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013622 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13623 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13624 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000013625 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013626 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13627 tp->fw_needed = FIRMWARE_TG3TSO5;
13628 else
13629 tp->fw_needed = FIRMWARE_TG3TSO;
13630 }
13631
Matt Carlsondabc5c62011-05-19 12:12:52 +000013632 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000013633 if (tg3_flag(tp, HW_TSO_1) ||
13634 tg3_flag(tp, HW_TSO_2) ||
13635 tg3_flag(tp, HW_TSO_3) ||
Matt Carlsondabc5c62011-05-19 12:12:52 +000013636 (tp->fw_needed && !tg3_flag(tp, ENABLE_ASF)))
13637 tg3_flag_set(tp, TSO_CAPABLE);
13638 else {
13639 tg3_flag_clear(tp, TSO_CAPABLE);
13640 tg3_flag_clear(tp, TSO_BUG);
13641 tp->fw_needed = NULL;
13642 }
13643
13644 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
13645 tp->fw_needed = FIRMWARE_TG3;
13646
Matt Carlson507399f2009-11-13 13:03:37 +000013647 tp->irq_max = 1;
13648
Joe Perches63c3a662011-04-26 08:12:10 +000013649 if (tg3_flag(tp, 5750_PLUS)) {
13650 tg3_flag_set(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070013651 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
13652 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
13653 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
13654 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
13655 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000013656 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070013657
Joe Perches63c3a662011-04-26 08:12:10 +000013658 if (tg3_flag(tp, 5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070013659 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000013660 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070013661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013662
Joe Perches63c3a662011-04-26 08:12:10 +000013663 if (tg3_flag(tp, 57765_PLUS)) {
13664 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000013665 tp->irq_max = TG3_IRQ_MAX_VECS;
13666 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013667 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000013668
Matt Carlson2ffcc982011-05-19 12:12:44 +000013669 /* All chips can get confused if TX buffers
13670 * straddle the 4GB address boundary.
13671 */
13672 tg3_flag_set(tp, 4G_DMA_BNDRY_BUG);
13673
13674 if (tg3_flag(tp, 5755_PLUS))
Joe Perches63c3a662011-04-26 08:12:10 +000013675 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlson2ffcc982011-05-19 12:12:44 +000013676 else
Joe Perches63c3a662011-04-26 08:12:10 +000013677 tg3_flag_set(tp, 40BIT_DMA_LIMIT_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013678
Joe Perches63c3a662011-04-26 08:12:10 +000013679 if (tg3_flag(tp, 5717_PLUS))
13680 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000013681
Joe Perches63c3a662011-04-26 08:12:10 +000013682 if (tg3_flag(tp, 57765_PLUS) &&
Matt Carlson2866d952011-02-10 20:06:46 -080013683 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
Joe Perches63c3a662011-04-26 08:12:10 +000013684 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000013685
Joe Perches63c3a662011-04-26 08:12:10 +000013686 if (!tg3_flag(tp, 5705_PLUS) ||
13687 tg3_flag(tp, 5780_CLASS) ||
13688 tg3_flag(tp, USE_JUMBO_BDFLAG))
13689 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070013690
Matt Carlson52f44902008-11-21 17:17:04 -080013691 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13692 &pci_state_reg);
13693
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013694 tp->pcie_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_EXP);
13695 if (tp->pcie_cap != 0) {
13696 u16 lnkctl;
13697
Joe Perches63c3a662011-04-26 08:12:10 +000013698 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013699
Matt Carlsoncf790032010-11-24 08:31:48 +000013700 tp->pcie_readrq = 4096;
Matt Carlsond78b59f2011-04-05 14:22:46 +000013701 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13702 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Matt Carlsonb4495ed2011-01-25 15:58:47 +000013703 tp->pcie_readrq = 2048;
Matt Carlsoncf790032010-11-24 08:31:48 +000013704
13705 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013706
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013707 pci_read_config_word(tp->pdev,
13708 tp->pcie_cap + PCI_EXP_LNKCTL,
13709 &lnkctl);
13710 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Matt Carlson7196cd62011-05-19 16:02:44 +000013711 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
13712 ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000013713 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000013714 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000013715 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013716 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013717 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000013718 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
13719 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000013720 tg3_flag_set(tp, CLKREQ_BUG);
Matt Carlson614b0592010-01-20 16:58:02 +000013721 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000013722 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080013723 }
Matt Carlson52f44902008-11-21 17:17:04 -080013724 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Joe Perches63c3a662011-04-26 08:12:10 +000013725 tg3_flag_set(tp, PCI_EXPRESS);
13726 } else if (!tg3_flag(tp, 5705_PLUS) ||
13727 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080013728 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
13729 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000013730 dev_err(&tp->pdev->dev,
13731 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080013732 return -EIO;
13733 }
13734
13735 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000013736 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080013737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013738
Michael Chan399de502005-10-03 14:02:39 -070013739 /* If we have an AMD 762 or VIA K8T800 chipset, write
13740 * reordering to the mailbox registers done by the host
13741 * controller can cause major troubles. We read back from
13742 * every mailbox register write to force the writes to be
13743 * posted to the chip in order.
13744 */
Matt Carlson41434702011-03-09 16:58:22 +000013745 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013746 !tg3_flag(tp, PCI_EXPRESS))
13747 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070013748
Matt Carlson69fc4052008-12-21 20:19:57 -080013749 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
13750 &tp->pci_cacheline_sz);
13751 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13752 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013753 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13754 tp->pci_lat_timer < 64) {
13755 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080013756 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13757 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013758 }
13759
Matt Carlson52f44902008-11-21 17:17:04 -080013760 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
13761 /* 5700 BX chips need to have their TX producer index
13762 * mailboxes written twice to workaround a bug.
13763 */
Joe Perches63c3a662011-04-26 08:12:10 +000013764 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070013765
Matt Carlson52f44902008-11-21 17:17:04 -080013766 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013767 *
13768 * The workaround is to use indirect register accesses
13769 * for all chip writes not to mailbox registers.
13770 */
Joe Perches63c3a662011-04-26 08:12:10 +000013771 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013772 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013773
Joe Perches63c3a662011-04-26 08:12:10 +000013774 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013775
13776 /* The chip can have it's power management PCI config
13777 * space registers clobbered due to this bug.
13778 * So explicitly force the chip into D0 here.
13779 */
Matt Carlson9974a352007-10-07 23:27:28 -070013780 pci_read_config_dword(tp->pdev,
13781 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013782 &pm_reg);
13783 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
13784 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070013785 pci_write_config_dword(tp->pdev,
13786 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013787 pm_reg);
13788
13789 /* Also, force SERR#/PERR# in PCI command. */
13790 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13791 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
13792 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13793 }
13794 }
13795
Linus Torvalds1da177e2005-04-16 15:20:36 -070013796 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000013797 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013798 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000013799 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013800
13801 /* Chip-specific fixup from Broadcom driver */
13802 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
13803 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
13804 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
13805 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
13806 }
13807
Michael Chan1ee582d2005-08-09 20:16:46 -070013808 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070013809 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013810 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070013811 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070013812 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013813 tp->write32_tx_mbox = tg3_write32;
13814 tp->write32_rx_mbox = tg3_write32;
13815
13816 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000013817 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070013818 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013819 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013820 (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson98efd8a2007-05-05 12:47:25 -070013821 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
13822 /*
13823 * Back to back register writes can cause problems on these
13824 * chips, the workaround is to read back all reg writes
13825 * except those to mailbox regs.
13826 *
13827 * See tg3_write_indirect_reg32().
13828 */
Michael Chan1ee582d2005-08-09 20:16:46 -070013829 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013830 }
13831
Joe Perches63c3a662011-04-26 08:12:10 +000013832 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070013833 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000013834 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070013835 tp->write32_rx_mbox = tg3_write_flush_reg32;
13836 }
Michael Chan20094932005-08-09 20:16:32 -070013837
Joe Perches63c3a662011-04-26 08:12:10 +000013838 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070013839 tp->read32 = tg3_read_indirect_reg32;
13840 tp->write32 = tg3_write_indirect_reg32;
13841 tp->read32_mbox = tg3_read_indirect_mbox;
13842 tp->write32_mbox = tg3_write_indirect_mbox;
13843 tp->write32_tx_mbox = tg3_write_indirect_mbox;
13844 tp->write32_rx_mbox = tg3_write_indirect_mbox;
13845
13846 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070013847 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070013848
13849 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13850 pci_cmd &= ~PCI_COMMAND_MEMORY;
13851 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13852 }
Michael Chanb5d37722006-09-27 16:06:21 -070013853 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13854 tp->read32_mbox = tg3_read32_mbox_5906;
13855 tp->write32_mbox = tg3_write32_mbox_5906;
13856 tp->write32_tx_mbox = tg3_write32_mbox_5906;
13857 tp->write32_rx_mbox = tg3_write32_mbox_5906;
13858 }
Michael Chan68929142005-08-09 20:17:14 -070013859
Michael Chanbbadf502006-04-06 21:46:34 -070013860 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013861 (tg3_flag(tp, PCIX_MODE) &&
Michael Chanbbadf502006-04-06 21:46:34 -070013862 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070013863 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000013864 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070013865
Michael Chan7d0c41e2005-04-21 17:06:20 -070013866 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000013867 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070013868 * determined before calling tg3_set_power_state() so that
13869 * we know whether or not to switch out of Vaux power.
13870 * When the flag is set, it means that GPIO1 is used for eeprom
13871 * write protect and also implies that it is a LOM where GPIOs
13872 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013873 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070013874 tg3_get_eeprom_hw_cfg(tp);
13875
Joe Perches63c3a662011-04-26 08:12:10 +000013876 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070013877 /* Allow reads and writes to the
13878 * APE register and memory space.
13879 */
13880 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000013881 PCISTATE_ALLOW_APE_SHMEM_WR |
13882 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070013883 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
13884 pci_state_reg);
13885 }
13886
Matt Carlson9936bcf2007-10-10 18:03:07 -070013887 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013888 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013889 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013890 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013891 tg3_flag(tp, 57765_PLUS))
13892 tg3_flag_set(tp, CPMU_PRESENT);
Matt Carlsond30cdd22007-10-07 23:28:35 -070013893
Matt Carlsonbea8a632011-04-25 12:42:49 +000013894 /* Set up tp->grc_local_ctrl before calling tg3_power_up().
Michael Chan314fba32005-04-21 17:07:04 -070013895 * GPIO1 driven high will bring 5700's external PHY out of reset.
13896 * It is also used as eeprom write protect on LOMs.
13897 */
13898 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Matt Carlson6ff6f812011-05-19 12:12:54 +000013899 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013900 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070013901 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
13902 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070013903 /* Unused GPIO3 must be driven as output on 5752 because there
13904 * are no pull-up resistors on unused GPIO pins.
13905 */
13906 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
13907 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070013908
Matt Carlson321d32a2008-11-21 17:22:19 -080013909 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000013910 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
13911 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Michael Chanaf36e6b2006-03-23 01:28:06 -080013912 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13913
Matt Carlson8d519ab2009-04-20 06:58:01 +000013914 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
13915 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070013916 /* Turn off the debug UART. */
13917 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013918 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070013919 /* Keep VMain power. */
13920 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
13921 GRC_LCLCTRL_GPIO_OUTPUT0;
13922 }
13923
Linus Torvalds1da177e2005-04-16 15:20:36 -070013924 /* Force the chip into D0. */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000013925 err = tg3_power_up(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013926 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000013927 dev_err(&tp->pdev->dev, "Transition to D0 failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070013928 return err;
13929 }
13930
Linus Torvalds1da177e2005-04-16 15:20:36 -070013931 /* Derive initial jumbo mode from MTU assigned in
13932 * ether_setup() via the alloc_etherdev() call
13933 */
Joe Perches63c3a662011-04-26 08:12:10 +000013934 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
13935 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013936
13937 /* Determine WakeOnLan speed to use. */
13938 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13939 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
13940 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
13941 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000013942 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013943 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013944 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013945 }
13946
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013947 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013948 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013949
Linus Torvalds1da177e2005-04-16 15:20:36 -070013950 /* A few boards don't want Ethernet@WireSpeed phy feature */
Matt Carlson6ff6f812011-05-19 12:12:54 +000013951 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13952 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013953 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070013954 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013955 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
13956 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13957 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013958
13959 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
13960 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013961 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013962 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013963 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013964
Joe Perches63c3a662011-04-26 08:12:10 +000013965 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013966 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080013967 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013968 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000013969 !tg3_flag(tp, 57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070013970 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013971 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013972 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13973 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080013974 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
13975 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013976 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080013977 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013978 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080013979 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013980 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070013981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013982
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013983 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13984 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
13985 tp->phy_otp = tg3_read_otp_phycfg(tp);
13986 if (tp->phy_otp == 0)
13987 tp->phy_otp = TG3_OTP_DEFAULT;
13988 }
13989
Joe Perches63c3a662011-04-26 08:12:10 +000013990 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070013991 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
13992 else
13993 tp->mi_mode = MAC_MI_MODE_BASE;
13994
Linus Torvalds1da177e2005-04-16 15:20:36 -070013995 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013996 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
13997 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
13998 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
13999
Matt Carlson4d958472011-04-20 07:57:35 +000014000 /* Set these bits to enable statistics workaround. */
14001 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14002 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
14003 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
14004 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
14005 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
14006 }
14007
Matt Carlson321d32a2008-11-21 17:22:19 -080014008 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14009 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000014010 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070014011
Matt Carlson158d7ab2008-05-29 01:37:54 -070014012 err = tg3_mdio_init(tp);
14013 if (err)
14014 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014015
14016 /* Initialize data/descriptor byte/word swapping. */
14017 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000014018 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14019 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
14020 GRC_MODE_WORD_SWAP_B2HRX_DATA |
14021 GRC_MODE_B2HRX_ENABLE |
14022 GRC_MODE_HTX2B_ENABLE |
14023 GRC_MODE_HOST_STACKUP);
14024 else
14025 val &= GRC_MODE_HOST_STACKUP;
14026
Linus Torvalds1da177e2005-04-16 15:20:36 -070014027 tw32(GRC_MODE, val | tp->grc_mode);
14028
14029 tg3_switch_clocks(tp);
14030
14031 /* Clear this out for sanity. */
14032 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
14033
14034 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14035 &pci_state_reg);
14036 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014037 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014038 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
14039
14040 if (chiprevid == CHIPREV_ID_5701_A0 ||
14041 chiprevid == CHIPREV_ID_5701_B0 ||
14042 chiprevid == CHIPREV_ID_5701_B2 ||
14043 chiprevid == CHIPREV_ID_5701_B5) {
14044 void __iomem *sram_base;
14045
14046 /* Write some dummy words into the SRAM status block
14047 * area, see if it reads back correctly. If the return
14048 * value is bad, force enable the PCIX workaround.
14049 */
14050 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
14051
14052 writel(0x00000000, sram_base);
14053 writel(0x00000000, sram_base + 4);
14054 writel(0xffffffff, sram_base + 4);
14055 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000014056 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014057 }
14058 }
14059
14060 udelay(50);
14061 tg3_nvram_init(tp);
14062
14063 grc_misc_cfg = tr32(GRC_MISC_CFG);
14064 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
14065
Linus Torvalds1da177e2005-04-16 15:20:36 -070014066 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14067 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
14068 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000014069 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014070
Joe Perches63c3a662011-04-26 08:12:10 +000014071 if (!tg3_flag(tp, IS_5788) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +000014072 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014073 tg3_flag_set(tp, TAGGED_STATUS);
14074 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070014075 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
14076 HOSTCC_MODE_CLRTICK_TXBD);
14077
14078 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
14079 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14080 tp->misc_host_ctrl);
14081 }
14082
Matt Carlson3bda1252008-08-15 14:08:22 -070014083 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000014084 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000014085 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070014086 else
14087 tp->mac_mode = TG3_DEF_MAC_MODE;
14088
Linus Torvalds1da177e2005-04-16 15:20:36 -070014089 /* these are limited to 10/100 only */
14090 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14091 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14092 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14093 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14094 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
14095 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
14096 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
14097 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14098 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080014099 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
14100 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014101 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000014102 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14103 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014104 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14105 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014106
14107 err = tg3_phy_probe(tp);
14108 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014109 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014110 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014111 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014112 }
14113
Matt Carlson184b8902010-04-05 10:19:25 +000014114 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080014115 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014116
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014117 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
14118 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014119 } else {
14120 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014121 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014122 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014123 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014124 }
14125
14126 /* 5700 {AX,BX} chips have a broken status block link
14127 * change bit implementation, so we must use the
14128 * status register in those cases.
14129 */
14130 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014131 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014132 else
Joe Perches63c3a662011-04-26 08:12:10 +000014133 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014134
14135 /* The led_ctrl is set during tg3_phy_probe, here we might
14136 * have to force the link status polling mechanism based
14137 * upon subsystem IDs.
14138 */
14139 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070014140 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014141 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
14142 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000014143 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014144 }
14145
14146 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014147 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000014148 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014149 else
Joe Perches63c3a662011-04-26 08:12:10 +000014150 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014151
Matt Carlsonbf933c82011-01-25 15:58:49 +000014152 tp->rx_offset = NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014153 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014154 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014155 tg3_flag(tp, PCIX_MODE)) {
Matt Carlsonbf933c82011-01-25 15:58:49 +000014156 tp->rx_offset = 0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014157#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000014158 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014159#endif
14160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014161
Matt Carlson2c49a442010-09-30 10:34:35 +000014162 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14163 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014164 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14165
Matt Carlson2c49a442010-09-30 10:34:35 +000014166 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070014167
14168 /* Increment the rx prod index on the rx std ring by at most
14169 * 8 for these chips to workaround hw errata.
14170 */
14171 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14172 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14173 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14174 tp->rx_std_max_post = 8;
14175
Joe Perches63c3a662011-04-26 08:12:10 +000014176 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070014177 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14178 PCIE_PWR_MGMT_L1_THRESH_MSK;
14179
Linus Torvalds1da177e2005-04-16 15:20:36 -070014180 return err;
14181}
14182
David S. Miller49b6e95f2007-03-29 01:38:42 -070014183#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014184static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14185{
14186 struct net_device *dev = tp->dev;
14187 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014188 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070014189 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014190 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014191
David S. Miller49b6e95f2007-03-29 01:38:42 -070014192 addr = of_get_property(dp, "local-mac-address", &len);
14193 if (addr && len == 6) {
14194 memcpy(dev->dev_addr, addr, 6);
14195 memcpy(dev->perm_addr, dev->dev_addr, 6);
14196 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014197 }
14198 return -ENODEV;
14199}
14200
14201static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14202{
14203 struct net_device *dev = tp->dev;
14204
14205 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070014206 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014207 return 0;
14208}
14209#endif
14210
14211static int __devinit tg3_get_device_address(struct tg3 *tp)
14212{
14213 struct net_device *dev = tp->dev;
14214 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080014215 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014216
David S. Miller49b6e95f2007-03-29 01:38:42 -070014217#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014218 if (!tg3_get_macaddr_sparc(tp))
14219 return 0;
14220#endif
14221
14222 mac_offset = 0x7c;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014223 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014224 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014225 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14226 mac_offset = 0xcc;
14227 if (tg3_nvram_lock(tp))
14228 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14229 else
14230 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000014231 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlsona50d0792010-06-05 17:24:37 +000014232 if (PCI_FUNC(tp->pdev->devfn) & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014233 mac_offset = 0xcc;
Matt Carlsona50d0792010-06-05 17:24:37 +000014234 if (PCI_FUNC(tp->pdev->devfn) > 1)
14235 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014236 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014237 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014238
14239 /* First try to get it from MAC address mailbox. */
14240 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14241 if ((hi >> 16) == 0x484b) {
14242 dev->dev_addr[0] = (hi >> 8) & 0xff;
14243 dev->dev_addr[1] = (hi >> 0) & 0xff;
14244
14245 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14246 dev->dev_addr[2] = (lo >> 24) & 0xff;
14247 dev->dev_addr[3] = (lo >> 16) & 0xff;
14248 dev->dev_addr[4] = (lo >> 8) & 0xff;
14249 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014250
Michael Chan008652b2006-03-27 23:14:53 -080014251 /* Some old bootcode may report a 0 MAC address in SRAM */
14252 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14253 }
14254 if (!addr_ok) {
14255 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000014256 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000014257 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000014258 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070014259 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14260 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080014261 }
14262 /* Finally just fetch it out of the MAC control regs. */
14263 else {
14264 hi = tr32(MAC_ADDR_0_HIGH);
14265 lo = tr32(MAC_ADDR_0_LOW);
14266
14267 dev->dev_addr[5] = lo & 0xff;
14268 dev->dev_addr[4] = (lo >> 8) & 0xff;
14269 dev->dev_addr[3] = (lo >> 16) & 0xff;
14270 dev->dev_addr[2] = (lo >> 24) & 0xff;
14271 dev->dev_addr[1] = hi & 0xff;
14272 dev->dev_addr[0] = (hi >> 8) & 0xff;
14273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014274 }
14275
14276 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014277#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014278 if (!tg3_get_default_macaddr_sparc(tp))
14279 return 0;
14280#endif
14281 return -EINVAL;
14282 }
John W. Linville2ff43692005-09-12 14:44:20 -070014283 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014284 return 0;
14285}
14286
David S. Miller59e6b432005-05-18 22:50:10 -070014287#define BOUNDARY_SINGLE_CACHELINE 1
14288#define BOUNDARY_MULTI_CACHELINE 2
14289
14290static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14291{
14292 int cacheline_size;
14293 u8 byte;
14294 int goal;
14295
14296 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14297 if (byte == 0)
14298 cacheline_size = 1024;
14299 else
14300 cacheline_size = (int) byte * 4;
14301
14302 /* On 5703 and later chips, the boundary bits have no
14303 * effect.
14304 */
14305 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14306 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014307 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070014308 goto out;
14309
14310#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14311 goal = BOUNDARY_MULTI_CACHELINE;
14312#else
14313#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14314 goal = BOUNDARY_SINGLE_CACHELINE;
14315#else
14316 goal = 0;
14317#endif
14318#endif
14319
Joe Perches63c3a662011-04-26 08:12:10 +000014320 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014321 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14322 goto out;
14323 }
14324
David S. Miller59e6b432005-05-18 22:50:10 -070014325 if (!goal)
14326 goto out;
14327
14328 /* PCI controllers on most RISC systems tend to disconnect
14329 * when a device tries to burst across a cache-line boundary.
14330 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14331 *
14332 * Unfortunately, for PCI-E there are only limited
14333 * write-side controls for this, and thus for reads
14334 * we will still get the disconnects. We'll also waste
14335 * these PCI cycles for both read and write for chips
14336 * other than 5700 and 5701 which do not implement the
14337 * boundary bits.
14338 */
Joe Perches63c3a662011-04-26 08:12:10 +000014339 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014340 switch (cacheline_size) {
14341 case 16:
14342 case 32:
14343 case 64:
14344 case 128:
14345 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14346 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14347 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14348 } else {
14349 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14350 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14351 }
14352 break;
14353
14354 case 256:
14355 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14356 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14357 break;
14358
14359 default:
14360 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14361 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14362 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014363 }
Joe Perches63c3a662011-04-26 08:12:10 +000014364 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014365 switch (cacheline_size) {
14366 case 16:
14367 case 32:
14368 case 64:
14369 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14370 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14371 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14372 break;
14373 }
14374 /* fallthrough */
14375 case 128:
14376 default:
14377 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14378 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14379 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014380 }
David S. Miller59e6b432005-05-18 22:50:10 -070014381 } else {
14382 switch (cacheline_size) {
14383 case 16:
14384 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14385 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14386 DMA_RWCTRL_WRITE_BNDRY_16);
14387 break;
14388 }
14389 /* fallthrough */
14390 case 32:
14391 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14392 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14393 DMA_RWCTRL_WRITE_BNDRY_32);
14394 break;
14395 }
14396 /* fallthrough */
14397 case 64:
14398 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14399 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14400 DMA_RWCTRL_WRITE_BNDRY_64);
14401 break;
14402 }
14403 /* fallthrough */
14404 case 128:
14405 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14406 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14407 DMA_RWCTRL_WRITE_BNDRY_128);
14408 break;
14409 }
14410 /* fallthrough */
14411 case 256:
14412 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14413 DMA_RWCTRL_WRITE_BNDRY_256);
14414 break;
14415 case 512:
14416 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14417 DMA_RWCTRL_WRITE_BNDRY_512);
14418 break;
14419 case 1024:
14420 default:
14421 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14422 DMA_RWCTRL_WRITE_BNDRY_1024);
14423 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014424 }
David S. Miller59e6b432005-05-18 22:50:10 -070014425 }
14426
14427out:
14428 return val;
14429}
14430
Linus Torvalds1da177e2005-04-16 15:20:36 -070014431static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
14432{
14433 struct tg3_internal_buffer_desc test_desc;
14434 u32 sram_dma_descs;
14435 int i, ret;
14436
14437 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
14438
14439 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
14440 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
14441 tw32(RDMAC_STATUS, 0);
14442 tw32(WDMAC_STATUS, 0);
14443
14444 tw32(BUFMGR_MODE, 0);
14445 tw32(FTQ_RESET, 0);
14446
14447 test_desc.addr_hi = ((u64) buf_dma) >> 32;
14448 test_desc.addr_lo = buf_dma & 0xffffffff;
14449 test_desc.nic_mbuf = 0x00002100;
14450 test_desc.len = size;
14451
14452 /*
14453 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
14454 * the *second* time the tg3 driver was getting loaded after an
14455 * initial scan.
14456 *
14457 * Broadcom tells me:
14458 * ...the DMA engine is connected to the GRC block and a DMA
14459 * reset may affect the GRC block in some unpredictable way...
14460 * The behavior of resets to individual blocks has not been tested.
14461 *
14462 * Broadcom noted the GRC reset will also reset all sub-components.
14463 */
14464 if (to_device) {
14465 test_desc.cqid_sqid = (13 << 8) | 2;
14466
14467 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
14468 udelay(40);
14469 } else {
14470 test_desc.cqid_sqid = (16 << 8) | 7;
14471
14472 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
14473 udelay(40);
14474 }
14475 test_desc.flags = 0x00000005;
14476
14477 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
14478 u32 val;
14479
14480 val = *(((u32 *)&test_desc) + i);
14481 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
14482 sram_dma_descs + (i * sizeof(u32)));
14483 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
14484 }
14485 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
14486
Matt Carlson859a588792010-04-05 10:19:28 +000014487 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014488 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000014489 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014490 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014491
14492 ret = -ENODEV;
14493 for (i = 0; i < 40; i++) {
14494 u32 val;
14495
14496 if (to_device)
14497 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
14498 else
14499 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
14500 if ((val & 0xffff) == sram_dma_descs) {
14501 ret = 0;
14502 break;
14503 }
14504
14505 udelay(100);
14506 }
14507
14508 return ret;
14509}
14510
David S. Millerded73402005-05-23 13:59:47 -070014511#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070014512
Matt Carlson41434702011-03-09 16:58:22 +000014513static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080014514 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14515 { },
14516};
14517
Linus Torvalds1da177e2005-04-16 15:20:36 -070014518static int __devinit tg3_test_dma(struct tg3 *tp)
14519{
14520 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070014521 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014522 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014523
Matt Carlson4bae65c2010-11-24 08:31:52 +000014524 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
14525 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014526 if (!buf) {
14527 ret = -ENOMEM;
14528 goto out_nofree;
14529 }
14530
14531 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
14532 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
14533
David S. Miller59e6b432005-05-18 22:50:10 -070014534 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014535
Joe Perches63c3a662011-04-26 08:12:10 +000014536 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014537 goto out;
14538
Joe Perches63c3a662011-04-26 08:12:10 +000014539 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014540 /* DMA read watermark not used on PCIE */
14541 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000014542 } else if (!tg3_flag(tp, PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070014543 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14544 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014545 tp->dma_rwctrl |= 0x003f0000;
14546 else
14547 tp->dma_rwctrl |= 0x003f000f;
14548 } else {
14549 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14550 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
14551 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080014552 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014553
Michael Chan4a29cc22006-03-19 13:21:12 -080014554 /* If the 5704 is behind the EPB bridge, we can
14555 * do the less restrictive ONE_DMA workaround for
14556 * better performance.
14557 */
Joe Perches63c3a662011-04-26 08:12:10 +000014558 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Michael Chan4a29cc22006-03-19 13:21:12 -080014559 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14560 tp->dma_rwctrl |= 0x8000;
14561 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014562 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
14563
Michael Chan49afdeb2007-02-13 12:17:03 -080014564 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
14565 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070014566 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080014567 tp->dma_rwctrl |=
14568 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
14569 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
14570 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070014571 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
14572 /* 5780 always in PCIX mode */
14573 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070014574 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
14575 /* 5714 always in PCIX mode */
14576 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014577 } else {
14578 tp->dma_rwctrl |= 0x001b000f;
14579 }
14580 }
14581
14582 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14583 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14584 tp->dma_rwctrl &= 0xfffffff0;
14585
14586 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14587 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
14588 /* Remove this if it causes problems for some boards. */
14589 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
14590
14591 /* On 5700/5701 chips, we need to set this bit.
14592 * Otherwise the chip will issue cacheline transactions
14593 * to streamable DMA memory with not all the byte
14594 * enables turned on. This is an error on several
14595 * RISC PCI controllers, in particular sparc64.
14596 *
14597 * On 5703/5704 chips, this bit has been reassigned
14598 * a different meaning. In particular, it is used
14599 * on those chips to enable a PCI-X workaround.
14600 */
14601 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
14602 }
14603
14604 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14605
14606#if 0
14607 /* Unneeded, already done by tg3_get_invariants. */
14608 tg3_switch_clocks(tp);
14609#endif
14610
Linus Torvalds1da177e2005-04-16 15:20:36 -070014611 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14612 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
14613 goto out;
14614
David S. Miller59e6b432005-05-18 22:50:10 -070014615 /* It is best to perform DMA test with maximum write burst size
14616 * to expose the 5700/5701 write DMA bug.
14617 */
14618 saved_dma_rwctrl = tp->dma_rwctrl;
14619 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14620 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14621
Linus Torvalds1da177e2005-04-16 15:20:36 -070014622 while (1) {
14623 u32 *p = buf, i;
14624
14625 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
14626 p[i] = i;
14627
14628 /* Send the buffer to the chip. */
14629 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
14630 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000014631 dev_err(&tp->pdev->dev,
14632 "%s: Buffer write failed. err = %d\n",
14633 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014634 break;
14635 }
14636
14637#if 0
14638 /* validate data reached card RAM correctly. */
14639 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14640 u32 val;
14641 tg3_read_mem(tp, 0x2100 + (i*4), &val);
14642 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000014643 dev_err(&tp->pdev->dev,
14644 "%s: Buffer corrupted on device! "
14645 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014646 /* ret = -ENODEV here? */
14647 }
14648 p[i] = 0;
14649 }
14650#endif
14651 /* Now read it back. */
14652 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
14653 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014654 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
14655 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014656 break;
14657 }
14658
14659 /* Verify it. */
14660 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14661 if (p[i] == i)
14662 continue;
14663
David S. Miller59e6b432005-05-18 22:50:10 -070014664 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14665 DMA_RWCTRL_WRITE_BNDRY_16) {
14666 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014667 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
14668 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14669 break;
14670 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000014671 dev_err(&tp->pdev->dev,
14672 "%s: Buffer corrupted on read back! "
14673 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014674 ret = -ENODEV;
14675 goto out;
14676 }
14677 }
14678
14679 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
14680 /* Success. */
14681 ret = 0;
14682 break;
14683 }
14684 }
David S. Miller59e6b432005-05-18 22:50:10 -070014685 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14686 DMA_RWCTRL_WRITE_BNDRY_16) {
14687 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070014688 * now look for chipsets that are known to expose the
14689 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070014690 */
Matt Carlson41434702011-03-09 16:58:22 +000014691 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014692 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14693 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000014694 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014695 /* Safe to use the calculated DMA boundary. */
14696 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000014697 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070014698
David S. Miller59e6b432005-05-18 22:50:10 -070014699 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014701
14702out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000014703 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014704out_nofree:
14705 return ret;
14706}
14707
Linus Torvalds1da177e2005-04-16 15:20:36 -070014708static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
14709{
Joe Perches63c3a662011-04-26 08:12:10 +000014710 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000014711 tp->bufmgr_config.mbuf_read_dma_low_water =
14712 DEFAULT_MB_RDMA_LOW_WATER_5705;
14713 tp->bufmgr_config.mbuf_mac_rx_low_water =
14714 DEFAULT_MB_MACRX_LOW_WATER_57765;
14715 tp->bufmgr_config.mbuf_high_water =
14716 DEFAULT_MB_HIGH_WATER_57765;
14717
14718 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14719 DEFAULT_MB_RDMA_LOW_WATER_5705;
14720 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14721 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
14722 tp->bufmgr_config.mbuf_high_water_jumbo =
14723 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000014724 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070014725 tp->bufmgr_config.mbuf_read_dma_low_water =
14726 DEFAULT_MB_RDMA_LOW_WATER_5705;
14727 tp->bufmgr_config.mbuf_mac_rx_low_water =
14728 DEFAULT_MB_MACRX_LOW_WATER_5705;
14729 tp->bufmgr_config.mbuf_high_water =
14730 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070014731 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14732 tp->bufmgr_config.mbuf_mac_rx_low_water =
14733 DEFAULT_MB_MACRX_LOW_WATER_5906;
14734 tp->bufmgr_config.mbuf_high_water =
14735 DEFAULT_MB_HIGH_WATER_5906;
14736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014737
Michael Chanfdfec1722005-07-25 12:31:48 -070014738 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14739 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
14740 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14741 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
14742 tp->bufmgr_config.mbuf_high_water_jumbo =
14743 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
14744 } else {
14745 tp->bufmgr_config.mbuf_read_dma_low_water =
14746 DEFAULT_MB_RDMA_LOW_WATER;
14747 tp->bufmgr_config.mbuf_mac_rx_low_water =
14748 DEFAULT_MB_MACRX_LOW_WATER;
14749 tp->bufmgr_config.mbuf_high_water =
14750 DEFAULT_MB_HIGH_WATER;
14751
14752 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14753 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
14754 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14755 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
14756 tp->bufmgr_config.mbuf_high_water_jumbo =
14757 DEFAULT_MB_HIGH_WATER_JUMBO;
14758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014759
14760 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
14761 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
14762}
14763
14764static char * __devinit tg3_phy_string(struct tg3 *tp)
14765{
Matt Carlson79eb6902010-02-17 15:17:03 +000014766 switch (tp->phy_id & TG3_PHY_ID_MASK) {
14767 case TG3_PHY_ID_BCM5400: return "5400";
14768 case TG3_PHY_ID_BCM5401: return "5401";
14769 case TG3_PHY_ID_BCM5411: return "5411";
14770 case TG3_PHY_ID_BCM5701: return "5701";
14771 case TG3_PHY_ID_BCM5703: return "5703";
14772 case TG3_PHY_ID_BCM5704: return "5704";
14773 case TG3_PHY_ID_BCM5705: return "5705";
14774 case TG3_PHY_ID_BCM5750: return "5750";
14775 case TG3_PHY_ID_BCM5752: return "5752";
14776 case TG3_PHY_ID_BCM5714: return "5714";
14777 case TG3_PHY_ID_BCM5780: return "5780";
14778 case TG3_PHY_ID_BCM5755: return "5755";
14779 case TG3_PHY_ID_BCM5787: return "5787";
14780 case TG3_PHY_ID_BCM5784: return "5784";
14781 case TG3_PHY_ID_BCM5756: return "5722/5756";
14782 case TG3_PHY_ID_BCM5906: return "5906";
14783 case TG3_PHY_ID_BCM5761: return "5761";
14784 case TG3_PHY_ID_BCM5718C: return "5718C";
14785 case TG3_PHY_ID_BCM5718S: return "5718S";
14786 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000014787 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000014788 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000014789 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070014790 case 0: return "serdes";
14791 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070014792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014793}
14794
Michael Chanf9804dd2005-09-27 12:13:10 -070014795static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
14796{
Joe Perches63c3a662011-04-26 08:12:10 +000014797 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070014798 strcpy(str, "PCI Express");
14799 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000014800 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070014801 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
14802
14803 strcpy(str, "PCIX:");
14804
14805 if ((clock_ctrl == 7) ||
14806 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
14807 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
14808 strcat(str, "133MHz");
14809 else if (clock_ctrl == 0)
14810 strcat(str, "33MHz");
14811 else if (clock_ctrl == 2)
14812 strcat(str, "50MHz");
14813 else if (clock_ctrl == 4)
14814 strcat(str, "66MHz");
14815 else if (clock_ctrl == 6)
14816 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070014817 } else {
14818 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000014819 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070014820 strcat(str, "66MHz");
14821 else
14822 strcat(str, "33MHz");
14823 }
Joe Perches63c3a662011-04-26 08:12:10 +000014824 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070014825 strcat(str, ":32-bit");
14826 else
14827 strcat(str, ":64-bit");
14828 return str;
14829}
14830
Michael Chan8c2dc7e2005-12-19 16:26:02 -080014831static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014832{
14833 struct pci_dev *peer;
14834 unsigned int func, devnr = tp->pdev->devfn & ~7;
14835
14836 for (func = 0; func < 8; func++) {
14837 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14838 if (peer && peer != tp->pdev)
14839 break;
14840 pci_dev_put(peer);
14841 }
Michael Chan16fe9d72005-12-13 21:09:54 -080014842 /* 5704 can be configured in single-port mode, set peer to
14843 * tp->pdev in that case.
14844 */
14845 if (!peer) {
14846 peer = tp->pdev;
14847 return peer;
14848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014849
14850 /*
14851 * We don't need to keep the refcount elevated; there's no way
14852 * to remove one half of this device without removing the other
14853 */
14854 pci_dev_put(peer);
14855
14856 return peer;
14857}
14858
David S. Miller15f98502005-05-18 22:49:26 -070014859static void __devinit tg3_init_coal(struct tg3 *tp)
14860{
14861 struct ethtool_coalesce *ec = &tp->coal;
14862
14863 memset(ec, 0, sizeof(*ec));
14864 ec->cmd = ETHTOOL_GCOALESCE;
14865 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
14866 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
14867 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
14868 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
14869 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
14870 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
14871 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
14872 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
14873 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
14874
14875 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
14876 HOSTCC_MODE_CLRTICK_TXBD)) {
14877 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
14878 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
14879 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
14880 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
14881 }
Michael Chand244c892005-07-05 14:42:33 -070014882
Joe Perches63c3a662011-04-26 08:12:10 +000014883 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070014884 ec->rx_coalesce_usecs_irq = 0;
14885 ec->tx_coalesce_usecs_irq = 0;
14886 ec->stats_block_coalesce_usecs = 0;
14887 }
David S. Miller15f98502005-05-18 22:49:26 -070014888}
14889
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014890static const struct net_device_ops tg3_netdev_ops = {
14891 .ndo_open = tg3_open,
14892 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080014893 .ndo_start_xmit = tg3_start_xmit,
Eric Dumazet511d2222010-07-07 20:44:24 +000014894 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -080014895 .ndo_validate_addr = eth_validate_addr,
14896 .ndo_set_multicast_list = tg3_set_rx_mode,
14897 .ndo_set_mac_address = tg3_set_mac_addr,
14898 .ndo_do_ioctl = tg3_ioctl,
14899 .ndo_tx_timeout = tg3_tx_timeout,
14900 .ndo_change_mtu = tg3_change_mtu,
Michał Mirosławdc668912011-04-07 03:35:07 +000014901 .ndo_fix_features = tg3_fix_features,
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000014902 .ndo_set_features = tg3_set_features,
Stephen Hemminger00829822008-11-20 20:14:53 -080014903#ifdef CONFIG_NET_POLL_CONTROLLER
14904 .ndo_poll_controller = tg3_poll_controller,
14905#endif
14906};
14907
Linus Torvalds1da177e2005-04-16 15:20:36 -070014908static int __devinit tg3_init_one(struct pci_dev *pdev,
14909 const struct pci_device_id *ent)
14910{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014911 struct net_device *dev;
14912 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000014913 int i, err, pm_cap;
14914 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070014915 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080014916 u64 dma_mask, persist_dma_mask;
Matt Carlson0da06062011-05-19 12:12:53 +000014917 u32 features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014918
Joe Perches05dbe002010-02-17 19:44:19 +000014919 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014920
14921 err = pci_enable_device(pdev);
14922 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014923 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014924 return err;
14925 }
14926
Linus Torvalds1da177e2005-04-16 15:20:36 -070014927 err = pci_request_regions(pdev, DRV_MODULE_NAME);
14928 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014929 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014930 goto err_out_disable_pdev;
14931 }
14932
14933 pci_set_master(pdev);
14934
14935 /* Find power-management capability. */
14936 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
14937 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000014938 dev_err(&pdev->dev,
14939 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014940 err = -EIO;
14941 goto err_out_free_res;
14942 }
14943
Matt Carlsonfe5f5782009-09-01 13:09:39 +000014944 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014945 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000014946 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014947 err = -ENOMEM;
14948 goto err_out_free_res;
14949 }
14950
Linus Torvalds1da177e2005-04-16 15:20:36 -070014951 SET_NETDEV_DEV(dev, &pdev->dev);
14952
Linus Torvalds1da177e2005-04-16 15:20:36 -070014953 tp = netdev_priv(dev);
14954 tp->pdev = pdev;
14955 tp->dev = dev;
14956 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014957 tp->rx_mode = TG3_DEF_RX_MODE;
14958 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070014959
Linus Torvalds1da177e2005-04-16 15:20:36 -070014960 if (tg3_debug > 0)
14961 tp->msg_enable = tg3_debug;
14962 else
14963 tp->msg_enable = TG3_DEF_MSG_ENABLE;
14964
14965 /* The word/byte swap controls here control register access byte
14966 * swapping. DMA data byte swapping is controlled in the GRC_MODE
14967 * setting below.
14968 */
14969 tp->misc_host_ctrl =
14970 MISC_HOST_CTRL_MASK_PCI_INT |
14971 MISC_HOST_CTRL_WORD_SWAP |
14972 MISC_HOST_CTRL_INDIR_ACCESS |
14973 MISC_HOST_CTRL_PCISTATE_RW;
14974
14975 /* The NONFRM (non-frame) byte/word swap controls take effect
14976 * on descriptor entries, anything which isn't packet data.
14977 *
14978 * The StrongARM chips on the board (one for tx, one for rx)
14979 * are running in big-endian mode.
14980 */
14981 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
14982 GRC_MODE_WSWAP_NONFRM_DATA);
14983#ifdef __BIG_ENDIAN
14984 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
14985#endif
14986 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014987 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000014988 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014989
Matt Carlsond5fe4882008-11-21 17:20:32 -080014990 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010014991 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014992 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014993 err = -ENOMEM;
14994 goto err_out_free_dev;
14995 }
14996
Linus Torvalds1da177e2005-04-16 15:20:36 -070014997 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
14998 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014999
Linus Torvalds1da177e2005-04-16 15:20:36 -070015000 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015001 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000015002 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015003 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015004
15005 err = tg3_get_invariants(tp);
15006 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015007 dev_err(&pdev->dev,
15008 "Problem fetching invariants of chip, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015009 goto err_out_iounmap;
15010 }
15011
Michael Chan4a29cc22006-03-19 13:21:12 -080015012 /* The EPB bridge inside 5714, 5715, and 5780 and any
15013 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080015014 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
15015 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
15016 * do DMA address check in tg3_start_xmit().
15017 */
Joe Perches63c3a662011-04-26 08:12:10 +000015018 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070015019 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000015020 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070015021 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080015022#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070015023 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015024#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080015025 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070015026 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015027
15028 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070015029 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080015030 err = pci_set_dma_mask(pdev, dma_mask);
15031 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000015032 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080015033 err = pci_set_consistent_dma_mask(pdev,
15034 persist_dma_mask);
15035 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015036 dev_err(&pdev->dev, "Unable to obtain 64 bit "
15037 "DMA for consistent allocations\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080015038 goto err_out_iounmap;
15039 }
15040 }
15041 }
Yang Hongyang284901a2009-04-06 19:01:15 -070015042 if (err || dma_mask == DMA_BIT_MASK(32)) {
15043 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080015044 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015045 dev_err(&pdev->dev,
15046 "No usable DMA configuration, aborting\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080015047 goto err_out_iounmap;
15048 }
15049 }
15050
Michael Chanfdfec1722005-07-25 12:31:48 -070015051 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015052
Matt Carlson0da06062011-05-19 12:12:53 +000015053 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
15054
15055 /* 5700 B0 chips do not support checksumming correctly due
15056 * to hardware bugs.
15057 */
15058 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
15059 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
15060
15061 if (tg3_flag(tp, 5755_PLUS))
15062 features |= NETIF_F_IPV6_CSUM;
15063 }
15064
Michael Chan4e3a7aa2006-03-20 17:47:44 -080015065 /* TSO is on by default on chips that support hardware TSO.
15066 * Firmware TSO on older chips gives lower performance, so it
15067 * is off by default, but can be enabled using ethtool.
15068 */
Joe Perches63c3a662011-04-26 08:12:10 +000015069 if ((tg3_flag(tp, HW_TSO_1) ||
15070 tg3_flag(tp, HW_TSO_2) ||
15071 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000015072 (features & NETIF_F_IP_CSUM))
15073 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000015074 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000015075 if (features & NETIF_F_IPV6_CSUM)
15076 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000015077 if (tg3_flag(tp, HW_TSO_3) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000015078 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070015079 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15080 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Joe Perches63c3a662011-04-26 08:12:10 +000015081 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000015082 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000015083 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070015084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015085
Matt Carlsond542fe22011-05-19 16:02:43 +000015086 dev->features |= features;
15087 dev->vlan_features |= features;
15088
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015089 /*
15090 * Add loopback capability only for a subset of devices that support
15091 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
15092 * loopback for the remaining devices.
15093 */
15094 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
15095 !tg3_flag(tp, CPMU_PRESENT))
15096 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000015097 features |= NETIF_F_LOOPBACK;
15098
Matt Carlson0da06062011-05-19 12:12:53 +000015099 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015100
Linus Torvalds1da177e2005-04-16 15:20:36 -070015101 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015102 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015103 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015104 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015105 tp->rx_pending = 63;
15106 }
15107
Linus Torvalds1da177e2005-04-16 15:20:36 -070015108 err = tg3_get_device_address(tp);
15109 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015110 dev_err(&pdev->dev,
15111 "Could not obtain valid ethernet address, aborting\n");
Matt Carlson026a6c22009-12-03 08:36:24 +000015112 goto err_out_iounmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015113 }
15114
Joe Perches63c3a662011-04-26 08:12:10 +000015115 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson63532392008-11-03 16:49:57 -080015116 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
Al Viro79ea13c2008-01-24 02:06:46 -080015117 if (!tp->aperegs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015118 dev_err(&pdev->dev,
15119 "Cannot map APE registers, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015120 err = -ENOMEM;
Matt Carlson026a6c22009-12-03 08:36:24 +000015121 goto err_out_iounmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015122 }
15123
15124 tg3_ape_lock_init(tp);
Matt Carlson7fd76442009-02-25 14:27:20 +000015125
Joe Perches63c3a662011-04-26 08:12:10 +000015126 if (tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000015127 tg3_read_dash_ver(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015128 }
15129
Matt Carlsonc88864d2007-11-12 21:07:01 -080015130 /*
15131 * Reset chip in case UNDI or EFI driver did not shutdown
15132 * DMA self test will enable WDMAC and we'll see (spurious)
15133 * pending DMA on the PCI bus at that point.
15134 */
15135 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15136 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15137 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15138 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15139 }
15140
15141 err = tg3_test_dma(tp);
15142 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015143 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080015144 goto err_out_apeunmap;
15145 }
15146
Matt Carlson78f90dc2009-11-13 13:03:42 +000015147 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15148 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15149 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000015150 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000015151 struct tg3_napi *tnapi = &tp->napi[i];
15152
15153 tnapi->tp = tp;
15154 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15155
15156 tnapi->int_mbox = intmbx;
15157 if (i < 4)
15158 intmbx += 0x8;
15159 else
15160 intmbx += 0x4;
15161
15162 tnapi->consmbox = rcvmbx;
15163 tnapi->prodmbox = sndmbx;
15164
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015165 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015166 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015167 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000015168 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000015169
Joe Perches63c3a662011-04-26 08:12:10 +000015170 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000015171 break;
15172
15173 /*
15174 * If we support MSIX, we'll be using RSS. If we're using
15175 * RSS, the first vector only handles link interrupts and the
15176 * remaining vectors handle rx and tx interrupts. Reuse the
15177 * mailbox values for the next iteration. The values we setup
15178 * above are still useful for the single vectored mode.
15179 */
15180 if (!i)
15181 continue;
15182
15183 rcvmbx += 0x8;
15184
15185 if (sndmbx & 0x4)
15186 sndmbx -= 0x4;
15187 else
15188 sndmbx += 0xc;
15189 }
15190
Matt Carlsonc88864d2007-11-12 21:07:01 -080015191 tg3_init_coal(tp);
15192
Michael Chanc49a1562006-12-17 17:07:29 -080015193 pci_set_drvdata(pdev, dev);
15194
Linus Torvalds1da177e2005-04-16 15:20:36 -070015195 err = register_netdev(dev);
15196 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015197 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015198 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015199 }
15200
Joe Perches05dbe002010-02-17 19:44:19 +000015201 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15202 tp->board_part_number,
15203 tp->pci_chip_rev_id,
15204 tg3_bus_string(tp, str),
15205 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015206
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015207 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000015208 struct phy_device *phydev;
15209 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000015210 netdev_info(dev,
15211 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015212 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015213 } else {
15214 char *ethtype;
15215
15216 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15217 ethtype = "10/100Base-TX";
15218 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15219 ethtype = "1000Base-SX";
15220 else
15221 ethtype = "10/100/1000Base-T";
15222
Matt Carlson5129c3a2010-04-05 10:19:23 +000015223 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000015224 "(WireSpeed[%d], EEE[%d])\n",
15225 tg3_phy_string(tp), ethtype,
15226 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
15227 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015228 }
Matt Carlsondf59c942008-11-03 16:52:56 -080015229
Joe Perches05dbe002010-02-17 19:44:19 +000015230 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000015231 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015232 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015233 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015234 tg3_flag(tp, ENABLE_ASF) != 0,
15235 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000015236 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15237 tp->dma_rwctrl,
15238 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15239 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015240
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015241 pci_save_state(pdev);
15242
Linus Torvalds1da177e2005-04-16 15:20:36 -070015243 return 0;
15244
Matt Carlson0d3031d2007-10-10 18:02:43 -070015245err_out_apeunmap:
15246 if (tp->aperegs) {
15247 iounmap(tp->aperegs);
15248 tp->aperegs = NULL;
15249 }
15250
Linus Torvalds1da177e2005-04-16 15:20:36 -070015251err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070015252 if (tp->regs) {
15253 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015254 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015256
15257err_out_free_dev:
15258 free_netdev(dev);
15259
15260err_out_free_res:
15261 pci_release_regions(pdev);
15262
15263err_out_disable_pdev:
15264 pci_disable_device(pdev);
15265 pci_set_drvdata(pdev, NULL);
15266 return err;
15267}
15268
15269static void __devexit tg3_remove_one(struct pci_dev *pdev)
15270{
15271 struct net_device *dev = pci_get_drvdata(pdev);
15272
15273 if (dev) {
15274 struct tg3 *tp = netdev_priv(dev);
15275
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015276 if (tp->fw)
15277 release_firmware(tp->fw);
15278
Tejun Heo23f333a2010-12-12 16:45:14 +010015279 cancel_work_sync(&tp->reset_task);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015280
Joe Perches63c3a662011-04-26 08:12:10 +000015281 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015282 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015283 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015284 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015285
Linus Torvalds1da177e2005-04-16 15:20:36 -070015286 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015287 if (tp->aperegs) {
15288 iounmap(tp->aperegs);
15289 tp->aperegs = NULL;
15290 }
Michael Chan68929142005-08-09 20:17:14 -070015291 if (tp->regs) {
15292 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015293 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015295 free_netdev(dev);
15296 pci_release_regions(pdev);
15297 pci_disable_device(pdev);
15298 pci_set_drvdata(pdev, NULL);
15299 }
15300}
15301
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015302#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015303static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015304{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015305 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015306 struct net_device *dev = pci_get_drvdata(pdev);
15307 struct tg3 *tp = netdev_priv(dev);
15308 int err;
15309
15310 if (!netif_running(dev))
15311 return 0;
15312
Tejun Heo23f333a2010-12-12 16:45:14 +010015313 flush_work_sync(&tp->reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015314 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015315 tg3_netif_stop(tp);
15316
15317 del_timer_sync(&tp->timer);
15318
David S. Millerf47c11e2005-06-24 20:18:35 -070015319 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015320 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015321 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015322
15323 netif_device_detach(dev);
15324
David S. Millerf47c11e2005-06-24 20:18:35 -070015325 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015326 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000015327 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070015328 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015329
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015330 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015331 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015332 int err2;
15333
David S. Millerf47c11e2005-06-24 20:18:35 -070015334 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015335
Joe Perches63c3a662011-04-26 08:12:10 +000015336 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015337 err2 = tg3_restart_hw(tp, 1);
15338 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015339 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015340
15341 tp->timer.expires = jiffies + tp->timer_offset;
15342 add_timer(&tp->timer);
15343
15344 netif_device_attach(dev);
15345 tg3_netif_start(tp);
15346
Michael Chanb9ec6c12006-07-25 16:37:27 -070015347out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015348 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015349
15350 if (!err2)
15351 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015352 }
15353
15354 return err;
15355}
15356
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015357static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015358{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015359 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015360 struct net_device *dev = pci_get_drvdata(pdev);
15361 struct tg3 *tp = netdev_priv(dev);
15362 int err;
15363
15364 if (!netif_running(dev))
15365 return 0;
15366
Linus Torvalds1da177e2005-04-16 15:20:36 -070015367 netif_device_attach(dev);
15368
David S. Millerf47c11e2005-06-24 20:18:35 -070015369 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015370
Joe Perches63c3a662011-04-26 08:12:10 +000015371 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070015372 err = tg3_restart_hw(tp, 1);
15373 if (err)
15374 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015375
15376 tp->timer.expires = jiffies + tp->timer_offset;
15377 add_timer(&tp->timer);
15378
Linus Torvalds1da177e2005-04-16 15:20:36 -070015379 tg3_netif_start(tp);
15380
Michael Chanb9ec6c12006-07-25 16:37:27 -070015381out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015382 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015383
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015384 if (!err)
15385 tg3_phy_start(tp);
15386
Michael Chanb9ec6c12006-07-25 16:37:27 -070015387 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015388}
15389
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015390static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015391#define TG3_PM_OPS (&tg3_pm_ops)
15392
15393#else
15394
15395#define TG3_PM_OPS NULL
15396
15397#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015398
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015399/**
15400 * tg3_io_error_detected - called when PCI error is detected
15401 * @pdev: Pointer to PCI device
15402 * @state: The current pci connection state
15403 *
15404 * This function is called after a PCI bus error affecting
15405 * this device has been detected.
15406 */
15407static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
15408 pci_channel_state_t state)
15409{
15410 struct net_device *netdev = pci_get_drvdata(pdev);
15411 struct tg3 *tp = netdev_priv(netdev);
15412 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
15413
15414 netdev_info(netdev, "PCI I/O error detected\n");
15415
15416 rtnl_lock();
15417
15418 if (!netif_running(netdev))
15419 goto done;
15420
15421 tg3_phy_stop(tp);
15422
15423 tg3_netif_stop(tp);
15424
15425 del_timer_sync(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +000015426 tg3_flag_clear(tp, RESTART_TIMER);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015427
15428 /* Want to make sure that the reset task doesn't run */
15429 cancel_work_sync(&tp->reset_task);
Joe Perches63c3a662011-04-26 08:12:10 +000015430 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
15431 tg3_flag_clear(tp, RESTART_TIMER);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015432
15433 netif_device_detach(netdev);
15434
15435 /* Clean up software state, even if MMIO is blocked */
15436 tg3_full_lock(tp, 0);
15437 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
15438 tg3_full_unlock(tp);
15439
15440done:
15441 if (state == pci_channel_io_perm_failure)
15442 err = PCI_ERS_RESULT_DISCONNECT;
15443 else
15444 pci_disable_device(pdev);
15445
15446 rtnl_unlock();
15447
15448 return err;
15449}
15450
15451/**
15452 * tg3_io_slot_reset - called after the pci bus has been reset.
15453 * @pdev: Pointer to PCI device
15454 *
15455 * Restart the card from scratch, as if from a cold-boot.
15456 * At this point, the card has exprienced a hard reset,
15457 * followed by fixups by BIOS, and has its config space
15458 * set up identically to what it was at cold boot.
15459 */
15460static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
15461{
15462 struct net_device *netdev = pci_get_drvdata(pdev);
15463 struct tg3 *tp = netdev_priv(netdev);
15464 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
15465 int err;
15466
15467 rtnl_lock();
15468
15469 if (pci_enable_device(pdev)) {
15470 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
15471 goto done;
15472 }
15473
15474 pci_set_master(pdev);
15475 pci_restore_state(pdev);
15476 pci_save_state(pdev);
15477
15478 if (!netif_running(netdev)) {
15479 rc = PCI_ERS_RESULT_RECOVERED;
15480 goto done;
15481 }
15482
15483 err = tg3_power_up(tp);
15484 if (err) {
15485 netdev_err(netdev, "Failed to restore register access.\n");
15486 goto done;
15487 }
15488
15489 rc = PCI_ERS_RESULT_RECOVERED;
15490
15491done:
15492 rtnl_unlock();
15493
15494 return rc;
15495}
15496
15497/**
15498 * tg3_io_resume - called when traffic can start flowing again.
15499 * @pdev: Pointer to PCI device
15500 *
15501 * This callback is called when the error recovery driver tells
15502 * us that its OK to resume normal operation.
15503 */
15504static void tg3_io_resume(struct pci_dev *pdev)
15505{
15506 struct net_device *netdev = pci_get_drvdata(pdev);
15507 struct tg3 *tp = netdev_priv(netdev);
15508 int err;
15509
15510 rtnl_lock();
15511
15512 if (!netif_running(netdev))
15513 goto done;
15514
15515 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000015516 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015517 err = tg3_restart_hw(tp, 1);
15518 tg3_full_unlock(tp);
15519 if (err) {
15520 netdev_err(netdev, "Cannot restart hardware after reset.\n");
15521 goto done;
15522 }
15523
15524 netif_device_attach(netdev);
15525
15526 tp->timer.expires = jiffies + tp->timer_offset;
15527 add_timer(&tp->timer);
15528
15529 tg3_netif_start(tp);
15530
15531 tg3_phy_start(tp);
15532
15533done:
15534 rtnl_unlock();
15535}
15536
15537static struct pci_error_handlers tg3_err_handler = {
15538 .error_detected = tg3_io_error_detected,
15539 .slot_reset = tg3_io_slot_reset,
15540 .resume = tg3_io_resume
15541};
15542
Linus Torvalds1da177e2005-04-16 15:20:36 -070015543static struct pci_driver tg3_driver = {
15544 .name = DRV_MODULE_NAME,
15545 .id_table = tg3_pci_tbl,
15546 .probe = tg3_init_one,
15547 .remove = __devexit_p(tg3_remove_one),
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015548 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015549 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015550};
15551
15552static int __init tg3_init(void)
15553{
Jeff Garzik29917622006-08-19 17:48:59 -040015554 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015555}
15556
15557static void __exit tg3_cleanup(void)
15558{
15559 pci_unregister_driver(&tg3_driver);
15560}
15561
15562module_init(tg3_init);
15563module_exit(tg3_cleanup);