blob: 7c7c9a897c09477835bb849452941ee14ec5e29a [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 Carlson64cad2a2011-04-25 12:42:50 +000091#define TG3_MIN_NUM 118
Matt Carlson6867c842010-07-11 09:31:44 +000092#define DRV_MODULE_VERSION \
93 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Matt Carlson64cad2a2011-04-25 12:42:50 +000094#define DRV_MODULE_RELDATE "April 22, 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)},
295 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296};
297
298MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
299
Andreas Mohr50da8592006-08-14 23:54:30 -0700300static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000302} ethtool_stats_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 { "rx_octets" },
304 { "rx_fragments" },
305 { "rx_ucast_packets" },
306 { "rx_mcast_packets" },
307 { "rx_bcast_packets" },
308 { "rx_fcs_errors" },
309 { "rx_align_errors" },
310 { "rx_xon_pause_rcvd" },
311 { "rx_xoff_pause_rcvd" },
312 { "rx_mac_ctrl_rcvd" },
313 { "rx_xoff_entered" },
314 { "rx_frame_too_long_errors" },
315 { "rx_jabbers" },
316 { "rx_undersize_packets" },
317 { "rx_in_length_errors" },
318 { "rx_out_length_errors" },
319 { "rx_64_or_less_octet_packets" },
320 { "rx_65_to_127_octet_packets" },
321 { "rx_128_to_255_octet_packets" },
322 { "rx_256_to_511_octet_packets" },
323 { "rx_512_to_1023_octet_packets" },
324 { "rx_1024_to_1522_octet_packets" },
325 { "rx_1523_to_2047_octet_packets" },
326 { "rx_2048_to_4095_octet_packets" },
327 { "rx_4096_to_8191_octet_packets" },
328 { "rx_8192_to_9022_octet_packets" },
329
330 { "tx_octets" },
331 { "tx_collisions" },
332
333 { "tx_xon_sent" },
334 { "tx_xoff_sent" },
335 { "tx_flow_control" },
336 { "tx_mac_errors" },
337 { "tx_single_collisions" },
338 { "tx_mult_collisions" },
339 { "tx_deferred" },
340 { "tx_excessive_collisions" },
341 { "tx_late_collisions" },
342 { "tx_collide_2times" },
343 { "tx_collide_3times" },
344 { "tx_collide_4times" },
345 { "tx_collide_5times" },
346 { "tx_collide_6times" },
347 { "tx_collide_7times" },
348 { "tx_collide_8times" },
349 { "tx_collide_9times" },
350 { "tx_collide_10times" },
351 { "tx_collide_11times" },
352 { "tx_collide_12times" },
353 { "tx_collide_13times" },
354 { "tx_collide_14times" },
355 { "tx_collide_15times" },
356 { "tx_ucast_packets" },
357 { "tx_mcast_packets" },
358 { "tx_bcast_packets" },
359 { "tx_carrier_sense_errors" },
360 { "tx_discards" },
361 { "tx_errors" },
362
363 { "dma_writeq_full" },
364 { "dma_write_prioq_full" },
365 { "rxbds_empty" },
366 { "rx_discards" },
Matt Carlson4d958472011-04-20 07:57:35 +0000367 { "mbuf_lwm_thresh_hit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 { "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" },
379 { "nic_tx_threshold_hit" }
380};
381
Matt Carlson48fa55a2011-04-13 11:05:06 +0000382#define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
383
384
Andreas Mohr50da8592006-08-14 23:54:30 -0700385static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700386 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000387} ethtool_test_keys[] = {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700388 { "nvram test (online) " },
389 { "link test (online) " },
390 { "register test (offline)" },
391 { "memory test (offline)" },
392 { "loopback test (offline)" },
393 { "interrupt test (offline)" },
394};
395
Matt Carlson48fa55a2011-04-13 11:05:06 +0000396#define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
397
398
Michael Chanb401e9e2005-12-19 16:27:04 -0800399static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
400{
401 writel(val, tp->regs + off);
402}
403
404static u32 tg3_read32(struct tg3 *tp, u32 off)
405{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000406 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800407}
408
Matt Carlson0d3031d2007-10-10 18:02:43 -0700409static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
410{
411 writel(val, tp->aperegs + off);
412}
413
414static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
415{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000416 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700417}
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
420{
Michael Chan68929142005-08-09 20:17:14 -0700421 unsigned long flags;
422
423 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700424 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
425 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700426 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700427}
428
429static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
430{
431 writel(val, tp->regs + off);
432 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Michael Chan68929142005-08-09 20:17:14 -0700435static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
436{
437 unsigned long flags;
438 u32 val;
439
440 spin_lock_irqsave(&tp->indirect_lock, flags);
441 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
442 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
443 spin_unlock_irqrestore(&tp->indirect_lock, flags);
444 return val;
445}
446
447static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
448{
449 unsigned long flags;
450
451 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
452 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
453 TG3_64BIT_REG_LOW, val);
454 return;
455 }
Matt Carlson66711e62009-11-13 13:03:49 +0000456 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700457 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
458 TG3_64BIT_REG_LOW, val);
459 return;
460 }
461
462 spin_lock_irqsave(&tp->indirect_lock, flags);
463 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
464 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
465 spin_unlock_irqrestore(&tp->indirect_lock, flags);
466
467 /* In indirect mode when disabling interrupts, we also need
468 * to clear the interrupt bit in the GRC local ctrl register.
469 */
470 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
471 (val == 0x1)) {
472 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
473 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
474 }
475}
476
477static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
478{
479 unsigned long flags;
480 u32 val;
481
482 spin_lock_irqsave(&tp->indirect_lock, flags);
483 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
484 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
485 spin_unlock_irqrestore(&tp->indirect_lock, flags);
486 return val;
487}
488
Michael Chanb401e9e2005-12-19 16:27:04 -0800489/* usec_wait specifies the wait time in usec when writing to certain registers
490 * where it is unsafe to read back the register without some delay.
491 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
492 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
493 */
494static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
Joe Perches63c3a662011-04-26 08:12:10 +0000496 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800497 /* Non-posted methods */
498 tp->write32(tp, off, val);
499 else {
500 /* Posted method */
501 tg3_write32(tp, off, val);
502 if (usec_wait)
503 udelay(usec_wait);
504 tp->read32(tp, off);
505 }
506 /* Wait again after the read for the posted method to guarantee that
507 * the wait time is met.
508 */
509 if (usec_wait)
510 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
Michael Chan09ee9292005-08-09 20:17:00 -0700513static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
514{
515 tp->write32_mbox(tp, off, val);
Joe Perches63c3a662011-04-26 08:12:10 +0000516 if (!tg3_flag(tp, MBOX_WRITE_REORDER) && !tg3_flag(tp, ICH_WORKAROUND))
Michael Chan68929142005-08-09 20:17:14 -0700517 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700518}
519
Michael Chan20094932005-08-09 20:16:32 -0700520static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
522 void __iomem *mbox = tp->regs + off;
523 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000524 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000526 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 readl(mbox);
528}
529
Michael Chanb5d37722006-09-27 16:06:21 -0700530static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
531{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000532 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700533}
534
535static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
536{
537 writel(val, tp->regs + off + GRCMBOX_BASE);
538}
539
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000540#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700541#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000542#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
543#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
544#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700545
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000546#define tw32(reg, val) tp->write32(tp, reg, val)
547#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
548#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
549#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
552{
Michael Chan68929142005-08-09 20:17:14 -0700553 unsigned long flags;
554
Michael Chanb5d37722006-09-27 16:06:21 -0700555 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
556 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
557 return;
558
Michael Chan68929142005-08-09 20:17:14 -0700559 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000560 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700561 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
562 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Michael Chanbbadf502006-04-06 21:46:34 -0700564 /* Always leave this as zero. */
565 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
566 } else {
567 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
568 tw32_f(TG3PCI_MEM_WIN_DATA, val);
569
570 /* Always leave this as zero. */
571 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
572 }
Michael Chan68929142005-08-09 20:17:14 -0700573 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
576static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
577{
Michael Chan68929142005-08-09 20:17:14 -0700578 unsigned long flags;
579
Michael Chanb5d37722006-09-27 16:06:21 -0700580 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
581 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
582 *val = 0;
583 return;
584 }
585
Michael Chan68929142005-08-09 20:17:14 -0700586 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000587 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700588 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
589 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Michael Chanbbadf502006-04-06 21:46:34 -0700591 /* Always leave this as zero. */
592 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
593 } else {
594 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
595 *val = tr32(TG3PCI_MEM_WIN_DATA);
596
597 /* Always leave this as zero. */
598 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
599 }
Michael Chan68929142005-08-09 20:17:14 -0700600 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
Matt Carlson0d3031d2007-10-10 18:02:43 -0700603static void tg3_ape_lock_init(struct tg3 *tp)
604{
605 int i;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000606 u32 regbase;
607
608 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
609 regbase = TG3_APE_LOCK_GRANT;
610 else
611 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700612
613 /* Make sure the driver hasn't any stale locks. */
614 for (i = 0; i < 8; i++)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000615 tg3_ape_write32(tp, regbase + 4 * i, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700616}
617
618static int tg3_ape_lock(struct tg3 *tp, int locknum)
619{
620 int i, off;
621 int ret = 0;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000622 u32 status, req, gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700623
Joe Perches63c3a662011-04-26 08:12:10 +0000624 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700625 return 0;
626
627 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000628 case TG3_APE_LOCK_GRC:
629 case TG3_APE_LOCK_MEM:
630 break;
631 default:
632 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700633 }
634
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000635 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
636 req = TG3_APE_LOCK_REQ;
637 gnt = TG3_APE_LOCK_GRANT;
638 } else {
639 req = TG3_APE_PER_LOCK_REQ;
640 gnt = TG3_APE_PER_LOCK_GRANT;
641 }
642
Matt Carlson0d3031d2007-10-10 18:02:43 -0700643 off = 4 * locknum;
644
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000645 tg3_ape_write32(tp, req + off, APE_LOCK_REQ_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700646
647 /* Wait for up to 1 millisecond to acquire lock. */
648 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000649 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700650 if (status == APE_LOCK_GRANT_DRIVER)
651 break;
652 udelay(10);
653 }
654
655 if (status != APE_LOCK_GRANT_DRIVER) {
656 /* Revoke the lock request. */
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000657 tg3_ape_write32(tp, gnt + off,
Matt Carlson0d3031d2007-10-10 18:02:43 -0700658 APE_LOCK_GRANT_DRIVER);
659
660 ret = -EBUSY;
661 }
662
663 return ret;
664}
665
666static void tg3_ape_unlock(struct tg3 *tp, int locknum)
667{
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000668 u32 gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700669
Joe Perches63c3a662011-04-26 08:12:10 +0000670 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700671 return;
672
673 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000674 case TG3_APE_LOCK_GRC:
675 case TG3_APE_LOCK_MEM:
676 break;
677 default:
678 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700679 }
680
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000681 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
682 gnt = TG3_APE_LOCK_GRANT;
683 else
684 gnt = TG3_APE_PER_LOCK_GRANT;
685
686 tg3_ape_write32(tp, gnt + 4 * locknum, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700687}
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689static void tg3_disable_ints(struct tg3 *tp)
690{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000691 int i;
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 tw32(TG3PCI_MISC_HOST_CTRL,
694 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000695 for (i = 0; i < tp->irq_max; i++)
696 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699static void tg3_enable_ints(struct tg3 *tp)
700{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000701 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000702
Michael Chanbbe832c2005-06-24 20:20:04 -0700703 tp->irq_sync = 0;
704 wmb();
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 tw32(TG3PCI_MISC_HOST_CTRL,
707 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000708
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000709 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000710 for (i = 0; i < tp->irq_cnt; i++) {
711 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000712
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000713 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +0000714 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000715 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
716
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000717 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000718 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000719
720 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +0000721 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000722 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
723 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
724 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000725 tw32(HOSTCC_MODE, tp->coal_now);
726
727 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
Matt Carlson17375d22009-08-28 14:02:18 +0000730static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700731{
Matt Carlson17375d22009-08-28 14:02:18 +0000732 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000733 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700734 unsigned int work_exists = 0;
735
736 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +0000737 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -0700738 if (sblk->status & SD_STATUS_LINK_CHG)
739 work_exists = 1;
740 }
741 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000742 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000743 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700744 work_exists = 1;
745
746 return work_exists;
747}
748
Matt Carlson17375d22009-08-28 14:02:18 +0000749/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700750 * similar to tg3_enable_ints, but it accurately determines whether there
751 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400752 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 */
Matt Carlson17375d22009-08-28 14:02:18 +0000754static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
Matt Carlson17375d22009-08-28 14:02:18 +0000756 struct tg3 *tp = tnapi->tp;
757
Matt Carlson898a56f2009-08-28 14:02:40 +0000758 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 mmiowb();
760
David S. Millerfac9b832005-05-18 22:46:34 -0700761 /* When doing tagged status, this work check is unnecessary.
762 * The last_tag we write above tells the chip which piece of
763 * work we've completed.
764 */
Joe Perches63c3a662011-04-26 08:12:10 +0000765 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700766 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000767 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770static void tg3_switch_clocks(struct tg3 *tp)
771{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000772 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 u32 orig_clock_ctrl;
774
Joe Perches63c3a662011-04-26 08:12:10 +0000775 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700776 return;
777
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000778 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 orig_clock_ctrl = clock_ctrl;
781 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
782 CLOCK_CTRL_CLKRUN_OENABLE |
783 0x1f);
784 tp->pci_clock_ctrl = clock_ctrl;
785
Joe Perches63c3a662011-04-26 08:12:10 +0000786 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800788 tw32_wait_f(TG3PCI_CLOCK_CTRL,
789 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
791 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800792 tw32_wait_f(TG3PCI_CLOCK_CTRL,
793 clock_ctrl |
794 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
795 40);
796 tw32_wait_f(TG3PCI_CLOCK_CTRL,
797 clock_ctrl | (CLOCK_CTRL_ALTCLK),
798 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800800 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801}
802
803#define PHY_BUSY_LOOPS 5000
804
805static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
806{
807 u32 frame_val;
808 unsigned int loops;
809 int ret;
810
811 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
812 tw32_f(MAC_MI_MODE,
813 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
814 udelay(80);
815 }
816
817 *val = 0x0;
818
Matt Carlson882e9792009-09-01 13:21:36 +0000819 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 MI_COM_PHY_ADDR_MASK);
821 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
822 MI_COM_REG_ADDR_MASK);
823 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 tw32_f(MAC_MI_COM, frame_val);
826
827 loops = PHY_BUSY_LOOPS;
828 while (loops != 0) {
829 udelay(10);
830 frame_val = tr32(MAC_MI_COM);
831
832 if ((frame_val & MI_COM_BUSY) == 0) {
833 udelay(5);
834 frame_val = tr32(MAC_MI_COM);
835 break;
836 }
837 loops -= 1;
838 }
839
840 ret = -EBUSY;
841 if (loops != 0) {
842 *val = frame_val & MI_COM_DATA_MASK;
843 ret = 0;
844 }
845
846 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
847 tw32_f(MAC_MI_MODE, tp->mi_mode);
848 udelay(80);
849 }
850
851 return ret;
852}
853
854static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
855{
856 u32 frame_val;
857 unsigned int loops;
858 int ret;
859
Matt Carlsonf07e9af2010-08-02 11:26:07 +0000860 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Michael Chanb5d37722006-09-27 16:06:21 -0700861 (reg == MII_TG3_CTRL || reg == MII_TG3_AUX_CTRL))
862 return 0;
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
865 tw32_f(MAC_MI_MODE,
866 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
867 udelay(80);
868 }
869
Matt Carlson882e9792009-09-01 13:21:36 +0000870 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 MI_COM_PHY_ADDR_MASK);
872 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
873 MI_COM_REG_ADDR_MASK);
874 frame_val |= (val & MI_COM_DATA_MASK);
875 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 tw32_f(MAC_MI_COM, frame_val);
878
879 loops = PHY_BUSY_LOOPS;
880 while (loops != 0) {
881 udelay(10);
882 frame_val = tr32(MAC_MI_COM);
883 if ((frame_val & MI_COM_BUSY) == 0) {
884 udelay(5);
885 frame_val = tr32(MAC_MI_COM);
886 break;
887 }
888 loops -= 1;
889 }
890
891 ret = -EBUSY;
892 if (loops != 0)
893 ret = 0;
894
895 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
896 tw32_f(MAC_MI_MODE, tp->mi_mode);
897 udelay(80);
898 }
899
900 return ret;
901}
902
Matt Carlsonb0988c12011-04-20 07:57:39 +0000903static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
904{
905 int err;
906
907 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
908 if (err)
909 goto done;
910
911 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
912 if (err)
913 goto done;
914
915 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
916 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
917 if (err)
918 goto done;
919
920 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
921
922done:
923 return err;
924}
925
926static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
927{
928 int err;
929
930 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
931 if (err)
932 goto done;
933
934 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
935 if (err)
936 goto done;
937
938 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
939 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
940 if (err)
941 goto done;
942
943 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
944
945done:
946 return err;
947}
948
949static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
950{
951 int err;
952
953 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
954 if (!err)
955 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
956
957 return err;
958}
959
960static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
961{
962 int err;
963
964 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
965 if (!err)
966 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
967
968 return err;
969}
970
Matt Carlson15ee95c2011-04-20 07:57:40 +0000971static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
972{
973 int err;
974
975 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
976 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
977 MII_TG3_AUXCTL_SHDWSEL_MISC);
978 if (!err)
979 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
980
981 return err;
982}
983
Matt Carlsonb4bd2922011-04-20 07:57:41 +0000984static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
985{
986 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
987 set |= MII_TG3_AUXCTL_MISC_WREN;
988
989 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
990}
991
Matt Carlson1d36ba42011-04-20 07:57:42 +0000992#define TG3_PHY_AUXCTL_SMDSP_ENABLE(tp) \
993 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
994 MII_TG3_AUXCTL_ACTL_SMDSP_ENA | \
995 MII_TG3_AUXCTL_ACTL_TX_6DB)
996
997#define TG3_PHY_AUXCTL_SMDSP_DISABLE(tp) \
998 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
999 MII_TG3_AUXCTL_ACTL_TX_6DB);
1000
Matt Carlson95e28692008-05-25 23:44:14 -07001001static int tg3_bmcr_reset(struct tg3 *tp)
1002{
1003 u32 phy_control;
1004 int limit, err;
1005
1006 /* OK, reset it, and poll the BMCR_RESET bit until it
1007 * clears or we time out.
1008 */
1009 phy_control = BMCR_RESET;
1010 err = tg3_writephy(tp, MII_BMCR, phy_control);
1011 if (err != 0)
1012 return -EBUSY;
1013
1014 limit = 5000;
1015 while (limit--) {
1016 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1017 if (err != 0)
1018 return -EBUSY;
1019
1020 if ((phy_control & BMCR_RESET) == 0) {
1021 udelay(40);
1022 break;
1023 }
1024 udelay(10);
1025 }
Roel Kluind4675b52009-02-12 16:33:27 -08001026 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001027 return -EBUSY;
1028
1029 return 0;
1030}
1031
Matt Carlson158d7ab2008-05-29 01:37:54 -07001032static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1033{
Francois Romieu3d165432009-01-19 16:56:50 -08001034 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001035 u32 val;
1036
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001037 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001038
1039 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001040 val = -EIO;
1041
1042 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001043
1044 return val;
1045}
1046
1047static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1048{
Francois Romieu3d165432009-01-19 16:56:50 -08001049 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001050 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001051
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001052 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001053
1054 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001055 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001056
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001057 spin_unlock_bh(&tp->lock);
1058
1059 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001060}
1061
1062static int tg3_mdio_reset(struct mii_bus *bp)
1063{
1064 return 0;
1065}
1066
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001067static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001068{
1069 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001070 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001071
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001072 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001073 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001074 case PHY_ID_BCM50610:
1075 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001076 val = MAC_PHYCFG2_50610_LED_MODES;
1077 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001078 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001079 val = MAC_PHYCFG2_AC131_LED_MODES;
1080 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001081 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001082 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1083 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001084 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001085 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1086 break;
1087 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001088 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001089 }
1090
1091 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1092 tw32(MAC_PHYCFG2, val);
1093
1094 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001095 val &= ~(MAC_PHYCFG1_RGMII_INT |
1096 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1097 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001098 tw32(MAC_PHYCFG1, val);
1099
1100 return;
1101 }
1102
Joe Perches63c3a662011-04-26 08:12:10 +00001103 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001104 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1105 MAC_PHYCFG2_FMODE_MASK_MASK |
1106 MAC_PHYCFG2_GMODE_MASK_MASK |
1107 MAC_PHYCFG2_ACT_MASK_MASK |
1108 MAC_PHYCFG2_QUAL_MASK_MASK |
1109 MAC_PHYCFG2_INBAND_ENABLE;
1110
1111 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001112
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001113 val = tr32(MAC_PHYCFG1);
1114 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1115 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001116 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1117 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001118 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001119 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001120 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1121 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001122 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1123 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1124 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001125
Matt Carlsona9daf362008-05-25 23:49:44 -07001126 val = tr32(MAC_EXT_RGMII_MODE);
1127 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1128 MAC_RGMII_MODE_RX_QUALITY |
1129 MAC_RGMII_MODE_RX_ACTIVITY |
1130 MAC_RGMII_MODE_RX_ENG_DET |
1131 MAC_RGMII_MODE_TX_ENABLE |
1132 MAC_RGMII_MODE_TX_LOWPWR |
1133 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001134 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1135 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001136 val |= MAC_RGMII_MODE_RX_INT_B |
1137 MAC_RGMII_MODE_RX_QUALITY |
1138 MAC_RGMII_MODE_RX_ACTIVITY |
1139 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001140 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001141 val |= MAC_RGMII_MODE_TX_ENABLE |
1142 MAC_RGMII_MODE_TX_LOWPWR |
1143 MAC_RGMII_MODE_TX_RESET;
1144 }
1145 tw32(MAC_EXT_RGMII_MODE, val);
1146}
1147
Matt Carlson158d7ab2008-05-29 01:37:54 -07001148static void tg3_mdio_start(struct tg3 *tp)
1149{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001150 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1151 tw32_f(MAC_MI_MODE, tp->mi_mode);
1152 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001153
Joe Perches63c3a662011-04-26 08:12:10 +00001154 if (tg3_flag(tp, MDIOBUS_INITED) &&
Matt Carlson9ea48182010-02-17 15:17:01 +00001155 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1156 tg3_mdio_config_5785(tp);
1157}
1158
1159static int tg3_mdio_init(struct tg3 *tp)
1160{
1161 int i;
1162 u32 reg;
1163 struct phy_device *phydev;
1164
Joe Perches63c3a662011-04-26 08:12:10 +00001165 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001166 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001167
Matt Carlson9c7df912010-06-05 17:24:36 +00001168 tp->phy_addr = PCI_FUNC(tp->pdev->devfn) + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001169
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001170 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1171 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1172 else
1173 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1174 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001175 if (is_serdes)
1176 tp->phy_addr += 7;
1177 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001178 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001179
Matt Carlson158d7ab2008-05-29 01:37:54 -07001180 tg3_mdio_start(tp);
1181
Joe Perches63c3a662011-04-26 08:12:10 +00001182 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001183 return 0;
1184
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001185 tp->mdio_bus = mdiobus_alloc();
1186 if (tp->mdio_bus == NULL)
1187 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001188
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001189 tp->mdio_bus->name = "tg3 mdio bus";
1190 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001191 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001192 tp->mdio_bus->priv = tp;
1193 tp->mdio_bus->parent = &tp->pdev->dev;
1194 tp->mdio_bus->read = &tg3_mdio_read;
1195 tp->mdio_bus->write = &tg3_mdio_write;
1196 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001197 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001198 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001199
1200 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001201 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001202
1203 /* The bus registration will look for all the PHYs on the mdio bus.
1204 * Unfortunately, it does not ensure the PHY is powered up before
1205 * accessing the PHY ID registers. A chip reset is the
1206 * quickest way to bring the device back to an operational state..
1207 */
1208 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1209 tg3_bmcr_reset(tp);
1210
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001211 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001212 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001213 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001214 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001215 return i;
1216 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001217
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001218 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001219
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001220 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001221 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001222 mdiobus_unregister(tp->mdio_bus);
1223 mdiobus_free(tp->mdio_bus);
1224 return -ENODEV;
1225 }
1226
1227 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001228 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001229 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001230 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001231 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001232 case PHY_ID_BCM50610:
1233 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001234 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001235 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001236 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001237 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001238 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001239 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001240 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001241 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001242 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001243 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001244 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001245 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001246 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001247 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001248 case PHY_ID_RTL8201E:
1249 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001250 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001251 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001252 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001253 break;
1254 }
1255
Joe Perches63c3a662011-04-26 08:12:10 +00001256 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001257
1258 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1259 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001260
1261 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001262}
1263
1264static void tg3_mdio_fini(struct tg3 *tp)
1265{
Joe Perches63c3a662011-04-26 08:12:10 +00001266 if (tg3_flag(tp, MDIOBUS_INITED)) {
1267 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001268 mdiobus_unregister(tp->mdio_bus);
1269 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001270 }
1271}
1272
Matt Carlson95e28692008-05-25 23:44:14 -07001273/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001274static inline void tg3_generate_fw_event(struct tg3 *tp)
1275{
1276 u32 val;
1277
1278 val = tr32(GRC_RX_CPU_EVENT);
1279 val |= GRC_RX_CPU_DRIVER_EVENT;
1280 tw32_f(GRC_RX_CPU_EVENT, val);
1281
1282 tp->last_event_jiffies = jiffies;
1283}
1284
1285#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1286
1287/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001288static void tg3_wait_for_event_ack(struct tg3 *tp)
1289{
1290 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001291 unsigned int delay_cnt;
1292 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001293
Matt Carlson4ba526c2008-08-15 14:10:04 -07001294 /* If enough time has passed, no wait is necessary. */
1295 time_remain = (long)(tp->last_event_jiffies + 1 +
1296 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1297 (long)jiffies;
1298 if (time_remain < 0)
1299 return;
1300
1301 /* Check if we can shorten the wait time. */
1302 delay_cnt = jiffies_to_usecs(time_remain);
1303 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1304 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1305 delay_cnt = (delay_cnt >> 3) + 1;
1306
1307 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001308 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1309 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001310 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001311 }
1312}
1313
1314/* tp->lock is held. */
1315static void tg3_ump_link_report(struct tg3 *tp)
1316{
1317 u32 reg;
1318 u32 val;
1319
Joe Perches63c3a662011-04-26 08:12:10 +00001320 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson95e28692008-05-25 23:44:14 -07001321 return;
1322
1323 tg3_wait_for_event_ack(tp);
1324
1325 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1326
1327 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1328
1329 val = 0;
1330 if (!tg3_readphy(tp, MII_BMCR, &reg))
1331 val = reg << 16;
1332 if (!tg3_readphy(tp, MII_BMSR, &reg))
1333 val |= (reg & 0xffff);
1334 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1335
1336 val = 0;
1337 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1338 val = reg << 16;
1339 if (!tg3_readphy(tp, MII_LPA, &reg))
1340 val |= (reg & 0xffff);
1341 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1342
1343 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001344 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001345 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1346 val = reg << 16;
1347 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1348 val |= (reg & 0xffff);
1349 }
1350 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1351
1352 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1353 val = reg << 16;
1354 else
1355 val = 0;
1356 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1357
Matt Carlson4ba526c2008-08-15 14:10:04 -07001358 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001359}
1360
1361static void tg3_link_report(struct tg3 *tp)
1362{
1363 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001364 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001365 tg3_ump_link_report(tp);
1366 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001367 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1368 (tp->link_config.active_speed == SPEED_1000 ?
1369 1000 :
1370 (tp->link_config.active_speed == SPEED_100 ?
1371 100 : 10)),
1372 (tp->link_config.active_duplex == DUPLEX_FULL ?
1373 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001374
Joe Perches05dbe002010-02-17 19:44:19 +00001375 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1376 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1377 "on" : "off",
1378 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1379 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001380
1381 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1382 netdev_info(tp->dev, "EEE is %s\n",
1383 tp->setlpicnt ? "enabled" : "disabled");
1384
Matt Carlson95e28692008-05-25 23:44:14 -07001385 tg3_ump_link_report(tp);
1386 }
1387}
1388
1389static u16 tg3_advert_flowctrl_1000T(u8 flow_ctrl)
1390{
1391 u16 miireg;
1392
Steve Glendinninge18ce342008-12-16 02:00:00 -08001393 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001394 miireg = ADVERTISE_PAUSE_CAP;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001395 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001396 miireg = ADVERTISE_PAUSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001397 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001398 miireg = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1399 else
1400 miireg = 0;
1401
1402 return miireg;
1403}
1404
1405static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1406{
1407 u16 miireg;
1408
Steve Glendinninge18ce342008-12-16 02:00:00 -08001409 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001410 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001411 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001412 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001413 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001414 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1415 else
1416 miireg = 0;
1417
1418 return miireg;
1419}
1420
Matt Carlson95e28692008-05-25 23:44:14 -07001421static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1422{
1423 u8 cap = 0;
1424
1425 if (lcladv & ADVERTISE_1000XPAUSE) {
1426 if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1427 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001428 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001429 else if (rmtadv & LPA_1000XPAUSE_ASYM)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001430 cap = FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001431 } else {
1432 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001433 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001434 }
1435 } else if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1436 if ((rmtadv & LPA_1000XPAUSE) && (rmtadv & LPA_1000XPAUSE_ASYM))
Steve Glendinninge18ce342008-12-16 02:00:00 -08001437 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001438 }
1439
1440 return cap;
1441}
1442
Matt Carlsonf51f3562008-05-25 23:45:08 -07001443static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001444{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001445 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001446 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001447 u32 old_rx_mode = tp->rx_mode;
1448 u32 old_tx_mode = tp->tx_mode;
1449
Joe Perches63c3a662011-04-26 08:12:10 +00001450 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001451 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001452 else
1453 autoneg = tp->link_config.autoneg;
1454
Joe Perches63c3a662011-04-26 08:12:10 +00001455 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001456 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001457 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001458 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001459 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001460 } else
1461 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001462
Matt Carlsonf51f3562008-05-25 23:45:08 -07001463 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001464
Steve Glendinninge18ce342008-12-16 02:00:00 -08001465 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001466 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1467 else
1468 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1469
Matt Carlsonf51f3562008-05-25 23:45:08 -07001470 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001471 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001472
Steve Glendinninge18ce342008-12-16 02:00:00 -08001473 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001474 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1475 else
1476 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1477
Matt Carlsonf51f3562008-05-25 23:45:08 -07001478 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001479 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001480}
1481
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001482static void tg3_adjust_link(struct net_device *dev)
1483{
1484 u8 oldflowctrl, linkmesg = 0;
1485 u32 mac_mode, lcl_adv, rmt_adv;
1486 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001487 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001488
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001489 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001490
1491 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1492 MAC_MODE_HALF_DUPLEX);
1493
1494 oldflowctrl = tp->link_config.active_flowctrl;
1495
1496 if (phydev->link) {
1497 lcl_adv = 0;
1498 rmt_adv = 0;
1499
1500 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1501 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001502 else if (phydev->speed == SPEED_1000 ||
1503 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001504 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001505 else
1506 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001507
1508 if (phydev->duplex == DUPLEX_HALF)
1509 mac_mode |= MAC_MODE_HALF_DUPLEX;
1510 else {
1511 lcl_adv = tg3_advert_flowctrl_1000T(
1512 tp->link_config.flowctrl);
1513
1514 if (phydev->pause)
1515 rmt_adv = LPA_PAUSE_CAP;
1516 if (phydev->asym_pause)
1517 rmt_adv |= LPA_PAUSE_ASYM;
1518 }
1519
1520 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1521 } else
1522 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1523
1524 if (mac_mode != tp->mac_mode) {
1525 tp->mac_mode = mac_mode;
1526 tw32_f(MAC_MODE, tp->mac_mode);
1527 udelay(40);
1528 }
1529
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001530 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1531 if (phydev->speed == SPEED_10)
1532 tw32(MAC_MI_STAT,
1533 MAC_MI_STAT_10MBPS_MODE |
1534 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1535 else
1536 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1537 }
1538
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001539 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1540 tw32(MAC_TX_LENGTHS,
1541 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1542 (6 << TX_LENGTHS_IPG_SHIFT) |
1543 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1544 else
1545 tw32(MAC_TX_LENGTHS,
1546 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1547 (6 << TX_LENGTHS_IPG_SHIFT) |
1548 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1549
1550 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1551 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1552 phydev->speed != tp->link_config.active_speed ||
1553 phydev->duplex != tp->link_config.active_duplex ||
1554 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001555 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001556
1557 tp->link_config.active_speed = phydev->speed;
1558 tp->link_config.active_duplex = phydev->duplex;
1559
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001560 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001561
1562 if (linkmesg)
1563 tg3_link_report(tp);
1564}
1565
1566static int tg3_phy_init(struct tg3 *tp)
1567{
1568 struct phy_device *phydev;
1569
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001570 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001571 return 0;
1572
1573 /* Bring the PHY back to a known state. */
1574 tg3_bmcr_reset(tp);
1575
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001576 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001577
1578 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad352008-11-10 13:55:14 -08001579 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001580 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001581 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001582 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001583 return PTR_ERR(phydev);
1584 }
1585
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001586 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001587 switch (phydev->interface) {
1588 case PHY_INTERFACE_MODE_GMII:
1589 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001590 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08001591 phydev->supported &= (PHY_GBIT_FEATURES |
1592 SUPPORTED_Pause |
1593 SUPPORTED_Asym_Pause);
1594 break;
1595 }
1596 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001597 case PHY_INTERFACE_MODE_MII:
1598 phydev->supported &= (PHY_BASIC_FEATURES |
1599 SUPPORTED_Pause |
1600 SUPPORTED_Asym_Pause);
1601 break;
1602 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001603 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001604 return -EINVAL;
1605 }
1606
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001607 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001608
1609 phydev->advertising = phydev->supported;
1610
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001611 return 0;
1612}
1613
1614static void tg3_phy_start(struct tg3 *tp)
1615{
1616 struct phy_device *phydev;
1617
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001618 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001619 return;
1620
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001621 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001622
Matt Carlson80096062010-08-02 11:26:06 +00001623 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
1624 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001625 phydev->speed = tp->link_config.orig_speed;
1626 phydev->duplex = tp->link_config.orig_duplex;
1627 phydev->autoneg = tp->link_config.orig_autoneg;
1628 phydev->advertising = tp->link_config.orig_advertising;
1629 }
1630
1631 phy_start(phydev);
1632
1633 phy_start_aneg(phydev);
1634}
1635
1636static void tg3_phy_stop(struct tg3 *tp)
1637{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001638 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001639 return;
1640
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001641 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001642}
1643
1644static void tg3_phy_fini(struct tg3 *tp)
1645{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001646 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001647 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001648 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001649 }
1650}
1651
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001652static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1653{
1654 u32 phytest;
1655
1656 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1657 u32 phy;
1658
1659 tg3_writephy(tp, MII_TG3_FET_TEST,
1660 phytest | MII_TG3_FET_SHADOW_EN);
1661 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1662 if (enable)
1663 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1664 else
1665 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1666 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1667 }
1668 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1669 }
1670}
1671
Matt Carlson6833c042008-11-21 17:18:59 -08001672static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1673{
1674 u32 reg;
1675
Joe Perches63c3a662011-04-26 08:12:10 +00001676 if (!tg3_flag(tp, 5705_PLUS) ||
1677 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001678 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001679 return;
1680
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001681 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001682 tg3_phy_fet_toggle_apd(tp, enable);
1683 return;
1684 }
1685
Matt Carlson6833c042008-11-21 17:18:59 -08001686 reg = MII_TG3_MISC_SHDW_WREN |
1687 MII_TG3_MISC_SHDW_SCR5_SEL |
1688 MII_TG3_MISC_SHDW_SCR5_LPED |
1689 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1690 MII_TG3_MISC_SHDW_SCR5_SDTL |
1691 MII_TG3_MISC_SHDW_SCR5_C125OE;
1692 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1693 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1694
1695 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1696
1697
1698 reg = MII_TG3_MISC_SHDW_WREN |
1699 MII_TG3_MISC_SHDW_APD_SEL |
1700 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1701 if (enable)
1702 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1703
1704 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1705}
1706
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001707static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
1708{
1709 u32 phy;
1710
Joe Perches63c3a662011-04-26 08:12:10 +00001711 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001712 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001713 return;
1714
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001715 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001716 u32 ephy;
1717
Matt Carlson535ef6e2009-08-25 10:09:36 +00001718 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
1719 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
1720
1721 tg3_writephy(tp, MII_TG3_FET_TEST,
1722 ephy | MII_TG3_FET_SHADOW_EN);
1723 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001724 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00001725 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001726 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00001727 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1728 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001729 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00001730 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001731 }
1732 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00001733 int ret;
1734
1735 ret = tg3_phy_auxctl_read(tp,
1736 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
1737 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001738 if (enable)
1739 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1740 else
1741 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001742 tg3_phy_auxctl_write(tp,
1743 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001744 }
1745 }
1746}
1747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748static void tg3_phy_set_wirespeed(struct tg3 *tp)
1749{
Matt Carlson15ee95c2011-04-20 07:57:40 +00001750 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 u32 val;
1752
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001753 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 return;
1755
Matt Carlson15ee95c2011-04-20 07:57:40 +00001756 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
1757 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001758 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
1759 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760}
1761
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001762static void tg3_phy_apply_otp(struct tg3 *tp)
1763{
1764 u32 otp, phy;
1765
1766 if (!tp->phy_otp)
1767 return;
1768
1769 otp = tp->phy_otp;
1770
Matt Carlson1d36ba42011-04-20 07:57:42 +00001771 if (TG3_PHY_AUXCTL_SMDSP_ENABLE(tp))
1772 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001773
1774 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
1775 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
1776 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
1777
1778 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
1779 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
1780 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
1781
1782 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
1783 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
1784 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
1785
1786 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
1787 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
1788
1789 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
1790 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
1791
1792 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
1793 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
1794 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
1795
Matt Carlson1d36ba42011-04-20 07:57:42 +00001796 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001797}
1798
Matt Carlson52b02d02010-10-14 10:37:41 +00001799static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
1800{
1801 u32 val;
1802
1803 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
1804 return;
1805
1806 tp->setlpicnt = 0;
1807
1808 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
1809 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00001810 tp->link_config.active_duplex == DUPLEX_FULL &&
1811 (tp->link_config.active_speed == SPEED_100 ||
1812 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00001813 u32 eeectl;
1814
1815 if (tp->link_config.active_speed == SPEED_1000)
1816 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
1817 else
1818 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
1819
1820 tw32(TG3_CPMU_EEE_CTRL, eeectl);
1821
Matt Carlson3110f5f52010-12-06 08:28:50 +00001822 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
1823 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00001824
Matt Carlson21a00ab2011-01-25 15:58:55 +00001825 switch (val) {
1826 case TG3_CL45_D7_EEERES_STAT_LP_1000T:
1827 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
1828 case ASIC_REV_5717:
1829 case ASIC_REV_5719:
1830 case ASIC_REV_57765:
Matt Carlson1d36ba42011-04-20 07:57:42 +00001831 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
1832 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26,
1833 0x0000);
1834 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
1835 }
Matt Carlson21a00ab2011-01-25 15:58:55 +00001836 }
1837 /* Fallthrough */
1838 case TG3_CL45_D7_EEERES_STAT_LP_100TX:
Matt Carlson52b02d02010-10-14 10:37:41 +00001839 tp->setlpicnt = 2;
Matt Carlson21a00ab2011-01-25 15:58:55 +00001840 }
Matt Carlson52b02d02010-10-14 10:37:41 +00001841 }
1842
1843 if (!tp->setlpicnt) {
1844 val = tr32(TG3_CPMU_EEE_MODE);
1845 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
1846 }
1847}
1848
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849static int tg3_wait_macro_done(struct tg3 *tp)
1850{
1851 int limit = 100;
1852
1853 while (limit--) {
1854 u32 tmp32;
1855
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001856 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 if ((tmp32 & 0x1000) == 0)
1858 break;
1859 }
1860 }
Roel Kluind4675b52009-02-12 16:33:27 -08001861 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 return -EBUSY;
1863
1864 return 0;
1865}
1866
1867static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
1868{
1869 static const u32 test_pat[4][6] = {
1870 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
1871 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
1872 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
1873 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
1874 };
1875 int chan;
1876
1877 for (chan = 0; chan < 4; chan++) {
1878 int i;
1879
1880 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1881 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001882 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
1884 for (i = 0; i < 6; i++)
1885 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
1886 test_pat[chan][i]);
1887
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001888 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 if (tg3_wait_macro_done(tp)) {
1890 *resetp = 1;
1891 return -EBUSY;
1892 }
1893
1894 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1895 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001896 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 if (tg3_wait_macro_done(tp)) {
1898 *resetp = 1;
1899 return -EBUSY;
1900 }
1901
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001902 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 if (tg3_wait_macro_done(tp)) {
1904 *resetp = 1;
1905 return -EBUSY;
1906 }
1907
1908 for (i = 0; i < 6; i += 2) {
1909 u32 low, high;
1910
1911 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
1912 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
1913 tg3_wait_macro_done(tp)) {
1914 *resetp = 1;
1915 return -EBUSY;
1916 }
1917 low &= 0x7fff;
1918 high &= 0x000f;
1919 if (low != test_pat[chan][i] ||
1920 high != test_pat[chan][i+1]) {
1921 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
1922 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
1923 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
1924
1925 return -EBUSY;
1926 }
1927 }
1928 }
1929
1930 return 0;
1931}
1932
1933static int tg3_phy_reset_chanpat(struct tg3 *tp)
1934{
1935 int chan;
1936
1937 for (chan = 0; chan < 4; chan++) {
1938 int i;
1939
1940 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1941 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001942 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 for (i = 0; i < 6; i++)
1944 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001945 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 if (tg3_wait_macro_done(tp))
1947 return -EBUSY;
1948 }
1949
1950 return 0;
1951}
1952
1953static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
1954{
1955 u32 reg32, phy9_orig;
1956 int retries, do_phy_reset, err;
1957
1958 retries = 10;
1959 do_phy_reset = 1;
1960 do {
1961 if (do_phy_reset) {
1962 err = tg3_bmcr_reset(tp);
1963 if (err)
1964 return err;
1965 do_phy_reset = 0;
1966 }
1967
1968 /* Disable transmitter and interrupt. */
1969 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
1970 continue;
1971
1972 reg32 |= 0x3000;
1973 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1974
1975 /* Set full-duplex, 1000 mbps. */
1976 tg3_writephy(tp, MII_BMCR,
1977 BMCR_FULLDPLX | TG3_BMCR_SPEED1000);
1978
1979 /* Set to master mode. */
1980 if (tg3_readphy(tp, MII_TG3_CTRL, &phy9_orig))
1981 continue;
1982
1983 tg3_writephy(tp, MII_TG3_CTRL,
1984 (MII_TG3_CTRL_AS_MASTER |
1985 MII_TG3_CTRL_ENABLE_AS_MASTER));
1986
Matt Carlson1d36ba42011-04-20 07:57:42 +00001987 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
1988 if (err)
1989 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
1991 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00001992 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
1994 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
1995 if (!err)
1996 break;
1997 } while (--retries);
1998
1999 err = tg3_phy_reset_chanpat(tp);
2000 if (err)
2001 return err;
2002
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002003 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
2005 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002006 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Matt Carlson1d36ba42011-04-20 07:57:42 +00002008 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
2010 tg3_writephy(tp, MII_TG3_CTRL, phy9_orig);
2011
2012 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2013 reg32 &= ~0x3000;
2014 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2015 } else if (!err)
2016 err = -EBUSY;
2017
2018 return err;
2019}
2020
2021/* This will reset the tigon3 PHY if there is no valid
2022 * link unless the FORCE argument is non-zero.
2023 */
2024static int tg3_phy_reset(struct tg3 *tp)
2025{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002026 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 int err;
2028
Michael Chan60189dd2006-12-17 17:08:07 -08002029 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002030 val = tr32(GRC_MISC_CFG);
2031 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2032 udelay(40);
2033 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002034 err = tg3_readphy(tp, MII_BMSR, &val);
2035 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 if (err != 0)
2037 return -EBUSY;
2038
Michael Chanc8e1e822006-04-29 18:55:17 -07002039 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
2040 netif_carrier_off(tp->dev);
2041 tg3_link_report(tp);
2042 }
2043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2045 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2046 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2047 err = tg3_phy_reset_5703_4_5(tp);
2048 if (err)
2049 return err;
2050 goto out;
2051 }
2052
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002053 cpmuctrl = 0;
2054 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2055 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2056 cpmuctrl = tr32(TG3_CPMU_CTRL);
2057 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2058 tw32(TG3_CPMU_CTRL,
2059 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2060 }
2061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 err = tg3_bmcr_reset(tp);
2063 if (err)
2064 return err;
2065
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002066 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002067 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2068 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002069
2070 tw32(TG3_CPMU_CTRL, cpmuctrl);
2071 }
2072
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002073 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2074 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002075 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2076 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2077 CPMU_LSPD_1000MB_MACCLK_12_5) {
2078 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2079 udelay(40);
2080 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2081 }
2082 }
2083
Joe Perches63c3a662011-04-26 08:12:10 +00002084 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002085 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002086 return 0;
2087
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002088 tg3_phy_apply_otp(tp);
2089
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002090 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002091 tg3_phy_toggle_apd(tp, true);
2092 else
2093 tg3_phy_toggle_apd(tp, false);
2094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002096 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
2097 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002098 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2099 tg3_phydsp_write(tp, 0x000a, 0x0323);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002100 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002102
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002103 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002104 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2105 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002107
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002108 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002109 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2110 tg3_phydsp_write(tp, 0x000a, 0x310b);
2111 tg3_phydsp_write(tp, 0x201f, 0x9506);
2112 tg3_phydsp_write(tp, 0x401f, 0x14e2);
2113 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2114 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002115 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002116 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2117 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2118 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2119 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2120 tg3_writephy(tp, MII_TG3_TEST1,
2121 MII_TG3_TEST1_TRIM_EN | 0x4);
2122 } else
2123 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2124
2125 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2126 }
Michael Chanc424cb22006-04-29 18:56:34 -07002127 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 /* Set Extended packet length bit (bit 14) on all chips that */
2130 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002131 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002133 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002134 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002136 err = tg3_phy_auxctl_read(tp,
2137 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2138 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002139 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2140 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 }
2142
2143 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2144 * jumbo frames transmission.
2145 */
Joe Perches63c3a662011-04-26 08:12:10 +00002146 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002147 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002148 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002149 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 }
2151
Michael Chan715116a2006-09-27 16:09:25 -07002152 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002153 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002154 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002155 }
2156
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002157 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 tg3_phy_set_wirespeed(tp);
2159 return 0;
2160}
2161
2162static void tg3_frob_aux_power(struct tg3 *tp)
2163{
Matt Carlson683644b2011-03-09 16:58:23 +00002164 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Matt Carlson334355a2010-01-20 16:58:10 +00002166 /* The GPIOs do something completely different on 57765. */
Joe Perches63c3a662011-04-26 08:12:10 +00002167 if (!tg3_flag(tp, IS_NIC) ||
Matt Carlsona50d0792010-06-05 17:24:37 +00002168 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson334355a2010-01-20 16:58:10 +00002169 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 return;
2171
Matt Carlson683644b2011-03-09 16:58:23 +00002172 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2173 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +00002174 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2175 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) &&
Matt Carlson683644b2011-03-09 16:58:23 +00002176 tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002177 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002179 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002180
Michael Chanbc1c7562006-03-20 17:48:03 -08002181 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002182 if (dev_peer) {
2183 struct tg3 *tp_peer = netdev_priv(dev_peer);
2184
Joe Perches63c3a662011-04-26 08:12:10 +00002185 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002186 return;
2187
Joe Perches63c3a662011-04-26 08:12:10 +00002188 if (tg3_flag(tp_peer, WOL_ENABLE) ||
2189 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002190 need_vaux = true;
2191 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
Joe Perches63c3a662011-04-26 08:12:10 +00002194 if (tg3_flag(tp, WOL_ENABLE) || tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002195 need_vaux = true;
2196
2197 if (need_vaux) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2199 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002200 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2201 (GRC_LCLCTRL_GPIO_OE0 |
2202 GRC_LCLCTRL_GPIO_OE1 |
2203 GRC_LCLCTRL_GPIO_OE2 |
2204 GRC_LCLCTRL_GPIO_OUTPUT0 |
2205 GRC_LCLCTRL_GPIO_OUTPUT1),
2206 100);
Matt Carlson8d519ab2009-04-20 06:58:01 +00002207 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2208 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -07002209 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2210 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2211 GRC_LCLCTRL_GPIO_OE1 |
2212 GRC_LCLCTRL_GPIO_OE2 |
2213 GRC_LCLCTRL_GPIO_OUTPUT0 |
2214 GRC_LCLCTRL_GPIO_OUTPUT1 |
2215 tp->grc_local_ctrl;
2216 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2217
2218 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2219 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2220
2221 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2222 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 } else {
2224 u32 no_gpio2;
Michael Chandc56b7d2005-12-19 16:26:28 -08002225 u32 grc_local_ctrl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Michael Chandc56b7d2005-12-19 16:26:28 -08002227 /* Workaround to prevent overdrawing Amps. */
2228 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2229 ASIC_REV_5714) {
2230 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chanb401e9e2005-12-19 16:27:04 -08002231 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2232 grc_local_ctrl, 100);
Michael Chandc56b7d2005-12-19 16:26:28 -08002233 }
2234
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 /* On 5753 and variants, GPIO2 cannot be used. */
2236 no_gpio2 = tp->nic_sram_data_cfg &
2237 NIC_SRAM_DATA_CFG_NO_GPIO2;
2238
Michael Chandc56b7d2005-12-19 16:26:28 -08002239 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 GRC_LCLCTRL_GPIO_OE1 |
2241 GRC_LCLCTRL_GPIO_OE2 |
2242 GRC_LCLCTRL_GPIO_OUTPUT1 |
2243 GRC_LCLCTRL_GPIO_OUTPUT2;
2244 if (no_gpio2) {
2245 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2246 GRC_LCLCTRL_GPIO_OUTPUT2);
2247 }
Michael Chanb401e9e2005-12-19 16:27:04 -08002248 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2249 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
2251 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2252
Michael Chanb401e9e2005-12-19 16:27:04 -08002253 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2254 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
2256 if (!no_gpio2) {
2257 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chanb401e9e2005-12-19 16:27:04 -08002258 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2259 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 }
2261 }
2262 } else {
2263 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
2264 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002265 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2266 (GRC_LCLCTRL_GPIO_OE1 |
2267 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
Michael Chanb401e9e2005-12-19 16:27:04 -08002269 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2270 GRC_LCLCTRL_GPIO_OE1, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
Michael Chanb401e9e2005-12-19 16:27:04 -08002272 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2273 (GRC_LCLCTRL_GPIO_OE1 |
2274 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 }
2276 }
2277}
2278
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002279static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2280{
2281 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2282 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002283 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002284 if (speed != SPEED_10)
2285 return 1;
2286 } else if (speed == SPEED_10)
2287 return 1;
2288
2289 return 0;
2290}
2291
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292static int tg3_setup_phy(struct tg3 *, int);
2293
2294#define RESET_KIND_SHUTDOWN 0
2295#define RESET_KIND_INIT 1
2296#define RESET_KIND_SUSPEND 2
2297
2298static void tg3_write_sig_post_reset(struct tg3 *, int);
2299static int tg3_halt_cpu(struct tg3 *, u32);
2300
Matt Carlson0a459aa2008-11-03 16:54:15 -08002301static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002302{
Matt Carlsonce057f02007-11-12 21:08:03 -08002303 u32 val;
2304
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002305 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002306 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2307 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2308 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2309
2310 sg_dig_ctrl |=
2311 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2312 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2313 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2314 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002315 return;
Michael Chan51297242007-02-13 12:17:57 -08002316 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002317
Michael Chan60189dd2006-12-17 17:08:07 -08002318 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002319 tg3_bmcr_reset(tp);
2320 val = tr32(GRC_MISC_CFG);
2321 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2322 udelay(40);
2323 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002324 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002325 u32 phytest;
2326 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2327 u32 phy;
2328
2329 tg3_writephy(tp, MII_ADVERTISE, 0);
2330 tg3_writephy(tp, MII_BMCR,
2331 BMCR_ANENABLE | BMCR_ANRESTART);
2332
2333 tg3_writephy(tp, MII_TG3_FET_TEST,
2334 phytest | MII_TG3_FET_SHADOW_EN);
2335 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2336 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2337 tg3_writephy(tp,
2338 MII_TG3_FET_SHDW_AUXMODE4,
2339 phy);
2340 }
2341 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2342 }
2343 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002344 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002345 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2346 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002347
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002348 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2349 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2350 MII_TG3_AUXCTL_PCTL_VREG_11V;
2351 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07002352 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002353
Michael Chan15c3b692006-03-22 01:06:52 -08002354 /* The PHY should not be powered down on some chips because
2355 * of bugs.
2356 */
2357 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2358 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2359 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002360 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Michael Chan15c3b692006-03-22 01:06:52 -08002361 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002362
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002363 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2364 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002365 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2366 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2367 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2368 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2369 }
2370
Michael Chan15c3b692006-03-22 01:06:52 -08002371 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2372}
2373
Matt Carlson3f007892008-11-03 16:51:36 -08002374/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002375static int tg3_nvram_lock(struct tg3 *tp)
2376{
Joe Perches63c3a662011-04-26 08:12:10 +00002377 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002378 int i;
2379
2380 if (tp->nvram_lock_cnt == 0) {
2381 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2382 for (i = 0; i < 8000; i++) {
2383 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2384 break;
2385 udelay(20);
2386 }
2387 if (i == 8000) {
2388 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2389 return -ENODEV;
2390 }
2391 }
2392 tp->nvram_lock_cnt++;
2393 }
2394 return 0;
2395}
2396
2397/* tp->lock is held. */
2398static void tg3_nvram_unlock(struct tg3 *tp)
2399{
Joe Perches63c3a662011-04-26 08:12:10 +00002400 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002401 if (tp->nvram_lock_cnt > 0)
2402 tp->nvram_lock_cnt--;
2403 if (tp->nvram_lock_cnt == 0)
2404 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2405 }
2406}
2407
2408/* tp->lock is held. */
2409static void tg3_enable_nvram_access(struct tg3 *tp)
2410{
Joe Perches63c3a662011-04-26 08:12:10 +00002411 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002412 u32 nvaccess = tr32(NVRAM_ACCESS);
2413
2414 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2415 }
2416}
2417
2418/* tp->lock is held. */
2419static void tg3_disable_nvram_access(struct tg3 *tp)
2420{
Joe Perches63c3a662011-04-26 08:12:10 +00002421 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002422 u32 nvaccess = tr32(NVRAM_ACCESS);
2423
2424 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2425 }
2426}
2427
2428static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2429 u32 offset, u32 *val)
2430{
2431 u32 tmp;
2432 int i;
2433
2434 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2435 return -EINVAL;
2436
2437 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2438 EEPROM_ADDR_DEVID_MASK |
2439 EEPROM_ADDR_READ);
2440 tw32(GRC_EEPROM_ADDR,
2441 tmp |
2442 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2443 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2444 EEPROM_ADDR_ADDR_MASK) |
2445 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2446
2447 for (i = 0; i < 1000; i++) {
2448 tmp = tr32(GRC_EEPROM_ADDR);
2449
2450 if (tmp & EEPROM_ADDR_COMPLETE)
2451 break;
2452 msleep(1);
2453 }
2454 if (!(tmp & EEPROM_ADDR_COMPLETE))
2455 return -EBUSY;
2456
Matt Carlson62cedd12009-04-20 14:52:29 -07002457 tmp = tr32(GRC_EEPROM_DATA);
2458
2459 /*
2460 * The data will always be opposite the native endian
2461 * format. Perform a blind byteswap to compensate.
2462 */
2463 *val = swab32(tmp);
2464
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002465 return 0;
2466}
2467
2468#define NVRAM_CMD_TIMEOUT 10000
2469
2470static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2471{
2472 int i;
2473
2474 tw32(NVRAM_CMD, nvram_cmd);
2475 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2476 udelay(10);
2477 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2478 udelay(10);
2479 break;
2480 }
2481 }
2482
2483 if (i == NVRAM_CMD_TIMEOUT)
2484 return -EBUSY;
2485
2486 return 0;
2487}
2488
2489static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2490{
Joe Perches63c3a662011-04-26 08:12:10 +00002491 if (tg3_flag(tp, NVRAM) &&
2492 tg3_flag(tp, NVRAM_BUFFERED) &&
2493 tg3_flag(tp, FLASH) &&
2494 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002495 (tp->nvram_jedecnum == JEDEC_ATMEL))
2496
2497 addr = ((addr / tp->nvram_pagesize) <<
2498 ATMEL_AT45DB0X1B_PAGE_POS) +
2499 (addr % tp->nvram_pagesize);
2500
2501 return addr;
2502}
2503
2504static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2505{
Joe Perches63c3a662011-04-26 08:12:10 +00002506 if (tg3_flag(tp, NVRAM) &&
2507 tg3_flag(tp, NVRAM_BUFFERED) &&
2508 tg3_flag(tp, FLASH) &&
2509 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002510 (tp->nvram_jedecnum == JEDEC_ATMEL))
2511
2512 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2513 tp->nvram_pagesize) +
2514 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2515
2516 return addr;
2517}
2518
Matt Carlsone4f34112009-02-25 14:25:00 +00002519/* NOTE: Data read in from NVRAM is byteswapped according to
2520 * the byteswapping settings for all other register accesses.
2521 * tg3 devices are BE devices, so on a BE machine, the data
2522 * returned will be exactly as it is seen in NVRAM. On a LE
2523 * machine, the 32-bit value will be byteswapped.
2524 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002525static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2526{
2527 int ret;
2528
Joe Perches63c3a662011-04-26 08:12:10 +00002529 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002530 return tg3_nvram_read_using_eeprom(tp, offset, val);
2531
2532 offset = tg3_nvram_phys_addr(tp, offset);
2533
2534 if (offset > NVRAM_ADDR_MSK)
2535 return -EINVAL;
2536
2537 ret = tg3_nvram_lock(tp);
2538 if (ret)
2539 return ret;
2540
2541 tg3_enable_nvram_access(tp);
2542
2543 tw32(NVRAM_ADDR, offset);
2544 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2545 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2546
2547 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002548 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002549
2550 tg3_disable_nvram_access(tp);
2551
2552 tg3_nvram_unlock(tp);
2553
2554 return ret;
2555}
2556
Matt Carlsona9dc5292009-02-25 14:25:30 +00002557/* Ensures NVRAM data is in bytestream format. */
2558static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002559{
2560 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002561 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002562 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002563 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002564 return res;
2565}
2566
2567/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08002568static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2569{
2570 u32 addr_high, addr_low;
2571 int i;
2572
2573 addr_high = ((tp->dev->dev_addr[0] << 8) |
2574 tp->dev->dev_addr[1]);
2575 addr_low = ((tp->dev->dev_addr[2] << 24) |
2576 (tp->dev->dev_addr[3] << 16) |
2577 (tp->dev->dev_addr[4] << 8) |
2578 (tp->dev->dev_addr[5] << 0));
2579 for (i = 0; i < 4; i++) {
2580 if (i == 1 && skip_mac_1)
2581 continue;
2582 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2583 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2584 }
2585
2586 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2587 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2588 for (i = 0; i < 12; i++) {
2589 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2590 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2591 }
2592 }
2593
2594 addr_high = (tp->dev->dev_addr[0] +
2595 tp->dev->dev_addr[1] +
2596 tp->dev->dev_addr[2] +
2597 tp->dev->dev_addr[3] +
2598 tp->dev->dev_addr[4] +
2599 tp->dev->dev_addr[5]) &
2600 TX_BACKOFF_SEED_MASK;
2601 tw32(MAC_TX_BACKOFF_SEED, addr_high);
2602}
2603
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002604static void tg3_enable_register_access(struct tg3 *tp)
2605{
2606 /*
2607 * Make sure register accesses (indirect or otherwise) will function
2608 * correctly.
2609 */
2610 pci_write_config_dword(tp->pdev,
2611 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
2612}
2613
2614static int tg3_power_up(struct tg3 *tp)
2615{
2616 tg3_enable_register_access(tp);
2617
2618 pci_set_power_state(tp->pdev, PCI_D0);
2619
2620 /* Switch out of Vaux if it is a NIC */
Joe Perches63c3a662011-04-26 08:12:10 +00002621 if (tg3_flag(tp, IS_NIC))
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002622 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl, 100);
2623
2624 return 0;
2625}
2626
2627static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628{
2629 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002630 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002632 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002633
2634 /* Restore the CLKREQ setting. */
Joe Perches63c3a662011-04-26 08:12:10 +00002635 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002636 u16 lnkctl;
2637
2638 pci_read_config_word(tp->pdev,
2639 tp->pcie_cap + PCI_EXP_LNKCTL,
2640 &lnkctl);
2641 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2642 pci_write_config_word(tp->pdev,
2643 tp->pcie_cap + PCI_EXP_LNKCTL,
2644 lnkctl);
2645 }
2646
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2648 tw32(TG3PCI_MISC_HOST_CTRL,
2649 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2650
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002651 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00002652 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002653
Joe Perches63c3a662011-04-26 08:12:10 +00002654 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002655 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002656 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00002657 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002658 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002659 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002660
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002661 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002662
Matt Carlson80096062010-08-02 11:26:06 +00002663 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002664
2665 tp->link_config.orig_speed = phydev->speed;
2666 tp->link_config.orig_duplex = phydev->duplex;
2667 tp->link_config.orig_autoneg = phydev->autoneg;
2668 tp->link_config.orig_advertising = phydev->advertising;
2669
2670 advertising = ADVERTISED_TP |
2671 ADVERTISED_Pause |
2672 ADVERTISED_Autoneg |
2673 ADVERTISED_10baseT_Half;
2674
Joe Perches63c3a662011-04-26 08:12:10 +00002675 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
2676 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002677 advertising |=
2678 ADVERTISED_100baseT_Half |
2679 ADVERTISED_100baseT_Full |
2680 ADVERTISED_10baseT_Full;
2681 else
2682 advertising |= ADVERTISED_10baseT_Full;
2683 }
2684
2685 phydev->advertising = advertising;
2686
2687 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002688
2689 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00002690 if (phyid != PHY_ID_BCMAC131) {
2691 phyid &= PHY_BCM_OUI_MASK;
2692 if (phyid == PHY_BCM_OUI_1 ||
2693 phyid == PHY_BCM_OUI_2 ||
2694 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08002695 do_low_power = true;
2696 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002697 }
Matt Carlsondd477002008-05-25 23:45:58 -07002698 } else {
Matt Carlson20232762008-12-21 20:18:56 -08002699 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002700
Matt Carlson80096062010-08-02 11:26:06 +00002701 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
2702 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07002703 tp->link_config.orig_speed = tp->link_config.speed;
2704 tp->link_config.orig_duplex = tp->link_config.duplex;
2705 tp->link_config.orig_autoneg = tp->link_config.autoneg;
2706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002708 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Matt Carlsondd477002008-05-25 23:45:58 -07002709 tp->link_config.speed = SPEED_10;
2710 tp->link_config.duplex = DUPLEX_HALF;
2711 tp->link_config.autoneg = AUTONEG_ENABLE;
2712 tg3_setup_phy(tp, 0);
2713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 }
2715
Michael Chanb5d37722006-09-27 16:06:21 -07002716 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2717 u32 val;
2718
2719 val = tr32(GRC_VCPU_EXT_CTRL);
2720 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00002721 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08002722 int i;
2723 u32 val;
2724
2725 for (i = 0; i < 200; i++) {
2726 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2727 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2728 break;
2729 msleep(1);
2730 }
2731 }
Joe Perches63c3a662011-04-26 08:12:10 +00002732 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07002733 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2734 WOL_DRV_STATE_SHUTDOWN |
2735 WOL_DRV_WOL |
2736 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08002737
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002738 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 u32 mac_mode;
2740
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002741 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002742 if (do_low_power &&
2743 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
2744 tg3_phy_auxctl_write(tp,
2745 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
2746 MII_TG3_AUXCTL_PCTL_WOL_EN |
2747 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2748 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07002749 udelay(40);
2750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002752 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07002753 mac_mode = MAC_MODE_PORT_MODE_GMII;
2754 else
2755 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002757 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2758 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2759 ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00002760 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002761 SPEED_100 : SPEED_10;
2762 if (tg3_5700_link_polarity(tp, speed))
2763 mac_mode |= MAC_MODE_LINK_POLARITY;
2764 else
2765 mac_mode &= ~MAC_MODE_LINK_POLARITY;
2766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 } else {
2768 mac_mode = MAC_MODE_PORT_MODE_TBI;
2769 }
2770
Joe Perches63c3a662011-04-26 08:12:10 +00002771 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 tw32(MAC_LED_CTRL, tp->led_ctrl);
2773
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002774 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00002775 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
2776 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002777 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
Joe Perches63c3a662011-04-26 08:12:10 +00002779 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00002780 mac_mode |= MAC_MODE_APE_TX_EN |
2781 MAC_MODE_APE_RX_EN |
2782 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07002783
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 tw32_f(MAC_MODE, mac_mode);
2785 udelay(100);
2786
2787 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2788 udelay(10);
2789 }
2790
Joe Perches63c3a662011-04-26 08:12:10 +00002791 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2793 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2794 u32 base_val;
2795
2796 base_val = tp->pci_clock_ctrl;
2797 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2798 CLOCK_CTRL_TXCLK_DISABLE);
2799
Michael Chanb401e9e2005-12-19 16:27:04 -08002800 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
2801 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00002802 } else if (tg3_flag(tp, 5780_CLASS) ||
2803 tg3_flag(tp, CPMU_PRESENT) ||
Michael Chand7b0a852007-02-13 12:17:38 -08002804 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)) {
Michael Chan4cf78e42005-07-25 12:29:19 -07002805 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00002806 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 u32 newbits1, newbits2;
2808
2809 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2810 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2811 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
2812 CLOCK_CTRL_TXCLK_DISABLE |
2813 CLOCK_CTRL_ALTCLK);
2814 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00002815 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 newbits1 = CLOCK_CTRL_625_CORE;
2817 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
2818 } else {
2819 newbits1 = CLOCK_CTRL_ALTCLK;
2820 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2821 }
2822
Michael Chanb401e9e2005-12-19 16:27:04 -08002823 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
2824 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
Michael Chanb401e9e2005-12-19 16:27:04 -08002826 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
2827 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
Joe Perches63c3a662011-04-26 08:12:10 +00002829 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 u32 newbits3;
2831
2832 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2833 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2834 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
2835 CLOCK_CTRL_TXCLK_DISABLE |
2836 CLOCK_CTRL_44MHZ_CORE);
2837 } else {
2838 newbits3 = CLOCK_CTRL_44MHZ_CORE;
2839 }
2840
Michael Chanb401e9e2005-12-19 16:27:04 -08002841 tw32_wait_f(TG3PCI_CLOCK_CTRL,
2842 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 }
2844 }
2845
Joe Perches63c3a662011-04-26 08:12:10 +00002846 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08002847 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08002848
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 tg3_frob_aux_power(tp);
2850
2851 /* Workaround for unstable PLL clock */
2852 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
2853 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
2854 u32 val = tr32(0x7d00);
2855
2856 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
2857 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00002858 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08002859 int err;
2860
2861 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08002863 if (!err)
2864 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08002865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 }
2867
Michael Chanbbadf502006-04-06 21:46:34 -07002868 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
2869
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 return 0;
2871}
2872
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002873static void tg3_power_down(struct tg3 *tp)
2874{
2875 tg3_power_down_prepare(tp);
2876
Joe Perches63c3a662011-04-26 08:12:10 +00002877 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002878 pci_set_power_state(tp->pdev, PCI_D3hot);
2879}
2880
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
2882{
2883 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
2884 case MII_TG3_AUX_STAT_10HALF:
2885 *speed = SPEED_10;
2886 *duplex = DUPLEX_HALF;
2887 break;
2888
2889 case MII_TG3_AUX_STAT_10FULL:
2890 *speed = SPEED_10;
2891 *duplex = DUPLEX_FULL;
2892 break;
2893
2894 case MII_TG3_AUX_STAT_100HALF:
2895 *speed = SPEED_100;
2896 *duplex = DUPLEX_HALF;
2897 break;
2898
2899 case MII_TG3_AUX_STAT_100FULL:
2900 *speed = SPEED_100;
2901 *duplex = DUPLEX_FULL;
2902 break;
2903
2904 case MII_TG3_AUX_STAT_1000HALF:
2905 *speed = SPEED_1000;
2906 *duplex = DUPLEX_HALF;
2907 break;
2908
2909 case MII_TG3_AUX_STAT_1000FULL:
2910 *speed = SPEED_1000;
2911 *duplex = DUPLEX_FULL;
2912 break;
2913
2914 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002915 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07002916 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
2917 SPEED_10;
2918 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
2919 DUPLEX_HALF;
2920 break;
2921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 *speed = SPEED_INVALID;
2923 *duplex = DUPLEX_INVALID;
2924 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926}
2927
2928static void tg3_phy_copper_begin(struct tg3 *tp)
2929{
2930 u32 new_adv;
2931 int i;
2932
Matt Carlson80096062010-08-02 11:26:06 +00002933 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 /* Entering low power mode. Disable gigabit and
2935 * 100baseT advertisements.
2936 */
2937 tg3_writephy(tp, MII_TG3_CTRL, 0);
2938
2939 new_adv = (ADVERTISE_10HALF | ADVERTISE_10FULL |
2940 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
Joe Perches63c3a662011-04-26 08:12:10 +00002941 if (tg3_flag(tp, WOL_SPEED_100MB))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 new_adv |= (ADVERTISE_100HALF | ADVERTISE_100FULL);
2943
2944 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2945 } else if (tp->link_config.speed == SPEED_INVALID) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002946 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 tp->link_config.advertising &=
2948 ~(ADVERTISED_1000baseT_Half |
2949 ADVERTISED_1000baseT_Full);
2950
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002951 new_adv = ADVERTISE_CSMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 if (tp->link_config.advertising & ADVERTISED_10baseT_Half)
2953 new_adv |= ADVERTISE_10HALF;
2954 if (tp->link_config.advertising & ADVERTISED_10baseT_Full)
2955 new_adv |= ADVERTISE_10FULL;
2956 if (tp->link_config.advertising & ADVERTISED_100baseT_Half)
2957 new_adv |= ADVERTISE_100HALF;
2958 if (tp->link_config.advertising & ADVERTISED_100baseT_Full)
2959 new_adv |= ADVERTISE_100FULL;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002960
2961 new_adv |= tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2962
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2964
2965 if (tp->link_config.advertising &
2966 (ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full)) {
2967 new_adv = 0;
2968 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
2969 new_adv |= MII_TG3_CTRL_ADV_1000_HALF;
2970 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
2971 new_adv |= MII_TG3_CTRL_ADV_1000_FULL;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002972 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2974 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0))
2975 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2976 MII_TG3_CTRL_ENABLE_AS_MASTER);
2977 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
2978 } else {
2979 tg3_writephy(tp, MII_TG3_CTRL, 0);
2980 }
2981 } else {
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002982 new_adv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2983 new_adv |= ADVERTISE_CSMA;
2984
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 /* Asking for a specific link mode. */
2986 if (tp->link_config.speed == SPEED_1000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2988
2989 if (tp->link_config.duplex == DUPLEX_FULL)
2990 new_adv = MII_TG3_CTRL_ADV_1000_FULL;
2991 else
2992 new_adv = MII_TG3_CTRL_ADV_1000_HALF;
2993 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2994 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
2995 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2996 MII_TG3_CTRL_ENABLE_AS_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 if (tp->link_config.speed == SPEED_100) {
2999 if (tp->link_config.duplex == DUPLEX_FULL)
3000 new_adv |= ADVERTISE_100FULL;
3001 else
3002 new_adv |= ADVERTISE_100HALF;
3003 } else {
3004 if (tp->link_config.duplex == DUPLEX_FULL)
3005 new_adv |= ADVERTISE_10FULL;
3006 else
3007 new_adv |= ADVERTISE_10HALF;
3008 }
3009 tg3_writephy(tp, MII_ADVERTISE, new_adv);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003010
3011 new_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003013
3014 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 }
3016
Matt Carlson52b02d02010-10-14 10:37:41 +00003017 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
Matt Carlsona6b68da2010-12-06 08:28:52 +00003018 u32 val;
Matt Carlson52b02d02010-10-14 10:37:41 +00003019
3020 tw32(TG3_CPMU_EEE_MODE,
3021 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
3022
Matt Carlson1d36ba42011-04-20 07:57:42 +00003023 TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +00003024
Matt Carlson21a00ab2011-01-25 15:58:55 +00003025 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
3026 case ASIC_REV_5717:
3027 case ASIC_REV_57765:
3028 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
3029 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
3030 MII_TG3_DSP_CH34TP2_HIBW01);
3031 /* Fall through */
3032 case ASIC_REV_5719:
3033 val = MII_TG3_DSP_TAP26_ALNOKO |
3034 MII_TG3_DSP_TAP26_RMRXSTO |
3035 MII_TG3_DSP_TAP26_OPCSINPT;
3036 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
3037 }
Matt Carlson52b02d02010-10-14 10:37:41 +00003038
Matt Carlsona6b68da2010-12-06 08:28:52 +00003039 val = 0;
Matt Carlson52b02d02010-10-14 10:37:41 +00003040 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3041 /* Advertise 100-BaseTX EEE ability */
3042 if (tp->link_config.advertising &
Matt Carlson3110f5f52010-12-06 08:28:50 +00003043 ADVERTISED_100baseT_Full)
3044 val |= MDIO_AN_EEE_ADV_100TX;
Matt Carlson52b02d02010-10-14 10:37:41 +00003045 /* Advertise 1000-BaseT EEE ability */
3046 if (tp->link_config.advertising &
Matt Carlson3110f5f52010-12-06 08:28:50 +00003047 ADVERTISED_1000baseT_Full)
3048 val |= MDIO_AN_EEE_ADV_1000T;
Matt Carlson52b02d02010-10-14 10:37:41 +00003049 }
Matt Carlson3110f5f52010-12-06 08:28:50 +00003050 tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlson52b02d02010-10-14 10:37:41 +00003051
Matt Carlson1d36ba42011-04-20 07:57:42 +00003052 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +00003053 }
3054
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
3056 tp->link_config.speed != SPEED_INVALID) {
3057 u32 bmcr, orig_bmcr;
3058
3059 tp->link_config.active_speed = tp->link_config.speed;
3060 tp->link_config.active_duplex = tp->link_config.duplex;
3061
3062 bmcr = 0;
3063 switch (tp->link_config.speed) {
3064 default:
3065 case SPEED_10:
3066 break;
3067
3068 case SPEED_100:
3069 bmcr |= BMCR_SPEED100;
3070 break;
3071
3072 case SPEED_1000:
3073 bmcr |= TG3_BMCR_SPEED1000;
3074 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
3077 if (tp->link_config.duplex == DUPLEX_FULL)
3078 bmcr |= BMCR_FULLDPLX;
3079
3080 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3081 (bmcr != orig_bmcr)) {
3082 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3083 for (i = 0; i < 1500; i++) {
3084 u32 tmp;
3085
3086 udelay(10);
3087 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3088 tg3_readphy(tp, MII_BMSR, &tmp))
3089 continue;
3090 if (!(tmp & BMSR_LSTATUS)) {
3091 udelay(40);
3092 break;
3093 }
3094 }
3095 tg3_writephy(tp, MII_BMCR, bmcr);
3096 udelay(40);
3097 }
3098 } else {
3099 tg3_writephy(tp, MII_BMCR,
3100 BMCR_ANENABLE | BMCR_ANRESTART);
3101 }
3102}
3103
3104static int tg3_init_5401phy_dsp(struct tg3 *tp)
3105{
3106 int err;
3107
3108 /* Turn off tap power management. */
3109 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003110 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003112 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3113 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3114 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3115 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3116 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
3118 udelay(40);
3119
3120 return err;
3121}
3122
Michael Chan3600d912006-12-07 00:21:48 -08003123static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124{
Michael Chan3600d912006-12-07 00:21:48 -08003125 u32 adv_reg, all_mask = 0;
3126
3127 if (mask & ADVERTISED_10baseT_Half)
3128 all_mask |= ADVERTISE_10HALF;
3129 if (mask & ADVERTISED_10baseT_Full)
3130 all_mask |= ADVERTISE_10FULL;
3131 if (mask & ADVERTISED_100baseT_Half)
3132 all_mask |= ADVERTISE_100HALF;
3133 if (mask & ADVERTISED_100baseT_Full)
3134 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
3136 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
3137 return 0;
3138
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 if ((adv_reg & all_mask) != all_mask)
3140 return 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003141 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 u32 tg3_ctrl;
3143
Michael Chan3600d912006-12-07 00:21:48 -08003144 all_mask = 0;
3145 if (mask & ADVERTISED_1000baseT_Half)
3146 all_mask |= ADVERTISE_1000HALF;
3147 if (mask & ADVERTISED_1000baseT_Full)
3148 all_mask |= ADVERTISE_1000FULL;
3149
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 if (tg3_readphy(tp, MII_TG3_CTRL, &tg3_ctrl))
3151 return 0;
3152
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 if ((tg3_ctrl & all_mask) != all_mask)
3154 return 0;
3155 }
3156 return 1;
3157}
3158
Matt Carlsonef167e22007-12-20 20:10:01 -08003159static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
3160{
3161 u32 curadv, reqadv;
3162
3163 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3164 return 1;
3165
3166 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3167 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3168
3169 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3170 if (curadv != reqadv)
3171 return 0;
3172
Joe Perches63c3a662011-04-26 08:12:10 +00003173 if (tg3_flag(tp, PAUSE_AUTONEG))
Matt Carlsonef167e22007-12-20 20:10:01 -08003174 tg3_readphy(tp, MII_LPA, rmtadv);
3175 } else {
3176 /* Reprogram the advertisement register, even if it
3177 * does not affect the current link. If the link
3178 * gets renegotiated in the future, we can save an
3179 * additional renegotiation cycle by advertising
3180 * it correctly in the first place.
3181 */
3182 if (curadv != reqadv) {
3183 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3184 ADVERTISE_PAUSE_ASYM);
3185 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3186 }
3187 }
3188
3189 return 1;
3190}
3191
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3193{
3194 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003195 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08003196 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 u16 current_speed;
3198 u8 current_duplex;
3199 int i, err;
3200
3201 tw32(MAC_EVENT, 0);
3202
3203 tw32_f(MAC_STATUS,
3204 (MAC_STATUS_SYNC_CHANGED |
3205 MAC_STATUS_CFG_CHANGED |
3206 MAC_STATUS_MI_COMPLETION |
3207 MAC_STATUS_LNKSTATE_CHANGED));
3208 udelay(40);
3209
Matt Carlson8ef21422008-05-02 16:47:53 -07003210 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3211 tw32_f(MAC_MI_MODE,
3212 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3213 udelay(80);
3214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003216 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
3218 /* Some third-party PHYs need to be reset on link going
3219 * down.
3220 */
3221 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3222 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3223 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3224 netif_carrier_ok(tp->dev)) {
3225 tg3_readphy(tp, MII_BMSR, &bmsr);
3226 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3227 !(bmsr & BMSR_LSTATUS))
3228 force_reset = 1;
3229 }
3230 if (force_reset)
3231 tg3_phy_reset(tp);
3232
Matt Carlson79eb6902010-02-17 15:17:03 +00003233 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 tg3_readphy(tp, MII_BMSR, &bmsr);
3235 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00003236 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 bmsr = 0;
3238
3239 if (!(bmsr & BMSR_LSTATUS)) {
3240 err = tg3_init_5401phy_dsp(tp);
3241 if (err)
3242 return err;
3243
3244 tg3_readphy(tp, MII_BMSR, &bmsr);
3245 for (i = 0; i < 1000; i++) {
3246 udelay(10);
3247 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3248 (bmsr & BMSR_LSTATUS)) {
3249 udelay(40);
3250 break;
3251 }
3252 }
3253
Matt Carlson79eb6902010-02-17 15:17:03 +00003254 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3255 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 !(bmsr & BMSR_LSTATUS) &&
3257 tp->link_config.active_speed == SPEED_1000) {
3258 err = tg3_phy_reset(tp);
3259 if (!err)
3260 err = tg3_init_5401phy_dsp(tp);
3261 if (err)
3262 return err;
3263 }
3264 }
3265 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3266 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3267 /* 5701 {A0,B0} CRC bug workaround */
3268 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00003269 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3270 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
3271 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 }
3273
3274 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003275 tg3_readphy(tp, MII_TG3_ISTAT, &val);
3276 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003278 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003280 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3282
3283 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3284 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3285 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3286 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3287 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3288 else
3289 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3290 }
3291
3292 current_link_up = 0;
3293 current_speed = SPEED_INVALID;
3294 current_duplex = DUPLEX_INVALID;
3295
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003296 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00003297 err = tg3_phy_auxctl_read(tp,
3298 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3299 &val);
3300 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003301 tg3_phy_auxctl_write(tp,
3302 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3303 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 goto relink;
3305 }
3306 }
3307
3308 bmsr = 0;
3309 for (i = 0; i < 100; i++) {
3310 tg3_readphy(tp, MII_BMSR, &bmsr);
3311 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3312 (bmsr & BMSR_LSTATUS))
3313 break;
3314 udelay(40);
3315 }
3316
3317 if (bmsr & BMSR_LSTATUS) {
3318 u32 aux_stat, bmcr;
3319
3320 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3321 for (i = 0; i < 2000; i++) {
3322 udelay(10);
3323 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3324 aux_stat)
3325 break;
3326 }
3327
3328 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3329 &current_speed,
3330 &current_duplex);
3331
3332 bmcr = 0;
3333 for (i = 0; i < 200; i++) {
3334 tg3_readphy(tp, MII_BMCR, &bmcr);
3335 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3336 continue;
3337 if (bmcr && bmcr != 0x7fff)
3338 break;
3339 udelay(10);
3340 }
3341
Matt Carlsonef167e22007-12-20 20:10:01 -08003342 lcl_adv = 0;
3343 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344
Matt Carlsonef167e22007-12-20 20:10:01 -08003345 tp->link_config.active_speed = current_speed;
3346 tp->link_config.active_duplex = current_duplex;
3347
3348 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3349 if ((bmcr & BMCR_ANENABLE) &&
3350 tg3_copper_is_advertising_all(tp,
3351 tp->link_config.advertising)) {
3352 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3353 &rmt_adv))
3354 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 }
3356 } else {
3357 if (!(bmcr & BMCR_ANENABLE) &&
3358 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003359 tp->link_config.duplex == current_duplex &&
3360 tp->link_config.flowctrl ==
3361 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 }
3364 }
3365
Matt Carlsonef167e22007-12-20 20:10:01 -08003366 if (current_link_up == 1 &&
3367 tp->link_config.active_duplex == DUPLEX_FULL)
3368 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 }
3370
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371relink:
Matt Carlson80096062010-08-02 11:26:06 +00003372 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 tg3_phy_copper_begin(tp);
3374
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003375 tg3_readphy(tp, MII_BMSR, &bmsr);
3376 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3377 (bmsr & BMSR_LSTATUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 current_link_up = 1;
3379 }
3380
3381 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3382 if (current_link_up == 1) {
3383 if (tp->link_config.active_speed == SPEED_100 ||
3384 tp->link_config.active_speed == SPEED_10)
3385 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3386 else
3387 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003388 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003389 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3390 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3392
3393 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3394 if (tp->link_config.active_duplex == DUPLEX_HALF)
3395 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3396
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003398 if (current_link_up == 1 &&
3399 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003401 else
3402 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 }
3404
3405 /* ??? Without this setting Netgear GA302T PHY does not
3406 * ??? send/receive packets...
3407 */
Matt Carlson79eb6902010-02-17 15:17:03 +00003408 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3410 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3411 tw32_f(MAC_MI_MODE, tp->mi_mode);
3412 udelay(80);
3413 }
3414
3415 tw32_f(MAC_MODE, tp->mac_mode);
3416 udelay(40);
3417
Matt Carlson52b02d02010-10-14 10:37:41 +00003418 tg3_phy_eee_adjust(tp, current_link_up);
3419
Joe Perches63c3a662011-04-26 08:12:10 +00003420 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 /* Polled via timer. */
3422 tw32_f(MAC_EVENT, 0);
3423 } else {
3424 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3425 }
3426 udelay(40);
3427
3428 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3429 current_link_up == 1 &&
3430 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00003431 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 udelay(120);
3433 tw32_f(MAC_STATUS,
3434 (MAC_STATUS_SYNC_CHANGED |
3435 MAC_STATUS_CFG_CHANGED));
3436 udelay(40);
3437 tg3_write_mem(tp,
3438 NIC_SRAM_FIRMWARE_MBOX,
3439 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3440 }
3441
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003442 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00003443 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003444 u16 oldlnkctl, newlnkctl;
3445
3446 pci_read_config_word(tp->pdev,
3447 tp->pcie_cap + PCI_EXP_LNKCTL,
3448 &oldlnkctl);
3449 if (tp->link_config.active_speed == SPEED_100 ||
3450 tp->link_config.active_speed == SPEED_10)
3451 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3452 else
3453 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3454 if (newlnkctl != oldlnkctl)
3455 pci_write_config_word(tp->pdev,
3456 tp->pcie_cap + PCI_EXP_LNKCTL,
3457 newlnkctl);
3458 }
3459
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 if (current_link_up != netif_carrier_ok(tp->dev)) {
3461 if (current_link_up)
3462 netif_carrier_on(tp->dev);
3463 else
3464 netif_carrier_off(tp->dev);
3465 tg3_link_report(tp);
3466 }
3467
3468 return 0;
3469}
3470
3471struct tg3_fiber_aneginfo {
3472 int state;
3473#define ANEG_STATE_UNKNOWN 0
3474#define ANEG_STATE_AN_ENABLE 1
3475#define ANEG_STATE_RESTART_INIT 2
3476#define ANEG_STATE_RESTART 3
3477#define ANEG_STATE_DISABLE_LINK_OK 4
3478#define ANEG_STATE_ABILITY_DETECT_INIT 5
3479#define ANEG_STATE_ABILITY_DETECT 6
3480#define ANEG_STATE_ACK_DETECT_INIT 7
3481#define ANEG_STATE_ACK_DETECT 8
3482#define ANEG_STATE_COMPLETE_ACK_INIT 9
3483#define ANEG_STATE_COMPLETE_ACK 10
3484#define ANEG_STATE_IDLE_DETECT_INIT 11
3485#define ANEG_STATE_IDLE_DETECT 12
3486#define ANEG_STATE_LINK_OK 13
3487#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3488#define ANEG_STATE_NEXT_PAGE_WAIT 15
3489
3490 u32 flags;
3491#define MR_AN_ENABLE 0x00000001
3492#define MR_RESTART_AN 0x00000002
3493#define MR_AN_COMPLETE 0x00000004
3494#define MR_PAGE_RX 0x00000008
3495#define MR_NP_LOADED 0x00000010
3496#define MR_TOGGLE_TX 0x00000020
3497#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3498#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3499#define MR_LP_ADV_SYM_PAUSE 0x00000100
3500#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3501#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3502#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3503#define MR_LP_ADV_NEXT_PAGE 0x00001000
3504#define MR_TOGGLE_RX 0x00002000
3505#define MR_NP_RX 0x00004000
3506
3507#define MR_LINK_OK 0x80000000
3508
3509 unsigned long link_time, cur_time;
3510
3511 u32 ability_match_cfg;
3512 int ability_match_count;
3513
3514 char ability_match, idle_match, ack_match;
3515
3516 u32 txconfig, rxconfig;
3517#define ANEG_CFG_NP 0x00000080
3518#define ANEG_CFG_ACK 0x00000040
3519#define ANEG_CFG_RF2 0x00000020
3520#define ANEG_CFG_RF1 0x00000010
3521#define ANEG_CFG_PS2 0x00000001
3522#define ANEG_CFG_PS1 0x00008000
3523#define ANEG_CFG_HD 0x00004000
3524#define ANEG_CFG_FD 0x00002000
3525#define ANEG_CFG_INVAL 0x00001f06
3526
3527};
3528#define ANEG_OK 0
3529#define ANEG_DONE 1
3530#define ANEG_TIMER_ENAB 2
3531#define ANEG_FAILED -1
3532
3533#define ANEG_STATE_SETTLE_TIME 10000
3534
3535static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3536 struct tg3_fiber_aneginfo *ap)
3537{
Matt Carlson5be73b42007-12-20 20:09:29 -08003538 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 unsigned long delta;
3540 u32 rx_cfg_reg;
3541 int ret;
3542
3543 if (ap->state == ANEG_STATE_UNKNOWN) {
3544 ap->rxconfig = 0;
3545 ap->link_time = 0;
3546 ap->cur_time = 0;
3547 ap->ability_match_cfg = 0;
3548 ap->ability_match_count = 0;
3549 ap->ability_match = 0;
3550 ap->idle_match = 0;
3551 ap->ack_match = 0;
3552 }
3553 ap->cur_time++;
3554
3555 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3556 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3557
3558 if (rx_cfg_reg != ap->ability_match_cfg) {
3559 ap->ability_match_cfg = rx_cfg_reg;
3560 ap->ability_match = 0;
3561 ap->ability_match_count = 0;
3562 } else {
3563 if (++ap->ability_match_count > 1) {
3564 ap->ability_match = 1;
3565 ap->ability_match_cfg = rx_cfg_reg;
3566 }
3567 }
3568 if (rx_cfg_reg & ANEG_CFG_ACK)
3569 ap->ack_match = 1;
3570 else
3571 ap->ack_match = 0;
3572
3573 ap->idle_match = 0;
3574 } else {
3575 ap->idle_match = 1;
3576 ap->ability_match_cfg = 0;
3577 ap->ability_match_count = 0;
3578 ap->ability_match = 0;
3579 ap->ack_match = 0;
3580
3581 rx_cfg_reg = 0;
3582 }
3583
3584 ap->rxconfig = rx_cfg_reg;
3585 ret = ANEG_OK;
3586
Matt Carlson33f401a2010-04-05 10:19:27 +00003587 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 case ANEG_STATE_UNKNOWN:
3589 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3590 ap->state = ANEG_STATE_AN_ENABLE;
3591
3592 /* fallthru */
3593 case ANEG_STATE_AN_ENABLE:
3594 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3595 if (ap->flags & MR_AN_ENABLE) {
3596 ap->link_time = 0;
3597 ap->cur_time = 0;
3598 ap->ability_match_cfg = 0;
3599 ap->ability_match_count = 0;
3600 ap->ability_match = 0;
3601 ap->idle_match = 0;
3602 ap->ack_match = 0;
3603
3604 ap->state = ANEG_STATE_RESTART_INIT;
3605 } else {
3606 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3607 }
3608 break;
3609
3610 case ANEG_STATE_RESTART_INIT:
3611 ap->link_time = ap->cur_time;
3612 ap->flags &= ~(MR_NP_LOADED);
3613 ap->txconfig = 0;
3614 tw32(MAC_TX_AUTO_NEG, 0);
3615 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3616 tw32_f(MAC_MODE, tp->mac_mode);
3617 udelay(40);
3618
3619 ret = ANEG_TIMER_ENAB;
3620 ap->state = ANEG_STATE_RESTART;
3621
3622 /* fallthru */
3623 case ANEG_STATE_RESTART:
3624 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00003625 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00003627 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 break;
3630
3631 case ANEG_STATE_DISABLE_LINK_OK:
3632 ret = ANEG_DONE;
3633 break;
3634
3635 case ANEG_STATE_ABILITY_DETECT_INIT:
3636 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003637 ap->txconfig = ANEG_CFG_FD;
3638 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3639 if (flowctrl & ADVERTISE_1000XPAUSE)
3640 ap->txconfig |= ANEG_CFG_PS1;
3641 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3642 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3644 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3645 tw32_f(MAC_MODE, tp->mac_mode);
3646 udelay(40);
3647
3648 ap->state = ANEG_STATE_ABILITY_DETECT;
3649 break;
3650
3651 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00003652 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 break;
3655
3656 case ANEG_STATE_ACK_DETECT_INIT:
3657 ap->txconfig |= ANEG_CFG_ACK;
3658 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3659 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3660 tw32_f(MAC_MODE, tp->mac_mode);
3661 udelay(40);
3662
3663 ap->state = ANEG_STATE_ACK_DETECT;
3664
3665 /* fallthru */
3666 case ANEG_STATE_ACK_DETECT:
3667 if (ap->ack_match != 0) {
3668 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3669 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3670 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3671 } else {
3672 ap->state = ANEG_STATE_AN_ENABLE;
3673 }
3674 } else if (ap->ability_match != 0 &&
3675 ap->rxconfig == 0) {
3676 ap->state = ANEG_STATE_AN_ENABLE;
3677 }
3678 break;
3679
3680 case ANEG_STATE_COMPLETE_ACK_INIT:
3681 if (ap->rxconfig & ANEG_CFG_INVAL) {
3682 ret = ANEG_FAILED;
3683 break;
3684 }
3685 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3686 MR_LP_ADV_HALF_DUPLEX |
3687 MR_LP_ADV_SYM_PAUSE |
3688 MR_LP_ADV_ASYM_PAUSE |
3689 MR_LP_ADV_REMOTE_FAULT1 |
3690 MR_LP_ADV_REMOTE_FAULT2 |
3691 MR_LP_ADV_NEXT_PAGE |
3692 MR_TOGGLE_RX |
3693 MR_NP_RX);
3694 if (ap->rxconfig & ANEG_CFG_FD)
3695 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3696 if (ap->rxconfig & ANEG_CFG_HD)
3697 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3698 if (ap->rxconfig & ANEG_CFG_PS1)
3699 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3700 if (ap->rxconfig & ANEG_CFG_PS2)
3701 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3702 if (ap->rxconfig & ANEG_CFG_RF1)
3703 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3704 if (ap->rxconfig & ANEG_CFG_RF2)
3705 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3706 if (ap->rxconfig & ANEG_CFG_NP)
3707 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3708
3709 ap->link_time = ap->cur_time;
3710
3711 ap->flags ^= (MR_TOGGLE_TX);
3712 if (ap->rxconfig & 0x0008)
3713 ap->flags |= MR_TOGGLE_RX;
3714 if (ap->rxconfig & ANEG_CFG_NP)
3715 ap->flags |= MR_NP_RX;
3716 ap->flags |= MR_PAGE_RX;
3717
3718 ap->state = ANEG_STATE_COMPLETE_ACK;
3719 ret = ANEG_TIMER_ENAB;
3720 break;
3721
3722 case ANEG_STATE_COMPLETE_ACK:
3723 if (ap->ability_match != 0 &&
3724 ap->rxconfig == 0) {
3725 ap->state = ANEG_STATE_AN_ENABLE;
3726 break;
3727 }
3728 delta = ap->cur_time - ap->link_time;
3729 if (delta > ANEG_STATE_SETTLE_TIME) {
3730 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3731 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3732 } else {
3733 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3734 !(ap->flags & MR_NP_RX)) {
3735 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3736 } else {
3737 ret = ANEG_FAILED;
3738 }
3739 }
3740 }
3741 break;
3742
3743 case ANEG_STATE_IDLE_DETECT_INIT:
3744 ap->link_time = ap->cur_time;
3745 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3746 tw32_f(MAC_MODE, tp->mac_mode);
3747 udelay(40);
3748
3749 ap->state = ANEG_STATE_IDLE_DETECT;
3750 ret = ANEG_TIMER_ENAB;
3751 break;
3752
3753 case ANEG_STATE_IDLE_DETECT:
3754 if (ap->ability_match != 0 &&
3755 ap->rxconfig == 0) {
3756 ap->state = ANEG_STATE_AN_ENABLE;
3757 break;
3758 }
3759 delta = ap->cur_time - ap->link_time;
3760 if (delta > ANEG_STATE_SETTLE_TIME) {
3761 /* XXX another gem from the Broadcom driver :( */
3762 ap->state = ANEG_STATE_LINK_OK;
3763 }
3764 break;
3765
3766 case ANEG_STATE_LINK_OK:
3767 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3768 ret = ANEG_DONE;
3769 break;
3770
3771 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3772 /* ??? unimplemented */
3773 break;
3774
3775 case ANEG_STATE_NEXT_PAGE_WAIT:
3776 /* ??? unimplemented */
3777 break;
3778
3779 default:
3780 ret = ANEG_FAILED;
3781 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783
3784 return ret;
3785}
3786
Matt Carlson5be73b42007-12-20 20:09:29 -08003787static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788{
3789 int res = 0;
3790 struct tg3_fiber_aneginfo aninfo;
3791 int status = ANEG_FAILED;
3792 unsigned int tick;
3793 u32 tmp;
3794
3795 tw32_f(MAC_TX_AUTO_NEG, 0);
3796
3797 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3798 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3799 udelay(40);
3800
3801 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3802 udelay(40);
3803
3804 memset(&aninfo, 0, sizeof(aninfo));
3805 aninfo.flags |= MR_AN_ENABLE;
3806 aninfo.state = ANEG_STATE_UNKNOWN;
3807 aninfo.cur_time = 0;
3808 tick = 0;
3809 while (++tick < 195000) {
3810 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3811 if (status == ANEG_DONE || status == ANEG_FAILED)
3812 break;
3813
3814 udelay(1);
3815 }
3816
3817 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3818 tw32_f(MAC_MODE, tp->mac_mode);
3819 udelay(40);
3820
Matt Carlson5be73b42007-12-20 20:09:29 -08003821 *txflags = aninfo.txconfig;
3822 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823
3824 if (status == ANEG_DONE &&
3825 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3826 MR_LP_ADV_FULL_DUPLEX)))
3827 res = 1;
3828
3829 return res;
3830}
3831
3832static void tg3_init_bcm8002(struct tg3 *tp)
3833{
3834 u32 mac_status = tr32(MAC_STATUS);
3835 int i;
3836
3837 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00003838 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 !(mac_status & MAC_STATUS_PCS_SYNCED))
3840 return;
3841
3842 /* Set PLL lock range. */
3843 tg3_writephy(tp, 0x16, 0x8007);
3844
3845 /* SW reset */
3846 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
3847
3848 /* Wait for reset to complete. */
3849 /* XXX schedule_timeout() ... */
3850 for (i = 0; i < 500; i++)
3851 udelay(10);
3852
3853 /* Config mode; select PMA/Ch 1 regs. */
3854 tg3_writephy(tp, 0x10, 0x8411);
3855
3856 /* Enable auto-lock and comdet, select txclk for tx. */
3857 tg3_writephy(tp, 0x11, 0x0a10);
3858
3859 tg3_writephy(tp, 0x18, 0x00a0);
3860 tg3_writephy(tp, 0x16, 0x41ff);
3861
3862 /* Assert and deassert POR. */
3863 tg3_writephy(tp, 0x13, 0x0400);
3864 udelay(40);
3865 tg3_writephy(tp, 0x13, 0x0000);
3866
3867 tg3_writephy(tp, 0x11, 0x0a50);
3868 udelay(40);
3869 tg3_writephy(tp, 0x11, 0x0a10);
3870
3871 /* Wait for signal to stabilize */
3872 /* XXX schedule_timeout() ... */
3873 for (i = 0; i < 15000; i++)
3874 udelay(10);
3875
3876 /* Deselect the channel register so we can read the PHYID
3877 * later.
3878 */
3879 tg3_writephy(tp, 0x10, 0x8011);
3880}
3881
3882static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
3883{
Matt Carlson82cd3d12007-12-20 20:09:00 -08003884 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 u32 sg_dig_ctrl, sg_dig_status;
3886 u32 serdes_cfg, expected_sg_dig_ctrl;
3887 int workaround, port_a;
3888 int current_link_up;
3889
3890 serdes_cfg = 0;
3891 expected_sg_dig_ctrl = 0;
3892 workaround = 0;
3893 port_a = 1;
3894 current_link_up = 0;
3895
3896 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
3897 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
3898 workaround = 1;
3899 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
3900 port_a = 0;
3901
3902 /* preserve bits 0-11,13,14 for signal pre-emphasis */
3903 /* preserve bits 20-23 for voltage regulator */
3904 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
3905 }
3906
3907 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3908
3909 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003910 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 if (workaround) {
3912 u32 val = serdes_cfg;
3913
3914 if (port_a)
3915 val |= 0xc010000;
3916 else
3917 val |= 0x4010000;
3918 tw32_f(MAC_SERDES_CFG, val);
3919 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003920
3921 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 }
3923 if (mac_status & MAC_STATUS_PCS_SYNCED) {
3924 tg3_setup_flow_control(tp, 0, 0);
3925 current_link_up = 1;
3926 }
3927 goto out;
3928 }
3929
3930 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003931 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932
Matt Carlson82cd3d12007-12-20 20:09:00 -08003933 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3934 if (flowctrl & ADVERTISE_1000XPAUSE)
3935 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
3936 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3937 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938
3939 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003940 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07003941 tp->serdes_counter &&
3942 ((mac_status & (MAC_STATUS_PCS_SYNCED |
3943 MAC_STATUS_RCVD_CFG)) ==
3944 MAC_STATUS_PCS_SYNCED)) {
3945 tp->serdes_counter--;
3946 current_link_up = 1;
3947 goto out;
3948 }
3949restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 if (workaround)
3951 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003952 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 udelay(5);
3954 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
3955
Michael Chan3d3ebe72006-09-27 15:59:15 -07003956 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003957 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
3959 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003960 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 mac_status = tr32(MAC_STATUS);
3962
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003963 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08003965 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966
Matt Carlson82cd3d12007-12-20 20:09:00 -08003967 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
3968 local_adv |= ADVERTISE_1000XPAUSE;
3969 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
3970 local_adv |= ADVERTISE_1000XPSE_ASYM;
3971
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003972 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003973 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003974 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003975 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976
3977 tg3_setup_flow_control(tp, local_adv, remote_adv);
3978 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003979 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003980 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003981 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003982 if (tp->serdes_counter)
3983 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 else {
3985 if (workaround) {
3986 u32 val = serdes_cfg;
3987
3988 if (port_a)
3989 val |= 0xc010000;
3990 else
3991 val |= 0x4010000;
3992
3993 tw32_f(MAC_SERDES_CFG, val);
3994 }
3995
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003996 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 udelay(40);
3998
3999 /* Link parallel detection - link is up */
4000 /* only if we have PCS_SYNC and not */
4001 /* receiving config code words */
4002 mac_status = tr32(MAC_STATUS);
4003 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
4004 !(mac_status & MAC_STATUS_RCVD_CFG)) {
4005 tg3_setup_flow_control(tp, 0, 0);
4006 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004007 tp->phy_flags |=
4008 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004009 tp->serdes_counter =
4010 SERDES_PARALLEL_DET_TIMEOUT;
4011 } else
4012 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 }
4014 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07004015 } else {
4016 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004017 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 }
4019
4020out:
4021 return current_link_up;
4022}
4023
4024static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
4025{
4026 int current_link_up = 0;
4027
Michael Chan5cf64b8a2007-05-05 12:11:21 -07004028 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030
4031 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08004032 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004034
Matt Carlson5be73b42007-12-20 20:09:29 -08004035 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4036 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037
Matt Carlson5be73b42007-12-20 20:09:29 -08004038 if (txflags & ANEG_CFG_PS1)
4039 local_adv |= ADVERTISE_1000XPAUSE;
4040 if (txflags & ANEG_CFG_PS2)
4041 local_adv |= ADVERTISE_1000XPSE_ASYM;
4042
4043 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4044 remote_adv |= LPA_1000XPAUSE;
4045 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4046 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047
4048 tg3_setup_flow_control(tp, local_adv, remote_adv);
4049
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 current_link_up = 1;
4051 }
4052 for (i = 0; i < 30; i++) {
4053 udelay(20);
4054 tw32_f(MAC_STATUS,
4055 (MAC_STATUS_SYNC_CHANGED |
4056 MAC_STATUS_CFG_CHANGED));
4057 udelay(40);
4058 if ((tr32(MAC_STATUS) &
4059 (MAC_STATUS_SYNC_CHANGED |
4060 MAC_STATUS_CFG_CHANGED)) == 0)
4061 break;
4062 }
4063
4064 mac_status = tr32(MAC_STATUS);
4065 if (current_link_up == 0 &&
4066 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4067 !(mac_status & MAC_STATUS_RCVD_CFG))
4068 current_link_up = 1;
4069 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004070 tg3_setup_flow_control(tp, 0, 0);
4071
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 /* Forcing 1000FD link up. */
4073 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074
4075 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4076 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004077
4078 tw32_f(MAC_MODE, tp->mac_mode);
4079 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 }
4081
4082out:
4083 return current_link_up;
4084}
4085
4086static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4087{
4088 u32 orig_pause_cfg;
4089 u16 orig_active_speed;
4090 u8 orig_active_duplex;
4091 u32 mac_status;
4092 int current_link_up;
4093 int i;
4094
Matt Carlson8d018622007-12-20 20:05:44 -08004095 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 orig_active_speed = tp->link_config.active_speed;
4097 orig_active_duplex = tp->link_config.active_duplex;
4098
Joe Perches63c3a662011-04-26 08:12:10 +00004099 if (!tg3_flag(tp, HW_AUTONEG) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 netif_carrier_ok(tp->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004101 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 mac_status = tr32(MAC_STATUS);
4103 mac_status &= (MAC_STATUS_PCS_SYNCED |
4104 MAC_STATUS_SIGNAL_DET |
4105 MAC_STATUS_CFG_CHANGED |
4106 MAC_STATUS_RCVD_CFG);
4107 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4108 MAC_STATUS_SIGNAL_DET)) {
4109 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4110 MAC_STATUS_CFG_CHANGED));
4111 return 0;
4112 }
4113 }
4114
4115 tw32_f(MAC_TX_AUTO_NEG, 0);
4116
4117 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4118 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
4119 tw32_f(MAC_MODE, tp->mac_mode);
4120 udelay(40);
4121
Matt Carlson79eb6902010-02-17 15:17:03 +00004122 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 tg3_init_bcm8002(tp);
4124
4125 /* Enable link change event even when serdes polling. */
4126 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4127 udelay(40);
4128
4129 current_link_up = 0;
4130 mac_status = tr32(MAC_STATUS);
4131
Joe Perches63c3a662011-04-26 08:12:10 +00004132 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4134 else
4135 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4136
Matt Carlson898a56f2009-08-28 14:02:40 +00004137 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00004139 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140
4141 for (i = 0; i < 100; i++) {
4142 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4143 MAC_STATUS_CFG_CHANGED));
4144 udelay(5);
4145 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07004146 MAC_STATUS_CFG_CHANGED |
4147 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 break;
4149 }
4150
4151 mac_status = tr32(MAC_STATUS);
4152 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4153 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004154 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4155 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 tw32_f(MAC_MODE, (tp->mac_mode |
4157 MAC_MODE_SEND_CONFIGS));
4158 udelay(1);
4159 tw32_f(MAC_MODE, tp->mac_mode);
4160 }
4161 }
4162
4163 if (current_link_up == 1) {
4164 tp->link_config.active_speed = SPEED_1000;
4165 tp->link_config.active_duplex = DUPLEX_FULL;
4166 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4167 LED_CTRL_LNKLED_OVERRIDE |
4168 LED_CTRL_1000MBPS_ON));
4169 } else {
4170 tp->link_config.active_speed = SPEED_INVALID;
4171 tp->link_config.active_duplex = DUPLEX_INVALID;
4172 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4173 LED_CTRL_LNKLED_OVERRIDE |
4174 LED_CTRL_TRAFFIC_OVERRIDE));
4175 }
4176
4177 if (current_link_up != netif_carrier_ok(tp->dev)) {
4178 if (current_link_up)
4179 netif_carrier_on(tp->dev);
4180 else
4181 netif_carrier_off(tp->dev);
4182 tg3_link_report(tp);
4183 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004184 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 if (orig_pause_cfg != now_pause_cfg ||
4186 orig_active_speed != tp->link_config.active_speed ||
4187 orig_active_duplex != tp->link_config.active_duplex)
4188 tg3_link_report(tp);
4189 }
4190
4191 return 0;
4192}
4193
Michael Chan747e8f82005-07-25 12:33:22 -07004194static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4195{
4196 int current_link_up, err = 0;
4197 u32 bmsr, bmcr;
4198 u16 current_speed;
4199 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004200 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004201
4202 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4203 tw32_f(MAC_MODE, tp->mac_mode);
4204 udelay(40);
4205
4206 tw32(MAC_EVENT, 0);
4207
4208 tw32_f(MAC_STATUS,
4209 (MAC_STATUS_SYNC_CHANGED |
4210 MAC_STATUS_CFG_CHANGED |
4211 MAC_STATUS_MI_COMPLETION |
4212 MAC_STATUS_LNKSTATE_CHANGED));
4213 udelay(40);
4214
4215 if (force_reset)
4216 tg3_phy_reset(tp);
4217
4218 current_link_up = 0;
4219 current_speed = SPEED_INVALID;
4220 current_duplex = DUPLEX_INVALID;
4221
4222 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4223 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004224 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4225 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4226 bmsr |= BMSR_LSTATUS;
4227 else
4228 bmsr &= ~BMSR_LSTATUS;
4229 }
Michael Chan747e8f82005-07-25 12:33:22 -07004230
4231 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4232
4233 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004234 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004235 /* do nothing, just check for link up at the end */
4236 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4237 u32 adv, new_adv;
4238
4239 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4240 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4241 ADVERTISE_1000XPAUSE |
4242 ADVERTISE_1000XPSE_ASYM |
4243 ADVERTISE_SLCT);
4244
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004245 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004246
4247 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4248 new_adv |= ADVERTISE_1000XHALF;
4249 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4250 new_adv |= ADVERTISE_1000XFULL;
4251
4252 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4253 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4254 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4255 tg3_writephy(tp, MII_BMCR, bmcr);
4256
4257 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004258 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004259 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004260
4261 return err;
4262 }
4263 } else {
4264 u32 new_bmcr;
4265
4266 bmcr &= ~BMCR_SPEED1000;
4267 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4268
4269 if (tp->link_config.duplex == DUPLEX_FULL)
4270 new_bmcr |= BMCR_FULLDPLX;
4271
4272 if (new_bmcr != bmcr) {
4273 /* BMCR_SPEED1000 is a reserved bit that needs
4274 * to be set on write.
4275 */
4276 new_bmcr |= BMCR_SPEED1000;
4277
4278 /* Force a linkdown */
4279 if (netif_carrier_ok(tp->dev)) {
4280 u32 adv;
4281
4282 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4283 adv &= ~(ADVERTISE_1000XFULL |
4284 ADVERTISE_1000XHALF |
4285 ADVERTISE_SLCT);
4286 tg3_writephy(tp, MII_ADVERTISE, adv);
4287 tg3_writephy(tp, MII_BMCR, bmcr |
4288 BMCR_ANRESTART |
4289 BMCR_ANENABLE);
4290 udelay(10);
4291 netif_carrier_off(tp->dev);
4292 }
4293 tg3_writephy(tp, MII_BMCR, new_bmcr);
4294 bmcr = new_bmcr;
4295 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4296 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004297 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4298 ASIC_REV_5714) {
4299 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4300 bmsr |= BMSR_LSTATUS;
4301 else
4302 bmsr &= ~BMSR_LSTATUS;
4303 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004304 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004305 }
4306 }
4307
4308 if (bmsr & BMSR_LSTATUS) {
4309 current_speed = SPEED_1000;
4310 current_link_up = 1;
4311 if (bmcr & BMCR_FULLDPLX)
4312 current_duplex = DUPLEX_FULL;
4313 else
4314 current_duplex = DUPLEX_HALF;
4315
Matt Carlsonef167e22007-12-20 20:10:01 -08004316 local_adv = 0;
4317 remote_adv = 0;
4318
Michael Chan747e8f82005-07-25 12:33:22 -07004319 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004320 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004321
4322 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4323 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4324 common = local_adv & remote_adv;
4325 if (common & (ADVERTISE_1000XHALF |
4326 ADVERTISE_1000XFULL)) {
4327 if (common & ADVERTISE_1000XFULL)
4328 current_duplex = DUPLEX_FULL;
4329 else
4330 current_duplex = DUPLEX_HALF;
Joe Perches63c3a662011-04-26 08:12:10 +00004331 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00004332 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00004333 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07004334 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00004335 }
Michael Chan747e8f82005-07-25 12:33:22 -07004336 }
4337 }
4338
Matt Carlsonef167e22007-12-20 20:10:01 -08004339 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4340 tg3_setup_flow_control(tp, local_adv, remote_adv);
4341
Michael Chan747e8f82005-07-25 12:33:22 -07004342 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4343 if (tp->link_config.active_duplex == DUPLEX_HALF)
4344 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4345
4346 tw32_f(MAC_MODE, tp->mac_mode);
4347 udelay(40);
4348
4349 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4350
4351 tp->link_config.active_speed = current_speed;
4352 tp->link_config.active_duplex = current_duplex;
4353
4354 if (current_link_up != netif_carrier_ok(tp->dev)) {
4355 if (current_link_up)
4356 netif_carrier_on(tp->dev);
4357 else {
4358 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004359 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004360 }
4361 tg3_link_report(tp);
4362 }
4363 return err;
4364}
4365
4366static void tg3_serdes_parallel_detect(struct tg3 *tp)
4367{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004368 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004369 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004370 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004371 return;
4372 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004373
Michael Chan747e8f82005-07-25 12:33:22 -07004374 if (!netif_carrier_ok(tp->dev) &&
4375 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4376 u32 bmcr;
4377
4378 tg3_readphy(tp, MII_BMCR, &bmcr);
4379 if (bmcr & BMCR_ANENABLE) {
4380 u32 phy1, phy2;
4381
4382 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004383 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
4384 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07004385
4386 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004387 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4388 MII_TG3_DSP_EXP1_INT_STAT);
4389 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
4390 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004391
4392 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4393 /* We have signal detect and not receiving
4394 * config code words, link is up by parallel
4395 * detection.
4396 */
4397
4398 bmcr &= ~BMCR_ANENABLE;
4399 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4400 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004401 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004402 }
4403 }
Matt Carlson859a588792010-04-05 10:19:28 +00004404 } else if (netif_carrier_ok(tp->dev) &&
4405 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004406 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004407 u32 phy2;
4408
4409 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004410 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4411 MII_TG3_DSP_EXP1_INT_STAT);
4412 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004413 if (phy2 & 0x20) {
4414 u32 bmcr;
4415
4416 /* Config code words received, turn on autoneg. */
4417 tg3_readphy(tp, MII_BMCR, &bmcr);
4418 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4419
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004420 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004421
4422 }
4423 }
4424}
4425
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4427{
Matt Carlsonf2096f92011-04-05 14:22:48 +00004428 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 int err;
4430
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004431 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004433 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07004434 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00004435 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437
Matt Carlsonbcb37f62008-11-03 16:52:09 -08004438 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00004439 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08004440
4441 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4442 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4443 scale = 65;
4444 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4445 scale = 6;
4446 else
4447 scale = 12;
4448
4449 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4450 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4451 tw32(GRC_MISC_CFG, val);
4452 }
4453
Matt Carlsonf2096f92011-04-05 14:22:48 +00004454 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4455 (6 << TX_LENGTHS_IPG_SHIFT);
4456 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
4457 val |= tr32(MAC_TX_LENGTHS) &
4458 (TX_LENGTHS_JMB_FRM_LEN_MSK |
4459 TX_LENGTHS_CNT_DWN_VAL_MSK);
4460
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 if (tp->link_config.active_speed == SPEED_1000 &&
4462 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00004463 tw32(MAC_TX_LENGTHS, val |
4464 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00004466 tw32(MAC_TX_LENGTHS, val |
4467 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468
Joe Perches63c3a662011-04-26 08:12:10 +00004469 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 if (netif_carrier_ok(tp->dev)) {
4471 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07004472 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 } else {
4474 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4475 }
4476 }
4477
Joe Perches63c3a662011-04-26 08:12:10 +00004478 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00004479 val = tr32(PCIE_PWR_MGMT_THRESH);
Matt Carlson8ed5d972007-05-07 00:25:49 -07004480 if (!netif_carrier_ok(tp->dev))
4481 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4482 tp->pwrmgmt_thresh;
4483 else
4484 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4485 tw32(PCIE_PWR_MGMT_THRESH, val);
4486 }
4487
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 return err;
4489}
4490
Matt Carlson66cfd1b2010-09-30 10:34:30 +00004491static inline int tg3_irq_sync(struct tg3 *tp)
4492{
4493 return tp->irq_sync;
4494}
4495
Matt Carlson97bd8e42011-04-13 11:05:04 +00004496static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
4497{
4498 int i;
4499
4500 dst = (u32 *)((u8 *)dst + off);
4501 for (i = 0; i < len; i += sizeof(u32))
4502 *dst++ = tr32(off + i);
4503}
4504
4505static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
4506{
4507 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
4508 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
4509 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
4510 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
4511 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
4512 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
4513 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
4514 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
4515 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
4516 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
4517 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
4518 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
4519 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
4520 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
4521 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
4522 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
4523 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
4524 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
4525 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
4526
Joe Perches63c3a662011-04-26 08:12:10 +00004527 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00004528 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
4529
4530 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
4531 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
4532 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
4533 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
4534 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
4535 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
4536 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
4537 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
4538
Joe Perches63c3a662011-04-26 08:12:10 +00004539 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00004540 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
4541 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
4542 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
4543 }
4544
4545 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
4546 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
4547 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
4548 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
4549 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
4550
Joe Perches63c3a662011-04-26 08:12:10 +00004551 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00004552 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
4553}
4554
4555static void tg3_dump_state(struct tg3 *tp)
4556{
4557 int i;
4558 u32 *regs;
4559
4560 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
4561 if (!regs) {
4562 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
4563 return;
4564 }
4565
Joe Perches63c3a662011-04-26 08:12:10 +00004566 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00004567 /* Read up to but not including private PCI registers */
4568 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
4569 regs[i / sizeof(u32)] = tr32(i);
4570 } else
4571 tg3_dump_legacy_regs(tp, regs);
4572
4573 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
4574 if (!regs[i + 0] && !regs[i + 1] &&
4575 !regs[i + 2] && !regs[i + 3])
4576 continue;
4577
4578 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
4579 i * 4,
4580 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
4581 }
4582
4583 kfree(regs);
4584
4585 for (i = 0; i < tp->irq_cnt; i++) {
4586 struct tg3_napi *tnapi = &tp->napi[i];
4587
4588 /* SW status block */
4589 netdev_err(tp->dev,
4590 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
4591 i,
4592 tnapi->hw_status->status,
4593 tnapi->hw_status->status_tag,
4594 tnapi->hw_status->rx_jumbo_consumer,
4595 tnapi->hw_status->rx_consumer,
4596 tnapi->hw_status->rx_mini_consumer,
4597 tnapi->hw_status->idx[0].rx_producer,
4598 tnapi->hw_status->idx[0].tx_consumer);
4599
4600 netdev_err(tp->dev,
4601 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
4602 i,
4603 tnapi->last_tag, tnapi->last_irq_tag,
4604 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
4605 tnapi->rx_rcb_ptr,
4606 tnapi->prodring.rx_std_prod_idx,
4607 tnapi->prodring.rx_std_cons_idx,
4608 tnapi->prodring.rx_jmb_prod_idx,
4609 tnapi->prodring.rx_jmb_cons_idx);
4610 }
4611}
4612
Michael Chandf3e6542006-05-26 17:48:07 -07004613/* This is called whenever we suspect that the system chipset is re-
4614 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4615 * is bogus tx completions. We try to recover by setting the
4616 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4617 * in the workqueue.
4618 */
4619static void tg3_tx_recover(struct tg3 *tp)
4620{
Joe Perches63c3a662011-04-26 08:12:10 +00004621 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07004622 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4623
Matt Carlson5129c3a2010-04-05 10:19:23 +00004624 netdev_warn(tp->dev,
4625 "The system may be re-ordering memory-mapped I/O "
4626 "cycles to the network device, attempting to recover. "
4627 "Please report the problem to the driver maintainer "
4628 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07004629
4630 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00004631 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07004632 spin_unlock(&tp->lock);
4633}
4634
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004635static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07004636{
Matt Carlsonf65aac12010-08-02 11:26:03 +00004637 /* Tell compiler to fetch tx indices from memory. */
4638 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004639 return tnapi->tx_pending -
4640 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07004641}
4642
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643/* Tigon3 never reports partial packet sends. So we do not
4644 * need special logic to handle SKBs that have not had all
4645 * of their frags sent yet, like SunGEM does.
4646 */
Matt Carlson17375d22009-08-28 14:02:18 +00004647static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648{
Matt Carlson17375d22009-08-28 14:02:18 +00004649 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004650 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004651 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004652 struct netdev_queue *txq;
4653 int index = tnapi - tp->napi;
4654
Joe Perches63c3a662011-04-26 08:12:10 +00004655 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004656 index--;
4657
4658 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659
4660 while (sw_idx != hw_idx) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00004661 struct ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004663 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664
Michael Chandf3e6542006-05-26 17:48:07 -07004665 if (unlikely(skb == NULL)) {
4666 tg3_tx_recover(tp);
4667 return;
4668 }
4669
Alexander Duyckf4188d82009-12-02 16:48:38 +00004670 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004671 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004672 skb_headlen(skb),
4673 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674
4675 ri->skb = NULL;
4676
4677 sw_idx = NEXT_TX(sw_idx);
4678
4679 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004680 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004681 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4682 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00004683
4684 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004685 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004686 skb_shinfo(skb)->frags[i].size,
4687 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 sw_idx = NEXT_TX(sw_idx);
4689 }
4690
David S. Millerf47c11e2005-06-24 20:18:35 -07004691 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004692
4693 if (unlikely(tx_bug)) {
4694 tg3_tx_recover(tp);
4695 return;
4696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 }
4698
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004699 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700
Michael Chan1b2a7202006-08-07 21:46:02 -07004701 /* Need to make the tx_cons update visible to tg3_start_xmit()
4702 * before checking for netif_queue_stopped(). Without the
4703 * memory barrier, there is a small possibility that tg3_start_xmit()
4704 * will miss it and cause the queue to be stopped forever.
4705 */
4706 smp_mb();
4707
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004708 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004709 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004710 __netif_tx_lock(txq, smp_processor_id());
4711 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004712 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004713 netif_tx_wake_queue(txq);
4714 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07004715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716}
4717
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004718static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
4719{
4720 if (!ri->skb)
4721 return;
4722
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004723 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004724 map_sz, PCI_DMA_FROMDEVICE);
4725 dev_kfree_skb_any(ri->skb);
4726 ri->skb = NULL;
4727}
4728
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729/* Returns size of skb allocated or < 0 on error.
4730 *
4731 * We only need to fill in the address because the other members
4732 * of the RX descriptor are invariant, see tg3_init_rings.
4733 *
4734 * Note the purposeful assymetry of cpu vs. chip accesses. For
4735 * posting buffers we only dirty the first cache line of the RX
4736 * descriptor (containing the address). Whereas for the RX status
4737 * buffers the cpu only reads the last cacheline of the RX descriptor
4738 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4739 */
Matt Carlson86b21e52009-11-13 13:03:45 +00004740static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00004741 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742{
4743 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00004744 struct ring_info *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 struct sk_buff *skb;
4746 dma_addr_t mapping;
4747 int skb_size, dest_idx;
4748
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 switch (opaque_key) {
4750 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004751 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00004752 desc = &tpr->rx_std[dest_idx];
4753 map = &tpr->rx_std_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004754 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 break;
4756
4757 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004758 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004759 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004760 map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004761 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 break;
4763
4764 default:
4765 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767
4768 /* Do not overwrite any of the map or rp information
4769 * until we are sure we can commit to a new buffer.
4770 *
4771 * Callers depend upon this behavior and assume that
4772 * we leave everything unchanged if we fail.
4773 */
Matt Carlson287be122009-08-28 13:58:46 +00004774 skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 if (skb == NULL)
4776 return -ENOMEM;
4777
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 skb_reserve(skb, tp->rx_offset);
4779
Matt Carlson287be122009-08-28 13:58:46 +00004780 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00004782 if (pci_dma_mapping_error(tp->pdev, mapping)) {
4783 dev_kfree_skb(skb);
4784 return -EIO;
4785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786
4787 map->skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004788 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 desc->addr_hi = ((u64)mapping >> 32);
4791 desc->addr_lo = ((u64)mapping & 0xffffffff);
4792
4793 return skb_size;
4794}
4795
4796/* We only need to move over in the address because the other
4797 * members of the RX descriptor are invariant. See notes above
4798 * tg3_alloc_rx_skb for full details.
4799 */
Matt Carlsona3896162009-11-13 13:03:44 +00004800static void tg3_recycle_rx(struct tg3_napi *tnapi,
4801 struct tg3_rx_prodring_set *dpr,
4802 u32 opaque_key, int src_idx,
4803 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804{
Matt Carlson17375d22009-08-28 14:02:18 +00004805 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4807 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004808 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004809 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810
4811 switch (opaque_key) {
4812 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004813 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004814 dest_desc = &dpr->rx_std[dest_idx];
4815 dest_map = &dpr->rx_std_buffers[dest_idx];
4816 src_desc = &spr->rx_std[src_idx];
4817 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 break;
4819
4820 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004821 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004822 dest_desc = &dpr->rx_jmb[dest_idx].std;
4823 dest_map = &dpr->rx_jmb_buffers[dest_idx];
4824 src_desc = &spr->rx_jmb[src_idx].std;
4825 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 break;
4827
4828 default:
4829 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831
4832 dest_map->skb = src_map->skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004833 dma_unmap_addr_set(dest_map, mapping,
4834 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 dest_desc->addr_hi = src_desc->addr_hi;
4836 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00004837
4838 /* Ensure that the update to the skb happens after the physical
4839 * addresses have been transferred to the new BD location.
4840 */
4841 smp_wmb();
4842
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 src_map->skb = NULL;
4844}
4845
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846/* The RX ring scheme is composed of multiple rings which post fresh
4847 * buffers to the chip, and one special ring the chip uses to report
4848 * status back to the host.
4849 *
4850 * The special ring reports the status of received packets to the
4851 * host. The chip does not write into the original descriptor the
4852 * RX buffer was obtained from. The chip simply takes the original
4853 * descriptor as provided by the host, updates the status and length
4854 * field, then writes this into the next status ring entry.
4855 *
4856 * Each ring the host uses to post buffers to the chip is described
4857 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
4858 * it is first placed into the on-chip ram. When the packet's length
4859 * is known, it walks down the TG3_BDINFO entries to select the ring.
4860 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
4861 * which is within the range of the new packet's length is chosen.
4862 *
4863 * The "separate ring for rx status" scheme may sound queer, but it makes
4864 * sense from a cache coherency perspective. If only the host writes
4865 * to the buffer post rings, and only the chip writes to the rx status
4866 * rings, then cache lines never move beyond shared-modified state.
4867 * If both the host and chip were to write into the same ring, cache line
4868 * eviction could occur since both entities want it in an exclusive state.
4869 */
Matt Carlson17375d22009-08-28 14:02:18 +00004870static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871{
Matt Carlson17375d22009-08-28 14:02:18 +00004872 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07004873 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004874 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00004875 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07004876 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004878 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004880 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 /*
4882 * We need to order the read of hw_idx and the read of
4883 * the opaque cookie.
4884 */
4885 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 work_mask = 0;
4887 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004888 std_prod_idx = tpr->rx_std_prod_idx;
4889 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00004891 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00004892 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 unsigned int len;
4894 struct sk_buff *skb;
4895 dma_addr_t dma_addr;
4896 u32 opaque_key, desc_idx, *post_ptr;
4897
4898 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
4899 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
4900 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004901 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004902 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004903 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004904 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07004905 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004907 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004908 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004909 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004910 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00004911 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913
4914 work_mask |= opaque_key;
4915
4916 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
4917 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
4918 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00004919 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 desc_idx, *post_ptr);
4921 drop_it_no_recycle:
4922 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00004923 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 goto next_pkt;
4925 }
4926
Matt Carlsonad829262008-11-21 17:16:16 -08004927 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
4928 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929
Matt Carlsond2757fc2010-04-12 06:58:27 +00004930 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 int skb_size;
4932
Matt Carlson86b21e52009-11-13 13:03:45 +00004933 skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00004934 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 if (skb_size < 0)
4936 goto drop_it;
4937
Matt Carlson287be122009-08-28 13:58:46 +00004938 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 PCI_DMA_FROMDEVICE);
4940
Matt Carlson61e800c2010-02-17 15:16:54 +00004941 /* Ensure that the update to the skb happens
4942 * after the usage of the old DMA mapping.
4943 */
4944 smp_wmb();
4945
4946 ri->skb = NULL;
4947
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 skb_put(skb, len);
4949 } else {
4950 struct sk_buff *copy_skb;
4951
Matt Carlsona3896162009-11-13 13:03:44 +00004952 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 desc_idx, *post_ptr);
4954
Matt Carlsonbf933c82011-01-25 15:58:49 +00004955 copy_skb = netdev_alloc_skb(tp->dev, len +
Matt Carlson9dc7a112010-04-12 06:58:28 +00004956 TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 if (copy_skb == NULL)
4958 goto drop_it_no_recycle;
4959
Matt Carlsonbf933c82011-01-25 15:58:49 +00004960 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 skb_put(copy_skb, len);
4962 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03004963 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
4965
4966 /* We'll reuse the original ring buffer. */
4967 skb = copy_skb;
4968 }
4969
Michał Mirosławdc668912011-04-07 03:35:07 +00004970 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
4972 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
4973 >> RXD_TCPCSUM_SHIFT) == 0xffff))
4974 skb->ip_summed = CHECKSUM_UNNECESSARY;
4975 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07004976 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977
4978 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004979
4980 if (len > (tp->dev->mtu + ETH_HLEN) &&
4981 skb->protocol != htons(ETH_P_8021Q)) {
4982 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00004983 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004984 }
4985
Matt Carlson9dc7a112010-04-12 06:58:28 +00004986 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00004987 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
4988 __vlan_hwaccel_put_tag(skb,
4989 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00004990
Matt Carlsonbf933c82011-01-25 15:58:49 +00004991 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 received++;
4994 budget--;
4995
4996next_pkt:
4997 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07004998
4999 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005000 tpr->rx_std_prod_idx = std_prod_idx &
5001 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00005002 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5003 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07005004 work_mask &= ~RXD_OPAQUE_RING_STD;
5005 rx_std_posted = 0;
5006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07005008 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00005009 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07005010
5011 /* Refresh hw_idx to see if there is new work */
5012 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005013 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07005014 rmb();
5015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 }
5017
5018 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00005019 tnapi->rx_rcb_ptr = sw_idx;
5020 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021
5022 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00005023 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005024 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005025 tpr->rx_std_prod_idx = std_prod_idx &
5026 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005027 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5028 tpr->rx_std_prod_idx);
5029 }
5030 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005031 tpr->rx_jmb_prod_idx = jmb_prod_idx &
5032 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005033 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5034 tpr->rx_jmb_prod_idx);
5035 }
5036 mmiowb();
5037 } else if (work_mask) {
5038 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
5039 * updated before the producer indices can be updated.
5040 */
5041 smp_wmb();
5042
Matt Carlson2c49a442010-09-30 10:34:35 +00005043 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
5044 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005045
Matt Carlsone4af1af2010-02-12 14:47:05 +00005046 if (tnapi != &tp->napi[1])
5047 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049
5050 return received;
5051}
5052
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005053static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00005056 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005057 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
5058
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 if (sblk->status & SD_STATUS_LINK_CHG) {
5060 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005061 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07005062 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005063 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07005064 tw32_f(MAC_STATUS,
5065 (MAC_STATUS_SYNC_CHANGED |
5066 MAC_STATUS_CFG_CHANGED |
5067 MAC_STATUS_MI_COMPLETION |
5068 MAC_STATUS_LNKSTATE_CHANGED));
5069 udelay(40);
5070 } else
5071 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07005072 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 }
5074 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005075}
5076
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005077static int tg3_rx_prodring_xfer(struct tg3 *tp,
5078 struct tg3_rx_prodring_set *dpr,
5079 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005080{
5081 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005082 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005083
5084 while (1) {
5085 src_prod_idx = spr->rx_std_prod_idx;
5086
5087 /* Make sure updates to the rx_std_buffers[] entries and the
5088 * standard producer index are seen in the correct order.
5089 */
5090 smp_rmb();
5091
5092 if (spr->rx_std_cons_idx == src_prod_idx)
5093 break;
5094
5095 if (spr->rx_std_cons_idx < src_prod_idx)
5096 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
5097 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005098 cpycnt = tp->rx_std_ring_mask + 1 -
5099 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005100
Matt Carlson2c49a442010-09-30 10:34:35 +00005101 cpycnt = min(cpycnt,
5102 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005103
5104 si = spr->rx_std_cons_idx;
5105 di = dpr->rx_std_prod_idx;
5106
Matt Carlsone92967b2010-02-12 14:47:06 +00005107 for (i = di; i < di + cpycnt; i++) {
5108 if (dpr->rx_std_buffers[i].skb) {
5109 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005110 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005111 break;
5112 }
5113 }
5114
5115 if (!cpycnt)
5116 break;
5117
5118 /* Ensure that updates to the rx_std_buffers ring and the
5119 * shadowed hardware producer ring from tg3_recycle_skb() are
5120 * ordered correctly WRT the skb check above.
5121 */
5122 smp_rmb();
5123
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005124 memcpy(&dpr->rx_std_buffers[di],
5125 &spr->rx_std_buffers[si],
5126 cpycnt * sizeof(struct ring_info));
5127
5128 for (i = 0; i < cpycnt; i++, di++, si++) {
5129 struct tg3_rx_buffer_desc *sbd, *dbd;
5130 sbd = &spr->rx_std[si];
5131 dbd = &dpr->rx_std[di];
5132 dbd->addr_hi = sbd->addr_hi;
5133 dbd->addr_lo = sbd->addr_lo;
5134 }
5135
Matt Carlson2c49a442010-09-30 10:34:35 +00005136 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
5137 tp->rx_std_ring_mask;
5138 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
5139 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005140 }
5141
5142 while (1) {
5143 src_prod_idx = spr->rx_jmb_prod_idx;
5144
5145 /* Make sure updates to the rx_jmb_buffers[] entries and
5146 * the jumbo producer index are seen in the correct order.
5147 */
5148 smp_rmb();
5149
5150 if (spr->rx_jmb_cons_idx == src_prod_idx)
5151 break;
5152
5153 if (spr->rx_jmb_cons_idx < src_prod_idx)
5154 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
5155 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005156 cpycnt = tp->rx_jmb_ring_mask + 1 -
5157 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005158
5159 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00005160 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005161
5162 si = spr->rx_jmb_cons_idx;
5163 di = dpr->rx_jmb_prod_idx;
5164
Matt Carlsone92967b2010-02-12 14:47:06 +00005165 for (i = di; i < di + cpycnt; i++) {
5166 if (dpr->rx_jmb_buffers[i].skb) {
5167 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005168 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005169 break;
5170 }
5171 }
5172
5173 if (!cpycnt)
5174 break;
5175
5176 /* Ensure that updates to the rx_jmb_buffers ring and the
5177 * shadowed hardware producer ring from tg3_recycle_skb() are
5178 * ordered correctly WRT the skb check above.
5179 */
5180 smp_rmb();
5181
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005182 memcpy(&dpr->rx_jmb_buffers[di],
5183 &spr->rx_jmb_buffers[si],
5184 cpycnt * sizeof(struct ring_info));
5185
5186 for (i = 0; i < cpycnt; i++, di++, si++) {
5187 struct tg3_rx_buffer_desc *sbd, *dbd;
5188 sbd = &spr->rx_jmb[si].std;
5189 dbd = &dpr->rx_jmb[di].std;
5190 dbd->addr_hi = sbd->addr_hi;
5191 dbd->addr_lo = sbd->addr_lo;
5192 }
5193
Matt Carlson2c49a442010-09-30 10:34:35 +00005194 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
5195 tp->rx_jmb_ring_mask;
5196 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
5197 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005198 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005199
5200 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005201}
5202
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005203static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
5204{
5205 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206
5207 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005208 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00005209 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00005210 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07005211 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 }
5213
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 /* run RX thread, within the bounds set by NAPI.
5215 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005216 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005218 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00005219 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220
Joe Perches63c3a662011-04-26 08:12:10 +00005221 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005222 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005223 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00005224 u32 std_prod_idx = dpr->rx_std_prod_idx;
5225 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005226
Matt Carlsone4af1af2010-02-12 14:47:05 +00005227 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005228 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00005229 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005230
5231 wmb();
5232
Matt Carlsone4af1af2010-02-12 14:47:05 +00005233 if (std_prod_idx != dpr->rx_std_prod_idx)
5234 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5235 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005236
Matt Carlsone4af1af2010-02-12 14:47:05 +00005237 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5238 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5239 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005240
5241 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005242
5243 if (err)
5244 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005245 }
5246
David S. Miller6f535762007-10-11 18:08:29 -07005247 return work_done;
5248}
David S. Millerf7383c22005-05-18 22:50:53 -07005249
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005250static int tg3_poll_msix(struct napi_struct *napi, int budget)
5251{
5252 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5253 struct tg3 *tp = tnapi->tp;
5254 int work_done = 0;
5255 struct tg3_hw_status *sblk = tnapi->hw_status;
5256
5257 while (1) {
5258 work_done = tg3_poll_work(tnapi, work_done, budget);
5259
Joe Perches63c3a662011-04-26 08:12:10 +00005260 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005261 goto tx_recovery;
5262
5263 if (unlikely(work_done >= budget))
5264 break;
5265
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005266 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005267 * to tell the hw how much work has been processed,
5268 * so we must read it before checking for more work.
5269 */
5270 tnapi->last_tag = sblk->status_tag;
5271 tnapi->last_irq_tag = tnapi->last_tag;
5272 rmb();
5273
5274 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005275 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5276 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005277 napi_complete(napi);
5278 /* Reenable interrupts. */
5279 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5280 mmiowb();
5281 break;
5282 }
5283 }
5284
5285 return work_done;
5286
5287tx_recovery:
5288 /* work_done is guaranteed to be less than budget. */
5289 napi_complete(napi);
5290 schedule_work(&tp->reset_task);
5291 return work_done;
5292}
5293
Matt Carlsone64de4e2011-04-13 11:05:05 +00005294static void tg3_process_error(struct tg3 *tp)
5295{
5296 u32 val;
5297 bool real_error = false;
5298
Joe Perches63c3a662011-04-26 08:12:10 +00005299 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00005300 return;
5301
5302 /* Check Flow Attention register */
5303 val = tr32(HOSTCC_FLOW_ATTN);
5304 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
5305 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
5306 real_error = true;
5307 }
5308
5309 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
5310 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
5311 real_error = true;
5312 }
5313
5314 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
5315 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
5316 real_error = true;
5317 }
5318
5319 if (!real_error)
5320 return;
5321
5322 tg3_dump_state(tp);
5323
Joe Perches63c3a662011-04-26 08:12:10 +00005324 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsone64de4e2011-04-13 11:05:05 +00005325 schedule_work(&tp->reset_task);
5326}
5327
David S. Miller6f535762007-10-11 18:08:29 -07005328static int tg3_poll(struct napi_struct *napi, int budget)
5329{
Matt Carlson8ef04422009-08-28 14:01:37 +00005330 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5331 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07005332 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00005333 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07005334
5335 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00005336 if (sblk->status & SD_STATUS_ERROR)
5337 tg3_process_error(tp);
5338
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005339 tg3_poll_link(tp);
5340
Matt Carlson17375d22009-08-28 14:02:18 +00005341 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07005342
Joe Perches63c3a662011-04-26 08:12:10 +00005343 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07005344 goto tx_recovery;
5345
5346 if (unlikely(work_done >= budget))
5347 break;
5348
Joe Perches63c3a662011-04-26 08:12:10 +00005349 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00005350 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07005351 * to tell the hw how much work has been processed,
5352 * so we must read it before checking for more work.
5353 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005354 tnapi->last_tag = sblk->status_tag;
5355 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07005356 rmb();
5357 } else
5358 sblk->status &= ~SD_STATUS_UPDATED;
5359
Matt Carlson17375d22009-08-28 14:02:18 +00005360 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005361 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00005362 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005363 break;
5364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 }
5366
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005367 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07005368
5369tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07005370 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08005371 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07005372 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07005373 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374}
5375
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005376static void tg3_napi_disable(struct tg3 *tp)
5377{
5378 int i;
5379
5380 for (i = tp->irq_cnt - 1; i >= 0; i--)
5381 napi_disable(&tp->napi[i].napi);
5382}
5383
5384static void tg3_napi_enable(struct tg3 *tp)
5385{
5386 int i;
5387
5388 for (i = 0; i < tp->irq_cnt; i++)
5389 napi_enable(&tp->napi[i].napi);
5390}
5391
5392static void tg3_napi_init(struct tg3 *tp)
5393{
5394 int i;
5395
5396 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
5397 for (i = 1; i < tp->irq_cnt; i++)
5398 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
5399}
5400
5401static void tg3_napi_fini(struct tg3 *tp)
5402{
5403 int i;
5404
5405 for (i = 0; i < tp->irq_cnt; i++)
5406 netif_napi_del(&tp->napi[i].napi);
5407}
5408
5409static inline void tg3_netif_stop(struct tg3 *tp)
5410{
5411 tp->dev->trans_start = jiffies; /* prevent tx timeout */
5412 tg3_napi_disable(tp);
5413 netif_tx_disable(tp->dev);
5414}
5415
5416static inline void tg3_netif_start(struct tg3 *tp)
5417{
5418 /* NOTE: unconditional netif_tx_wake_all_queues is only
5419 * appropriate so long as all callers are assured to
5420 * have free tx slots (such as after tg3_init_hw)
5421 */
5422 netif_tx_wake_all_queues(tp->dev);
5423
5424 tg3_napi_enable(tp);
5425 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
5426 tg3_enable_ints(tp);
5427}
5428
David S. Millerf47c11e2005-06-24 20:18:35 -07005429static void tg3_irq_quiesce(struct tg3 *tp)
5430{
Matt Carlson4f125f42009-09-01 12:55:02 +00005431 int i;
5432
David S. Millerf47c11e2005-06-24 20:18:35 -07005433 BUG_ON(tp->irq_sync);
5434
5435 tp->irq_sync = 1;
5436 smp_mb();
5437
Matt Carlson4f125f42009-09-01 12:55:02 +00005438 for (i = 0; i < tp->irq_cnt; i++)
5439 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07005440}
5441
David S. Millerf47c11e2005-06-24 20:18:35 -07005442/* Fully shutdown all tg3 driver activity elsewhere in the system.
5443 * If irq_sync is non-zero, then the IRQ handler must be synchronized
5444 * with as well. Most of the time, this is not necessary except when
5445 * shutting down the device.
5446 */
5447static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
5448{
Michael Chan46966542007-07-11 19:47:19 -07005449 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07005450 if (irq_sync)
5451 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005452}
5453
5454static inline void tg3_full_unlock(struct tg3 *tp)
5455{
David S. Millerf47c11e2005-06-24 20:18:35 -07005456 spin_unlock_bh(&tp->lock);
5457}
5458
Michael Chanfcfa0a32006-03-20 22:28:41 -08005459/* One-shot MSI handler - Chip automatically disables interrupt
5460 * after sending MSI so driver doesn't have to do it.
5461 */
David Howells7d12e782006-10-05 14:55:46 +01005462static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08005463{
Matt Carlson09943a12009-08-28 14:01:57 +00005464 struct tg3_napi *tnapi = dev_id;
5465 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08005466
Matt Carlson898a56f2009-08-28 14:02:40 +00005467 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005468 if (tnapi->rx_rcb)
5469 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005470
5471 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005472 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005473
5474 return IRQ_HANDLED;
5475}
5476
Michael Chan88b06bc22005-04-21 17:13:25 -07005477/* MSI ISR - No need to check for interrupt sharing and no need to
5478 * flush status block and interrupt mailbox. PCI ordering rules
5479 * guarantee that MSI will arrive after the status block.
5480 */
David Howells7d12e782006-10-05 14:55:46 +01005481static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07005482{
Matt Carlson09943a12009-08-28 14:01:57 +00005483 struct tg3_napi *tnapi = dev_id;
5484 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07005485
Matt Carlson898a56f2009-08-28 14:02:40 +00005486 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005487 if (tnapi->rx_rcb)
5488 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07005489 /*
David S. Millerfac9b832005-05-18 22:46:34 -07005490 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07005491 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07005492 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07005493 * NIC to stop sending us irqs, engaging "in-intr-handler"
5494 * event coalescing.
5495 */
5496 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07005497 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005498 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07005499
Michael Chan88b06bc22005-04-21 17:13:25 -07005500 return IRQ_RETVAL(1);
5501}
5502
David Howells7d12e782006-10-05 14:55:46 +01005503static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504{
Matt Carlson09943a12009-08-28 14:01:57 +00005505 struct tg3_napi *tnapi = dev_id;
5506 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005507 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 unsigned int handled = 1;
5509
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 /* In INTx mode, it is possible for the interrupt to arrive at
5511 * the CPU before the status block posted prior to the interrupt.
5512 * Reading the PCI State register will confirm whether the
5513 * interrupt is ours and will flush the status block.
5514 */
Michael Chand18edcb2007-03-24 20:57:11 -07005515 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00005516 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07005517 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5518 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005519 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07005520 }
Michael Chand18edcb2007-03-24 20:57:11 -07005521 }
5522
5523 /*
5524 * Writing any value to intr-mbox-0 clears PCI INTA# and
5525 * chip-internal interrupt pending events.
5526 * Writing non-zero to intr-mbox-0 additional tells the
5527 * NIC to stop sending us irqs, engaging "in-intr-handler"
5528 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005529 *
5530 * Flush the mailbox to de-assert the IRQ immediately to prevent
5531 * spurious interrupts. The flush impacts performance but
5532 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005533 */
Michael Chanc04cb342007-05-07 00:26:15 -07005534 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07005535 if (tg3_irq_sync(tp))
5536 goto out;
5537 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00005538 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00005539 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00005540 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07005541 } else {
5542 /* No work, shared interrupt perhaps? re-enable
5543 * interrupts, and flush that PCI write
5544 */
5545 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
5546 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07005547 }
David S. Millerf47c11e2005-06-24 20:18:35 -07005548out:
David S. Millerfac9b832005-05-18 22:46:34 -07005549 return IRQ_RETVAL(handled);
5550}
5551
David Howells7d12e782006-10-05 14:55:46 +01005552static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07005553{
Matt Carlson09943a12009-08-28 14:01:57 +00005554 struct tg3_napi *tnapi = dev_id;
5555 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005556 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07005557 unsigned int handled = 1;
5558
David S. Millerfac9b832005-05-18 22:46:34 -07005559 /* In INTx mode, it is possible for the interrupt to arrive at
5560 * the CPU before the status block posted prior to the interrupt.
5561 * Reading the PCI State register will confirm whether the
5562 * interrupt is ours and will flush the status block.
5563 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005564 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00005565 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07005566 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5567 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005568 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 }
Michael Chand18edcb2007-03-24 20:57:11 -07005570 }
5571
5572 /*
5573 * writing any value to intr-mbox-0 clears PCI INTA# and
5574 * chip-internal interrupt pending events.
5575 * writing non-zero to intr-mbox-0 additional tells the
5576 * NIC to stop sending us irqs, engaging "in-intr-handler"
5577 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005578 *
5579 * Flush the mailbox to de-assert the IRQ immediately to prevent
5580 * spurious interrupts. The flush impacts performance but
5581 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005582 */
Michael Chanc04cb342007-05-07 00:26:15 -07005583 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00005584
5585 /*
5586 * In a shared interrupt configuration, sometimes other devices'
5587 * interrupts will scream. We record the current status tag here
5588 * so that the above check can report that the screaming interrupts
5589 * are unhandled. Eventually they will be silenced.
5590 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005591 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00005592
Michael Chand18edcb2007-03-24 20:57:11 -07005593 if (tg3_irq_sync(tp))
5594 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00005595
Matt Carlson72334482009-08-28 14:03:01 +00005596 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00005597
Matt Carlson09943a12009-08-28 14:01:57 +00005598 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00005599
David S. Millerf47c11e2005-06-24 20:18:35 -07005600out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 return IRQ_RETVAL(handled);
5602}
5603
Michael Chan79381092005-04-21 17:13:59 -07005604/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01005605static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07005606{
Matt Carlson09943a12009-08-28 14:01:57 +00005607 struct tg3_napi *tnapi = dev_id;
5608 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005609 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07005610
Michael Chanf9804dd2005-09-27 12:13:10 -07005611 if ((sblk->status & SD_STATUS_UPDATED) ||
5612 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07005613 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07005614 return IRQ_RETVAL(1);
5615 }
5616 return IRQ_RETVAL(0);
5617}
5618
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07005619static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07005620static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621
Michael Chanb9ec6c12006-07-25 16:37:27 -07005622/* Restart hardware after configuration changes, self-test, etc.
5623 * Invoked with tp->lock held.
5624 */
5625static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07005626 __releases(tp->lock)
5627 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005628{
5629 int err;
5630
5631 err = tg3_init_hw(tp, reset_phy);
5632 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00005633 netdev_err(tp->dev,
5634 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07005635 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
5636 tg3_full_unlock(tp);
5637 del_timer_sync(&tp->timer);
5638 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00005639 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005640 dev_close(tp->dev);
5641 tg3_full_lock(tp, 0);
5642 }
5643 return err;
5644}
5645
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646#ifdef CONFIG_NET_POLL_CONTROLLER
5647static void tg3_poll_controller(struct net_device *dev)
5648{
Matt Carlson4f125f42009-09-01 12:55:02 +00005649 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07005650 struct tg3 *tp = netdev_priv(dev);
5651
Matt Carlson4f125f42009-09-01 12:55:02 +00005652 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00005653 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654}
5655#endif
5656
David Howellsc4028952006-11-22 14:57:56 +00005657static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658{
David Howellsc4028952006-11-22 14:57:56 +00005659 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005660 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661 unsigned int restart_timer;
5662
Michael Chan7faa0062006-02-02 17:29:28 -08005663 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08005664
5665 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08005666 tg3_full_unlock(tp);
5667 return;
5668 }
5669
5670 tg3_full_unlock(tp);
5671
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005672 tg3_phy_stop(tp);
5673
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 tg3_netif_stop(tp);
5675
David S. Millerf47c11e2005-06-24 20:18:35 -07005676 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677
Joe Perches63c3a662011-04-26 08:12:10 +00005678 restart_timer = tg3_flag(tp, RESTART_TIMER);
5679 tg3_flag_clear(tp, RESTART_TIMER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680
Joe Perches63c3a662011-04-26 08:12:10 +00005681 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
Michael Chandf3e6542006-05-26 17:48:07 -07005682 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5683 tp->write32_rx_mbox = tg3_write_flush_reg32;
Joe Perches63c3a662011-04-26 08:12:10 +00005684 tg3_flag_set(tp, MBOX_WRITE_REORDER);
5685 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005686 }
5687
Michael Chan944d9802005-05-29 14:57:48 -07005688 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005689 err = tg3_init_hw(tp, 1);
5690 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005691 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692
5693 tg3_netif_start(tp);
5694
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695 if (restart_timer)
5696 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08005697
Michael Chanb9ec6c12006-07-25 16:37:27 -07005698out:
Michael Chan7faa0062006-02-02 17:29:28 -08005699 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005700
5701 if (!err)
5702 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703}
5704
5705static void tg3_tx_timeout(struct net_device *dev)
5706{
5707 struct tg3 *tp = netdev_priv(dev);
5708
Michael Chanb0408752007-02-13 12:18:30 -08005709 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00005710 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00005711 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08005712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713
5714 schedule_work(&tp->reset_task);
5715}
5716
Michael Chanc58ec932005-09-17 00:46:27 -07005717/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5718static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5719{
5720 u32 base = (u32) mapping & 0xffffffff;
5721
Eric Dumazet807540b2010-09-23 05:40:09 +00005722 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07005723}
5724
Michael Chan72f2afb2006-03-06 19:28:35 -08005725/* Test for DMA addresses > 40-bit */
5726static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5727 int len)
5728{
5729#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00005730 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00005731 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08005732 return 0;
5733#else
5734 return 0;
5735#endif
5736}
5737
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005738static void tg3_set_txd(struct tg3_napi *, int, dma_addr_t, int, u32, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739
Michael Chan72f2afb2006-03-06 19:28:35 -08005740/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005741static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
5742 struct sk_buff *skb, u32 last_plus_one,
5743 u32 *start, u32 base_flags, u32 mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744{
Matt Carlson24f4efd2009-11-13 13:03:35 +00005745 struct tg3 *tp = tnapi->tp;
Matt Carlson41588ba2008-04-19 18:12:33 -07005746 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07005747 dma_addr_t new_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 u32 entry = *start;
Michael Chanc58ec932005-09-17 00:46:27 -07005749 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750
Matt Carlson41588ba2008-04-19 18:12:33 -07005751 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
5752 new_skb = skb_copy(skb, GFP_ATOMIC);
5753 else {
5754 int more_headroom = 4 - ((unsigned long)skb->data & 3);
5755
5756 new_skb = skb_copy_expand(skb,
5757 skb_headroom(skb) + more_headroom,
5758 skb_tailroom(skb), GFP_ATOMIC);
5759 }
5760
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07005762 ret = -1;
5763 } else {
5764 /* New SKB is guaranteed to be linear. */
5765 entry = *start;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005766 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
5767 PCI_DMA_TODEVICE);
5768 /* Make sure the mapping succeeded */
5769 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
5770 ret = -1;
5771 dev_kfree_skb(new_skb);
5772 new_skb = NULL;
David S. Miller90079ce2008-09-11 04:52:51 -07005773
Michael Chanc58ec932005-09-17 00:46:27 -07005774 /* Make sure new skb does not cross any 4G boundaries.
5775 * Drop the packet if it does.
5776 */
Joe Perches63c3a662011-04-26 08:12:10 +00005777 } else if (tg3_flag(tp, 4G_DMA_BNDRY_BUG) &&
5778 tg3_4g_overflow_test(new_addr, new_skb->len)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00005779 pci_unmap_single(tp->pdev, new_addr, new_skb->len,
5780 PCI_DMA_TODEVICE);
Michael Chanc58ec932005-09-17 00:46:27 -07005781 ret = -1;
5782 dev_kfree_skb(new_skb);
5783 new_skb = NULL;
5784 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005785 tg3_set_txd(tnapi, entry, new_addr, new_skb->len,
Michael Chanc58ec932005-09-17 00:46:27 -07005786 base_flags, 1 | (mss << 1));
5787 *start = NEXT_TX(entry);
5788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789 }
5790
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791 /* Now clean up the sw ring entries. */
5792 i = 0;
5793 while (entry != last_plus_one) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00005794 int len;
5795
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005796 if (i == 0)
Alexander Duyckf4188d82009-12-02 16:48:38 +00005797 len = skb_headlen(skb);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005798 else
Alexander Duyckf4188d82009-12-02 16:48:38 +00005799 len = skb_shinfo(skb)->frags[i-1].size;
5800
5801 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005802 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00005803 mapping),
5804 len, PCI_DMA_TODEVICE);
5805 if (i == 0) {
5806 tnapi->tx_buffers[entry].skb = new_skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005807 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00005808 new_addr);
5809 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005810 tnapi->tx_buffers[entry].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 entry = NEXT_TX(entry);
5813 i++;
5814 }
5815
5816 dev_kfree_skb(skb);
5817
Michael Chanc58ec932005-09-17 00:46:27 -07005818 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819}
5820
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005821static void tg3_set_txd(struct tg3_napi *tnapi, int entry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822 dma_addr_t mapping, int len, u32 flags,
5823 u32 mss_and_is_end)
5824{
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005825 struct tg3_tx_buffer_desc *txd = &tnapi->tx_ring[entry];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826 int is_end = (mss_and_is_end & 0x1);
5827 u32 mss = (mss_and_is_end >> 1);
5828 u32 vlan_tag = 0;
5829
5830 if (is_end)
5831 flags |= TXD_FLAG_END;
5832 if (flags & TXD_FLAG_VLAN) {
5833 vlan_tag = flags >> 16;
5834 flags &= 0xffff;
5835 }
5836 vlan_tag |= (mss << TXD_MSS_SHIFT);
5837
5838 txd->addr_hi = ((u64) mapping >> 32);
5839 txd->addr_lo = ((u64) mapping & 0xffffffff);
5840 txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
5841 txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
5842}
5843
Michael Chan5a6f3072006-03-20 22:28:05 -08005844/* hard_start_xmit for devices that don't have any bugs and
Joe Perches63c3a662011-04-26 08:12:10 +00005845 * support TG3_FLAG_HW_TSO_2 and TG3_FLAG_HW_TSO_3 only.
Michael Chan5a6f3072006-03-20 22:28:05 -08005846 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005847static netdev_tx_t tg3_start_xmit(struct sk_buff *skb,
5848 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849{
5850 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 u32 len, entry, base_flags, mss;
David S. Miller90079ce2008-09-11 04:52:51 -07005852 dma_addr_t mapping;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005853 struct tg3_napi *tnapi;
5854 struct netdev_queue *txq;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005855 unsigned int i, last;
5856
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005857 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5858 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00005859 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005860 tnapi++;
Michael Chan5a6f3072006-03-20 22:28:05 -08005861
Michael Chan00b70502006-06-17 21:58:45 -07005862 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005863 * and TX reclaim runs via tp->napi.poll inside of a software
Michael Chan5a6f3072006-03-20 22:28:05 -08005864 * interrupt. Furthermore, IRQ processing runs lockless so we have
5865 * no IRQ context deadlocks to worry about either. Rejoice!
5866 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005867 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005868 if (!netif_tx_queue_stopped(txq)) {
5869 netif_tx_stop_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005870
5871 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00005872 netdev_err(dev,
5873 "BUG! Tx Ring full when queue awake!\n");
Michael Chan5a6f3072006-03-20 22:28:05 -08005874 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005875 return NETDEV_TX_BUSY;
5876 }
5877
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005878 entry = tnapi->tx_prod;
Michael Chan5a6f3072006-03-20 22:28:05 -08005879 base_flags = 0;
Matt Carlsonbe98da62010-07-11 09:31:46 +00005880 mss = skb_shinfo(skb)->gso_size;
5881 if (mss) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005882 int tcp_opt_len, ip_tcp_len;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005883 u32 hdrlen;
Michael Chan5a6f3072006-03-20 22:28:05 -08005884
5885 if (skb_header_cloned(skb) &&
5886 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5887 dev_kfree_skb(skb);
5888 goto out_unlock;
5889 }
5890
Matt Carlson02e96082010-09-15 08:59:59 +00005891 if (skb_is_gso_v6(skb)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005892 hdrlen = skb_headlen(skb) - ETH_HLEN;
Matt Carlson02e96082010-09-15 08:59:59 +00005893 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005894 struct iphdr *iph = ip_hdr(skb);
5895
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005896 tcp_opt_len = tcp_optlen(skb);
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03005897 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
Michael Chanb0026622006-07-03 19:42:14 -07005898
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005899 iph->check = 0;
5900 iph->tot_len = htons(mss + ip_tcp_len + tcp_opt_len);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005901 hdrlen = ip_tcp_len + tcp_opt_len;
Michael Chanb0026622006-07-03 19:42:14 -07005902 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005903
Joe Perches63c3a662011-04-26 08:12:10 +00005904 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005905 mss |= (hdrlen & 0xc) << 12;
5906 if (hdrlen & 0x10)
5907 base_flags |= 0x00000010;
5908 base_flags |= (hdrlen & 0x3e0) << 5;
5909 } else
5910 mss |= hdrlen << 9;
5911
Michael Chan5a6f3072006-03-20 22:28:05 -08005912 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5913 TXD_FLAG_CPU_POST_DMA);
5914
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005915 tcp_hdr(skb)->check = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08005916
Matt Carlson859a588792010-04-05 10:19:28 +00005917 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005918 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson859a588792010-04-05 10:19:28 +00005919 }
5920
Jesse Grosseab6d182010-10-20 13:56:03 +00005921 if (vlan_tx_tag_present(skb))
Michael Chan5a6f3072006-03-20 22:28:05 -08005922 base_flags |= (TXD_FLAG_VLAN |
5923 (vlan_tx_tag_get(skb) << 16));
Michael Chan5a6f3072006-03-20 22:28:05 -08005924
Alexander Duyckf4188d82009-12-02 16:48:38 +00005925 len = skb_headlen(skb);
5926
5927 /* Queue skb data, a.k.a. the main skb fragment. */
5928 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
5929 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07005930 dev_kfree_skb(skb);
5931 goto out_unlock;
5932 }
5933
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005934 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005935 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005936
Joe Perches63c3a662011-04-26 08:12:10 +00005937 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
Matt Carlson8fc2f992010-12-06 08:28:49 +00005938 !mss && skb->len > VLAN_ETH_FRAME_LEN)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005939 base_flags |= TXD_FLAG_JMB_PKT;
5940
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005941 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Michael Chan5a6f3072006-03-20 22:28:05 -08005942 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5943
5944 entry = NEXT_TX(entry);
5945
5946 /* Now loop through additional data fragments, and queue them. */
5947 if (skb_shinfo(skb)->nr_frags > 0) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005948 last = skb_shinfo(skb)->nr_frags - 1;
5949 for (i = 0; i <= last; i++) {
5950 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5951
5952 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005953 mapping = pci_map_page(tp->pdev,
5954 frag->page,
5955 frag->page_offset,
5956 len, PCI_DMA_TODEVICE);
5957 if (pci_dma_mapping_error(tp->pdev, mapping))
5958 goto dma_error;
5959
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005960 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005961 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00005962 mapping);
Michael Chan5a6f3072006-03-20 22:28:05 -08005963
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005964 tg3_set_txd(tnapi, entry, mapping, len,
Michael Chan5a6f3072006-03-20 22:28:05 -08005965 base_flags, (i == last) | (mss << 1));
5966
5967 entry = NEXT_TX(entry);
5968 }
5969 }
5970
5971 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005972 tw32_tx_mbox(tnapi->prodmbox, entry);
Michael Chan5a6f3072006-03-20 22:28:05 -08005973
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005974 tnapi->tx_prod = entry;
5975 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005976 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00005977
5978 /* netif_tx_stop_queue() must be done before checking
5979 * checking tx index in tg3_tx_avail() below, because in
5980 * tg3_tx(), we update tx index before checking for
5981 * netif_tx_queue_stopped().
5982 */
5983 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005984 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005985 netif_tx_wake_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005986 }
5987
5988out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005989 mmiowb();
Michael Chan5a6f3072006-03-20 22:28:05 -08005990
5991 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005992
5993dma_error:
5994 last = i;
5995 entry = tnapi->tx_prod;
5996 tnapi->tx_buffers[entry].skb = NULL;
5997 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005998 dma_unmap_addr(&tnapi->tx_buffers[entry], mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005999 skb_headlen(skb),
6000 PCI_DMA_TODEVICE);
6001 for (i = 0; i <= last; i++) {
6002 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6003 entry = NEXT_TX(entry);
6004
6005 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006006 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00006007 mapping),
6008 frag->size, PCI_DMA_TODEVICE);
6009 }
6010
6011 dev_kfree_skb(skb);
6012 return NETDEV_TX_OK;
Michael Chan5a6f3072006-03-20 22:28:05 -08006013}
6014
Stephen Hemminger613573252009-08-31 19:50:58 +00006015static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *,
6016 struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07006017
6018/* Use GSO to workaround a rare TSO bug that may be triggered when the
6019 * TSO header is greater than 80 bytes.
6020 */
6021static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
6022{
6023 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006024 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07006025
6026 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006027 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07006028 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006029
6030 /* netif_tx_stop_queue() must be done before checking
6031 * checking tx index in tg3_tx_avail() below, because in
6032 * tg3_tx(), we update tx index before checking for
6033 * netif_tx_queue_stopped().
6034 */
6035 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006036 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08006037 return NETDEV_TX_BUSY;
6038
6039 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006040 }
6041
6042 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07006043 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07006044 goto tg3_tso_bug_end;
6045
6046 do {
6047 nskb = segs;
6048 segs = segs->next;
6049 nskb->next = NULL;
6050 tg3_start_xmit_dma_bug(nskb, tp->dev);
6051 } while (segs);
6052
6053tg3_tso_bug_end:
6054 dev_kfree_skb(skb);
6055
6056 return NETDEV_TX_OK;
6057}
Michael Chan52c0fd82006-06-29 20:15:54 -07006058
Michael Chan5a6f3072006-03-20 22:28:05 -08006059/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00006060 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08006061 */
Stephen Hemminger613573252009-08-31 19:50:58 +00006062static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *skb,
6063 struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08006064{
6065 struct tg3 *tp = netdev_priv(dev);
Michael Chan5a6f3072006-03-20 22:28:05 -08006066 u32 len, entry, base_flags, mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067 int would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07006068 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006069 struct tg3_napi *tnapi;
6070 struct netdev_queue *txq;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006071 unsigned int i, last;
6072
Matt Carlson24f4efd2009-11-13 13:03:35 +00006073 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
6074 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00006075 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006076 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077
Michael Chan00b70502006-06-17 21:58:45 -07006078 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006079 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07006080 * interrupt. Furthermore, IRQ processing runs lockless so we have
6081 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006083 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006084 if (!netif_tx_queue_stopped(txq)) {
6085 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006086
6087 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00006088 netdev_err(dev,
6089 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091 return NETDEV_TX_BUSY;
6092 }
6093
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006094 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07006096 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006098
Matt Carlsonbe98da62010-07-11 09:31:46 +00006099 mss = skb_shinfo(skb)->gso_size;
6100 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006101 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00006102 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103
6104 if (skb_header_cloned(skb) &&
6105 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
6106 dev_kfree_skb(skb);
6107 goto out_unlock;
6108 }
6109
Matt Carlson34195c32010-07-11 09:31:42 +00006110 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07006111 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112
Matt Carlson02e96082010-09-15 08:59:59 +00006113 if (skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00006114 hdr_len = skb_headlen(skb) - ETH_HLEN;
6115 } else {
6116 u32 ip_tcp_len;
6117
6118 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
6119 hdr_len = ip_tcp_len + tcp_opt_len;
6120
6121 iph->check = 0;
6122 iph->tot_len = htons(mss + hdr_len);
6123 }
6124
Michael Chan52c0fd82006-06-29 20:15:54 -07006125 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00006126 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00006127 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07006128
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
6130 TXD_FLAG_CPU_POST_DMA);
6131
Joe Perches63c3a662011-04-26 08:12:10 +00006132 if (tg3_flag(tp, HW_TSO_1) ||
6133 tg3_flag(tp, HW_TSO_2) ||
6134 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006135 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006137 } else
6138 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
6139 iph->daddr, 0,
6140 IPPROTO_TCP,
6141 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142
Joe Perches63c3a662011-04-26 08:12:10 +00006143 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00006144 mss |= (hdr_len & 0xc) << 12;
6145 if (hdr_len & 0x10)
6146 base_flags |= 0x00000010;
6147 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00006148 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006149 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00006150 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006151 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006152 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006153 int tsflags;
6154
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006155 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156 mss |= (tsflags << 11);
6157 }
6158 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006159 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160 int tsflags;
6161
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006162 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163 base_flags |= tsflags << 12;
6164 }
6165 }
6166 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00006167
Jesse Grosseab6d182010-10-20 13:56:03 +00006168 if (vlan_tx_tag_present(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169 base_flags |= (TXD_FLAG_VLAN |
6170 (vlan_tx_tag_get(skb) << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171
Joe Perches63c3a662011-04-26 08:12:10 +00006172 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
Matt Carlson8fc2f992010-12-06 08:28:49 +00006173 !mss && skb->len > VLAN_ETH_FRAME_LEN)
Matt Carlson615774f2009-11-13 13:03:39 +00006174 base_flags |= TXD_FLAG_JMB_PKT;
6175
Alexander Duyckf4188d82009-12-02 16:48:38 +00006176 len = skb_headlen(skb);
6177
6178 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
6179 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07006180 dev_kfree_skb(skb);
6181 goto out_unlock;
6182 }
6183
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006184 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006185 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186
6187 would_hit_hwbug = 0;
6188
Joe Perches63c3a662011-04-26 08:12:10 +00006189 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006190 would_hit_hwbug = 1;
6191
Joe Perches63c3a662011-04-26 08:12:10 +00006192 if (tg3_flag(tp, 4G_DMA_BNDRY_BUG) &&
Matt Carlson0e1406d2009-11-02 12:33:33 +00006193 tg3_4g_overflow_test(mapping, len))
Matt Carlson41588ba2008-04-19 18:12:33 -07006194 would_hit_hwbug = 1;
Matt Carlson0e1406d2009-11-02 12:33:33 +00006195
Joe Perches63c3a662011-04-26 08:12:10 +00006196 if (tg3_flag(tp, 40BIT_DMA_LIMIT_BUG) &&
Matt Carlson0e1406d2009-11-02 12:33:33 +00006197 tg3_40bit_overflow_test(tp, mapping, len))
6198 would_hit_hwbug = 1;
6199
Joe Perches63c3a662011-04-26 08:12:10 +00006200 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07006201 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006203 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
6205
6206 entry = NEXT_TX(entry);
6207
6208 /* Now loop through additional data fragments, and queue them. */
6209 if (skb_shinfo(skb)->nr_frags > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210 last = skb_shinfo(skb)->nr_frags - 1;
6211 for (i = 0; i <= last; i++) {
6212 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6213
6214 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006215 mapping = pci_map_page(tp->pdev,
6216 frag->page,
6217 frag->page_offset,
6218 len, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006220 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006221 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006222 mapping);
6223 if (pci_dma_mapping_error(tp->pdev, mapping))
6224 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225
Joe Perches63c3a662011-04-26 08:12:10 +00006226 if (tg3_flag(tp, SHORT_DMA_BUG) &&
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006227 len <= 8)
6228 would_hit_hwbug = 1;
6229
Joe Perches63c3a662011-04-26 08:12:10 +00006230 if (tg3_flag(tp, 4G_DMA_BNDRY_BUG) &&
Matt Carlson0e1406d2009-11-02 12:33:33 +00006231 tg3_4g_overflow_test(mapping, len))
Michael Chanc58ec932005-09-17 00:46:27 -07006232 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233
Joe Perches63c3a662011-04-26 08:12:10 +00006234 if (tg3_flag(tp, 40BIT_DMA_LIMIT_BUG) &&
Matt Carlson0e1406d2009-11-02 12:33:33 +00006235 tg3_40bit_overflow_test(tp, mapping, len))
Michael Chan72f2afb2006-03-06 19:28:35 -08006236 would_hit_hwbug = 1;
6237
Joe Perches63c3a662011-04-26 08:12:10 +00006238 if (tg3_flag(tp, HW_TSO_1) ||
6239 tg3_flag(tp, HW_TSO_2) ||
6240 tg3_flag(tp, HW_TSO_3))
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006241 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242 base_flags, (i == last)|(mss << 1));
6243 else
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006244 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245 base_flags, (i == last));
6246
6247 entry = NEXT_TX(entry);
6248 }
6249 }
6250
6251 if (would_hit_hwbug) {
6252 u32 last_plus_one = entry;
6253 u32 start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254
Michael Chanc58ec932005-09-17 00:46:27 -07006255 start = entry - 1 - skb_shinfo(skb)->nr_frags;
6256 start &= (TG3_TX_RING_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257
6258 /* If the workaround fails due to memory/mapping
6259 * failure, silently drop this packet.
6260 */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006261 if (tigon3_dma_hwbug_workaround(tnapi, skb, last_plus_one,
Michael Chanc58ec932005-09-17 00:46:27 -07006262 &start, base_flags, mss))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263 goto out_unlock;
6264
6265 entry = start;
6266 }
6267
6268 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006269 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006270
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006271 tnapi->tx_prod = entry;
6272 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006273 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006274
6275 /* netif_tx_stop_queue() must be done before checking
6276 * checking tx index in tg3_tx_avail() below, because in
6277 * tg3_tx(), we update tx index before checking for
6278 * netif_tx_queue_stopped().
6279 */
6280 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006281 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006282 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006284
6285out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00006286 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287
6288 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006289
6290dma_error:
6291 last = i;
6292 entry = tnapi->tx_prod;
6293 tnapi->tx_buffers[entry].skb = NULL;
6294 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006295 dma_unmap_addr(&tnapi->tx_buffers[entry], mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006296 skb_headlen(skb),
6297 PCI_DMA_TODEVICE);
6298 for (i = 0; i <= last; i++) {
6299 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6300 entry = NEXT_TX(entry);
6301
6302 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006303 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00006304 mapping),
6305 frag->size, PCI_DMA_TODEVICE);
6306 }
6307
6308 dev_kfree_skb(skb);
6309 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006310}
6311
Michał Mirosławdc668912011-04-07 03:35:07 +00006312static u32 tg3_fix_features(struct net_device *dev, u32 features)
6313{
6314 struct tg3 *tp = netdev_priv(dev);
6315
Joe Perches63c3a662011-04-26 08:12:10 +00006316 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00006317 features &= ~NETIF_F_ALL_TSO;
6318
6319 return features;
6320}
6321
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
6323 int new_mtu)
6324{
6325 dev->mtu = new_mtu;
6326
Michael Chanef7f5ec2005-07-25 12:32:25 -07006327 if (new_mtu > ETH_DATA_LEN) {
Joe Perches63c3a662011-04-26 08:12:10 +00006328 if (tg3_flag(tp, 5780_CLASS)) {
Michał Mirosławdc668912011-04-07 03:35:07 +00006329 netdev_update_features(dev);
Joe Perches63c3a662011-04-26 08:12:10 +00006330 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson859a588792010-04-05 10:19:28 +00006331 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00006332 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Matt Carlson859a588792010-04-05 10:19:28 +00006333 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07006334 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00006335 if (tg3_flag(tp, 5780_CLASS)) {
6336 tg3_flag_set(tp, TSO_CAPABLE);
Michał Mirosławdc668912011-04-07 03:35:07 +00006337 netdev_update_features(dev);
6338 }
Joe Perches63c3a662011-04-26 08:12:10 +00006339 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
Michael Chanef7f5ec2005-07-25 12:32:25 -07006340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341}
6342
6343static int tg3_change_mtu(struct net_device *dev, int new_mtu)
6344{
6345 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07006346 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347
6348 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
6349 return -EINVAL;
6350
6351 if (!netif_running(dev)) {
6352 /* We'll just catch it later when the
6353 * device is up'd.
6354 */
6355 tg3_set_mtu(dev, tp, new_mtu);
6356 return 0;
6357 }
6358
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006359 tg3_phy_stop(tp);
6360
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006362
6363 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006364
Michael Chan944d9802005-05-29 14:57:48 -07006365 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366
6367 tg3_set_mtu(dev, tp, new_mtu);
6368
Michael Chanb9ec6c12006-07-25 16:37:27 -07006369 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370
Michael Chanb9ec6c12006-07-25 16:37:27 -07006371 if (!err)
6372 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373
David S. Millerf47c11e2005-06-24 20:18:35 -07006374 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006376 if (!err)
6377 tg3_phy_start(tp);
6378
Michael Chanb9ec6c12006-07-25 16:37:27 -07006379 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380}
6381
Matt Carlson21f581a2009-08-28 14:00:25 +00006382static void tg3_rx_prodring_free(struct tg3 *tp,
6383 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385 int i;
6386
Matt Carlson8fea32b2010-09-15 08:59:58 +00006387 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006388 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006389 i = (i + 1) & tp->rx_std_ring_mask)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006390 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6391 tp->rx_pkt_map_sz);
6392
Joe Perches63c3a662011-04-26 08:12:10 +00006393 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006394 for (i = tpr->rx_jmb_cons_idx;
6395 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006396 i = (i + 1) & tp->rx_jmb_ring_mask) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006397 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6398 TG3_RX_JMB_MAP_SZ);
6399 }
6400 }
6401
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006402 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404
Matt Carlson2c49a442010-09-30 10:34:35 +00006405 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006406 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6407 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408
Joe Perches63c3a662011-04-26 08:12:10 +00006409 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006410 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006411 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6412 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006413 }
6414}
6415
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006416/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417 *
6418 * The chip has been shut down and the driver detached from
6419 * the networking, so no interrupts or new tx packets will
6420 * end up in the driver. tp->{tx,}lock are held and thus
6421 * we may not sleep.
6422 */
Matt Carlson21f581a2009-08-28 14:00:25 +00006423static int tg3_rx_prodring_alloc(struct tg3 *tp,
6424 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006425{
Matt Carlson287be122009-08-28 13:58:46 +00006426 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006428 tpr->rx_std_cons_idx = 0;
6429 tpr->rx_std_prod_idx = 0;
6430 tpr->rx_jmb_cons_idx = 0;
6431 tpr->rx_jmb_prod_idx = 0;
6432
Matt Carlson8fea32b2010-09-15 08:59:58 +00006433 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006434 memset(&tpr->rx_std_buffers[0], 0,
6435 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00006436 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006437 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00006438 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006439 goto done;
6440 }
6441
Linus Torvalds1da177e2005-04-16 15:20:36 -07006442 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00006443 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006444
Matt Carlson287be122009-08-28 13:58:46 +00006445 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00006446 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00006447 tp->dev->mtu > ETH_DATA_LEN)
6448 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
6449 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07006450
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451 /* Initialize invariants of the rings, we only set this
6452 * stuff once. This works because the card does not
6453 * write into the rx buffer posting rings.
6454 */
Matt Carlson2c49a442010-09-30 10:34:35 +00006455 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006456 struct tg3_rx_buffer_desc *rxd;
6457
Matt Carlson21f581a2009-08-28 14:00:25 +00006458 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00006459 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
6461 rxd->opaque = (RXD_OPAQUE_RING_STD |
6462 (i << RXD_OPAQUE_INDEX_SHIFT));
6463 }
6464
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006465 /* Now allocate fresh SKBs for each rx ring. */
6466 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson86b21e52009-11-13 13:03:45 +00006467 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006468 netdev_warn(tp->dev,
6469 "Using a smaller RX standard ring. Only "
6470 "%d out of %d buffers were allocated "
6471 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006472 if (i == 0)
6473 goto initfail;
6474 tp->rx_pending = i;
6475 break;
6476 }
6477 }
6478
Joe Perches63c3a662011-04-26 08:12:10 +00006479 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006480 goto done;
6481
Matt Carlson2c49a442010-09-30 10:34:35 +00006482 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006483
Joe Perches63c3a662011-04-26 08:12:10 +00006484 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00006485 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006486
Matt Carlson2c49a442010-09-30 10:34:35 +00006487 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00006488 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489
Matt Carlson0d86df82010-02-17 15:17:00 +00006490 rxd = &tpr->rx_jmb[i].std;
6491 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
6492 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
6493 RXD_FLAG_JUMBO;
6494 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
6495 (i << RXD_OPAQUE_INDEX_SHIFT));
6496 }
6497
6498 for (i = 0; i < tp->rx_jumbo_pending; i++) {
6499 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006500 netdev_warn(tp->dev,
6501 "Using a smaller RX jumbo ring. Only %d "
6502 "out of %d buffers were allocated "
6503 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00006504 if (i == 0)
6505 goto initfail;
6506 tp->rx_jumbo_pending = i;
6507 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006508 }
6509 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006510
6511done:
Michael Chan32d8c572006-07-25 16:38:29 -07006512 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006513
6514initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00006515 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006516 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517}
6518
Matt Carlson21f581a2009-08-28 14:00:25 +00006519static void tg3_rx_prodring_fini(struct tg3 *tp,
6520 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006521{
Matt Carlson21f581a2009-08-28 14:00:25 +00006522 kfree(tpr->rx_std_buffers);
6523 tpr->rx_std_buffers = NULL;
6524 kfree(tpr->rx_jmb_buffers);
6525 tpr->rx_jmb_buffers = NULL;
6526 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006527 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
6528 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006529 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006530 }
Matt Carlson21f581a2009-08-28 14:00:25 +00006531 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006532 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
6533 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006534 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006535 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006536}
6537
Matt Carlson21f581a2009-08-28 14:00:25 +00006538static int tg3_rx_prodring_init(struct tg3 *tp,
6539 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006540{
Matt Carlson2c49a442010-09-30 10:34:35 +00006541 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
6542 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006543 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006544 return -ENOMEM;
6545
Matt Carlson4bae65c2010-11-24 08:31:52 +00006546 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
6547 TG3_RX_STD_RING_BYTES(tp),
6548 &tpr->rx_std_mapping,
6549 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006550 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006551 goto err_out;
6552
Joe Perches63c3a662011-04-26 08:12:10 +00006553 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006554 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00006555 GFP_KERNEL);
6556 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006557 goto err_out;
6558
Matt Carlson4bae65c2010-11-24 08:31:52 +00006559 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
6560 TG3_RX_JMB_RING_BYTES(tp),
6561 &tpr->rx_jmb_mapping,
6562 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006563 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006564 goto err_out;
6565 }
6566
6567 return 0;
6568
6569err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00006570 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006571 return -ENOMEM;
6572}
6573
6574/* Free up pending packets in all rx/tx rings.
6575 *
6576 * The chip has been shut down and the driver detached from
6577 * the networking, so no interrupts or new tx packets will
6578 * end up in the driver. tp->{tx,}lock is not held and we are not
6579 * in an interrupt context and thus may sleep.
6580 */
6581static void tg3_free_rings(struct tg3 *tp)
6582{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006583 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006584
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006585 for (j = 0; j < tp->irq_cnt; j++) {
6586 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006587
Matt Carlson8fea32b2010-09-15 08:59:58 +00006588 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00006589
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006590 if (!tnapi->tx_buffers)
6591 continue;
6592
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006593 for (i = 0; i < TG3_TX_RING_SIZE; ) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006594 struct ring_info *txp;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006595 struct sk_buff *skb;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006596 unsigned int k;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006597
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006598 txp = &tnapi->tx_buffers[i];
6599 skb = txp->skb;
6600
6601 if (skb == NULL) {
6602 i++;
6603 continue;
6604 }
6605
Alexander Duyckf4188d82009-12-02 16:48:38 +00006606 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006607 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006608 skb_headlen(skb),
6609 PCI_DMA_TODEVICE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006610 txp->skb = NULL;
6611
Alexander Duyckf4188d82009-12-02 16:48:38 +00006612 i++;
6613
6614 for (k = 0; k < skb_shinfo(skb)->nr_frags; k++) {
6615 txp = &tnapi->tx_buffers[i & (TG3_TX_RING_SIZE - 1)];
6616 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006617 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006618 skb_shinfo(skb)->frags[k].size,
6619 PCI_DMA_TODEVICE);
6620 i++;
6621 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006622
6623 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006624 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006625 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006626}
6627
6628/* Initialize tx/rx rings for packet processing.
6629 *
6630 * The chip has been shut down and the driver detached from
6631 * the networking, so no interrupts or new tx packets will
6632 * end up in the driver. tp->{tx,}lock are held and thus
6633 * we may not sleep.
6634 */
6635static int tg3_init_rings(struct tg3 *tp)
6636{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006637 int i;
Matt Carlson72334482009-08-28 14:03:01 +00006638
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006639 /* Free up all the SKBs. */
6640 tg3_free_rings(tp);
6641
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006642 for (i = 0; i < tp->irq_cnt; i++) {
6643 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006644
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006645 tnapi->last_tag = 0;
6646 tnapi->last_irq_tag = 0;
6647 tnapi->hw_status->status = 0;
6648 tnapi->hw_status->status_tag = 0;
6649 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6650
6651 tnapi->tx_prod = 0;
6652 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006653 if (tnapi->tx_ring)
6654 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006655
6656 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006657 if (tnapi->rx_rcb)
6658 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006659
Matt Carlson8fea32b2010-09-15 08:59:58 +00006660 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00006661 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006662 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006663 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006664 }
Matt Carlson72334482009-08-28 14:03:01 +00006665
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006666 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006667}
6668
6669/*
6670 * Must not be invoked with interrupt sources disabled and
6671 * the hardware shutdown down.
6672 */
6673static void tg3_free_consistent(struct tg3 *tp)
6674{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006675 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006676
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006677 for (i = 0; i < tp->irq_cnt; i++) {
6678 struct tg3_napi *tnapi = &tp->napi[i];
6679
6680 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006681 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006682 tnapi->tx_ring, tnapi->tx_desc_mapping);
6683 tnapi->tx_ring = NULL;
6684 }
6685
6686 kfree(tnapi->tx_buffers);
6687 tnapi->tx_buffers = NULL;
6688
6689 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006690 dma_free_coherent(&tp->pdev->dev,
6691 TG3_RX_RCB_RING_BYTES(tp),
6692 tnapi->rx_rcb,
6693 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006694 tnapi->rx_rcb = NULL;
6695 }
6696
Matt Carlson8fea32b2010-09-15 08:59:58 +00006697 tg3_rx_prodring_fini(tp, &tnapi->prodring);
6698
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006699 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006700 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
6701 tnapi->hw_status,
6702 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006703 tnapi->hw_status = NULL;
6704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006705 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006706
Linus Torvalds1da177e2005-04-16 15:20:36 -07006707 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006708 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
6709 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710 tp->hw_stats = NULL;
6711 }
6712}
6713
6714/*
6715 * Must not be invoked with interrupt sources disabled and
6716 * the hardware shutdown down. Can sleep.
6717 */
6718static int tg3_alloc_consistent(struct tg3 *tp)
6719{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006720 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006721
Matt Carlson4bae65c2010-11-24 08:31:52 +00006722 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
6723 sizeof(struct tg3_hw_stats),
6724 &tp->stats_mapping,
6725 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006726 if (!tp->hw_stats)
6727 goto err_out;
6728
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6730
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006731 for (i = 0; i < tp->irq_cnt; i++) {
6732 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006733 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006734
Matt Carlson4bae65c2010-11-24 08:31:52 +00006735 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
6736 TG3_HW_STATUS_SIZE,
6737 &tnapi->status_mapping,
6738 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006739 if (!tnapi->hw_status)
6740 goto err_out;
6741
6742 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006743 sblk = tnapi->hw_status;
6744
Matt Carlson8fea32b2010-09-15 08:59:58 +00006745 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
6746 goto err_out;
6747
Matt Carlson19cfaec2009-12-03 08:36:20 +00006748 /* If multivector TSS is enabled, vector 0 does not handle
6749 * tx interrupts. Don't allocate any resources for it.
6750 */
Joe Perches63c3a662011-04-26 08:12:10 +00006751 if ((!i && !tg3_flag(tp, ENABLE_TSS)) ||
6752 (i && tg3_flag(tp, ENABLE_TSS))) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00006753 tnapi->tx_buffers = kzalloc(sizeof(struct ring_info) *
6754 TG3_TX_RING_SIZE,
6755 GFP_KERNEL);
6756 if (!tnapi->tx_buffers)
6757 goto err_out;
6758
Matt Carlson4bae65c2010-11-24 08:31:52 +00006759 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
6760 TG3_TX_RING_BYTES,
6761 &tnapi->tx_desc_mapping,
6762 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00006763 if (!tnapi->tx_ring)
6764 goto err_out;
6765 }
6766
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006767 /*
6768 * When RSS is enabled, the status block format changes
6769 * slightly. The "rx_jumbo_consumer", "reserved",
6770 * and "rx_mini_consumer" members get mapped to the
6771 * other three rx return ring producer indexes.
6772 */
6773 switch (i) {
6774 default:
6775 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
6776 break;
6777 case 2:
6778 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
6779 break;
6780 case 3:
6781 tnapi->rx_rcb_prod_idx = &sblk->reserved;
6782 break;
6783 case 4:
6784 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
6785 break;
6786 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006787
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006788 /*
6789 * If multivector RSS is enabled, vector 0 does not handle
6790 * rx or tx interrupts. Don't allocate any resources for it.
6791 */
Joe Perches63c3a662011-04-26 08:12:10 +00006792 if (!i && tg3_flag(tp, ENABLE_RSS))
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006793 continue;
6794
Matt Carlson4bae65c2010-11-24 08:31:52 +00006795 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
6796 TG3_RX_RCB_RING_BYTES(tp),
6797 &tnapi->rx_rcb_mapping,
6798 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006799 if (!tnapi->rx_rcb)
6800 goto err_out;
6801
6802 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006803 }
6804
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805 return 0;
6806
6807err_out:
6808 tg3_free_consistent(tp);
6809 return -ENOMEM;
6810}
6811
6812#define MAX_WAIT_CNT 1000
6813
6814/* To stop a block, clear the enable bit and poll till it
6815 * clears. tp->lock is held.
6816 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006817static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006818{
6819 unsigned int i;
6820 u32 val;
6821
Joe Perches63c3a662011-04-26 08:12:10 +00006822 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823 switch (ofs) {
6824 case RCVLSC_MODE:
6825 case DMAC_MODE:
6826 case MBFREE_MODE:
6827 case BUFMGR_MODE:
6828 case MEMARB_MODE:
6829 /* We can't enable/disable these bits of the
6830 * 5705/5750, just say success.
6831 */
6832 return 0;
6833
6834 default:
6835 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006837 }
6838
6839 val = tr32(ofs);
6840 val &= ~enable_bit;
6841 tw32_f(ofs, val);
6842
6843 for (i = 0; i < MAX_WAIT_CNT; i++) {
6844 udelay(100);
6845 val = tr32(ofs);
6846 if ((val & enable_bit) == 0)
6847 break;
6848 }
6849
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006850 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00006851 dev_err(&tp->pdev->dev,
6852 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
6853 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006854 return -ENODEV;
6855 }
6856
6857 return 0;
6858}
6859
6860/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006861static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006862{
6863 int i, err;
6864
6865 tg3_disable_ints(tp);
6866
6867 tp->rx_mode &= ~RX_MODE_ENABLE;
6868 tw32_f(MAC_RX_MODE, tp->rx_mode);
6869 udelay(10);
6870
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006871 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
6872 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
6873 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
6874 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
6875 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
6876 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006878 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
6879 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
6880 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
6881 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
6882 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
6883 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
6884 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006885
6886 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
6887 tw32_f(MAC_MODE, tp->mac_mode);
6888 udelay(40);
6889
6890 tp->tx_mode &= ~TX_MODE_ENABLE;
6891 tw32_f(MAC_TX_MODE, tp->tx_mode);
6892
6893 for (i = 0; i < MAX_WAIT_CNT; i++) {
6894 udelay(100);
6895 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
6896 break;
6897 }
6898 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00006899 dev_err(&tp->pdev->dev,
6900 "%s timed out, TX_MODE_ENABLE will not clear "
6901 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07006902 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903 }
6904
Michael Chane6de8ad2005-05-05 14:42:41 -07006905 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006906 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
6907 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908
6909 tw32(FTQ_RESET, 0xffffffff);
6910 tw32(FTQ_RESET, 0x00000000);
6911
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006912 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
6913 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006915 for (i = 0; i < tp->irq_cnt; i++) {
6916 struct tg3_napi *tnapi = &tp->napi[i];
6917 if (tnapi->hw_status)
6918 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006920 if (tp->hw_stats)
6921 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6922
Linus Torvalds1da177e2005-04-16 15:20:36 -07006923 return err;
6924}
6925
Matt Carlson0d3031d2007-10-10 18:02:43 -07006926static void tg3_ape_send_event(struct tg3 *tp, u32 event)
6927{
6928 int i;
6929 u32 apedata;
6930
Matt Carlsondc6d0742010-09-15 08:59:55 +00006931 /* NCSI does not support APE events */
Joe Perches63c3a662011-04-26 08:12:10 +00006932 if (tg3_flag(tp, APE_HAS_NCSI))
Matt Carlsondc6d0742010-09-15 08:59:55 +00006933 return;
6934
Matt Carlson0d3031d2007-10-10 18:02:43 -07006935 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
6936 if (apedata != APE_SEG_SIG_MAGIC)
6937 return;
6938
6939 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07006940 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006941 return;
6942
6943 /* Wait for up to 1 millisecond for APE to service previous event. */
6944 for (i = 0; i < 10; i++) {
6945 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
6946 return;
6947
6948 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
6949
6950 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6951 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
6952 event | APE_EVENT_STATUS_EVENT_PENDING);
6953
6954 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
6955
6956 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6957 break;
6958
6959 udelay(100);
6960 }
6961
6962 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6963 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
6964}
6965
6966static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
6967{
6968 u32 event;
6969 u32 apedata;
6970
Joe Perches63c3a662011-04-26 08:12:10 +00006971 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006972 return;
6973
6974 switch (kind) {
Matt Carlson33f401a2010-04-05 10:19:27 +00006975 case RESET_KIND_INIT:
6976 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
6977 APE_HOST_SEG_SIG_MAGIC);
6978 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
6979 APE_HOST_SEG_LEN_MAGIC);
6980 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
6981 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
6982 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
Matt Carlson6867c842010-07-11 09:31:44 +00006983 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
Matt Carlson33f401a2010-04-05 10:19:27 +00006984 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
6985 APE_HOST_BEHAV_NO_PHYLOCK);
Matt Carlsondc6d0742010-09-15 08:59:55 +00006986 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
6987 TG3_APE_HOST_DRVR_STATE_START);
Matt Carlson0d3031d2007-10-10 18:02:43 -07006988
Matt Carlson33f401a2010-04-05 10:19:27 +00006989 event = APE_EVENT_STATUS_STATE_START;
6990 break;
6991 case RESET_KIND_SHUTDOWN:
6992 /* With the interface we are currently using,
6993 * APE does not track driver state. Wiping
6994 * out the HOST SEGMENT SIGNATURE forces
6995 * the APE to assume OS absent status.
6996 */
6997 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
Matt Carlsonb2aee152008-11-03 16:51:11 -08006998
Matt Carlsondc6d0742010-09-15 08:59:55 +00006999 if (device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00007000 tg3_flag(tp, WOL_ENABLE)) {
Matt Carlsondc6d0742010-09-15 08:59:55 +00007001 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
7002 TG3_APE_HOST_WOL_SPEED_AUTO);
7003 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
7004 } else
7005 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
7006
7007 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
7008
Matt Carlson33f401a2010-04-05 10:19:27 +00007009 event = APE_EVENT_STATUS_STATE_UNLOAD;
7010 break;
7011 case RESET_KIND_SUSPEND:
7012 event = APE_EVENT_STATUS_STATE_SUSPEND;
7013 break;
7014 default:
7015 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007016 }
7017
7018 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
7019
7020 tg3_ape_send_event(tp, event);
7021}
7022
Michael Chane6af3012005-04-21 17:12:05 -07007023/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007024static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
7025{
David S. Millerf49639e2006-06-09 11:58:36 -07007026 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
7027 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007028
Joe Perches63c3a662011-04-26 08:12:10 +00007029 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007030 switch (kind) {
7031 case RESET_KIND_INIT:
7032 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7033 DRV_STATE_START);
7034 break;
7035
7036 case RESET_KIND_SHUTDOWN:
7037 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7038 DRV_STATE_UNLOAD);
7039 break;
7040
7041 case RESET_KIND_SUSPEND:
7042 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7043 DRV_STATE_SUSPEND);
7044 break;
7045
7046 default:
7047 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07007050
7051 if (kind == RESET_KIND_INIT ||
7052 kind == RESET_KIND_SUSPEND)
7053 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007054}
7055
7056/* tp->lock is held. */
7057static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
7058{
Joe Perches63c3a662011-04-26 08:12:10 +00007059 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007060 switch (kind) {
7061 case RESET_KIND_INIT:
7062 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7063 DRV_STATE_START_DONE);
7064 break;
7065
7066 case RESET_KIND_SHUTDOWN:
7067 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7068 DRV_STATE_UNLOAD_DONE);
7069 break;
7070
7071 default:
7072 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007074 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07007075
7076 if (kind == RESET_KIND_SHUTDOWN)
7077 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007078}
7079
7080/* tp->lock is held. */
7081static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
7082{
Joe Perches63c3a662011-04-26 08:12:10 +00007083 if (tg3_flag(tp, ENABLE_ASF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007084 switch (kind) {
7085 case RESET_KIND_INIT:
7086 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7087 DRV_STATE_START);
7088 break;
7089
7090 case RESET_KIND_SHUTDOWN:
7091 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7092 DRV_STATE_UNLOAD);
7093 break;
7094
7095 case RESET_KIND_SUSPEND:
7096 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7097 DRV_STATE_SUSPEND);
7098 break;
7099
7100 default:
7101 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103 }
7104}
7105
Michael Chan7a6f4362006-09-27 16:03:31 -07007106static int tg3_poll_fw(struct tg3 *tp)
7107{
7108 int i;
7109 u32 val;
7110
Michael Chanb5d37722006-09-27 16:06:21 -07007111 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08007112 /* Wait up to 20ms for init done. */
7113 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07007114 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
7115 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08007116 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07007117 }
7118 return -ENODEV;
7119 }
7120
Michael Chan7a6f4362006-09-27 16:03:31 -07007121 /* Wait for firmware initialization to complete. */
7122 for (i = 0; i < 100000; i++) {
7123 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
7124 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
7125 break;
7126 udelay(10);
7127 }
7128
7129 /* Chip might not be fitted with firmware. Some Sun onboard
7130 * parts are configured like that. So don't signal the timeout
7131 * of the above loop as an error, but do report the lack of
7132 * running firmware once.
7133 */
Joe Perches63c3a662011-04-26 08:12:10 +00007134 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
7135 tg3_flag_set(tp, NO_FWARE_REPORTED);
Michael Chan7a6f4362006-09-27 16:03:31 -07007136
Joe Perches05dbe002010-02-17 19:44:19 +00007137 netdev_info(tp->dev, "No firmware running\n");
Michael Chan7a6f4362006-09-27 16:03:31 -07007138 }
7139
Matt Carlson6b10c162010-02-12 14:47:08 +00007140 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
7141 /* The 57765 A0 needs a little more
7142 * time to do some important work.
7143 */
7144 mdelay(10);
7145 }
7146
Michael Chan7a6f4362006-09-27 16:03:31 -07007147 return 0;
7148}
7149
Michael Chanee6a99b2007-07-18 21:49:10 -07007150/* Save PCI command register before chip reset */
7151static void tg3_save_pci_state(struct tg3 *tp)
7152{
Matt Carlson8a6eac92007-10-21 16:17:55 -07007153 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007154}
7155
7156/* Restore PCI state after chip reset */
7157static void tg3_restore_pci_state(struct tg3 *tp)
7158{
7159 u32 val;
7160
7161 /* Re-enable indirect register accesses. */
7162 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7163 tp->misc_host_ctrl);
7164
7165 /* Set MAX PCI retry to zero. */
7166 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7167 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007168 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07007169 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007170 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00007171 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007172 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00007173 PCISTATE_ALLOW_APE_SHMEM_WR |
7174 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07007175 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7176
Matt Carlson8a6eac92007-10-21 16:17:55 -07007177 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007178
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007179 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
Joe Perches63c3a662011-04-26 08:12:10 +00007180 if (tg3_flag(tp, PCI_EXPRESS))
Matt Carlsoncf790032010-11-24 08:31:48 +00007181 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007182 else {
7183 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7184 tp->pci_cacheline_sz);
7185 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7186 tp->pci_lat_timer);
7187 }
Michael Chan114342f2007-10-15 02:12:26 -07007188 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08007189
Michael Chanee6a99b2007-07-18 21:49:10 -07007190 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00007191 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07007192 u16 pcix_cmd;
7193
7194 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7195 &pcix_cmd);
7196 pcix_cmd &= ~PCI_X_CMD_ERO;
7197 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7198 pcix_cmd);
7199 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007200
Joe Perches63c3a662011-04-26 08:12:10 +00007201 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007202
7203 /* Chip reset on 5780 will reset MSI enable bit,
7204 * so need to restore it.
7205 */
Joe Perches63c3a662011-04-26 08:12:10 +00007206 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007207 u16 ctrl;
7208
7209 pci_read_config_word(tp->pdev,
7210 tp->msi_cap + PCI_MSI_FLAGS,
7211 &ctrl);
7212 pci_write_config_word(tp->pdev,
7213 tp->msi_cap + PCI_MSI_FLAGS,
7214 ctrl | PCI_MSI_FLAGS_ENABLE);
7215 val = tr32(MSGINT_MODE);
7216 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7217 }
7218 }
7219}
7220
Linus Torvalds1da177e2005-04-16 15:20:36 -07007221static void tg3_stop_fw(struct tg3 *);
7222
7223/* tp->lock is held. */
7224static int tg3_chip_reset(struct tg3 *tp)
7225{
7226 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007227 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007228 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007229
David S. Millerf49639e2006-06-09 11:58:36 -07007230 tg3_nvram_lock(tp);
7231
Matt Carlson77b483f2008-08-15 14:07:24 -07007232 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7233
David S. Millerf49639e2006-06-09 11:58:36 -07007234 /* No matching tg3_nvram_unlock() after this because
7235 * chip reset below will undo the nvram lock.
7236 */
7237 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007238
Michael Chanee6a99b2007-07-18 21:49:10 -07007239 /* GRC_MISC_CFG core clock reset will clear the memory
7240 * enable bit in PCI register 4 and the MSI enable bit
7241 * on some chips, so we save relevant registers here.
7242 */
7243 tg3_save_pci_state(tp);
7244
Michael Chand9ab5ad12006-03-20 22:27:35 -08007245 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00007246 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08007247 tw32(GRC_FASTBOOT_PC, 0);
7248
Linus Torvalds1da177e2005-04-16 15:20:36 -07007249 /*
7250 * We must avoid the readl() that normally takes place.
7251 * It locks machines, causes machine checks, and other
7252 * fun things. So, temporarily disable the 5701
7253 * hardware workaround, while we do the reset.
7254 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007255 write_op = tp->write32;
7256 if (write_op == tg3_write_flush_reg32)
7257 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007258
Michael Chand18edcb2007-03-24 20:57:11 -07007259 /* Prevent the irq handler from reading or writing PCI registers
7260 * during chip reset when the memory enable bit in the PCI command
7261 * register may be cleared. The chip does not generate interrupt
7262 * at this time, but the irq handler may still be called due to irq
7263 * sharing or irqpoll.
7264 */
Joe Perches63c3a662011-04-26 08:12:10 +00007265 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007266 for (i = 0; i < tp->irq_cnt; i++) {
7267 struct tg3_napi *tnapi = &tp->napi[i];
7268 if (tnapi->hw_status) {
7269 tnapi->hw_status->status = 0;
7270 tnapi->hw_status->status_tag = 0;
7271 }
7272 tnapi->last_tag = 0;
7273 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007274 }
Michael Chand18edcb2007-03-24 20:57:11 -07007275 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007276
7277 for (i = 0; i < tp->irq_cnt; i++)
7278 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007279
Matt Carlson255ca312009-08-25 10:07:27 +00007280 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7281 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7282 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7283 }
7284
Linus Torvalds1da177e2005-04-16 15:20:36 -07007285 /* do the reset */
7286 val = GRC_MISC_CFG_CORECLK_RESET;
7287
Joe Perches63c3a662011-04-26 08:12:10 +00007288 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00007289 /* Force PCIe 1.0a mode */
7290 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007291 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00007292 tr32(TG3_PCIE_PHY_TSTCTL) ==
7293 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7294 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7295
Linus Torvalds1da177e2005-04-16 15:20:36 -07007296 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7297 tw32(GRC_MISC_CFG, (1 << 29));
7298 val |= (1 << 29);
7299 }
7300 }
7301
Michael Chanb5d37722006-09-27 16:06:21 -07007302 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7303 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7304 tw32(GRC_VCPU_EXT_CTRL,
7305 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7306 }
7307
Matt Carlsonf37500d2010-08-02 11:25:59 +00007308 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00007309 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007310 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007311
Linus Torvalds1da177e2005-04-16 15:20:36 -07007312 tw32(GRC_MISC_CFG, val);
7313
Michael Chan1ee582d2005-08-09 20:16:46 -07007314 /* restore 5701 hardware bug workaround write method */
7315 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007316
7317 /* Unfortunately, we have to delay before the PCI read back.
7318 * Some 575X chips even will not respond to a PCI cfg access
7319 * when the reset command is given to the chip.
7320 *
7321 * How do these hardware designers expect things to work
7322 * properly if the PCI write is posted for a long period
7323 * of time? It is always necessary to have some method by
7324 * which a register read back can occur to push the write
7325 * out which does the reset.
7326 *
7327 * For most tg3 variants the trick below was working.
7328 * Ho hum...
7329 */
7330 udelay(120);
7331
7332 /* Flush PCI posted writes. The normal MMIO registers
7333 * are inaccessible at this time so this is the only
7334 * way to make this reliably (actually, this is no longer
7335 * the case, see above). I tried to use indirect
7336 * register read/write but this upset some 5701 variants.
7337 */
7338 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7339
7340 udelay(120);
7341
Joe Perches63c3a662011-04-26 08:12:10 +00007342 if (tg3_flag(tp, PCI_EXPRESS) && tp->pcie_cap) {
Matt Carlsone7126992009-08-25 10:08:16 +00007343 u16 val16;
7344
Linus Torvalds1da177e2005-04-16 15:20:36 -07007345 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7346 int i;
7347 u32 cfg_val;
7348
7349 /* Wait for link training to complete. */
7350 for (i = 0; i < 5000; i++)
7351 udelay(100);
7352
7353 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7354 pci_write_config_dword(tp->pdev, 0xc4,
7355 cfg_val | (1 << 15));
7356 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007357
Matt Carlsone7126992009-08-25 10:08:16 +00007358 /* Clear the "no snoop" and "relaxed ordering" bits. */
7359 pci_read_config_word(tp->pdev,
7360 tp->pcie_cap + PCI_EXP_DEVCTL,
7361 &val16);
7362 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7363 PCI_EXP_DEVCTL_NOSNOOP_EN);
7364 /*
7365 * Older PCIe devices only support the 128 byte
7366 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007367 */
Joe Perches63c3a662011-04-26 08:12:10 +00007368 if (!tg3_flag(tp, CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007369 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007370 pci_write_config_word(tp->pdev,
7371 tp->pcie_cap + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007372 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007373
Matt Carlsoncf790032010-11-24 08:31:48 +00007374 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007375
7376 /* Clear error status */
7377 pci_write_config_word(tp->pdev,
7378 tp->pcie_cap + PCI_EXP_DEVSTA,
7379 PCI_EXP_DEVSTA_CED |
7380 PCI_EXP_DEVSTA_NFED |
7381 PCI_EXP_DEVSTA_FED |
7382 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007383 }
7384
Michael Chanee6a99b2007-07-18 21:49:10 -07007385 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007386
Joe Perches63c3a662011-04-26 08:12:10 +00007387 tg3_flag_clear(tp, CHIP_RESETTING);
7388 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07007389
Michael Chanee6a99b2007-07-18 21:49:10 -07007390 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007391 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07007392 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007393 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007394
7395 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7396 tg3_stop_fw(tp);
7397 tw32(0x5000, 0x400);
7398 }
7399
7400 tw32(GRC_MODE, tp->grc_mode);
7401
7402 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007403 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007404
7405 tw32(0xc4, val | (1 << 15));
7406 }
7407
7408 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7409 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7410 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7411 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7412 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7413 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7414 }
7415
Joe Perches63c3a662011-04-26 08:12:10 +00007416 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007417 tp->mac_mode = MAC_MODE_APE_TX_EN |
7418 MAC_MODE_APE_RX_EN |
7419 MAC_MODE_TDE_ENABLE;
7420
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007421 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007422 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
7423 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007424 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007425 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7426 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007427 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007428 val = 0;
7429
7430 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007431 udelay(40);
7432
Matt Carlson77b483f2008-08-15 14:07:24 -07007433 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7434
Michael Chan7a6f4362006-09-27 16:03:31 -07007435 err = tg3_poll_fw(tp);
7436 if (err)
7437 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007438
Matt Carlson0a9140c2009-08-28 12:27:50 +00007439 tg3_mdio_start(tp);
7440
Joe Perches63c3a662011-04-26 08:12:10 +00007441 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007442 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7443 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007444 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007445 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007446
7447 tw32(0x7c00, val | (1 << 25));
7448 }
7449
Matt Carlsond78b59f2011-04-05 14:22:46 +00007450 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
7451 val = tr32(TG3_CPMU_CLCK_ORIDE);
7452 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
7453 }
7454
Linus Torvalds1da177e2005-04-16 15:20:36 -07007455 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00007456 tg3_flag_clear(tp, ENABLE_ASF);
7457 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007458 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7459 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7460 u32 nic_cfg;
7461
7462 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7463 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00007464 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007465 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00007466 if (tg3_flag(tp, 5750_PLUS))
7467 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007468 }
7469 }
7470
7471 return 0;
7472}
7473
7474/* tp->lock is held. */
7475static void tg3_stop_fw(struct tg3 *tp)
7476{
Joe Perches63c3a662011-04-26 08:12:10 +00007477 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07007478 /* Wait for RX cpu to ACK the previous event. */
7479 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007480
7481 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007482
7483 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007484
Matt Carlson7c5026a2008-05-02 16:49:29 -07007485 /* Wait for RX cpu to ACK this event. */
7486 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007487 }
7488}
7489
7490/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007491static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007492{
7493 int err;
7494
7495 tg3_stop_fw(tp);
7496
Michael Chan944d9802005-05-29 14:57:48 -07007497 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007498
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007499 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007500 err = tg3_chip_reset(tp);
7501
Matt Carlsondaba2a62009-04-20 06:58:52 +00007502 __tg3_set_mac_addr(tp, 0);
7503
Michael Chan944d9802005-05-29 14:57:48 -07007504 tg3_write_sig_legacy(tp, kind);
7505 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007506
7507 if (err)
7508 return err;
7509
7510 return 0;
7511}
7512
Linus Torvalds1da177e2005-04-16 15:20:36 -07007513#define RX_CPU_SCRATCH_BASE 0x30000
7514#define RX_CPU_SCRATCH_SIZE 0x04000
7515#define TX_CPU_SCRATCH_BASE 0x34000
7516#define TX_CPU_SCRATCH_SIZE 0x04000
7517
7518/* tp->lock is held. */
7519static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
7520{
7521 int i;
7522
Joe Perches63c3a662011-04-26 08:12:10 +00007523 BUG_ON(offset == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007524
Michael Chanb5d37722006-09-27 16:06:21 -07007525 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7526 u32 val = tr32(GRC_VCPU_EXT_CTRL);
7527
7528 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
7529 return 0;
7530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007531 if (offset == RX_CPU_BASE) {
7532 for (i = 0; i < 10000; i++) {
7533 tw32(offset + CPU_STATE, 0xffffffff);
7534 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7535 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7536 break;
7537 }
7538
7539 tw32(offset + CPU_STATE, 0xffffffff);
7540 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
7541 udelay(10);
7542 } else {
7543 for (i = 0; i < 10000; i++) {
7544 tw32(offset + CPU_STATE, 0xffffffff);
7545 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7546 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7547 break;
7548 }
7549 }
7550
7551 if (i >= 10000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007552 netdev_err(tp->dev, "%s timed out, %s CPU\n",
7553 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007554 return -ENODEV;
7555 }
Michael Chanec41c7d2006-01-17 02:40:55 -08007556
7557 /* Clear firmware's nvram arbitration. */
Joe Perches63c3a662011-04-26 08:12:10 +00007558 if (tg3_flag(tp, NVRAM))
Michael Chanec41c7d2006-01-17 02:40:55 -08007559 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007560 return 0;
7561}
7562
7563struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007564 unsigned int fw_base;
7565 unsigned int fw_len;
7566 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007567};
7568
7569/* tp->lock is held. */
7570static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
7571 int cpu_scratch_size, struct fw_info *info)
7572{
Michael Chanec41c7d2006-01-17 02:40:55 -08007573 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007574 void (*write_op)(struct tg3 *, u32, u32);
7575
Joe Perches63c3a662011-04-26 08:12:10 +00007576 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007577 netdev_err(tp->dev,
7578 "%s: Trying to load TX cpu firmware which is 5705\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007579 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007580 return -EINVAL;
7581 }
7582
Joe Perches63c3a662011-04-26 08:12:10 +00007583 if (tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007584 write_op = tg3_write_mem;
7585 else
7586 write_op = tg3_write_indirect_reg32;
7587
Michael Chan1b628152005-05-29 14:59:49 -07007588 /* It is possible that bootcode is still loading at this point.
7589 * Get the nvram lock first before halting the cpu.
7590 */
Michael Chanec41c7d2006-01-17 02:40:55 -08007591 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007592 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08007593 if (!lock_err)
7594 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007595 if (err)
7596 goto out;
7597
7598 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
7599 write_op(tp, cpu_scratch_base + i, 0);
7600 tw32(cpu_base + CPU_STATE, 0xffffffff);
7601 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007602 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007603 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007604 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07007605 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007606 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007607
7608 err = 0;
7609
7610out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007611 return err;
7612}
7613
7614/* tp->lock is held. */
7615static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
7616{
7617 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007618 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007619 int err, i;
7620
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007621 fw_data = (void *)tp->fw->data;
7622
7623 /* Firmware blob starts with version numbers, followed by
7624 start address and length. We are setting complete length.
7625 length = end_address_of_bss - start_address_of_text.
7626 Remainder is the blob to be loaded contiguously
7627 from start address. */
7628
7629 info.fw_base = be32_to_cpu(fw_data[1]);
7630 info.fw_len = tp->fw->size - 12;
7631 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007632
7633 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
7634 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
7635 &info);
7636 if (err)
7637 return err;
7638
7639 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
7640 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
7641 &info);
7642 if (err)
7643 return err;
7644
7645 /* Now startup only the RX cpu. */
7646 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007647 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007648
7649 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007650 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007651 break;
7652 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7653 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007654 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007655 udelay(1000);
7656 }
7657 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007658 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
7659 "should be %08x\n", __func__,
Joe Perches05dbe002010-02-17 19:44:19 +00007660 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007661 return -ENODEV;
7662 }
7663 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7664 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
7665
7666 return 0;
7667}
7668
Linus Torvalds1da177e2005-04-16 15:20:36 -07007669/* tp->lock is held. */
7670static int tg3_load_tso_firmware(struct tg3 *tp)
7671{
7672 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007673 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007674 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
7675 int err, i;
7676
Joe Perches63c3a662011-04-26 08:12:10 +00007677 if (tg3_flag(tp, HW_TSO_1) ||
7678 tg3_flag(tp, HW_TSO_2) ||
7679 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007680 return 0;
7681
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007682 fw_data = (void *)tp->fw->data;
7683
7684 /* Firmware blob starts with version numbers, followed by
7685 start address and length. We are setting complete length.
7686 length = end_address_of_bss - start_address_of_text.
7687 Remainder is the blob to be loaded contiguously
7688 from start address. */
7689
7690 info.fw_base = be32_to_cpu(fw_data[1]);
7691 cpu_scratch_size = tp->fw_len;
7692 info.fw_len = tp->fw->size - 12;
7693 info.fw_data = &fw_data[3];
7694
Linus Torvalds1da177e2005-04-16 15:20:36 -07007695 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007696 cpu_base = RX_CPU_BASE;
7697 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007698 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007699 cpu_base = TX_CPU_BASE;
7700 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
7701 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
7702 }
7703
7704 err = tg3_load_firmware_cpu(tp, cpu_base,
7705 cpu_scratch_base, cpu_scratch_size,
7706 &info);
7707 if (err)
7708 return err;
7709
7710 /* Now startup the cpu. */
7711 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007712 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007713
7714 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007715 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007716 break;
7717 tw32(cpu_base + CPU_STATE, 0xffffffff);
7718 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007719 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007720 udelay(1000);
7721 }
7722 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007723 netdev_err(tp->dev,
7724 "%s fails to set CPU PC, is %08x should be %08x\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007725 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007726 return -ENODEV;
7727 }
7728 tw32(cpu_base + CPU_STATE, 0xffffffff);
7729 tw32_f(cpu_base + CPU_MODE, 0x00000000);
7730 return 0;
7731}
7732
Linus Torvalds1da177e2005-04-16 15:20:36 -07007733
Linus Torvalds1da177e2005-04-16 15:20:36 -07007734static int tg3_set_mac_addr(struct net_device *dev, void *p)
7735{
7736 struct tg3 *tp = netdev_priv(dev);
7737 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007738 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007739
Michael Chanf9804dd2005-09-27 12:13:10 -07007740 if (!is_valid_ether_addr(addr->sa_data))
7741 return -EINVAL;
7742
Linus Torvalds1da177e2005-04-16 15:20:36 -07007743 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7744
Michael Chane75f7c92006-03-20 21:33:26 -08007745 if (!netif_running(dev))
7746 return 0;
7747
Joe Perches63c3a662011-04-26 08:12:10 +00007748 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007749 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007750
Michael Chan986e0ae2007-05-05 12:10:20 -07007751 addr0_high = tr32(MAC_ADDR_0_HIGH);
7752 addr0_low = tr32(MAC_ADDR_0_LOW);
7753 addr1_high = tr32(MAC_ADDR_1_HIGH);
7754 addr1_low = tr32(MAC_ADDR_1_LOW);
7755
7756 /* Skip MAC addr 1 if ASF is using it. */
7757 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7758 !(addr1_high == 0 && addr1_low == 0))
7759 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007760 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007761 spin_lock_bh(&tp->lock);
7762 __tg3_set_mac_addr(tp, skip_mac_1);
7763 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007764
Michael Chanb9ec6c12006-07-25 16:37:27 -07007765 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007766}
7767
7768/* tp->lock is held. */
7769static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7770 dma_addr_t mapping, u32 maxlen_flags,
7771 u32 nic_addr)
7772{
7773 tg3_write_mem(tp,
7774 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7775 ((u64) mapping >> 32));
7776 tg3_write_mem(tp,
7777 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7778 ((u64) mapping & 0xffffffff));
7779 tg3_write_mem(tp,
7780 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7781 maxlen_flags);
7782
Joe Perches63c3a662011-04-26 08:12:10 +00007783 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007784 tg3_write_mem(tp,
7785 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7786 nic_addr);
7787}
7788
7789static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007790static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007791{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007792 int i;
7793
Joe Perches63c3a662011-04-26 08:12:10 +00007794 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007795 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7796 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7797 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007798 } else {
7799 tw32(HOSTCC_TXCOL_TICKS, 0);
7800 tw32(HOSTCC_TXMAX_FRAMES, 0);
7801 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007802 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007803
Joe Perches63c3a662011-04-26 08:12:10 +00007804 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007805 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
7806 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
7807 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
7808 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007809 tw32(HOSTCC_RXCOL_TICKS, 0);
7810 tw32(HOSTCC_RXMAX_FRAMES, 0);
7811 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07007812 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007813
Joe Perches63c3a662011-04-26 08:12:10 +00007814 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07007815 u32 val = ec->stats_block_coalesce_usecs;
7816
Matt Carlsonb6080e12009-09-01 13:12:00 +00007817 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
7818 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
7819
David S. Miller15f98502005-05-18 22:49:26 -07007820 if (!netif_carrier_ok(tp->dev))
7821 val = 0;
7822
7823 tw32(HOSTCC_STAT_COAL_TICKS, val);
7824 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007825
7826 for (i = 0; i < tp->irq_cnt - 1; i++) {
7827 u32 reg;
7828
7829 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
7830 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007831 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
7832 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007833 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
7834 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007835
Joe Perches63c3a662011-04-26 08:12:10 +00007836 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007837 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
7838 tw32(reg, ec->tx_coalesce_usecs);
7839 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
7840 tw32(reg, ec->tx_max_coalesced_frames);
7841 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
7842 tw32(reg, ec->tx_max_coalesced_frames_irq);
7843 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007844 }
7845
7846 for (; i < tp->irq_max - 1; i++) {
7847 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007848 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007849 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007850
Joe Perches63c3a662011-04-26 08:12:10 +00007851 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007852 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
7853 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
7854 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7855 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007856 }
David S. Miller15f98502005-05-18 22:49:26 -07007857}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007858
7859/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00007860static void tg3_rings_reset(struct tg3 *tp)
7861{
7862 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007863 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007864 struct tg3_napi *tnapi = &tp->napi[0];
7865
7866 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00007867 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007868 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00007869 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00007870 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlsonb703df62009-12-03 08:36:21 +00007871 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
7872 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007873 else
7874 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7875
7876 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7877 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
7878 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
7879 BDINFO_FLAGS_DISABLED);
7880
7881
7882 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00007883 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007884 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00007885 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007886 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007887 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7888 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00007889 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
7890 else
7891 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7892
7893 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7894 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
7895 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
7896 BDINFO_FLAGS_DISABLED);
7897
7898 /* Disable interrupts */
7899 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
7900
7901 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00007902 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00007903 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007904 tp->napi[i].tx_prod = 0;
7905 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007906 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00007907 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007908 tw32_rx_mbox(tp->napi[i].consmbox, 0);
7909 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
7910 }
Joe Perches63c3a662011-04-26 08:12:10 +00007911 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00007912 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007913 } else {
7914 tp->napi[0].tx_prod = 0;
7915 tp->napi[0].tx_cons = 0;
7916 tw32_mailbox(tp->napi[0].prodmbox, 0);
7917 tw32_rx_mbox(tp->napi[0].consmbox, 0);
7918 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007919
7920 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00007921 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00007922 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
7923 for (i = 0; i < 16; i++)
7924 tw32_tx_mbox(mbox + i * 8, 0);
7925 }
7926
7927 txrcb = NIC_SRAM_SEND_RCB;
7928 rxrcb = NIC_SRAM_RCV_RET_RCB;
7929
7930 /* Clear status block in ram. */
7931 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7932
7933 /* Set status block DMA address */
7934 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7935 ((u64) tnapi->status_mapping >> 32));
7936 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7937 ((u64) tnapi->status_mapping & 0xffffffff));
7938
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007939 if (tnapi->tx_ring) {
7940 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7941 (TG3_TX_RING_SIZE <<
7942 BDINFO_FLAGS_MAXLEN_SHIFT),
7943 NIC_SRAM_TX_BUFFER_DESC);
7944 txrcb += TG3_BDINFO_SIZE;
7945 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007946
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007947 if (tnapi->rx_rcb) {
7948 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007949 (tp->rx_ret_ring_mask + 1) <<
7950 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007951 rxrcb += TG3_BDINFO_SIZE;
7952 }
7953
7954 stblk = HOSTCC_STATBLCK_RING1;
7955
7956 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
7957 u64 mapping = (u64)tnapi->status_mapping;
7958 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
7959 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
7960
7961 /* Clear status block in ram. */
7962 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7963
Matt Carlson19cfaec2009-12-03 08:36:20 +00007964 if (tnapi->tx_ring) {
7965 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7966 (TG3_TX_RING_SIZE <<
7967 BDINFO_FLAGS_MAXLEN_SHIFT),
7968 NIC_SRAM_TX_BUFFER_DESC);
7969 txrcb += TG3_BDINFO_SIZE;
7970 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007971
7972 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007973 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007974 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7975
7976 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007977 rxrcb += TG3_BDINFO_SIZE;
7978 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007979}
7980
Matt Carlsoneb07a942011-04-20 07:57:36 +00007981static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
7982{
7983 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
7984
Joe Perches63c3a662011-04-26 08:12:10 +00007985 if (!tg3_flag(tp, 5750_PLUS) ||
7986 tg3_flag(tp, 5780_CLASS) ||
Matt Carlsoneb07a942011-04-20 07:57:36 +00007987 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
7988 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
7989 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
7990 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7991 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
7992 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
7993 else
7994 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
7995
7996 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
7997 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
7998
7999 val = min(nic_rep_thresh, host_rep_thresh);
8000 tw32(RCVBDI_STD_THRESH, val);
8001
Joe Perches63c3a662011-04-26 08:12:10 +00008002 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008003 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
8004
Joe Perches63c3a662011-04-26 08:12:10 +00008005 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008006 return;
8007
Joe Perches63c3a662011-04-26 08:12:10 +00008008 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008009 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
8010 else
8011 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5717;
8012
8013 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
8014
8015 val = min(bdcache_maxcnt / 2, host_rep_thresh);
8016 tw32(RCVBDI_JUMBO_THRESH, val);
8017
Joe Perches63c3a662011-04-26 08:12:10 +00008018 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008019 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
8020}
8021
Matt Carlson2d31eca2009-09-01 12:53:31 +00008022/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008023static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008024{
8025 u32 val, rdmac_mode;
8026 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008027 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008028
8029 tg3_disable_ints(tp);
8030
8031 tg3_stop_fw(tp);
8032
8033 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
8034
Joe Perches63c3a662011-04-26 08:12:10 +00008035 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07008036 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008037
Matt Carlson699c0192010-12-06 08:28:51 +00008038 /* Enable MAC control of LPI */
8039 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
8040 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
8041 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
8042 TG3_CPMU_EEE_LNKIDL_UART_IDL);
8043
8044 tw32_f(TG3_CPMU_EEE_CTRL,
8045 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
8046
Matt Carlsona386b902010-12-06 08:28:53 +00008047 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
8048 TG3_CPMU_EEEMD_LPI_IN_TX |
8049 TG3_CPMU_EEEMD_LPI_IN_RX |
8050 TG3_CPMU_EEEMD_EEE_ENABLE;
8051
8052 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8053 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
8054
Joe Perches63c3a662011-04-26 08:12:10 +00008055 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00008056 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
8057
8058 tw32_f(TG3_CPMU_EEE_MODE, val);
8059
8060 tw32_f(TG3_CPMU_EEE_DBTMR1,
8061 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
8062 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
8063
8064 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00008065 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00008066 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00008067 }
8068
Matt Carlson603f1172010-02-12 14:47:10 +00008069 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08008070 tg3_phy_reset(tp);
8071
Linus Torvalds1da177e2005-04-16 15:20:36 -07008072 err = tg3_chip_reset(tp);
8073 if (err)
8074 return err;
8075
8076 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
8077
Matt Carlsonbcb37f62008-11-03 16:52:09 -08008078 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008079 val = tr32(TG3_CPMU_CTRL);
8080 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
8081 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08008082
8083 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8084 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8085 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8086 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
8087
8088 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
8089 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
8090 val |= CPMU_LNK_AWARE_MACCLK_6_25;
8091 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
8092
8093 val = tr32(TG3_CPMU_HST_ACC);
8094 val &= ~CPMU_HST_ACC_MACCLK_MASK;
8095 val |= CPMU_HST_ACC_MACCLK_6_25;
8096 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07008097 }
8098
Matt Carlson33466d92009-04-20 06:57:41 +00008099 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
8100 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
8101 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
8102 PCIE_PWR_MGMT_L1_THRESH_4MS;
8103 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00008104
8105 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
8106 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
8107
8108 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00008109
Matt Carlsonf40386c2009-11-02 14:24:02 +00008110 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8111 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00008112 }
8113
Joe Perches63c3a662011-04-26 08:12:10 +00008114 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00008115 u32 grc_mode = tr32(GRC_MODE);
8116
8117 /* Access the lower 1K of PL PCIE block registers. */
8118 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8119 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
8120
8121 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
8122 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
8123 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
8124
8125 tw32(GRC_MODE, grc_mode);
8126 }
8127
Matt Carlson5093eed2010-11-24 08:31:45 +00008128 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
8129 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
8130 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00008131
Matt Carlson5093eed2010-11-24 08:31:45 +00008132 /* Access the lower 1K of PL PCIE block registers. */
8133 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8134 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00008135
Matt Carlson5093eed2010-11-24 08:31:45 +00008136 val = tr32(TG3_PCIE_TLDLPL_PORT +
8137 TG3_PCIE_PL_LO_PHYCTL5);
8138 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
8139 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00008140
Matt Carlson5093eed2010-11-24 08:31:45 +00008141 tw32(GRC_MODE, grc_mode);
8142 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00008143
8144 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8145 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8146 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8147 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008148 }
8149
Linus Torvalds1da177e2005-04-16 15:20:36 -07008150 /* This works around an issue with Athlon chipsets on
8151 * B3 tigon3 silicon. This bit has no effect on any
8152 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008153 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008154 */
Joe Perches63c3a662011-04-26 08:12:10 +00008155 if (!tg3_flag(tp, CPMU_PRESENT)) {
8156 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07008157 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8158 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008160
8161 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008162 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008163 val = tr32(TG3PCI_PCISTATE);
8164 val |= PCISTATE_RETRY_SAME_DMA;
8165 tw32(TG3PCI_PCISTATE, val);
8166 }
8167
Joe Perches63c3a662011-04-26 08:12:10 +00008168 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07008169 /* Allow reads and writes to the
8170 * APE register and memory space.
8171 */
8172 val = tr32(TG3PCI_PCISTATE);
8173 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008174 PCISTATE_ALLOW_APE_SHMEM_WR |
8175 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008176 tw32(TG3PCI_PCISTATE, val);
8177 }
8178
Linus Torvalds1da177e2005-04-16 15:20:36 -07008179 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8180 /* Enable some hw fixes. */
8181 val = tr32(TG3PCI_MSI_DATA);
8182 val |= (1 << 26) | (1 << 28) | (1 << 29);
8183 tw32(TG3PCI_MSI_DATA, val);
8184 }
8185
8186 /* Descriptor ring init may make accesses to the
8187 * NIC SRAM area to setup the TX descriptors, so we
8188 * can only do this after the hardware has been
8189 * successfully reset.
8190 */
Michael Chan32d8c572006-07-25 16:38:29 -07008191 err = tg3_init_rings(tp);
8192 if (err)
8193 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008194
Joe Perches63c3a662011-04-26 08:12:10 +00008195 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008196 val = tr32(TG3PCI_DMA_RW_CTRL) &
8197 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008198 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8199 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson0aebff42011-04-25 12:42:45 +00008200 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765 &&
8201 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8202 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008203 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8204 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8205 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008206 /* This value is determined during the probe time DMA
8207 * engine test, tg3_test_dma.
8208 */
8209 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008211
8212 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8213 GRC_MODE_4X_NIC_SEND_RINGS |
8214 GRC_MODE_NO_TX_PHDR_CSUM |
8215 GRC_MODE_NO_RX_PHDR_CSUM);
8216 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07008217
8218 /* Pseudo-header checksum is done by hardware logic and not
8219 * the offload processers, so make the chip do the pseudo-
8220 * header checksums on receive. For transmit it is more
8221 * convenient to do the pseudo-header checksum in software
8222 * as Linux does that on transmit for us in all cases.
8223 */
8224 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008225
8226 tw32(GRC_MODE,
8227 tp->grc_mode |
8228 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8229
8230 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8231 val = tr32(GRC_MISC_CFG);
8232 val &= ~0xff;
8233 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8234 tw32(GRC_MISC_CFG, val);
8235
8236 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00008237 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008238 /* Do nothing. */
8239 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8240 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8241 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8242 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8243 else
8244 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8245 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8246 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00008247 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008248 int fw_len;
8249
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008250 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008251 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8252 tw32(BUFMGR_MB_POOL_ADDR,
8253 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8254 tw32(BUFMGR_MB_POOL_SIZE,
8255 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008257
Michael Chan0f893dc2005-07-25 12:30:38 -07008258 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008259 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8260 tp->bufmgr_config.mbuf_read_dma_low_water);
8261 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8262 tp->bufmgr_config.mbuf_mac_rx_low_water);
8263 tw32(BUFMGR_MB_HIGH_WATER,
8264 tp->bufmgr_config.mbuf_high_water);
8265 } else {
8266 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8267 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8268 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8269 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8270 tw32(BUFMGR_MB_HIGH_WATER,
8271 tp->bufmgr_config.mbuf_high_water_jumbo);
8272 }
8273 tw32(BUFMGR_DMA_LOW_WATER,
8274 tp->bufmgr_config.dma_low_water);
8275 tw32(BUFMGR_DMA_HIGH_WATER,
8276 tp->bufmgr_config.dma_high_water);
8277
Matt Carlsond309a462010-09-30 10:34:31 +00008278 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8279 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8280 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00008281 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8282 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
8283 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
8284 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00008285 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008286 for (i = 0; i < 2000; i++) {
8287 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8288 break;
8289 udelay(10);
8290 }
8291 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008292 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008293 return -ENODEV;
8294 }
8295
Matt Carlsoneb07a942011-04-20 07:57:36 +00008296 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8297 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07008298
Matt Carlsoneb07a942011-04-20 07:57:36 +00008299 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008300
8301 /* Initialize TG3_BDINFO's at:
8302 * RCVDBDI_STD_BD: standard eth size rx ring
8303 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8304 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8305 *
8306 * like so:
8307 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8308 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8309 * ring attribute flags
8310 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8311 *
8312 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8313 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8314 *
8315 * The size of each ring is fixed in the firmware, but the location is
8316 * configurable.
8317 */
8318 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008319 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008320 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008321 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00008322 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00008323 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8324 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008325
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008326 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00008327 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008328 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8329 BDINFO_FLAGS_DISABLED);
8330
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008331 /* Program the jumbo buffer descriptor ring control
8332 * blocks on those devices that have them.
8333 */
Matt Carlsonbb18bb92011-03-09 16:58:19 +00008334 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008335 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008336
Joe Perches63c3a662011-04-26 08:12:10 +00008337 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008338 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008339 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008340 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008341 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008342 val = TG3_RX_JMB_RING_SIZE(tp) <<
8343 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008344 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008345 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00008346 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Matt Carlsona50d0792010-06-05 17:24:37 +00008347 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson87668d32009-11-13 13:03:34 +00008348 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8349 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008350 } else {
8351 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8352 BDINFO_FLAGS_DISABLED);
8353 }
8354
Joe Perches63c3a662011-04-26 08:12:10 +00008355 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008356 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsonde9f5232011-04-05 14:22:43 +00008357 val = TG3_RX_STD_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008358 else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008359 val = TG3_RX_STD_MAX_SIZE_5717;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008360 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8361 val |= (TG3_RX_STD_DMA_SZ << 2);
8362 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008363 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008364 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008365 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008366
8367 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008368
Matt Carlson411da642009-11-13 13:03:46 +00008369 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00008370 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008371
Joe Perches63c3a662011-04-26 08:12:10 +00008372 tpr->rx_jmb_prod_idx =
8373 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00008374 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008375
Matt Carlson2d31eca2009-09-01 12:53:31 +00008376 tg3_rings_reset(tp);
8377
Linus Torvalds1da177e2005-04-16 15:20:36 -07008378 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008379 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008380
8381 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008382 tw32(MAC_RX_MTU_SIZE,
8383 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008384
8385 /* The slot time is changed by tg3_setup_phy if we
8386 * run at gigabit with half duplex.
8387 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00008388 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8389 (6 << TX_LENGTHS_IPG_SHIFT) |
8390 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8391
8392 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8393 val |= tr32(MAC_TX_LENGTHS) &
8394 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8395 TX_LENGTHS_CNT_DWN_VAL_MSK);
8396
8397 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008398
8399 /* Receive rules. */
8400 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8401 tw32(RCVLPC_CONFIG, 0x0181);
8402
8403 /* Calculate RDMAC_MODE setting early, we need it to determine
8404 * the RCVLPC_STATE_ENABLE mask.
8405 */
8406 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8407 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8408 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8409 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8410 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008411
Matt Carlsondeabaac2010-11-24 08:31:50 +00008412 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008413 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8414
Matt Carlson57e69832008-05-25 23:48:31 -07008415 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008416 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8417 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008418 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8419 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8420 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8421
Matt Carlsonc5908932011-03-09 16:58:25 +00008422 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8423 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008424 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008425 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008426 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8427 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008428 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008429 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8430 }
8431 }
8432
Joe Perches63c3a662011-04-26 08:12:10 +00008433 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07008434 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8435
Joe Perches63c3a662011-04-26 08:12:10 +00008436 if (tg3_flag(tp, HW_TSO_1) ||
8437 tg3_flag(tp, HW_TSO_2) ||
8438 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08008439 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8440
Joe Perches63c3a662011-04-26 08:12:10 +00008441 if (tg3_flag(tp, HW_TSO_3) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00008442 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008443 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8444 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008445
Matt Carlsonf2096f92011-04-05 14:22:48 +00008446 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8447 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8448
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008449 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8450 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8451 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8452 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008453 tg3_flag(tp, 57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008454 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00008455 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8456 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008457 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8458 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8459 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8460 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8461 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8462 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008463 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008464 tw32(TG3_RDMA_RSRVCTRL_REG,
8465 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8466 }
8467
Matt Carlsond78b59f2011-04-05 14:22:46 +00008468 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8469 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00008470 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8471 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8472 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8473 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8474 }
8475
Linus Torvalds1da177e2005-04-16 15:20:36 -07008476 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00008477 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07008478 val = tr32(RCVLPC_STATS_ENABLE);
8479 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8480 tw32(RCVLPC_STATS_ENABLE, val);
8481 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008482 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008483 val = tr32(RCVLPC_STATS_ENABLE);
8484 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8485 tw32(RCVLPC_STATS_ENABLE, val);
8486 } else {
8487 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8488 }
8489 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8490 tw32(SNDDATAI_STATSENAB, 0xffffff);
8491 tw32(SNDDATAI_STATSCTRL,
8492 (SNDDATAI_SCTRL_ENABLE |
8493 SNDDATAI_SCTRL_FASTUPD));
8494
8495 /* Setup host coalescing engine. */
8496 tw32(HOSTCC_MODE, 0);
8497 for (i = 0; i < 2000; i++) {
8498 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8499 break;
8500 udelay(10);
8501 }
8502
Michael Chand244c892005-07-05 14:42:33 -07008503 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008504
Joe Perches63c3a662011-04-26 08:12:10 +00008505 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008506 /* Status/statistics block address. See tg3_timer,
8507 * the tg3_periodic_fetch_stats call there, and
8508 * tg3_get_stats to see how this works for 5705/5750 chips.
8509 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008510 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8511 ((u64) tp->stats_mapping >> 32));
8512 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8513 ((u64) tp->stats_mapping & 0xffffffff));
8514 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008515
Linus Torvalds1da177e2005-04-16 15:20:36 -07008516 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008517
8518 /* Clear statistics and status block memory areas */
8519 for (i = NIC_SRAM_STATS_BLK;
8520 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8521 i += sizeof(u32)) {
8522 tg3_write_mem(tp, i, 0);
8523 udelay(40);
8524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008525 }
8526
8527 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8528
8529 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8530 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008531 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008532 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8533
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008534 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8535 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07008536 /* reset to prevent losing 1st rx packet intermittently */
8537 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8538 udelay(10);
8539 }
8540
Joe Perches63c3a662011-04-26 08:12:10 +00008541 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008542 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -07008543 else
8544 tp->mac_mode = 0;
8545 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07008546 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008547 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008548 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008549 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8550 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008551 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8552 udelay(40);
8553
Michael Chan314fba32005-04-21 17:07:04 -07008554 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00008555 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008556 * register to preserve the GPIO settings for LOMs. The GPIOs,
8557 * whether used as inputs or outputs, are set by boot code after
8558 * reset.
8559 */
Joe Perches63c3a662011-04-26 08:12:10 +00008560 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008561 u32 gpio_mask;
8562
Michael Chan9d26e212006-12-07 00:21:14 -08008563 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8564 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8565 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008566
8567 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8568 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8569 GRC_LCLCTRL_GPIO_OUTPUT3;
8570
Michael Chanaf36e6b2006-03-23 01:28:06 -08008571 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8572 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8573
Gary Zambranoaaf84462007-05-05 11:51:45 -07008574 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008575 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8576
8577 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00008578 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08008579 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8580 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008582 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8583 udelay(100);
8584
Joe Perches63c3a662011-04-26 08:12:10 +00008585 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008586 val = tr32(MSGINT_MODE);
8587 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
8588 tw32(MSGINT_MODE, val);
8589 }
8590
Joe Perches63c3a662011-04-26 08:12:10 +00008591 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008592 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8593 udelay(40);
8594 }
8595
8596 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8597 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8598 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8599 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8600 WDMAC_MODE_LNGREAD_ENAB);
8601
Matt Carlsonc5908932011-03-09 16:58:25 +00008602 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8603 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008604 if (tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008605 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8606 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8607 /* nothing */
8608 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008609 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008610 val |= WDMAC_MODE_RX_ACCEL;
8611 }
8612 }
8613
Michael Chand9ab5ad12006-03-20 22:27:35 -08008614 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00008615 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07008616 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -08008617
Matt Carlson788a0352009-11-02 14:26:03 +00008618 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8619 val |= WDMAC_MODE_BURST_ALL_DATA;
8620
Linus Torvalds1da177e2005-04-16 15:20:36 -07008621 tw32_f(WDMAC_MODE, val);
8622 udelay(40);
8623
Joe Perches63c3a662011-04-26 08:12:10 +00008624 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008625 u16 pcix_cmd;
8626
8627 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8628 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008629 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008630 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8631 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008632 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008633 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8634 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008635 }
Matt Carlson9974a352007-10-07 23:27:28 -07008636 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8637 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008638 }
8639
8640 tw32_f(RDMAC_MODE, rdmac_mode);
8641 udelay(40);
8642
8643 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008644 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008645 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008646
8647 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8648 tw32(SNDDATAC_MODE,
8649 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8650 else
8651 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8652
Linus Torvalds1da177e2005-04-16 15:20:36 -07008653 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8654 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008655 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00008656 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008657 val |= RCVDBDI_MODE_LRG_RING_SZ;
8658 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008659 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008660 if (tg3_flag(tp, HW_TSO_1) ||
8661 tg3_flag(tp, HW_TSO_2) ||
8662 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008663 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008664 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008665 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008666 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8667 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008668 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8669
8670 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8671 err = tg3_load_5701_a0_firmware_fix(tp);
8672 if (err)
8673 return err;
8674 }
8675
Joe Perches63c3a662011-04-26 08:12:10 +00008676 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008677 err = tg3_load_tso_firmware(tp);
8678 if (err)
8679 return err;
8680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008681
8682 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008683
Joe Perches63c3a662011-04-26 08:12:10 +00008684 if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsonb1d05212010-06-05 17:24:31 +00008685 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8686 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008687
8688 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8689 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
8690 tp->tx_mode &= ~val;
8691 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
8692 }
8693
Linus Torvalds1da177e2005-04-16 15:20:36 -07008694 tw32_f(MAC_TX_MODE, tp->tx_mode);
8695 udelay(100);
8696
Joe Perches63c3a662011-04-26 08:12:10 +00008697 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008698 u32 reg = MAC_RSS_INDIR_TBL_0;
8699 u8 *ent = (u8 *)&val;
8700
8701 /* Setup the indirection table */
8702 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8703 int idx = i % sizeof(val);
8704
Matt Carlson5efeeea2010-07-11 09:31:40 +00008705 ent[idx] = i % (tp->irq_cnt - 1);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008706 if (idx == sizeof(val) - 1) {
8707 tw32(reg, val);
8708 reg += 4;
8709 }
8710 }
8711
8712 /* Setup the "secret" hash key. */
8713 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8714 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8715 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8716 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8717 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8718 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8719 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8720 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8721 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8722 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8723 }
8724
Linus Torvalds1da177e2005-04-16 15:20:36 -07008725 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008726 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08008727 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8728
Joe Perches63c3a662011-04-26 08:12:10 +00008729 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008730 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8731 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8732 RX_MODE_RSS_IPV6_HASH_EN |
8733 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8734 RX_MODE_RSS_IPV4_HASH_EN |
8735 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8736
Linus Torvalds1da177e2005-04-16 15:20:36 -07008737 tw32_f(MAC_RX_MODE, tp->rx_mode);
8738 udelay(10);
8739
Linus Torvalds1da177e2005-04-16 15:20:36 -07008740 tw32(MAC_LED_CTRL, tp->led_ctrl);
8741
8742 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008743 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008744 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8745 udelay(10);
8746 }
8747 tw32_f(MAC_RX_MODE, tp->rx_mode);
8748 udelay(10);
8749
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008750 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008751 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008752 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008753 /* Set drive transmission level to 1.2V */
8754 /* only if the signal pre-emphasis bit is not set */
8755 val = tr32(MAC_SERDES_CFG);
8756 val &= 0xfffff000;
8757 val |= 0x880;
8758 tw32(MAC_SERDES_CFG, val);
8759 }
8760 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8761 tw32(MAC_SERDES_CFG, 0x616000);
8762 }
8763
8764 /* Prevent chip from dropping frames when flow control
8765 * is enabled.
8766 */
Matt Carlson666bc832010-01-20 16:58:03 +00008767 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8768 val = 1;
8769 else
8770 val = 2;
8771 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008772
8773 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008774 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008775 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00008776 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008777 }
8778
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008779 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Michael Chand4d2c552006-03-20 17:47:20 -08008780 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)) {
8781 u32 tmp;
8782
8783 tmp = tr32(SERDES_RX_CTRL);
8784 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
8785 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
8786 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
8787 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8788 }
8789
Joe Perches63c3a662011-04-26 08:12:10 +00008790 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson80096062010-08-02 11:26:06 +00008791 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
8792 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07008793 tp->link_config.speed = tp->link_config.orig_speed;
8794 tp->link_config.duplex = tp->link_config.orig_duplex;
8795 tp->link_config.autoneg = tp->link_config.orig_autoneg;
8796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008797
Matt Carlsondd477002008-05-25 23:45:58 -07008798 err = tg3_setup_phy(tp, 0);
8799 if (err)
8800 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008801
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008802 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
8803 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07008804 u32 tmp;
8805
8806 /* Clear CRC stats. */
8807 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
8808 tg3_writephy(tp, MII_TG3_TEST1,
8809 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00008810 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07008811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008812 }
8813 }
8814
8815 __tg3_set_rx_mode(tp->dev);
8816
8817 /* Initialize receive rules. */
8818 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
8819 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
8820 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
8821 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
8822
Joe Perches63c3a662011-04-26 08:12:10 +00008823 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008824 limit = 8;
8825 else
8826 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008827 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008828 limit -= 4;
8829 switch (limit) {
8830 case 16:
8831 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
8832 case 15:
8833 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
8834 case 14:
8835 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
8836 case 13:
8837 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
8838 case 12:
8839 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
8840 case 11:
8841 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
8842 case 10:
8843 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
8844 case 9:
8845 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
8846 case 8:
8847 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
8848 case 7:
8849 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
8850 case 6:
8851 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
8852 case 5:
8853 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
8854 case 4:
8855 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
8856 case 3:
8857 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
8858 case 2:
8859 case 1:
8860
8861 default:
8862 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008864
Joe Perches63c3a662011-04-26 08:12:10 +00008865 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07008866 /* Write our heartbeat update interval to APE. */
8867 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
8868 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07008869
Linus Torvalds1da177e2005-04-16 15:20:36 -07008870 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
8871
Linus Torvalds1da177e2005-04-16 15:20:36 -07008872 return 0;
8873}
8874
8875/* Called at device open time to get the chip ready for
8876 * packet processing. Invoked with tp->lock held.
8877 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008878static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008879{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008880 tg3_switch_clocks(tp);
8881
8882 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
8883
Matt Carlson2f751b62008-08-04 23:17:34 -07008884 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008885}
8886
8887#define TG3_STAT_ADD32(PSTAT, REG) \
8888do { u32 __val = tr32(REG); \
8889 (PSTAT)->low += __val; \
8890 if ((PSTAT)->low < __val) \
8891 (PSTAT)->high += 1; \
8892} while (0)
8893
8894static void tg3_periodic_fetch_stats(struct tg3 *tp)
8895{
8896 struct tg3_hw_stats *sp = tp->hw_stats;
8897
8898 if (!netif_carrier_ok(tp->dev))
8899 return;
8900
8901 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
8902 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
8903 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
8904 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
8905 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
8906 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
8907 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
8908 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
8909 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
8910 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
8911 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
8912 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
8913 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
8914
8915 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
8916 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
8917 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
8918 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
8919 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
8920 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
8921 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
8922 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
8923 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
8924 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
8925 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
8926 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
8927 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
8928 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07008929
8930 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson4d958472011-04-20 07:57:35 +00008931 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717) {
8932 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
8933 } else {
8934 u32 val = tr32(HOSTCC_FLOW_ATTN);
8935 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
8936 if (val) {
8937 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
8938 sp->rx_discards.low += val;
8939 if (sp->rx_discards.low < val)
8940 sp->rx_discards.high += 1;
8941 }
8942 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
8943 }
Michael Chan463d3052006-05-22 16:36:27 -07008944 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008945}
8946
8947static void tg3_timer(unsigned long __opaque)
8948{
8949 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008950
Michael Chanf475f162006-03-27 23:20:14 -08008951 if (tp->irq_sync)
8952 goto restart_timer;
8953
David S. Millerf47c11e2005-06-24 20:18:35 -07008954 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008955
Joe Perches63c3a662011-04-26 08:12:10 +00008956 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -07008957 /* All of this garbage is because when using non-tagged
8958 * IRQ status the mailbox/status_block protocol the chip
8959 * uses with the cpu is race prone.
8960 */
Matt Carlson898a56f2009-08-28 14:02:40 +00008961 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07008962 tw32(GRC_LOCAL_CTRL,
8963 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
8964 } else {
8965 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008966 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07008967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008968
David S. Millerfac9b832005-05-18 22:46:34 -07008969 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +00008970 tg3_flag_set(tp, RESTART_TIMER);
David S. Millerf47c11e2005-06-24 20:18:35 -07008971 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07008972 schedule_work(&tp->reset_task);
8973 return;
8974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008975 }
8976
Linus Torvalds1da177e2005-04-16 15:20:36 -07008977 /* This part only runs once per second. */
8978 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00008979 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -07008980 tg3_periodic_fetch_stats(tp);
8981
Matt Carlson52b02d02010-10-14 10:37:41 +00008982 if (tp->setlpicnt && !--tp->setlpicnt) {
8983 u32 val = tr32(TG3_CPMU_EEE_MODE);
8984 tw32(TG3_CPMU_EEE_MODE,
8985 val | TG3_CPMU_EEEMD_LPI_ENABLE);
8986 }
8987
Joe Perches63c3a662011-04-26 08:12:10 +00008988 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008989 u32 mac_stat;
8990 int phy_event;
8991
8992 mac_stat = tr32(MAC_STATUS);
8993
8994 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008995 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008996 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
8997 phy_event = 1;
8998 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
8999 phy_event = 1;
9000
9001 if (phy_event)
9002 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +00009003 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009004 u32 mac_stat = tr32(MAC_STATUS);
9005 int need_setup = 0;
9006
9007 if (netif_carrier_ok(tp->dev) &&
9008 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
9009 need_setup = 1;
9010 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00009011 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009012 (mac_stat & (MAC_STATUS_PCS_SYNCED |
9013 MAC_STATUS_SIGNAL_DET))) {
9014 need_setup = 1;
9015 }
9016 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07009017 if (!tp->serdes_counter) {
9018 tw32_f(MAC_MODE,
9019 (tp->mac_mode &
9020 ~MAC_MODE_PORT_MODE_MASK));
9021 udelay(40);
9022 tw32_f(MAC_MODE, tp->mac_mode);
9023 udelay(40);
9024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009025 tg3_setup_phy(tp, 0);
9026 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009027 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009028 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07009029 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00009030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009031
9032 tp->timer_counter = tp->timer_multiplier;
9033 }
9034
Michael Chan130b8e42006-09-27 16:00:40 -07009035 /* Heartbeat is only sent once every 2 seconds.
9036 *
9037 * The heartbeat is to tell the ASF firmware that the host
9038 * driver is still alive. In the event that the OS crashes,
9039 * ASF needs to reset the hardware to free up the FIFO space
9040 * that may be filled with rx packets destined for the host.
9041 * If the FIFO is full, ASF will no longer function properly.
9042 *
9043 * Unintended resets have been reported on real time kernels
9044 * where the timer doesn't run on time. Netpoll will also have
9045 * same problem.
9046 *
9047 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
9048 * to check the ring condition when the heartbeat is expiring
9049 * before doing the reset. This will prevent most unintended
9050 * resets.
9051 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009052 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009053 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07009054 tg3_wait_for_event_ack(tp);
9055
Michael Chanbbadf502006-04-06 21:46:34 -07009056 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07009057 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07009058 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009059 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
9060 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009061
9062 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009063 }
9064 tp->asf_counter = tp->asf_multiplier;
9065 }
9066
David S. Millerf47c11e2005-06-24 20:18:35 -07009067 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009068
Michael Chanf475f162006-03-27 23:20:14 -08009069restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07009070 tp->timer.expires = jiffies + tp->timer_offset;
9071 add_timer(&tp->timer);
9072}
9073
Matt Carlson4f125f42009-09-01 12:55:02 +00009074static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08009075{
David Howells7d12e782006-10-05 14:55:46 +01009076 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009077 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00009078 char *name;
9079 struct tg3_napi *tnapi = &tp->napi[irq_num];
9080
9081 if (tp->irq_cnt == 1)
9082 name = tp->dev->name;
9083 else {
9084 name = &tnapi->irq_lbl[0];
9085 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
9086 name[IFNAMSIZ-1] = 0;
9087 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009088
Joe Perches63c3a662011-04-26 08:12:10 +00009089 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08009090 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +00009091 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009092 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009093 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009094 } else {
9095 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +00009096 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009097 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009098 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009099 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009100
9101 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009102}
9103
Michael Chan79381092005-04-21 17:13:59 -07009104static int tg3_test_interrupt(struct tg3 *tp)
9105{
Matt Carlson09943a12009-08-28 14:01:57 +00009106 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07009107 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07009108 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009109 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07009110
Michael Chand4bc3922005-05-29 14:59:20 -07009111 if (!netif_running(dev))
9112 return -ENODEV;
9113
Michael Chan79381092005-04-21 17:13:59 -07009114 tg3_disable_ints(tp);
9115
Matt Carlson4f125f42009-09-01 12:55:02 +00009116 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009117
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009118 /*
9119 * Turn off MSI one shot mode. Otherwise this test has no
9120 * observable way to know whether the interrupt was delivered.
9121 */
Joe Perches63c3a662011-04-26 08:12:10 +00009122 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009123 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
9124 tw32(MSGINT_MODE, val);
9125 }
9126
Matt Carlson4f125f42009-09-01 12:55:02 +00009127 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00009128 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009129 if (err)
9130 return err;
9131
Matt Carlson898a56f2009-08-28 14:02:40 +00009132 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07009133 tg3_enable_ints(tp);
9134
9135 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009136 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07009137
9138 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07009139 u32 int_mbox, misc_host_ctrl;
9140
Matt Carlson898a56f2009-08-28 14:02:40 +00009141 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07009142 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
9143
9144 if ((int_mbox != 0) ||
9145 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
9146 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07009147 break;
Michael Chanb16250e2006-09-27 16:10:14 -07009148 }
9149
Michael Chan79381092005-04-21 17:13:59 -07009150 msleep(10);
9151 }
9152
9153 tg3_disable_ints(tp);
9154
Matt Carlson4f125f42009-09-01 12:55:02 +00009155 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009156
Matt Carlson4f125f42009-09-01 12:55:02 +00009157 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009158
9159 if (err)
9160 return err;
9161
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009162 if (intr_ok) {
9163 /* Reenable MSI one shot mode. */
Joe Perches63c3a662011-04-26 08:12:10 +00009164 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009165 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
9166 tw32(MSGINT_MODE, val);
9167 }
Michael Chan79381092005-04-21 17:13:59 -07009168 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009169 }
Michael Chan79381092005-04-21 17:13:59 -07009170
9171 return -EIO;
9172}
9173
9174/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
9175 * successfully restored
9176 */
9177static int tg3_test_msi(struct tg3 *tp)
9178{
Michael Chan79381092005-04-21 17:13:59 -07009179 int err;
9180 u16 pci_cmd;
9181
Joe Perches63c3a662011-04-26 08:12:10 +00009182 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -07009183 return 0;
9184
9185 /* Turn off SERR reporting in case MSI terminates with Master
9186 * Abort.
9187 */
9188 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
9189 pci_write_config_word(tp->pdev, PCI_COMMAND,
9190 pci_cmd & ~PCI_COMMAND_SERR);
9191
9192 err = tg3_test_interrupt(tp);
9193
9194 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
9195
9196 if (!err)
9197 return 0;
9198
9199 /* other failures */
9200 if (err != -EIO)
9201 return err;
9202
9203 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009204 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
9205 "to INTx mode. Please report this failure to the PCI "
9206 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07009207
Matt Carlson4f125f42009-09-01 12:55:02 +00009208 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00009209
Michael Chan79381092005-04-21 17:13:59 -07009210 pci_disable_msi(tp->pdev);
9211
Joe Perches63c3a662011-04-26 08:12:10 +00009212 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +00009213 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07009214
Matt Carlson4f125f42009-09-01 12:55:02 +00009215 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009216 if (err)
9217 return err;
9218
9219 /* Need to reset the chip because the MSI cycle may have terminated
9220 * with Master Abort.
9221 */
David S. Millerf47c11e2005-06-24 20:18:35 -07009222 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009223
Michael Chan944d9802005-05-29 14:57:48 -07009224 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009225 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009226
David S. Millerf47c11e2005-06-24 20:18:35 -07009227 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009228
9229 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00009230 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07009231
9232 return err;
9233}
9234
Matt Carlson9e9fd122009-01-19 16:57:45 -08009235static int tg3_request_firmware(struct tg3 *tp)
9236{
9237 const __be32 *fw_data;
9238
9239 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009240 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
9241 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009242 return -ENOENT;
9243 }
9244
9245 fw_data = (void *)tp->fw->data;
9246
9247 /* Firmware blob starts with version numbers, followed by
9248 * start address and _full_ length including BSS sections
9249 * (which must be longer than the actual data, of course
9250 */
9251
9252 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9253 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009254 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9255 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009256 release_firmware(tp->fw);
9257 tp->fw = NULL;
9258 return -EINVAL;
9259 }
9260
9261 /* We no longer need firmware; we have it. */
9262 tp->fw_needed = NULL;
9263 return 0;
9264}
9265
Matt Carlson679563f2009-09-01 12:55:46 +00009266static bool tg3_enable_msix(struct tg3 *tp)
9267{
9268 int i, rc, cpus = num_online_cpus();
9269 struct msix_entry msix_ent[tp->irq_max];
9270
9271 if (cpus == 1)
9272 /* Just fallback to the simpler MSI mode. */
9273 return false;
9274
9275 /*
9276 * We want as many rx rings enabled as there are cpus.
9277 * The first MSIX vector only deals with link interrupts, etc,
9278 * so we add one to the number of vectors we are requesting.
9279 */
9280 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
9281
9282 for (i = 0; i < tp->irq_max; i++) {
9283 msix_ent[i].entry = i;
9284 msix_ent[i].vector = 0;
9285 }
9286
9287 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009288 if (rc < 0) {
9289 return false;
9290 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009291 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9292 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009293 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9294 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009295 tp->irq_cnt = rc;
9296 }
9297
9298 for (i = 0; i < tp->irq_max; i++)
9299 tp->napi[i].irq_vec = msix_ent[i].vector;
9300
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009301 netif_set_real_num_tx_queues(tp->dev, 1);
9302 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9303 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9304 pci_disable_msix(tp->pdev);
9305 return false;
9306 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009307
9308 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +00009309 tg3_flag_set(tp, ENABLE_RSS);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009310
9311 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9312 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Joe Perches63c3a662011-04-26 08:12:10 +00009313 tg3_flag_set(tp, ENABLE_TSS);
Matt Carlsonb92b9042010-11-24 08:31:51 +00009314 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9315 }
9316 }
Matt Carlson2430b032010-06-05 17:24:34 +00009317
Matt Carlson679563f2009-09-01 12:55:46 +00009318 return true;
9319}
9320
Matt Carlson07b01732009-08-28 14:01:15 +00009321static void tg3_ints_init(struct tg3 *tp)
9322{
Joe Perches63c3a662011-04-26 08:12:10 +00009323 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
9324 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009325 /* All MSI supporting chips should support tagged
9326 * status. Assert that this is the case.
9327 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009328 netdev_warn(tp->dev,
9329 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009330 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009331 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009332
Joe Perches63c3a662011-04-26 08:12:10 +00009333 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
9334 tg3_flag_set(tp, USING_MSIX);
9335 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
9336 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +00009337
Joe Perches63c3a662011-04-26 08:12:10 +00009338 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009339 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +00009340 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009341 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson679563f2009-09-01 12:55:46 +00009342 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9343 }
9344defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +00009345 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009346 tp->irq_cnt = 1;
9347 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009348 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009349 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009350 }
Matt Carlson07b01732009-08-28 14:01:15 +00009351}
9352
9353static void tg3_ints_fini(struct tg3 *tp)
9354{
Joe Perches63c3a662011-04-26 08:12:10 +00009355 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +00009356 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009357 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +00009358 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009359 tg3_flag_clear(tp, USING_MSI);
9360 tg3_flag_clear(tp, USING_MSIX);
9361 tg3_flag_clear(tp, ENABLE_RSS);
9362 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009363}
9364
Linus Torvalds1da177e2005-04-16 15:20:36 -07009365static int tg3_open(struct net_device *dev)
9366{
9367 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009368 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009369
Matt Carlson9e9fd122009-01-19 16:57:45 -08009370 if (tp->fw_needed) {
9371 err = tg3_request_firmware(tp);
9372 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9373 if (err)
9374 return err;
9375 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009376 netdev_warn(tp->dev, "TSO capability disabled\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009377 tg3_flag_clear(tp, TSO_CAPABLE);
9378 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009379 netdev_notice(tp->dev, "TSO capability restored\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009380 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009381 }
9382 }
9383
Michael Chanc49a1562006-12-17 17:07:29 -08009384 netif_carrier_off(tp->dev);
9385
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009386 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -07009387 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009388 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009389
9390 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009391
Linus Torvalds1da177e2005-04-16 15:20:36 -07009392 tg3_disable_ints(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009393 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009394
David S. Millerf47c11e2005-06-24 20:18:35 -07009395 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009396
Matt Carlson679563f2009-09-01 12:55:46 +00009397 /*
9398 * Setup interrupts first so we know how
9399 * many NAPI resources to allocate
9400 */
9401 tg3_ints_init(tp);
9402
Linus Torvalds1da177e2005-04-16 15:20:36 -07009403 /* The placement of this call is tied
9404 * to the setup and use of Host TX descriptors.
9405 */
9406 err = tg3_alloc_consistent(tp);
9407 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009408 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009409
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009410 tg3_napi_init(tp);
9411
Matt Carlsonfed97812009-09-01 13:10:19 +00009412 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07009413
Matt Carlson4f125f42009-09-01 12:55:02 +00009414 for (i = 0; i < tp->irq_cnt; i++) {
9415 struct tg3_napi *tnapi = &tp->napi[i];
9416 err = tg3_request_irq(tp, i);
9417 if (err) {
9418 for (i--; i >= 0; i--)
9419 free_irq(tnapi->irq_vec, tnapi);
9420 break;
9421 }
9422 }
Matt Carlson07b01732009-08-28 14:01:15 +00009423
9424 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009425 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00009426
David S. Millerf47c11e2005-06-24 20:18:35 -07009427 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009428
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009429 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009430 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07009431 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009432 tg3_free_rings(tp);
9433 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00009434 if (tg3_flag(tp, TAGGED_STATUS))
David S. Millerfac9b832005-05-18 22:46:34 -07009435 tp->timer_offset = HZ;
9436 else
9437 tp->timer_offset = HZ / 10;
9438
9439 BUG_ON(tp->timer_offset > HZ);
9440 tp->timer_counter = tp->timer_multiplier =
9441 (HZ / tp->timer_offset);
9442 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07009443 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009444
9445 init_timer(&tp->timer);
9446 tp->timer.expires = jiffies + tp->timer_offset;
9447 tp->timer.data = (unsigned long) tp;
9448 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009449 }
9450
David S. Millerf47c11e2005-06-24 20:18:35 -07009451 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009452
Matt Carlson07b01732009-08-28 14:01:15 +00009453 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009454 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009455
Joe Perches63c3a662011-04-26 08:12:10 +00009456 if (tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -07009457 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07009458
Michael Chan79381092005-04-21 17:13:59 -07009459 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009460 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07009461 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07009462 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07009463 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009464
Matt Carlson679563f2009-09-01 12:55:46 +00009465 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07009466 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009467
Joe Perches63c3a662011-04-26 08:12:10 +00009468 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009469 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009470
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009471 tw32(PCIE_TRANSACTION_CFG,
9472 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009473 }
Michael Chan79381092005-04-21 17:13:59 -07009474 }
9475
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009476 tg3_phy_start(tp);
9477
David S. Millerf47c11e2005-06-24 20:18:35 -07009478 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009479
Michael Chan79381092005-04-21 17:13:59 -07009480 add_timer(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +00009481 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009482 tg3_enable_ints(tp);
9483
David S. Millerf47c11e2005-06-24 20:18:35 -07009484 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009485
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009486 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009487
9488 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00009489
Matt Carlson679563f2009-09-01 12:55:46 +00009490err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00009491 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9492 struct tg3_napi *tnapi = &tp->napi[i];
9493 free_irq(tnapi->irq_vec, tnapi);
9494 }
Matt Carlson07b01732009-08-28 14:01:15 +00009495
Matt Carlson679563f2009-09-01 12:55:46 +00009496err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00009497 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009498 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009499 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00009500
9501err_out1:
9502 tg3_ints_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009503 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009504}
9505
Eric Dumazet511d2222010-07-07 20:44:24 +00009506static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
9507 struct rtnl_link_stats64 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009508static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
9509
9510static int tg3_close(struct net_device *dev)
9511{
Matt Carlson4f125f42009-09-01 12:55:02 +00009512 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009513 struct tg3 *tp = netdev_priv(dev);
9514
Matt Carlsonfed97812009-09-01 13:10:19 +00009515 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07009516 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08009517
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009518 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009519
9520 del_timer_sync(&tp->timer);
9521
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009522 tg3_phy_stop(tp);
9523
David S. Millerf47c11e2005-06-24 20:18:35 -07009524 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009525
9526 tg3_disable_ints(tp);
9527
Michael Chan944d9802005-05-29 14:57:48 -07009528 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009529 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009530 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009531
David S. Millerf47c11e2005-06-24 20:18:35 -07009532 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009533
Matt Carlson4f125f42009-09-01 12:55:02 +00009534 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9535 struct tg3_napi *tnapi = &tp->napi[i];
9536 free_irq(tnapi->irq_vec, tnapi);
9537 }
Matt Carlson07b01732009-08-28 14:01:15 +00009538
9539 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009540
Eric Dumazet511d2222010-07-07 20:44:24 +00009541 tg3_get_stats64(tp->dev, &tp->net_stats_prev);
9542
Linus Torvalds1da177e2005-04-16 15:20:36 -07009543 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9544 sizeof(tp->estats_prev));
9545
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009546 tg3_napi_fini(tp);
9547
Linus Torvalds1da177e2005-04-16 15:20:36 -07009548 tg3_free_consistent(tp);
9549
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009550 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -08009551
9552 netif_carrier_off(tp->dev);
9553
Linus Torvalds1da177e2005-04-16 15:20:36 -07009554 return 0;
9555}
9556
Eric Dumazet511d2222010-07-07 20:44:24 +00009557static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -07009558{
9559 return ((u64)val->high << 32) | ((u64)val->low);
9560}
9561
Eric Dumazet511d2222010-07-07 20:44:24 +00009562static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009563{
9564 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9565
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009566 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009567 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9568 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009569 u32 val;
9570
David S. Millerf47c11e2005-06-24 20:18:35 -07009571 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009572 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9573 tg3_writephy(tp, MII_TG3_TEST1,
9574 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009575 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009576 } else
9577 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009578 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009579
9580 tp->phy_crc_errors += val;
9581
9582 return tp->phy_crc_errors;
9583 }
9584
9585 return get_stat64(&hw_stats->rx_fcs_errors);
9586}
9587
9588#define ESTAT_ADD(member) \
9589 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +00009590 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009591
9592static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9593{
9594 struct tg3_ethtool_stats *estats = &tp->estats;
9595 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9596 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9597
9598 if (!hw_stats)
9599 return old_estats;
9600
9601 ESTAT_ADD(rx_octets);
9602 ESTAT_ADD(rx_fragments);
9603 ESTAT_ADD(rx_ucast_packets);
9604 ESTAT_ADD(rx_mcast_packets);
9605 ESTAT_ADD(rx_bcast_packets);
9606 ESTAT_ADD(rx_fcs_errors);
9607 ESTAT_ADD(rx_align_errors);
9608 ESTAT_ADD(rx_xon_pause_rcvd);
9609 ESTAT_ADD(rx_xoff_pause_rcvd);
9610 ESTAT_ADD(rx_mac_ctrl_rcvd);
9611 ESTAT_ADD(rx_xoff_entered);
9612 ESTAT_ADD(rx_frame_too_long_errors);
9613 ESTAT_ADD(rx_jabbers);
9614 ESTAT_ADD(rx_undersize_packets);
9615 ESTAT_ADD(rx_in_length_errors);
9616 ESTAT_ADD(rx_out_length_errors);
9617 ESTAT_ADD(rx_64_or_less_octet_packets);
9618 ESTAT_ADD(rx_65_to_127_octet_packets);
9619 ESTAT_ADD(rx_128_to_255_octet_packets);
9620 ESTAT_ADD(rx_256_to_511_octet_packets);
9621 ESTAT_ADD(rx_512_to_1023_octet_packets);
9622 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9623 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9624 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9625 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9626 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9627
9628 ESTAT_ADD(tx_octets);
9629 ESTAT_ADD(tx_collisions);
9630 ESTAT_ADD(tx_xon_sent);
9631 ESTAT_ADD(tx_xoff_sent);
9632 ESTAT_ADD(tx_flow_control);
9633 ESTAT_ADD(tx_mac_errors);
9634 ESTAT_ADD(tx_single_collisions);
9635 ESTAT_ADD(tx_mult_collisions);
9636 ESTAT_ADD(tx_deferred);
9637 ESTAT_ADD(tx_excessive_collisions);
9638 ESTAT_ADD(tx_late_collisions);
9639 ESTAT_ADD(tx_collide_2times);
9640 ESTAT_ADD(tx_collide_3times);
9641 ESTAT_ADD(tx_collide_4times);
9642 ESTAT_ADD(tx_collide_5times);
9643 ESTAT_ADD(tx_collide_6times);
9644 ESTAT_ADD(tx_collide_7times);
9645 ESTAT_ADD(tx_collide_8times);
9646 ESTAT_ADD(tx_collide_9times);
9647 ESTAT_ADD(tx_collide_10times);
9648 ESTAT_ADD(tx_collide_11times);
9649 ESTAT_ADD(tx_collide_12times);
9650 ESTAT_ADD(tx_collide_13times);
9651 ESTAT_ADD(tx_collide_14times);
9652 ESTAT_ADD(tx_collide_15times);
9653 ESTAT_ADD(tx_ucast_packets);
9654 ESTAT_ADD(tx_mcast_packets);
9655 ESTAT_ADD(tx_bcast_packets);
9656 ESTAT_ADD(tx_carrier_sense_errors);
9657 ESTAT_ADD(tx_discards);
9658 ESTAT_ADD(tx_errors);
9659
9660 ESTAT_ADD(dma_writeq_full);
9661 ESTAT_ADD(dma_write_prioq_full);
9662 ESTAT_ADD(rxbds_empty);
9663 ESTAT_ADD(rx_discards);
9664 ESTAT_ADD(rx_errors);
9665 ESTAT_ADD(rx_threshold_hit);
9666
9667 ESTAT_ADD(dma_readq_full);
9668 ESTAT_ADD(dma_read_prioq_full);
9669 ESTAT_ADD(tx_comp_queue_full);
9670
9671 ESTAT_ADD(ring_set_send_prod_index);
9672 ESTAT_ADD(ring_status_update);
9673 ESTAT_ADD(nic_irqs);
9674 ESTAT_ADD(nic_avoided_irqs);
9675 ESTAT_ADD(nic_tx_threshold_hit);
9676
9677 return estats;
9678}
9679
Eric Dumazet511d2222010-07-07 20:44:24 +00009680static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9681 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009682{
9683 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +00009684 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009685 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9686
9687 if (!hw_stats)
9688 return old_stats;
9689
9690 stats->rx_packets = old_stats->rx_packets +
9691 get_stat64(&hw_stats->rx_ucast_packets) +
9692 get_stat64(&hw_stats->rx_mcast_packets) +
9693 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009694
Linus Torvalds1da177e2005-04-16 15:20:36 -07009695 stats->tx_packets = old_stats->tx_packets +
9696 get_stat64(&hw_stats->tx_ucast_packets) +
9697 get_stat64(&hw_stats->tx_mcast_packets) +
9698 get_stat64(&hw_stats->tx_bcast_packets);
9699
9700 stats->rx_bytes = old_stats->rx_bytes +
9701 get_stat64(&hw_stats->rx_octets);
9702 stats->tx_bytes = old_stats->tx_bytes +
9703 get_stat64(&hw_stats->tx_octets);
9704
9705 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009706 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009707 stats->tx_errors = old_stats->tx_errors +
9708 get_stat64(&hw_stats->tx_errors) +
9709 get_stat64(&hw_stats->tx_mac_errors) +
9710 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9711 get_stat64(&hw_stats->tx_discards);
9712
9713 stats->multicast = old_stats->multicast +
9714 get_stat64(&hw_stats->rx_mcast_packets);
9715 stats->collisions = old_stats->collisions +
9716 get_stat64(&hw_stats->tx_collisions);
9717
9718 stats->rx_length_errors = old_stats->rx_length_errors +
9719 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9720 get_stat64(&hw_stats->rx_undersize_packets);
9721
9722 stats->rx_over_errors = old_stats->rx_over_errors +
9723 get_stat64(&hw_stats->rxbds_empty);
9724 stats->rx_frame_errors = old_stats->rx_frame_errors +
9725 get_stat64(&hw_stats->rx_align_errors);
9726 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9727 get_stat64(&hw_stats->tx_discards);
9728 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
9729 get_stat64(&hw_stats->tx_carrier_sense_errors);
9730
9731 stats->rx_crc_errors = old_stats->rx_crc_errors +
9732 calc_crc_errors(tp);
9733
John W. Linville4f63b872005-09-12 14:43:18 -07009734 stats->rx_missed_errors = old_stats->rx_missed_errors +
9735 get_stat64(&hw_stats->rx_discards);
9736
Eric Dumazetb0057c52010-10-10 19:55:52 +00009737 stats->rx_dropped = tp->rx_dropped;
9738
Linus Torvalds1da177e2005-04-16 15:20:36 -07009739 return stats;
9740}
9741
9742static inline u32 calc_crc(unsigned char *buf, int len)
9743{
9744 u32 reg;
9745 u32 tmp;
9746 int j, k;
9747
9748 reg = 0xffffffff;
9749
9750 for (j = 0; j < len; j++) {
9751 reg ^= buf[j];
9752
9753 for (k = 0; k < 8; k++) {
9754 tmp = reg & 0x01;
9755
9756 reg >>= 1;
9757
Matt Carlson859a588792010-04-05 10:19:28 +00009758 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009759 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009760 }
9761 }
9762
9763 return ~reg;
9764}
9765
9766static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9767{
9768 /* accept or reject all multicast frames */
9769 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9770 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9771 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9772 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9773}
9774
9775static void __tg3_set_rx_mode(struct net_device *dev)
9776{
9777 struct tg3 *tp = netdev_priv(dev);
9778 u32 rx_mode;
9779
9780 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9781 RX_MODE_KEEP_VLAN_TAG);
9782
Matt Carlsonbf933c82011-01-25 15:58:49 +00009783#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009784 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9785 * flag clear.
9786 */
Joe Perches63c3a662011-04-26 08:12:10 +00009787 if (!tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009788 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9789#endif
9790
9791 if (dev->flags & IFF_PROMISC) {
9792 /* Promiscuous mode. */
9793 rx_mode |= RX_MODE_PROMISC;
9794 } else if (dev->flags & IFF_ALLMULTI) {
9795 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009796 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00009797 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009798 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009799 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009800 } else {
9801 /* Accept one or more multicast(s). */
Jiri Pirko22bedad32010-04-01 21:22:57 +00009802 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009803 u32 mc_filter[4] = { 0, };
9804 u32 regidx;
9805 u32 bit;
9806 u32 crc;
9807
Jiri Pirko22bedad32010-04-01 21:22:57 +00009808 netdev_for_each_mc_addr(ha, dev) {
9809 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009810 bit = ~crc & 0x7f;
9811 regidx = (bit & 0x60) >> 5;
9812 bit &= 0x1f;
9813 mc_filter[regidx] |= (1 << bit);
9814 }
9815
9816 tw32(MAC_HASH_REG_0, mc_filter[0]);
9817 tw32(MAC_HASH_REG_1, mc_filter[1]);
9818 tw32(MAC_HASH_REG_2, mc_filter[2]);
9819 tw32(MAC_HASH_REG_3, mc_filter[3]);
9820 }
9821
9822 if (rx_mode != tp->rx_mode) {
9823 tp->rx_mode = rx_mode;
9824 tw32_f(MAC_RX_MODE, rx_mode);
9825 udelay(10);
9826 }
9827}
9828
9829static void tg3_set_rx_mode(struct net_device *dev)
9830{
9831 struct tg3 *tp = netdev_priv(dev);
9832
Michael Chane75f7c92006-03-20 21:33:26 -08009833 if (!netif_running(dev))
9834 return;
9835
David S. Millerf47c11e2005-06-24 20:18:35 -07009836 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009837 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -07009838 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009839}
9840
Linus Torvalds1da177e2005-04-16 15:20:36 -07009841static int tg3_get_regs_len(struct net_device *dev)
9842{
Matt Carlson97bd8e42011-04-13 11:05:04 +00009843 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009844}
9845
9846static void tg3_get_regs(struct net_device *dev,
9847 struct ethtool_regs *regs, void *_p)
9848{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009849 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009850
9851 regs->version = 0;
9852
Matt Carlson97bd8e42011-04-13 11:05:04 +00009853 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009854
Matt Carlson80096062010-08-02 11:26:06 +00009855 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009856 return;
9857
David S. Millerf47c11e2005-06-24 20:18:35 -07009858 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009859
Matt Carlson97bd8e42011-04-13 11:05:04 +00009860 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009861
David S. Millerf47c11e2005-06-24 20:18:35 -07009862 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009863}
9864
9865static int tg3_get_eeprom_len(struct net_device *dev)
9866{
9867 struct tg3 *tp = netdev_priv(dev);
9868
9869 return tp->nvram_size;
9870}
9871
Linus Torvalds1da177e2005-04-16 15:20:36 -07009872static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9873{
9874 struct tg3 *tp = netdev_priv(dev);
9875 int ret;
9876 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -08009877 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009878 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009879
Joe Perches63c3a662011-04-26 08:12:10 +00009880 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +00009881 return -EINVAL;
9882
Matt Carlson80096062010-08-02 11:26:06 +00009883 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009884 return -EAGAIN;
9885
Linus Torvalds1da177e2005-04-16 15:20:36 -07009886 offset = eeprom->offset;
9887 len = eeprom->len;
9888 eeprom->len = 0;
9889
9890 eeprom->magic = TG3_EEPROM_MAGIC;
9891
9892 if (offset & 3) {
9893 /* adjustments to start on required 4 byte boundary */
9894 b_offset = offset & 3;
9895 b_count = 4 - b_offset;
9896 if (b_count > len) {
9897 /* i.e. offset=1 len=2 */
9898 b_count = len;
9899 }
Matt Carlsona9dc5292009-02-25 14:25:30 +00009900 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009901 if (ret)
9902 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +00009903 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009904 len -= b_count;
9905 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009906 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009907 }
9908
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009909 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009910 pd = &data[eeprom->len];
9911 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00009912 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009913 if (ret) {
9914 eeprom->len += i;
9915 return ret;
9916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009917 memcpy(pd + i, &val, 4);
9918 }
9919 eeprom->len += i;
9920
9921 if (len & 3) {
9922 /* read last bytes not ending on 4 byte boundary */
9923 pd = &data[eeprom->len];
9924 b_count = len & 3;
9925 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009926 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009927 if (ret)
9928 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009929 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009930 eeprom->len += b_count;
9931 }
9932 return 0;
9933}
9934
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009935static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009936
9937static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9938{
9939 struct tg3 *tp = netdev_priv(dev);
9940 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009941 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009942 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009943 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009944
Matt Carlson80096062010-08-02 11:26:06 +00009945 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009946 return -EAGAIN;
9947
Joe Perches63c3a662011-04-26 08:12:10 +00009948 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +00009949 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009950 return -EINVAL;
9951
9952 offset = eeprom->offset;
9953 len = eeprom->len;
9954
9955 if ((b_offset = (offset & 3))) {
9956 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +00009957 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009958 if (ret)
9959 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009960 len += b_offset;
9961 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -07009962 if (len < 4)
9963 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009964 }
9965
9966 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -07009967 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009968 /* adjustments to end on required 4 byte boundary */
9969 odd_len = 1;
9970 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009971 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009972 if (ret)
9973 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009974 }
9975
9976 buf = data;
9977 if (b_offset || odd_len) {
9978 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009979 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009980 return -ENOMEM;
9981 if (b_offset)
9982 memcpy(buf, &start, 4);
9983 if (odd_len)
9984 memcpy(buf+len-4, &end, 4);
9985 memcpy(buf + b_offset, data, eeprom->len);
9986 }
9987
9988 ret = tg3_nvram_write_block(tp, offset, len, buf);
9989
9990 if (buf != data)
9991 kfree(buf);
9992
9993 return ret;
9994}
9995
9996static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9997{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009998 struct tg3 *tp = netdev_priv(dev);
9999
Joe Perches63c3a662011-04-26 08:12:10 +000010000 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010001 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010002 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010003 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010004 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10005 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010006 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010007
Linus Torvalds1da177e2005-04-16 15:20:36 -070010008 cmd->supported = (SUPPORTED_Autoneg);
10009
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010010 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010011 cmd->supported |= (SUPPORTED_1000baseT_Half |
10012 SUPPORTED_1000baseT_Full);
10013
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010014 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010015 cmd->supported |= (SUPPORTED_100baseT_Half |
10016 SUPPORTED_100baseT_Full |
10017 SUPPORTED_10baseT_Half |
10018 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080010019 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070010020 cmd->port = PORT_TP;
10021 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010022 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070010023 cmd->port = PORT_FIBRE;
10024 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010025
Linus Torvalds1da177e2005-04-16 15:20:36 -070010026 cmd->advertising = tp->link_config.advertising;
10027 if (netif_running(dev)) {
David Decotigny70739492011-04-27 18:32:40 +000010028 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010029 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson64c22182010-10-14 10:37:44 +000010030 } else {
David Decotigny70739492011-04-27 18:32:40 +000010031 ethtool_cmd_speed_set(cmd, SPEED_INVALID);
Matt Carlson64c22182010-10-14 10:37:44 +000010032 cmd->duplex = DUPLEX_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010033 }
Matt Carlson882e9792009-09-01 13:21:36 +000010034 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010035 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010036 cmd->autoneg = tp->link_config.autoneg;
10037 cmd->maxtxpkt = 0;
10038 cmd->maxrxpkt = 0;
10039 return 0;
10040}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010041
Linus Torvalds1da177e2005-04-16 15:20:36 -070010042static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10043{
10044 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000010045 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010046
Joe Perches63c3a662011-04-26 08:12:10 +000010047 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010048 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010049 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010050 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010051 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10052 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010053 }
10054
Matt Carlson7e5856b2009-02-25 14:23:01 +000010055 if (cmd->autoneg != AUTONEG_ENABLE &&
10056 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070010057 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010058
10059 if (cmd->autoneg == AUTONEG_DISABLE &&
10060 cmd->duplex != DUPLEX_FULL &&
10061 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070010062 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010063
Matt Carlson7e5856b2009-02-25 14:23:01 +000010064 if (cmd->autoneg == AUTONEG_ENABLE) {
10065 u32 mask = ADVERTISED_Autoneg |
10066 ADVERTISED_Pause |
10067 ADVERTISED_Asym_Pause;
10068
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010069 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010070 mask |= ADVERTISED_1000baseT_Half |
10071 ADVERTISED_1000baseT_Full;
10072
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010073 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010074 mask |= ADVERTISED_100baseT_Half |
10075 ADVERTISED_100baseT_Full |
10076 ADVERTISED_10baseT_Half |
10077 ADVERTISED_10baseT_Full |
10078 ADVERTISED_TP;
10079 else
10080 mask |= ADVERTISED_FIBRE;
10081
10082 if (cmd->advertising & ~mask)
10083 return -EINVAL;
10084
10085 mask &= (ADVERTISED_1000baseT_Half |
10086 ADVERTISED_1000baseT_Full |
10087 ADVERTISED_100baseT_Half |
10088 ADVERTISED_100baseT_Full |
10089 ADVERTISED_10baseT_Half |
10090 ADVERTISED_10baseT_Full);
10091
10092 cmd->advertising &= mask;
10093 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010094 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000010095 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010096 return -EINVAL;
10097
10098 if (cmd->duplex != DUPLEX_FULL)
10099 return -EINVAL;
10100 } else {
David Decotigny25db0332011-04-27 18:32:39 +000010101 if (speed != SPEED_100 &&
10102 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010103 return -EINVAL;
10104 }
10105 }
10106
David S. Millerf47c11e2005-06-24 20:18:35 -070010107 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010108
10109 tp->link_config.autoneg = cmd->autoneg;
10110 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070010111 tp->link_config.advertising = (cmd->advertising |
10112 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010113 tp->link_config.speed = SPEED_INVALID;
10114 tp->link_config.duplex = DUPLEX_INVALID;
10115 } else {
10116 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000010117 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010118 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010119 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010120
Michael Chan24fcad62006-12-17 17:06:46 -080010121 tp->link_config.orig_speed = tp->link_config.speed;
10122 tp->link_config.orig_duplex = tp->link_config.duplex;
10123 tp->link_config.orig_autoneg = tp->link_config.autoneg;
10124
Linus Torvalds1da177e2005-04-16 15:20:36 -070010125 if (netif_running(dev))
10126 tg3_setup_phy(tp, 1);
10127
David S. Millerf47c11e2005-06-24 20:18:35 -070010128 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010129
Linus Torvalds1da177e2005-04-16 15:20:36 -070010130 return 0;
10131}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010132
Linus Torvalds1da177e2005-04-16 15:20:36 -070010133static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10134{
10135 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010136
Linus Torvalds1da177e2005-04-16 15:20:36 -070010137 strcpy(info->driver, DRV_MODULE_NAME);
10138 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -080010139 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010140 strcpy(info->bus_info, pci_name(tp->pdev));
10141}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010142
Linus Torvalds1da177e2005-04-16 15:20:36 -070010143static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10144{
10145 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010146
Joe Perches63c3a662011-04-26 08:12:10 +000010147 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070010148 wol->supported = WAKE_MAGIC;
10149 else
10150 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010151 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010152 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010153 wol->wolopts = WAKE_MAGIC;
10154 memset(&wol->sopass, 0, sizeof(wol->sopass));
10155}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010156
Linus Torvalds1da177e2005-04-16 15:20:36 -070010157static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10158{
10159 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010160 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010161
Linus Torvalds1da177e2005-04-16 15:20:36 -070010162 if (wol->wolopts & ~WAKE_MAGIC)
10163 return -EINVAL;
10164 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010165 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010166 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010167
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010168 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
10169
David S. Millerf47c11e2005-06-24 20:18:35 -070010170 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010171 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000010172 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010173 else
Joe Perches63c3a662011-04-26 08:12:10 +000010174 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070010175 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010176
Linus Torvalds1da177e2005-04-16 15:20:36 -070010177 return 0;
10178}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010179
Linus Torvalds1da177e2005-04-16 15:20:36 -070010180static u32 tg3_get_msglevel(struct net_device *dev)
10181{
10182 struct tg3 *tp = netdev_priv(dev);
10183 return tp->msg_enable;
10184}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010185
Linus Torvalds1da177e2005-04-16 15:20:36 -070010186static void tg3_set_msglevel(struct net_device *dev, u32 value)
10187{
10188 struct tg3 *tp = netdev_priv(dev);
10189 tp->msg_enable = value;
10190}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010191
Linus Torvalds1da177e2005-04-16 15:20:36 -070010192static int tg3_nway_reset(struct net_device *dev)
10193{
10194 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010195 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010196
Linus Torvalds1da177e2005-04-16 15:20:36 -070010197 if (!netif_running(dev))
10198 return -EAGAIN;
10199
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010200 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010201 return -EINVAL;
10202
Joe Perches63c3a662011-04-26 08:12:10 +000010203 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010204 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010205 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010206 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010207 } else {
10208 u32 bmcr;
10209
10210 spin_lock_bh(&tp->lock);
10211 r = -EINVAL;
10212 tg3_readphy(tp, MII_BMCR, &bmcr);
10213 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10214 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010215 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010216 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10217 BMCR_ANENABLE);
10218 r = 0;
10219 }
10220 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010221 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010222
Linus Torvalds1da177e2005-04-16 15:20:36 -070010223 return r;
10224}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010225
Linus Torvalds1da177e2005-04-16 15:20:36 -070010226static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10227{
10228 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010229
Matt Carlson2c49a442010-09-30 10:34:35 +000010230 ering->rx_max_pending = tp->rx_std_ring_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010231 ering->rx_mini_max_pending = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010232 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000010233 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010234 else
10235 ering->rx_jumbo_max_pending = 0;
10236
10237 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010238
10239 ering->rx_pending = tp->rx_pending;
10240 ering->rx_mini_pending = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010241 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080010242 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10243 else
10244 ering->rx_jumbo_pending = 0;
10245
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010246 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010247}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010248
Linus Torvalds1da177e2005-04-16 15:20:36 -070010249static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10250{
10251 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010252 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010253
Matt Carlson2c49a442010-09-30 10:34:35 +000010254 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10255 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010256 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10257 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000010258 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010259 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010260 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010261
Michael Chanbbe832c2005-06-24 20:20:04 -070010262 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010263 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010264 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010265 irq_sync = 1;
10266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010267
Michael Chanbbe832c2005-06-24 20:20:04 -070010268 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010269
Linus Torvalds1da177e2005-04-16 15:20:36 -070010270 tp->rx_pending = ering->rx_pending;
10271
Joe Perches63c3a662011-04-26 08:12:10 +000010272 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010273 tp->rx_pending > 63)
10274 tp->rx_pending = 63;
10275 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010276
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010277 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010278 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010279
10280 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010281 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010282 err = tg3_restart_hw(tp, 1);
10283 if (!err)
10284 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010285 }
10286
David S. Millerf47c11e2005-06-24 20:18:35 -070010287 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010288
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010289 if (irq_sync && !err)
10290 tg3_phy_start(tp);
10291
Michael Chanb9ec6c12006-07-25 16:37:27 -070010292 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010293}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010294
Linus Torvalds1da177e2005-04-16 15:20:36 -070010295static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10296{
10297 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010298
Joe Perches63c3a662011-04-26 08:12:10 +000010299 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080010300
Steve Glendinninge18ce342008-12-16 02:00:00 -080010301 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010302 epause->rx_pause = 1;
10303 else
10304 epause->rx_pause = 0;
10305
Steve Glendinninge18ce342008-12-16 02:00:00 -080010306 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010307 epause->tx_pause = 1;
10308 else
10309 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010310}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010311
Linus Torvalds1da177e2005-04-16 15:20:36 -070010312static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10313{
10314 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010315 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010316
Joe Perches63c3a662011-04-26 08:12:10 +000010317 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000010318 u32 newadv;
10319 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010320
Matt Carlson27121682010-02-17 15:16:57 +000010321 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010322
Matt Carlson27121682010-02-17 15:16:57 +000010323 if (!(phydev->supported & SUPPORTED_Pause) ||
10324 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010325 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010326 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010327
Matt Carlson27121682010-02-17 15:16:57 +000010328 tp->link_config.flowctrl = 0;
10329 if (epause->rx_pause) {
10330 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010331
Matt Carlson27121682010-02-17 15:16:57 +000010332 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010333 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010334 newadv = ADVERTISED_Pause;
10335 } else
10336 newadv = ADVERTISED_Pause |
10337 ADVERTISED_Asym_Pause;
10338 } else if (epause->tx_pause) {
10339 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10340 newadv = ADVERTISED_Asym_Pause;
10341 } else
10342 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010343
Matt Carlson27121682010-02-17 15:16:57 +000010344 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010345 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010346 else
Joe Perches63c3a662011-04-26 08:12:10 +000010347 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010348
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010349 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010350 u32 oldadv = phydev->advertising &
10351 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10352 if (oldadv != newadv) {
10353 phydev->advertising &=
10354 ~(ADVERTISED_Pause |
10355 ADVERTISED_Asym_Pause);
10356 phydev->advertising |= newadv;
10357 if (phydev->autoneg) {
10358 /*
10359 * Always renegotiate the link to
10360 * inform our link partner of our
10361 * flow control settings, even if the
10362 * flow control is forced. Let
10363 * tg3_adjust_link() do the final
10364 * flow control setup.
10365 */
10366 return phy_start_aneg(phydev);
10367 }
10368 }
10369
10370 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010371 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010372 } else {
10373 tp->link_config.orig_advertising &=
10374 ~(ADVERTISED_Pause |
10375 ADVERTISED_Asym_Pause);
10376 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010377 }
10378 } else {
10379 int irq_sync = 0;
10380
10381 if (netif_running(dev)) {
10382 tg3_netif_stop(tp);
10383 irq_sync = 1;
10384 }
10385
10386 tg3_full_lock(tp, irq_sync);
10387
10388 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010389 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010390 else
Joe Perches63c3a662011-04-26 08:12:10 +000010391 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010392 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010393 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010394 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010395 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010396 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010397 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010398 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010399 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010400
10401 if (netif_running(dev)) {
10402 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10403 err = tg3_restart_hw(tp, 1);
10404 if (!err)
10405 tg3_netif_start(tp);
10406 }
10407
10408 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010410
Michael Chanb9ec6c12006-07-25 16:37:27 -070010411 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010412}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010413
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010414static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010415{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010416 switch (sset) {
10417 case ETH_SS_TEST:
10418 return TG3_NUM_TEST;
10419 case ETH_SS_STATS:
10420 return TG3_NUM_STATS;
10421 default:
10422 return -EOPNOTSUPP;
10423 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010424}
10425
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010426static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010427{
10428 switch (stringset) {
10429 case ETH_SS_STATS:
10430 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10431 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010432 case ETH_SS_TEST:
10433 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10434 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010435 default:
10436 WARN_ON(1); /* we need a WARN() */
10437 break;
10438 }
10439}
10440
stephen hemminger81b87092011-04-04 08:43:50 +000010441static int tg3_set_phys_id(struct net_device *dev,
10442 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070010443{
10444 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070010445
10446 if (!netif_running(tp->dev))
10447 return -EAGAIN;
10448
stephen hemminger81b87092011-04-04 08:43:50 +000010449 switch (state) {
10450 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000010451 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070010452
stephen hemminger81b87092011-04-04 08:43:50 +000010453 case ETHTOOL_ID_ON:
10454 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10455 LED_CTRL_1000MBPS_ON |
10456 LED_CTRL_100MBPS_ON |
10457 LED_CTRL_10MBPS_ON |
10458 LED_CTRL_TRAFFIC_OVERRIDE |
10459 LED_CTRL_TRAFFIC_BLINK |
10460 LED_CTRL_TRAFFIC_LED);
10461 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010462
stephen hemminger81b87092011-04-04 08:43:50 +000010463 case ETHTOOL_ID_OFF:
10464 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10465 LED_CTRL_TRAFFIC_OVERRIDE);
10466 break;
Michael Chan4009a932005-09-05 17:52:54 -070010467
stephen hemminger81b87092011-04-04 08:43:50 +000010468 case ETHTOOL_ID_INACTIVE:
10469 tw32(MAC_LED_CTRL, tp->led_ctrl);
10470 break;
Michael Chan4009a932005-09-05 17:52:54 -070010471 }
stephen hemminger81b87092011-04-04 08:43:50 +000010472
Michael Chan4009a932005-09-05 17:52:54 -070010473 return 0;
10474}
10475
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010476static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010477 struct ethtool_stats *estats, u64 *tmp_stats)
10478{
10479 struct tg3 *tp = netdev_priv(dev);
10480 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10481}
10482
Matt Carlsonc3e94502011-04-13 11:05:08 +000010483static __be32 * tg3_vpd_readblock(struct tg3 *tp)
10484{
10485 int i;
10486 __be32 *buf;
10487 u32 offset = 0, len = 0;
10488 u32 magic, val;
10489
Joe Perches63c3a662011-04-26 08:12:10 +000010490 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000010491 return NULL;
10492
10493 if (magic == TG3_EEPROM_MAGIC) {
10494 for (offset = TG3_NVM_DIR_START;
10495 offset < TG3_NVM_DIR_END;
10496 offset += TG3_NVM_DIRENT_SIZE) {
10497 if (tg3_nvram_read(tp, offset, &val))
10498 return NULL;
10499
10500 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
10501 TG3_NVM_DIRTYPE_EXTVPD)
10502 break;
10503 }
10504
10505 if (offset != TG3_NVM_DIR_END) {
10506 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
10507 if (tg3_nvram_read(tp, offset + 4, &offset))
10508 return NULL;
10509
10510 offset = tg3_nvram_logical_addr(tp, offset);
10511 }
10512 }
10513
10514 if (!offset || !len) {
10515 offset = TG3_NVM_VPD_OFF;
10516 len = TG3_NVM_VPD_LEN;
10517 }
10518
10519 buf = kmalloc(len, GFP_KERNEL);
10520 if (buf == NULL)
10521 return NULL;
10522
10523 if (magic == TG3_EEPROM_MAGIC) {
10524 for (i = 0; i < len; i += 4) {
10525 /* The data is in little-endian format in NVRAM.
10526 * Use the big-endian read routines to preserve
10527 * the byte order as it exists in NVRAM.
10528 */
10529 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
10530 goto error;
10531 }
10532 } else {
10533 u8 *ptr;
10534 ssize_t cnt;
10535 unsigned int pos = 0;
10536
10537 ptr = (u8 *)&buf[0];
10538 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
10539 cnt = pci_read_vpd(tp->pdev, pos,
10540 len - pos, ptr);
10541 if (cnt == -ETIMEDOUT || cnt == -EINTR)
10542 cnt = 0;
10543 else if (cnt < 0)
10544 goto error;
10545 }
10546 if (pos != len)
10547 goto error;
10548 }
10549
10550 return buf;
10551
10552error:
10553 kfree(buf);
10554 return NULL;
10555}
10556
Michael Chan566f86a2005-05-29 14:56:58 -070010557#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010558#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10559#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10560#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Michael Chanb16250e2006-09-27 16:10:14 -070010561#define NVRAM_SELFBOOT_HW_SIZE 0x20
10562#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010563
10564static int tg3_test_nvram(struct tg3 *tp)
10565{
Al Virob9fc7dc2007-12-17 22:59:57 -080010566 u32 csum, magic;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010567 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010568 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010569
Joe Perches63c3a662011-04-26 08:12:10 +000010570 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010571 return 0;
10572
Matt Carlsone4f34112009-02-25 14:25:00 +000010573 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010574 return -EIO;
10575
Michael Chan1b277772006-03-20 22:27:48 -080010576 if (magic == TG3_EEPROM_MAGIC)
10577 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010578 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010579 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10580 TG3_EEPROM_SB_FORMAT_1) {
10581 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10582 case TG3_EEPROM_SB_REVISION_0:
10583 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10584 break;
10585 case TG3_EEPROM_SB_REVISION_2:
10586 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10587 break;
10588 case TG3_EEPROM_SB_REVISION_3:
10589 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10590 break;
10591 default:
10592 return 0;
10593 }
10594 } else
Michael Chan1b277772006-03-20 22:27:48 -080010595 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010596 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10597 size = NVRAM_SELFBOOT_HW_SIZE;
10598 else
Michael Chan1b277772006-03-20 22:27:48 -080010599 return -EIO;
10600
10601 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010602 if (buf == NULL)
10603 return -ENOMEM;
10604
Michael Chan1b277772006-03-20 22:27:48 -080010605 err = -EIO;
10606 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010607 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10608 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010609 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010610 }
Michael Chan1b277772006-03-20 22:27:48 -080010611 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010612 goto out;
10613
Michael Chan1b277772006-03-20 22:27:48 -080010614 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010615 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010616 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010617 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010618 u8 *buf8 = (u8 *) buf, csum8 = 0;
10619
Al Virob9fc7dc2007-12-17 22:59:57 -080010620 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010621 TG3_EEPROM_SB_REVISION_2) {
10622 /* For rev 2, the csum doesn't include the MBA. */
10623 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10624 csum8 += buf8[i];
10625 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10626 csum8 += buf8[i];
10627 } else {
10628 for (i = 0; i < size; i++)
10629 csum8 += buf8[i];
10630 }
Michael Chan1b277772006-03-20 22:27:48 -080010631
Adrian Bunkad96b482006-04-05 22:21:04 -070010632 if (csum8 == 0) {
10633 err = 0;
10634 goto out;
10635 }
10636
10637 err = -EIO;
10638 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010639 }
Michael Chan566f86a2005-05-29 14:56:58 -070010640
Al Virob9fc7dc2007-12-17 22:59:57 -080010641 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010642 TG3_EEPROM_MAGIC_HW) {
10643 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010644 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010645 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010646
10647 /* Separate the parity bits and the data bytes. */
10648 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10649 if ((i == 0) || (i == 8)) {
10650 int l;
10651 u8 msk;
10652
10653 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10654 parity[k++] = buf8[i] & msk;
10655 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000010656 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010657 int l;
10658 u8 msk;
10659
10660 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10661 parity[k++] = buf8[i] & msk;
10662 i++;
10663
10664 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10665 parity[k++] = buf8[i] & msk;
10666 i++;
10667 }
10668 data[j++] = buf8[i];
10669 }
10670
10671 err = -EIO;
10672 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10673 u8 hw8 = hweight8(data[i]);
10674
10675 if ((hw8 & 0x1) && parity[i])
10676 goto out;
10677 else if (!(hw8 & 0x1) && !parity[i])
10678 goto out;
10679 }
10680 err = 0;
10681 goto out;
10682 }
10683
Matt Carlson01c3a392011-03-09 16:58:20 +000010684 err = -EIO;
10685
Michael Chan566f86a2005-05-29 14:56:58 -070010686 /* Bootstrap checksum at offset 0x10 */
10687 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000010688 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010689 goto out;
10690
10691 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10692 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000010693 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000010694 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010695
Matt Carlsonc3e94502011-04-13 11:05:08 +000010696 kfree(buf);
10697
10698 buf = tg3_vpd_readblock(tp);
10699 if (!buf)
10700 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000010701
10702 i = pci_vpd_find_tag((u8 *)buf, 0, TG3_NVM_VPD_LEN,
10703 PCI_VPD_LRDT_RO_DATA);
10704 if (i > 0) {
10705 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
10706 if (j < 0)
10707 goto out;
10708
10709 if (i + PCI_VPD_LRDT_TAG_SIZE + j > TG3_NVM_VPD_LEN)
10710 goto out;
10711
10712 i += PCI_VPD_LRDT_TAG_SIZE;
10713 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
10714 PCI_VPD_RO_KEYWORD_CHKSUM);
10715 if (j > 0) {
10716 u8 csum8 = 0;
10717
10718 j += PCI_VPD_INFO_FLD_HDR_SIZE;
10719
10720 for (i = 0; i <= j; i++)
10721 csum8 += ((u8 *)buf)[i];
10722
10723 if (csum8)
10724 goto out;
10725 }
10726 }
10727
Michael Chan566f86a2005-05-29 14:56:58 -070010728 err = 0;
10729
10730out:
10731 kfree(buf);
10732 return err;
10733}
10734
Michael Chanca430072005-05-29 14:57:23 -070010735#define TG3_SERDES_TIMEOUT_SEC 2
10736#define TG3_COPPER_TIMEOUT_SEC 6
10737
10738static int tg3_test_link(struct tg3 *tp)
10739{
10740 int i, max;
10741
10742 if (!netif_running(tp->dev))
10743 return -ENODEV;
10744
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010745 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070010746 max = TG3_SERDES_TIMEOUT_SEC;
10747 else
10748 max = TG3_COPPER_TIMEOUT_SEC;
10749
10750 for (i = 0; i < max; i++) {
10751 if (netif_carrier_ok(tp->dev))
10752 return 0;
10753
10754 if (msleep_interruptible(1000))
10755 break;
10756 }
10757
10758 return -EIO;
10759}
10760
Michael Chana71116d2005-05-29 14:58:11 -070010761/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080010762static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070010763{
Michael Chanb16250e2006-09-27 16:10:14 -070010764 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070010765 u32 offset, read_mask, write_mask, val, save_val, read_val;
10766 static struct {
10767 u16 offset;
10768 u16 flags;
10769#define TG3_FL_5705 0x1
10770#define TG3_FL_NOT_5705 0x2
10771#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070010772#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070010773 u32 read_mask;
10774 u32 write_mask;
10775 } reg_tbl[] = {
10776 /* MAC Control Registers */
10777 { MAC_MODE, TG3_FL_NOT_5705,
10778 0x00000000, 0x00ef6f8c },
10779 { MAC_MODE, TG3_FL_5705,
10780 0x00000000, 0x01ef6b8c },
10781 { MAC_STATUS, TG3_FL_NOT_5705,
10782 0x03800107, 0x00000000 },
10783 { MAC_STATUS, TG3_FL_5705,
10784 0x03800100, 0x00000000 },
10785 { MAC_ADDR_0_HIGH, 0x0000,
10786 0x00000000, 0x0000ffff },
10787 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010788 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070010789 { MAC_RX_MTU_SIZE, 0x0000,
10790 0x00000000, 0x0000ffff },
10791 { MAC_TX_MODE, 0x0000,
10792 0x00000000, 0x00000070 },
10793 { MAC_TX_LENGTHS, 0x0000,
10794 0x00000000, 0x00003fff },
10795 { MAC_RX_MODE, TG3_FL_NOT_5705,
10796 0x00000000, 0x000007fc },
10797 { MAC_RX_MODE, TG3_FL_5705,
10798 0x00000000, 0x000007dc },
10799 { MAC_HASH_REG_0, 0x0000,
10800 0x00000000, 0xffffffff },
10801 { MAC_HASH_REG_1, 0x0000,
10802 0x00000000, 0xffffffff },
10803 { MAC_HASH_REG_2, 0x0000,
10804 0x00000000, 0xffffffff },
10805 { MAC_HASH_REG_3, 0x0000,
10806 0x00000000, 0xffffffff },
10807
10808 /* Receive Data and Receive BD Initiator Control Registers. */
10809 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
10810 0x00000000, 0xffffffff },
10811 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
10812 0x00000000, 0xffffffff },
10813 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
10814 0x00000000, 0x00000003 },
10815 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
10816 0x00000000, 0xffffffff },
10817 { RCVDBDI_STD_BD+0, 0x0000,
10818 0x00000000, 0xffffffff },
10819 { RCVDBDI_STD_BD+4, 0x0000,
10820 0x00000000, 0xffffffff },
10821 { RCVDBDI_STD_BD+8, 0x0000,
10822 0x00000000, 0xffff0002 },
10823 { RCVDBDI_STD_BD+0xc, 0x0000,
10824 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010825
Michael Chana71116d2005-05-29 14:58:11 -070010826 /* Receive BD Initiator Control Registers. */
10827 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
10828 0x00000000, 0xffffffff },
10829 { RCVBDI_STD_THRESH, TG3_FL_5705,
10830 0x00000000, 0x000003ff },
10831 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
10832 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010833
Michael Chana71116d2005-05-29 14:58:11 -070010834 /* Host Coalescing Control Registers. */
10835 { HOSTCC_MODE, TG3_FL_NOT_5705,
10836 0x00000000, 0x00000004 },
10837 { HOSTCC_MODE, TG3_FL_5705,
10838 0x00000000, 0x000000f6 },
10839 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
10840 0x00000000, 0xffffffff },
10841 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
10842 0x00000000, 0x000003ff },
10843 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
10844 0x00000000, 0xffffffff },
10845 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
10846 0x00000000, 0x000003ff },
10847 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
10848 0x00000000, 0xffffffff },
10849 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10850 0x00000000, 0x000000ff },
10851 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
10852 0x00000000, 0xffffffff },
10853 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10854 0x00000000, 0x000000ff },
10855 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
10856 0x00000000, 0xffffffff },
10857 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
10858 0x00000000, 0xffffffff },
10859 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10860 0x00000000, 0xffffffff },
10861 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10862 0x00000000, 0x000000ff },
10863 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10864 0x00000000, 0xffffffff },
10865 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10866 0x00000000, 0x000000ff },
10867 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
10868 0x00000000, 0xffffffff },
10869 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
10870 0x00000000, 0xffffffff },
10871 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
10872 0x00000000, 0xffffffff },
10873 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
10874 0x00000000, 0xffffffff },
10875 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
10876 0x00000000, 0xffffffff },
10877 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
10878 0xffffffff, 0x00000000 },
10879 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
10880 0xffffffff, 0x00000000 },
10881
10882 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070010883 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010884 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070010885 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010886 0x00000000, 0x007fffff },
10887 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
10888 0x00000000, 0x0000003f },
10889 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
10890 0x00000000, 0x000001ff },
10891 { BUFMGR_MB_HIGH_WATER, 0x0000,
10892 0x00000000, 0x000001ff },
10893 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
10894 0xffffffff, 0x00000000 },
10895 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
10896 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010897
Michael Chana71116d2005-05-29 14:58:11 -070010898 /* Mailbox Registers */
10899 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
10900 0x00000000, 0x000001ff },
10901 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
10902 0x00000000, 0x000001ff },
10903 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
10904 0x00000000, 0x000007ff },
10905 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
10906 0x00000000, 0x000001ff },
10907
10908 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
10909 };
10910
Michael Chanb16250e2006-09-27 16:10:14 -070010911 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010912 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070010913 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000010914 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070010915 is_5750 = 1;
10916 }
Michael Chana71116d2005-05-29 14:58:11 -070010917
10918 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
10919 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
10920 continue;
10921
10922 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
10923 continue;
10924
Joe Perches63c3a662011-04-26 08:12:10 +000010925 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070010926 (reg_tbl[i].flags & TG3_FL_NOT_5788))
10927 continue;
10928
Michael Chanb16250e2006-09-27 16:10:14 -070010929 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
10930 continue;
10931
Michael Chana71116d2005-05-29 14:58:11 -070010932 offset = (u32) reg_tbl[i].offset;
10933 read_mask = reg_tbl[i].read_mask;
10934 write_mask = reg_tbl[i].write_mask;
10935
10936 /* Save the original register content */
10937 save_val = tr32(offset);
10938
10939 /* Determine the read-only value. */
10940 read_val = save_val & read_mask;
10941
10942 /* Write zero to the register, then make sure the read-only bits
10943 * are not changed and the read/write bits are all zeros.
10944 */
10945 tw32(offset, 0);
10946
10947 val = tr32(offset);
10948
10949 /* Test the read-only and read/write bits. */
10950 if (((val & read_mask) != read_val) || (val & write_mask))
10951 goto out;
10952
10953 /* Write ones to all the bits defined by RdMask and WrMask, then
10954 * make sure the read-only bits are not changed and the
10955 * read/write bits are all ones.
10956 */
10957 tw32(offset, read_mask | write_mask);
10958
10959 val = tr32(offset);
10960
10961 /* Test the read-only bits. */
10962 if ((val & read_mask) != read_val)
10963 goto out;
10964
10965 /* Test the read/write bits. */
10966 if ((val & write_mask) != write_mask)
10967 goto out;
10968
10969 tw32(offset, save_val);
10970 }
10971
10972 return 0;
10973
10974out:
Michael Chan9f88f292006-12-07 00:22:54 -080010975 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000010976 netdev_err(tp->dev,
10977 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070010978 tw32(offset, save_val);
10979 return -EIO;
10980}
10981
Michael Chan7942e1d2005-05-29 14:58:36 -070010982static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
10983{
Arjan van de Venf71e1302006-03-03 21:33:57 -050010984 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070010985 int i;
10986 u32 j;
10987
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020010988 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070010989 for (j = 0; j < len; j += 4) {
10990 u32 val;
10991
10992 tg3_write_mem(tp, offset + j, test_pattern[i]);
10993 tg3_read_mem(tp, offset + j, &val);
10994 if (val != test_pattern[i])
10995 return -EIO;
10996 }
10997 }
10998 return 0;
10999}
11000
11001static int tg3_test_memory(struct tg3 *tp)
11002{
11003 static struct mem_entry {
11004 u32 offset;
11005 u32 len;
11006 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080011007 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070011008 { 0x00002000, 0x1c000},
11009 { 0xffffffff, 0x00000}
11010 }, mem_tbl_5705[] = {
11011 { 0x00000100, 0x0000c},
11012 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070011013 { 0x00004000, 0x00800},
11014 { 0x00006000, 0x01000},
11015 { 0x00008000, 0x02000},
11016 { 0x00010000, 0x0e000},
11017 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080011018 }, mem_tbl_5755[] = {
11019 { 0x00000200, 0x00008},
11020 { 0x00004000, 0x00800},
11021 { 0x00006000, 0x00800},
11022 { 0x00008000, 0x02000},
11023 { 0x00010000, 0x0c000},
11024 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070011025 }, mem_tbl_5906[] = {
11026 { 0x00000200, 0x00008},
11027 { 0x00004000, 0x00400},
11028 { 0x00006000, 0x00400},
11029 { 0x00008000, 0x01000},
11030 { 0x00010000, 0x01000},
11031 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011032 }, mem_tbl_5717[] = {
11033 { 0x00000200, 0x00008},
11034 { 0x00010000, 0x0a000},
11035 { 0x00020000, 0x13c00},
11036 { 0xffffffff, 0x00000}
11037 }, mem_tbl_57765[] = {
11038 { 0x00000200, 0x00008},
11039 { 0x00004000, 0x00800},
11040 { 0x00006000, 0x09800},
11041 { 0x00010000, 0x0a000},
11042 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070011043 };
11044 struct mem_entry *mem_tbl;
11045 int err = 0;
11046 int i;
11047
Joe Perches63c3a662011-04-26 08:12:10 +000011048 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011049 mem_tbl = mem_tbl_5717;
11050 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
11051 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000011052 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011053 mem_tbl = mem_tbl_5755;
11054 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11055 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000011056 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011057 mem_tbl = mem_tbl_5705;
11058 else
Michael Chan7942e1d2005-05-29 14:58:36 -070011059 mem_tbl = mem_tbl_570x;
11060
11061 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000011062 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
11063 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070011064 break;
11065 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011066
Michael Chan7942e1d2005-05-29 14:58:36 -070011067 return err;
11068}
11069
Michael Chan9f40dea2005-09-05 17:53:06 -070011070#define TG3_MAC_LOOPBACK 0
11071#define TG3_PHY_LOOPBACK 1
Matt Carlsonbb158d62011-04-25 12:42:47 +000011072#define TG3_TSO_LOOPBACK 2
11073
11074#define TG3_TSO_MSS 500
11075
11076#define TG3_TSO_IP_HDR_LEN 20
11077#define TG3_TSO_TCP_HDR_LEN 20
11078#define TG3_TSO_TCP_OPT_LEN 12
11079
11080static const u8 tg3_tso_header[] = {
110810x08, 0x00,
110820x45, 0x00, 0x00, 0x00,
110830x00, 0x00, 0x40, 0x00,
110840x40, 0x06, 0x00, 0x00,
110850x0a, 0x00, 0x00, 0x01,
110860x0a, 0x00, 0x00, 0x02,
110870x0d, 0x00, 0xe0, 0x00,
110880x00, 0x00, 0x01, 0x00,
110890x00, 0x00, 0x02, 0x00,
110900x80, 0x10, 0x10, 0x00,
110910x14, 0x09, 0x00, 0x00,
110920x01, 0x01, 0x08, 0x0a,
110930x11, 0x11, 0x11, 0x11,
110940x11, 0x11, 0x11, 0x11,
11095};
Michael Chan9f40dea2005-09-05 17:53:06 -070011096
Matt Carlson4852a862011-04-13 11:05:07 +000011097static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, int loopback_mode)
Michael Chanc76949a2005-05-29 14:58:59 -070011098{
Michael Chan9f40dea2005-09-05 17:53:06 -070011099 u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011100 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Michael Chanc76949a2005-05-29 14:58:59 -070011101 struct sk_buff *skb, *rx_skb;
11102 u8 *tx_data;
11103 dma_addr_t map;
11104 int num_pkts, tx_len, rx_len, i, err;
11105 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000011106 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000011107 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070011108
Matt Carlsonc8873402010-02-12 14:47:11 +000011109 tnapi = &tp->napi[0];
11110 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011111 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000011112 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000011113 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000011114 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000011115 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011116 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011117 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000011118
Michael Chan9f40dea2005-09-05 17:53:06 -070011119 if (loopback_mode == TG3_MAC_LOOPBACK) {
Michael Chanc94e3942005-09-27 12:12:42 -070011120 /* HW errata - mac loopback fails in some cases on 5780.
11121 * Normal traffic and PHY loopback are not affected by
Matt Carlsonaba49f22011-01-25 15:58:53 +000011122 * errata. Also, the MAC loopback test is deprecated for
11123 * all newer ASIC revisions.
Michael Chanc94e3942005-09-27 12:12:42 -070011124 */
Matt Carlsonaba49f22011-01-25 15:58:53 +000011125 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000011126 tg3_flag(tp, CPMU_PRESENT))
Michael Chanc94e3942005-09-27 12:12:42 -070011127 return 0;
11128
Matt Carlson49692ca2011-01-25 15:58:52 +000011129 mac_mode = tp->mac_mode &
11130 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
11131 mac_mode |= MAC_MODE_PORT_INT_LPBACK;
Joe Perches63c3a662011-04-26 08:12:10 +000011132 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011133 mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011134 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
Michael Chan3f7045c2006-09-27 16:02:29 -070011135 mac_mode |= MAC_MODE_PORT_MODE_MII;
11136 else
11137 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chan9f40dea2005-09-05 17:53:06 -070011138 tw32(MAC_MODE, mac_mode);
Matt Carlsonbb158d62011-04-25 12:42:47 +000011139 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011140 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +000011141 tg3_phy_fet_toggle_apd(tp, false);
Michael Chan5d64ad32006-12-07 00:19:40 -080011142 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
11143 } else
11144 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
Michael Chan3f7045c2006-09-27 16:02:29 -070011145
Matt Carlson9ef8ca92007-07-11 19:48:29 -070011146 tg3_phy_toggle_automdix(tp, 0);
11147
Michael Chan3f7045c2006-09-27 16:02:29 -070011148 tg3_writephy(tp, MII_BMCR, val);
Michael Chanc94e3942005-09-27 12:12:42 -070011149 udelay(40);
Michael Chan5d64ad32006-12-07 00:19:40 -080011150
Matt Carlson49692ca2011-01-25 15:58:52 +000011151 mac_mode = tp->mac_mode &
11152 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011153 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson1061b7c2010-02-12 14:47:12 +000011154 tg3_writephy(tp, MII_TG3_FET_PTEST,
11155 MII_TG3_FET_PTEST_FRC_TX_LINK |
11156 MII_TG3_FET_PTEST_FRC_TX_LOCK);
11157 /* The write needs to be flushed for the AC131 */
11158 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
11159 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
Michael Chan5d64ad32006-12-07 00:19:40 -080011160 mac_mode |= MAC_MODE_PORT_MODE_MII;
11161 } else
11162 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chanb16250e2006-09-27 16:10:14 -070011163
Michael Chanc94e3942005-09-27 12:12:42 -070011164 /* reset to prevent losing 1st rx packet intermittently */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011165 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Michael Chanc94e3942005-09-27 12:12:42 -070011166 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
11167 udelay(10);
11168 tw32_f(MAC_RX_MODE, tp->rx_mode);
11169 }
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011170 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlson79eb6902010-02-17 15:17:03 +000011171 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
11172 if (masked_phy_id == TG3_PHY_ID_BCM5401)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011173 mac_mode &= ~MAC_MODE_LINK_POLARITY;
Matt Carlson79eb6902010-02-17 15:17:03 +000011174 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011175 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chanff18ff02006-03-27 23:17:27 -080011176 tg3_writephy(tp, MII_TG3_EXT_CTRL,
11177 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
11178 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011179 tw32(MAC_MODE, mac_mode);
Matt Carlson49692ca2011-01-25 15:58:52 +000011180
11181 /* Wait for link */
11182 for (i = 0; i < 100; i++) {
11183 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
11184 break;
11185 mdelay(1);
11186 }
Matt Carlson859a588792010-04-05 10:19:28 +000011187 }
Michael Chanc76949a2005-05-29 14:58:59 -070011188
11189 err = -EIO;
11190
Matt Carlson4852a862011-04-13 11:05:07 +000011191 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070011192 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070011193 if (!skb)
11194 return -ENOMEM;
11195
Michael Chanc76949a2005-05-29 14:58:59 -070011196 tx_data = skb_put(skb, tx_len);
11197 memcpy(tx_data, tp->dev->dev_addr, 6);
11198 memset(tx_data + 6, 0x0, 8);
11199
Matt Carlson4852a862011-04-13 11:05:07 +000011200 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070011201
Matt Carlsonbb158d62011-04-25 12:42:47 +000011202 if (loopback_mode == TG3_TSO_LOOPBACK) {
11203 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
11204
11205 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
11206 TG3_TSO_TCP_OPT_LEN;
11207
11208 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
11209 sizeof(tg3_tso_header));
11210 mss = TG3_TSO_MSS;
11211
11212 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
11213 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
11214
11215 /* Set the total length field in the IP header */
11216 iph->tot_len = htons((u16)(mss + hdr_len));
11217
11218 base_flags = (TXD_FLAG_CPU_PRE_DMA |
11219 TXD_FLAG_CPU_POST_DMA);
11220
Joe Perches63c3a662011-04-26 08:12:10 +000011221 if (tg3_flag(tp, HW_TSO_1) ||
11222 tg3_flag(tp, HW_TSO_2) ||
11223 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011224 struct tcphdr *th;
11225 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
11226 th = (struct tcphdr *)&tx_data[val];
11227 th->check = 0;
11228 } else
11229 base_flags |= TXD_FLAG_TCPUDP_CSUM;
11230
Joe Perches63c3a662011-04-26 08:12:10 +000011231 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011232 mss |= (hdr_len & 0xc) << 12;
11233 if (hdr_len & 0x10)
11234 base_flags |= 0x00000010;
11235 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000011236 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011237 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000011238 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlsonbb158d62011-04-25 12:42:47 +000011239 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
11240 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
11241 } else {
11242 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
11243 }
11244
11245 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
11246 } else {
11247 num_pkts = 1;
11248 data_off = ETH_HLEN;
11249 }
11250
11251 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070011252 tx_data[i] = (u8) (i & 0xff);
11253
Alexander Duyckf4188d82009-12-02 16:48:38 +000011254 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
11255 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000011256 dev_kfree_skb(skb);
11257 return -EIO;
11258 }
Michael Chanc76949a2005-05-29 14:58:59 -070011259
11260 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011261 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011262
11263 udelay(10);
11264
Matt Carlson898a56f2009-08-28 14:02:40 +000011265 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011266
Matt Carlsonbb158d62011-04-25 12:42:47 +000011267 tg3_set_txd(tnapi, tnapi->tx_prod, map, tx_len,
11268 base_flags, (mss << 1) | 1);
Michael Chanc76949a2005-05-29 14:58:59 -070011269
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011270 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011271
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011272 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11273 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011274
11275 udelay(10);
11276
Matt Carlson303fc922009-11-02 14:27:34 +000011277 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11278 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011279 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011280 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011281
11282 udelay(10);
11283
Matt Carlson898a56f2009-08-28 14:02:40 +000011284 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11285 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011286 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011287 (rx_idx == (rx_start_idx + num_pkts)))
11288 break;
11289 }
11290
Alexander Duyckf4188d82009-12-02 16:48:38 +000011291 pci_unmap_single(tp->pdev, map, tx_len, PCI_DMA_TODEVICE);
Michael Chanc76949a2005-05-29 14:58:59 -070011292 dev_kfree_skb(skb);
11293
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011294 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011295 goto out;
11296
11297 if (rx_idx != rx_start_idx + num_pkts)
11298 goto out;
11299
Matt Carlsonbb158d62011-04-25 12:42:47 +000011300 val = data_off;
11301 while (rx_idx != rx_start_idx) {
11302 desc = &rnapi->rx_rcb[rx_start_idx++];
11303 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11304 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070011305
Matt Carlsonbb158d62011-04-25 12:42:47 +000011306 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11307 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000011308 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070011309
Matt Carlsonbb158d62011-04-25 12:42:47 +000011310 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
11311 - ETH_FCS_LEN;
11312
11313 if (loopback_mode != TG3_TSO_LOOPBACK) {
11314 if (rx_len != tx_len)
11315 goto out;
11316
11317 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
11318 if (opaque_key != RXD_OPAQUE_RING_STD)
11319 goto out;
11320 } else {
11321 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
11322 goto out;
11323 }
11324 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
11325 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
11326 >> RXD_TCPCSUM_SHIFT == 0xffff) {
11327 goto out;
11328 }
11329
11330 if (opaque_key == RXD_OPAQUE_RING_STD) {
11331 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
11332 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
11333 mapping);
11334 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
11335 rx_skb = tpr->rx_jmb_buffers[desc_idx].skb;
11336 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
11337 mapping);
11338 } else
Matt Carlson4852a862011-04-13 11:05:07 +000011339 goto out;
11340
Matt Carlsonbb158d62011-04-25 12:42:47 +000011341 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
11342 PCI_DMA_FROMDEVICE);
11343
11344 for (i = data_off; i < rx_len; i++, val++) {
11345 if (*(rx_skb->data + i) != (u8) (val & 0xff))
11346 goto out;
11347 }
Matt Carlson4852a862011-04-13 11:05:07 +000011348 }
11349
Michael Chanc76949a2005-05-29 14:58:59 -070011350 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011351
Michael Chanc76949a2005-05-29 14:58:59 -070011352 /* tg3_free_rings will unmap and free the rx_skb */
11353out:
11354 return err;
11355}
11356
Matt Carlson00c266b2011-04-25 12:42:46 +000011357#define TG3_STD_LOOPBACK_FAILED 1
11358#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000011359#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson00c266b2011-04-25 12:42:46 +000011360
11361#define TG3_MAC_LOOPBACK_SHIFT 0
11362#define TG3_PHY_LOOPBACK_SHIFT 4
Matt Carlsonbb158d62011-04-25 12:42:47 +000011363#define TG3_LOOPBACK_FAILED 0x00000077
Michael Chan9f40dea2005-09-05 17:53:06 -070011364
11365static int tg3_test_loopback(struct tg3 *tp)
11366{
11367 int err = 0;
Matt Carlsonab789042011-01-25 15:58:54 +000011368 u32 eee_cap, cpmuctrl = 0;
Michael Chan9f40dea2005-09-05 17:53:06 -070011369
11370 if (!netif_running(tp->dev))
11371 return TG3_LOOPBACK_FAILED;
11372
Matt Carlsonab789042011-01-25 15:58:54 +000011373 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11374 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11375
Michael Chanb9ec6c12006-07-25 16:37:27 -070011376 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011377 if (err) {
11378 err = TG3_LOOPBACK_FAILED;
11379 goto done;
11380 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011381
Joe Perches63c3a662011-04-26 08:12:10 +000011382 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000011383 int i;
11384
11385 /* Reroute all rx packets to the 1st queue */
11386 for (i = MAC_RSS_INDIR_TBL_0;
11387 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
11388 tw32(i, 0x0);
11389 }
11390
Matt Carlson6833c042008-11-21 17:18:59 -080011391 /* Turn off gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011392 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080011393 tg3_phy_toggle_apd(tp, false);
11394
Joe Perches63c3a662011-04-26 08:12:10 +000011395 if (tg3_flag(tp, CPMU_PRESENT)) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070011396 int i;
11397 u32 status;
11398
11399 tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
11400
11401 /* Wait for up to 40 microseconds to acquire lock. */
11402 for (i = 0; i < 4; i++) {
11403 status = tr32(TG3_CPMU_MUTEX_GNT);
11404 if (status == CPMU_MUTEX_GNT_DRIVER)
11405 break;
11406 udelay(10);
11407 }
11408
Matt Carlsonab789042011-01-25 15:58:54 +000011409 if (status != CPMU_MUTEX_GNT_DRIVER) {
11410 err = TG3_LOOPBACK_FAILED;
11411 goto done;
11412 }
Matt Carlson9936bcf2007-10-10 18:03:07 -070011413
Matt Carlsonb2a5c192008-04-03 21:44:44 -070011414 /* Turn off link-based power management. */
Matt Carlsone8750932007-11-12 21:11:51 -080011415 cpmuctrl = tr32(TG3_CPMU_CTRL);
Matt Carlson109115e2008-05-02 16:48:59 -070011416 tw32(TG3_CPMU_CTRL,
11417 cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
11418 CPMU_CTRL_LINK_AWARE_MODE));
Matt Carlson9936bcf2007-10-10 18:03:07 -070011419 }
11420
Matt Carlson4852a862011-04-13 11:05:07 +000011421 if (tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_MAC_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011422 err |= TG3_STD_LOOPBACK_FAILED << TG3_MAC_LOOPBACK_SHIFT;
Matt Carlson9936bcf2007-10-10 18:03:07 -070011423
Joe Perches63c3a662011-04-26 08:12:10 +000011424 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson4852a862011-04-13 11:05:07 +000011425 tg3_run_loopback(tp, 9000 + ETH_HLEN, TG3_MAC_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011426 err |= TG3_JMB_LOOPBACK_FAILED << TG3_MAC_LOOPBACK_SHIFT;
Matt Carlson4852a862011-04-13 11:05:07 +000011427
Joe Perches63c3a662011-04-26 08:12:10 +000011428 if (tg3_flag(tp, CPMU_PRESENT)) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070011429 tw32(TG3_CPMU_CTRL, cpmuctrl);
11430
11431 /* Release the mutex */
11432 tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
11433 }
11434
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011435 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011436 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson4852a862011-04-13 11:05:07 +000011437 if (tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_PHY_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011438 err |= TG3_STD_LOOPBACK_FAILED <<
11439 TG3_PHY_LOOPBACK_SHIFT;
Joe Perches63c3a662011-04-26 08:12:10 +000011440 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonbb158d62011-04-25 12:42:47 +000011441 tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_TSO_LOOPBACK))
11442 err |= TG3_TSO_LOOPBACK_FAILED <<
11443 TG3_PHY_LOOPBACK_SHIFT;
Joe Perches63c3a662011-04-26 08:12:10 +000011444 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson4852a862011-04-13 11:05:07 +000011445 tg3_run_loopback(tp, 9000 + ETH_HLEN, TG3_PHY_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011446 err |= TG3_JMB_LOOPBACK_FAILED <<
11447 TG3_PHY_LOOPBACK_SHIFT;
Michael Chan9f40dea2005-09-05 17:53:06 -070011448 }
11449
Matt Carlson6833c042008-11-21 17:18:59 -080011450 /* Re-enable gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011451 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080011452 tg3_phy_toggle_apd(tp, true);
11453
Matt Carlsonab789042011-01-25 15:58:54 +000011454done:
11455 tp->phy_flags |= eee_cap;
11456
Michael Chan9f40dea2005-09-05 17:53:06 -070011457 return err;
11458}
11459
Michael Chan4cafd3f2005-05-29 14:56:34 -070011460static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11461 u64 *data)
11462{
Michael Chan566f86a2005-05-29 14:56:58 -070011463 struct tg3 *tp = netdev_priv(dev);
11464
Matt Carlson80096062010-08-02 11:26:06 +000011465 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011466 tg3_power_up(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011467
Michael Chan566f86a2005-05-29 14:56:58 -070011468 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
11469
11470 if (tg3_test_nvram(tp) != 0) {
11471 etest->flags |= ETH_TEST_FL_FAILED;
11472 data[0] = 1;
11473 }
Michael Chanca430072005-05-29 14:57:23 -070011474 if (tg3_test_link(tp) != 0) {
11475 etest->flags |= ETH_TEST_FL_FAILED;
11476 data[1] = 1;
11477 }
Michael Chana71116d2005-05-29 14:58:11 -070011478 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011479 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070011480
Michael Chanbbe832c2005-06-24 20:20:04 -070011481 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011482 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011483 tg3_netif_stop(tp);
11484 irq_sync = 1;
11485 }
11486
11487 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070011488
11489 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080011490 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011491 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000011492 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070011493 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080011494 if (!err)
11495 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011496
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011497 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad12006-03-20 22:27:35 -080011498 tg3_phy_reset(tp);
11499
Michael Chana71116d2005-05-29 14:58:11 -070011500 if (tg3_test_registers(tp) != 0) {
11501 etest->flags |= ETH_TEST_FL_FAILED;
11502 data[2] = 1;
11503 }
Michael Chan7942e1d2005-05-29 14:58:36 -070011504 if (tg3_test_memory(tp) != 0) {
11505 etest->flags |= ETH_TEST_FL_FAILED;
11506 data[3] = 1;
11507 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011508 if ((data[4] = tg3_test_loopback(tp)) != 0)
Michael Chanc76949a2005-05-29 14:58:59 -070011509 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070011510
David S. Millerf47c11e2005-06-24 20:18:35 -070011511 tg3_full_unlock(tp);
11512
Michael Chand4bc3922005-05-29 14:59:20 -070011513 if (tg3_test_interrupt(tp) != 0) {
11514 etest->flags |= ETH_TEST_FL_FAILED;
11515 data[5] = 1;
11516 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011517
11518 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070011519
Michael Chana71116d2005-05-29 14:58:11 -070011520 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11521 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011522 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011523 err2 = tg3_restart_hw(tp, 1);
11524 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070011525 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011526 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011527
11528 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011529
11530 if (irq_sync && !err2)
11531 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011532 }
Matt Carlson80096062010-08-02 11:26:06 +000011533 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011534 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011535
Michael Chan4cafd3f2005-05-29 14:56:34 -070011536}
11537
Linus Torvalds1da177e2005-04-16 15:20:36 -070011538static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
11539{
11540 struct mii_ioctl_data *data = if_mii(ifr);
11541 struct tg3 *tp = netdev_priv(dev);
11542 int err;
11543
Joe Perches63c3a662011-04-26 08:12:10 +000011544 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011545 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011546 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011547 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011548 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000011549 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011550 }
11551
Matt Carlson33f401a2010-04-05 10:19:27 +000011552 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011553 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000011554 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011555
11556 /* fallthru */
11557 case SIOCGMIIREG: {
11558 u32 mii_regval;
11559
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011560 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011561 break; /* We have no PHY */
11562
Matt Carlson34eea5a2011-04-20 07:57:38 +000011563 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011564 return -EAGAIN;
11565
David S. Millerf47c11e2005-06-24 20:18:35 -070011566 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011567 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070011568 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011569
11570 data->val_out = mii_regval;
11571
11572 return err;
11573 }
11574
11575 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011576 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011577 break; /* We have no PHY */
11578
Matt Carlson34eea5a2011-04-20 07:57:38 +000011579 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011580 return -EAGAIN;
11581
David S. Millerf47c11e2005-06-24 20:18:35 -070011582 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011583 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070011584 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011585
11586 return err;
11587
11588 default:
11589 /* do nothing */
11590 break;
11591 }
11592 return -EOPNOTSUPP;
11593}
11594
David S. Miller15f98502005-05-18 22:49:26 -070011595static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11596{
11597 struct tg3 *tp = netdev_priv(dev);
11598
11599 memcpy(ec, &tp->coal, sizeof(*ec));
11600 return 0;
11601}
11602
Michael Chand244c892005-07-05 14:42:33 -070011603static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11604{
11605 struct tg3 *tp = netdev_priv(dev);
11606 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11607 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11608
Joe Perches63c3a662011-04-26 08:12:10 +000011609 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070011610 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11611 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11612 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11613 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11614 }
11615
11616 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11617 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11618 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11619 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11620 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11621 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11622 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11623 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11624 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11625 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11626 return -EINVAL;
11627
11628 /* No rx interrupts will be generated if both are zero */
11629 if ((ec->rx_coalesce_usecs == 0) &&
11630 (ec->rx_max_coalesced_frames == 0))
11631 return -EINVAL;
11632
11633 /* No tx interrupts will be generated if both are zero */
11634 if ((ec->tx_coalesce_usecs == 0) &&
11635 (ec->tx_max_coalesced_frames == 0))
11636 return -EINVAL;
11637
11638 /* Only copy relevant parameters, ignore all others. */
11639 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11640 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11641 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11642 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11643 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11644 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11645 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11646 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11647 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11648
11649 if (netif_running(dev)) {
11650 tg3_full_lock(tp, 0);
11651 __tg3_set_coalesce(tp, &tp->coal);
11652 tg3_full_unlock(tp);
11653 }
11654 return 0;
11655}
11656
Jeff Garzik7282d492006-09-13 14:30:00 -040011657static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011658 .get_settings = tg3_get_settings,
11659 .set_settings = tg3_set_settings,
11660 .get_drvinfo = tg3_get_drvinfo,
11661 .get_regs_len = tg3_get_regs_len,
11662 .get_regs = tg3_get_regs,
11663 .get_wol = tg3_get_wol,
11664 .set_wol = tg3_set_wol,
11665 .get_msglevel = tg3_get_msglevel,
11666 .set_msglevel = tg3_set_msglevel,
11667 .nway_reset = tg3_nway_reset,
11668 .get_link = ethtool_op_get_link,
11669 .get_eeprom_len = tg3_get_eeprom_len,
11670 .get_eeprom = tg3_get_eeprom,
11671 .set_eeprom = tg3_set_eeprom,
11672 .get_ringparam = tg3_get_ringparam,
11673 .set_ringparam = tg3_set_ringparam,
11674 .get_pauseparam = tg3_get_pauseparam,
11675 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011676 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011677 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000011678 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011679 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011680 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011681 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011682 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011683};
11684
11685static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11686{
Michael Chan1b277772006-03-20 22:27:48 -080011687 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011688
11689 tp->nvram_size = EEPROM_CHIP_SIZE;
11690
Matt Carlsone4f34112009-02-25 14:25:00 +000011691 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011692 return;
11693
Michael Chanb16250e2006-09-27 16:10:14 -070011694 if ((magic != TG3_EEPROM_MAGIC) &&
11695 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11696 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011697 return;
11698
11699 /*
11700 * Size the chip by reading offsets at increasing powers of two.
11701 * When we encounter our validation signature, we know the addressing
11702 * has wrapped around, and thus have our chip size.
11703 */
Michael Chan1b277772006-03-20 22:27:48 -080011704 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011705
11706 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011707 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011708 return;
11709
Michael Chan18201802006-03-20 22:29:15 -080011710 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011711 break;
11712
11713 cursize <<= 1;
11714 }
11715
11716 tp->nvram_size = cursize;
11717}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011718
Linus Torvalds1da177e2005-04-16 15:20:36 -070011719static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11720{
11721 u32 val;
11722
Joe Perches63c3a662011-04-26 08:12:10 +000011723 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011724 return;
11725
11726 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011727 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011728 tg3_get_eeprom_size(tp);
11729 return;
11730 }
11731
Matt Carlson6d348f22009-02-25 14:25:52 +000011732 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011733 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011734 /* This is confusing. We want to operate on the
11735 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11736 * call will read from NVRAM and byteswap the data
11737 * according to the byteswapping settings for all
11738 * other register accesses. This ensures the data we
11739 * want will always reside in the lower 16-bits.
11740 * However, the data in NVRAM is in LE format, which
11741 * means the data from the NVRAM read will always be
11742 * opposite the endianness of the CPU. The 16-bit
11743 * byteswap then brings the data to CPU endianness.
11744 */
11745 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011746 return;
11747 }
11748 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070011749 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011750}
11751
11752static void __devinit tg3_get_nvram_info(struct tg3 *tp)
11753{
11754 u32 nvcfg1;
11755
11756 nvcfg1 = tr32(NVRAM_CFG1);
11757 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000011758 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011759 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011760 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11761 tw32(NVRAM_CFG1, nvcfg1);
11762 }
11763
Michael Chan4c987482005-09-05 17:52:38 -070011764 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) ||
Joe Perches63c3a662011-04-26 08:12:10 +000011765 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011766 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011767 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
11768 tp->nvram_jedecnum = JEDEC_ATMEL;
11769 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011770 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011771 break;
11772 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
11773 tp->nvram_jedecnum = JEDEC_ATMEL;
11774 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
11775 break;
11776 case FLASH_VENDOR_ATMEL_EEPROM:
11777 tp->nvram_jedecnum = JEDEC_ATMEL;
11778 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011779 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011780 break;
11781 case FLASH_VENDOR_ST:
11782 tp->nvram_jedecnum = JEDEC_ST;
11783 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011784 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011785 break;
11786 case FLASH_VENDOR_SAIFUN:
11787 tp->nvram_jedecnum = JEDEC_SAIFUN;
11788 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
11789 break;
11790 case FLASH_VENDOR_SST_SMALL:
11791 case FLASH_VENDOR_SST_LARGE:
11792 tp->nvram_jedecnum = JEDEC_SST;
11793 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
11794 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011795 }
Matt Carlson8590a602009-08-28 12:29:16 +000011796 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011797 tp->nvram_jedecnum = JEDEC_ATMEL;
11798 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011799 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011800 }
11801}
11802
Matt Carlsona1b950d2009-09-01 13:20:17 +000011803static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
11804{
11805 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
11806 case FLASH_5752PAGE_SIZE_256:
11807 tp->nvram_pagesize = 256;
11808 break;
11809 case FLASH_5752PAGE_SIZE_512:
11810 tp->nvram_pagesize = 512;
11811 break;
11812 case FLASH_5752PAGE_SIZE_1K:
11813 tp->nvram_pagesize = 1024;
11814 break;
11815 case FLASH_5752PAGE_SIZE_2K:
11816 tp->nvram_pagesize = 2048;
11817 break;
11818 case FLASH_5752PAGE_SIZE_4K:
11819 tp->nvram_pagesize = 4096;
11820 break;
11821 case FLASH_5752PAGE_SIZE_264:
11822 tp->nvram_pagesize = 264;
11823 break;
11824 case FLASH_5752PAGE_SIZE_528:
11825 tp->nvram_pagesize = 528;
11826 break;
11827 }
11828}
11829
Michael Chan361b4ac2005-04-21 17:11:21 -070011830static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
11831{
11832 u32 nvcfg1;
11833
11834 nvcfg1 = tr32(NVRAM_CFG1);
11835
Michael Chane6af3012005-04-21 17:12:05 -070011836 /* NVRAM protection for TPM */
11837 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000011838 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070011839
Michael Chan361b4ac2005-04-21 17:11:21 -070011840 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011841 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
11842 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
11843 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011844 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011845 break;
11846 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11847 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011848 tg3_flag_set(tp, NVRAM_BUFFERED);
11849 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011850 break;
11851 case FLASH_5752VENDOR_ST_M45PE10:
11852 case FLASH_5752VENDOR_ST_M45PE20:
11853 case FLASH_5752VENDOR_ST_M45PE40:
11854 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011855 tg3_flag_set(tp, NVRAM_BUFFERED);
11856 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011857 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070011858 }
11859
Joe Perches63c3a662011-04-26 08:12:10 +000011860 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000011861 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000011862 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070011863 /* For eeprom, set pagesize to maximum eeprom size */
11864 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11865
11866 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11867 tw32(NVRAM_CFG1, nvcfg1);
11868 }
11869}
11870
Michael Chand3c7b882006-03-23 01:28:25 -080011871static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
11872{
Matt Carlson989a9d22007-05-05 11:51:05 -070011873 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080011874
11875 nvcfg1 = tr32(NVRAM_CFG1);
11876
11877 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070011878 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011879 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070011880 protect = 1;
11881 }
Michael Chand3c7b882006-03-23 01:28:25 -080011882
Matt Carlson989a9d22007-05-05 11:51:05 -070011883 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11884 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011885 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11886 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11887 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11888 case FLASH_5755VENDOR_ATMEL_FLASH_5:
11889 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011890 tg3_flag_set(tp, NVRAM_BUFFERED);
11891 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011892 tp->nvram_pagesize = 264;
11893 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
11894 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
11895 tp->nvram_size = (protect ? 0x3e200 :
11896 TG3_NVRAM_SIZE_512KB);
11897 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
11898 tp->nvram_size = (protect ? 0x1f200 :
11899 TG3_NVRAM_SIZE_256KB);
11900 else
11901 tp->nvram_size = (protect ? 0x1f200 :
11902 TG3_NVRAM_SIZE_128KB);
11903 break;
11904 case FLASH_5752VENDOR_ST_M45PE10:
11905 case FLASH_5752VENDOR_ST_M45PE20:
11906 case FLASH_5752VENDOR_ST_M45PE40:
11907 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011908 tg3_flag_set(tp, NVRAM_BUFFERED);
11909 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011910 tp->nvram_pagesize = 256;
11911 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
11912 tp->nvram_size = (protect ?
11913 TG3_NVRAM_SIZE_64KB :
11914 TG3_NVRAM_SIZE_128KB);
11915 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
11916 tp->nvram_size = (protect ?
11917 TG3_NVRAM_SIZE_64KB :
11918 TG3_NVRAM_SIZE_256KB);
11919 else
11920 tp->nvram_size = (protect ?
11921 TG3_NVRAM_SIZE_128KB :
11922 TG3_NVRAM_SIZE_512KB);
11923 break;
Michael Chand3c7b882006-03-23 01:28:25 -080011924 }
11925}
11926
Michael Chan1b277772006-03-20 22:27:48 -080011927static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
11928{
11929 u32 nvcfg1;
11930
11931 nvcfg1 = tr32(NVRAM_CFG1);
11932
11933 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011934 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
11935 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11936 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
11937 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11938 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011939 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011940 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080011941
Matt Carlson8590a602009-08-28 12:29:16 +000011942 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11943 tw32(NVRAM_CFG1, nvcfg1);
11944 break;
11945 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11946 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11947 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11948 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11949 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011950 tg3_flag_set(tp, NVRAM_BUFFERED);
11951 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011952 tp->nvram_pagesize = 264;
11953 break;
11954 case FLASH_5752VENDOR_ST_M45PE10:
11955 case FLASH_5752VENDOR_ST_M45PE20:
11956 case FLASH_5752VENDOR_ST_M45PE40:
11957 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011958 tg3_flag_set(tp, NVRAM_BUFFERED);
11959 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011960 tp->nvram_pagesize = 256;
11961 break;
Michael Chan1b277772006-03-20 22:27:48 -080011962 }
11963}
11964
Matt Carlson6b91fa02007-10-10 18:01:09 -070011965static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
11966{
11967 u32 nvcfg1, protect = 0;
11968
11969 nvcfg1 = tr32(NVRAM_CFG1);
11970
11971 /* NVRAM protection for TPM */
11972 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011973 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070011974 protect = 1;
11975 }
11976
11977 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11978 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011979 case FLASH_5761VENDOR_ATMEL_ADB021D:
11980 case FLASH_5761VENDOR_ATMEL_ADB041D:
11981 case FLASH_5761VENDOR_ATMEL_ADB081D:
11982 case FLASH_5761VENDOR_ATMEL_ADB161D:
11983 case FLASH_5761VENDOR_ATMEL_MDB021D:
11984 case FLASH_5761VENDOR_ATMEL_MDB041D:
11985 case FLASH_5761VENDOR_ATMEL_MDB081D:
11986 case FLASH_5761VENDOR_ATMEL_MDB161D:
11987 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011988 tg3_flag_set(tp, NVRAM_BUFFERED);
11989 tg3_flag_set(tp, FLASH);
11990 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000011991 tp->nvram_pagesize = 256;
11992 break;
11993 case FLASH_5761VENDOR_ST_A_M45PE20:
11994 case FLASH_5761VENDOR_ST_A_M45PE40:
11995 case FLASH_5761VENDOR_ST_A_M45PE80:
11996 case FLASH_5761VENDOR_ST_A_M45PE16:
11997 case FLASH_5761VENDOR_ST_M_M45PE20:
11998 case FLASH_5761VENDOR_ST_M_M45PE40:
11999 case FLASH_5761VENDOR_ST_M_M45PE80:
12000 case FLASH_5761VENDOR_ST_M_M45PE16:
12001 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012002 tg3_flag_set(tp, NVRAM_BUFFERED);
12003 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012004 tp->nvram_pagesize = 256;
12005 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012006 }
12007
12008 if (protect) {
12009 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
12010 } else {
12011 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012012 case FLASH_5761VENDOR_ATMEL_ADB161D:
12013 case FLASH_5761VENDOR_ATMEL_MDB161D:
12014 case FLASH_5761VENDOR_ST_A_M45PE16:
12015 case FLASH_5761VENDOR_ST_M_M45PE16:
12016 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
12017 break;
12018 case FLASH_5761VENDOR_ATMEL_ADB081D:
12019 case FLASH_5761VENDOR_ATMEL_MDB081D:
12020 case FLASH_5761VENDOR_ST_A_M45PE80:
12021 case FLASH_5761VENDOR_ST_M_M45PE80:
12022 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12023 break;
12024 case FLASH_5761VENDOR_ATMEL_ADB041D:
12025 case FLASH_5761VENDOR_ATMEL_MDB041D:
12026 case FLASH_5761VENDOR_ST_A_M45PE40:
12027 case FLASH_5761VENDOR_ST_M_M45PE40:
12028 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12029 break;
12030 case FLASH_5761VENDOR_ATMEL_ADB021D:
12031 case FLASH_5761VENDOR_ATMEL_MDB021D:
12032 case FLASH_5761VENDOR_ST_A_M45PE20:
12033 case FLASH_5761VENDOR_ST_M_M45PE20:
12034 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12035 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012036 }
12037 }
12038}
12039
Michael Chanb5d37722006-09-27 16:06:21 -070012040static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
12041{
12042 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012043 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070012044 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12045}
12046
Matt Carlson321d32a2008-11-21 17:22:19 -080012047static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
12048{
12049 u32 nvcfg1;
12050
12051 nvcfg1 = tr32(NVRAM_CFG1);
12052
12053 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12054 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12055 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12056 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012057 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080012058 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12059
12060 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12061 tw32(NVRAM_CFG1, nvcfg1);
12062 return;
12063 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12064 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12065 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12066 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12067 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12068 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12069 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12070 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012071 tg3_flag_set(tp, NVRAM_BUFFERED);
12072 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012073
12074 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12075 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12076 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12077 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12078 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12079 break;
12080 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12081 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12082 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12083 break;
12084 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12085 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12086 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12087 break;
12088 }
12089 break;
12090 case FLASH_5752VENDOR_ST_M45PE10:
12091 case FLASH_5752VENDOR_ST_M45PE20:
12092 case FLASH_5752VENDOR_ST_M45PE40:
12093 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012094 tg3_flag_set(tp, NVRAM_BUFFERED);
12095 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012096
12097 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12098 case FLASH_5752VENDOR_ST_M45PE10:
12099 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12100 break;
12101 case FLASH_5752VENDOR_ST_M45PE20:
12102 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12103 break;
12104 case FLASH_5752VENDOR_ST_M45PE40:
12105 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12106 break;
12107 }
12108 break;
12109 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012110 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080012111 return;
12112 }
12113
Matt Carlsona1b950d2009-09-01 13:20:17 +000012114 tg3_nvram_get_pagesize(tp, nvcfg1);
12115 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012116 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012117}
12118
12119
12120static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
12121{
12122 u32 nvcfg1;
12123
12124 nvcfg1 = tr32(NVRAM_CFG1);
12125
12126 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12127 case FLASH_5717VENDOR_ATMEL_EEPROM:
12128 case FLASH_5717VENDOR_MICRO_EEPROM:
12129 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012130 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012131 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12132
12133 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12134 tw32(NVRAM_CFG1, nvcfg1);
12135 return;
12136 case FLASH_5717VENDOR_ATMEL_MDB011D:
12137 case FLASH_5717VENDOR_ATMEL_ADB011B:
12138 case FLASH_5717VENDOR_ATMEL_ADB011D:
12139 case FLASH_5717VENDOR_ATMEL_MDB021D:
12140 case FLASH_5717VENDOR_ATMEL_ADB021B:
12141 case FLASH_5717VENDOR_ATMEL_ADB021D:
12142 case FLASH_5717VENDOR_ATMEL_45USPT:
12143 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012144 tg3_flag_set(tp, NVRAM_BUFFERED);
12145 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012146
12147 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12148 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012149 /* Detect size with tg3_nvram_get_size() */
12150 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012151 case FLASH_5717VENDOR_ATMEL_ADB021B:
12152 case FLASH_5717VENDOR_ATMEL_ADB021D:
12153 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12154 break;
12155 default:
12156 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12157 break;
12158 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012159 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012160 case FLASH_5717VENDOR_ST_M_M25PE10:
12161 case FLASH_5717VENDOR_ST_A_M25PE10:
12162 case FLASH_5717VENDOR_ST_M_M45PE10:
12163 case FLASH_5717VENDOR_ST_A_M45PE10:
12164 case FLASH_5717VENDOR_ST_M_M25PE20:
12165 case FLASH_5717VENDOR_ST_A_M25PE20:
12166 case FLASH_5717VENDOR_ST_M_M45PE20:
12167 case FLASH_5717VENDOR_ST_A_M45PE20:
12168 case FLASH_5717VENDOR_ST_25USPT:
12169 case FLASH_5717VENDOR_ST_45USPT:
12170 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012171 tg3_flag_set(tp, NVRAM_BUFFERED);
12172 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012173
12174 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12175 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012176 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012177 /* Detect size with tg3_nvram_get_size() */
12178 break;
12179 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012180 case FLASH_5717VENDOR_ST_A_M45PE20:
12181 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12182 break;
12183 default:
12184 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12185 break;
12186 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012187 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012188 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012189 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012190 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080012191 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000012192
12193 tg3_nvram_get_pagesize(tp, nvcfg1);
12194 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012195 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080012196}
12197
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012198static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
12199{
12200 u32 nvcfg1, nvmpinstrp;
12201
12202 nvcfg1 = tr32(NVRAM_CFG1);
12203 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
12204
12205 switch (nvmpinstrp) {
12206 case FLASH_5720_EEPROM_HD:
12207 case FLASH_5720_EEPROM_LD:
12208 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012209 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012210
12211 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12212 tw32(NVRAM_CFG1, nvcfg1);
12213 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
12214 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12215 else
12216 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
12217 return;
12218 case FLASH_5720VENDOR_M_ATMEL_DB011D:
12219 case FLASH_5720VENDOR_A_ATMEL_DB011B:
12220 case FLASH_5720VENDOR_A_ATMEL_DB011D:
12221 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12222 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12223 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12224 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12225 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12226 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12227 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12228 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12229 case FLASH_5720VENDOR_ATMEL_45USPT:
12230 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012231 tg3_flag_set(tp, NVRAM_BUFFERED);
12232 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012233
12234 switch (nvmpinstrp) {
12235 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12236 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12237 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12238 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12239 break;
12240 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12241 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12242 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12243 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12244 break;
12245 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12246 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12247 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12248 break;
12249 default:
12250 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12251 break;
12252 }
12253 break;
12254 case FLASH_5720VENDOR_M_ST_M25PE10:
12255 case FLASH_5720VENDOR_M_ST_M45PE10:
12256 case FLASH_5720VENDOR_A_ST_M25PE10:
12257 case FLASH_5720VENDOR_A_ST_M45PE10:
12258 case FLASH_5720VENDOR_M_ST_M25PE20:
12259 case FLASH_5720VENDOR_M_ST_M45PE20:
12260 case FLASH_5720VENDOR_A_ST_M25PE20:
12261 case FLASH_5720VENDOR_A_ST_M45PE20:
12262 case FLASH_5720VENDOR_M_ST_M25PE40:
12263 case FLASH_5720VENDOR_M_ST_M45PE40:
12264 case FLASH_5720VENDOR_A_ST_M25PE40:
12265 case FLASH_5720VENDOR_A_ST_M45PE40:
12266 case FLASH_5720VENDOR_M_ST_M25PE80:
12267 case FLASH_5720VENDOR_M_ST_M45PE80:
12268 case FLASH_5720VENDOR_A_ST_M25PE80:
12269 case FLASH_5720VENDOR_A_ST_M45PE80:
12270 case FLASH_5720VENDOR_ST_25USPT:
12271 case FLASH_5720VENDOR_ST_45USPT:
12272 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012273 tg3_flag_set(tp, NVRAM_BUFFERED);
12274 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012275
12276 switch (nvmpinstrp) {
12277 case FLASH_5720VENDOR_M_ST_M25PE20:
12278 case FLASH_5720VENDOR_M_ST_M45PE20:
12279 case FLASH_5720VENDOR_A_ST_M25PE20:
12280 case FLASH_5720VENDOR_A_ST_M45PE20:
12281 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12282 break;
12283 case FLASH_5720VENDOR_M_ST_M25PE40:
12284 case FLASH_5720VENDOR_M_ST_M45PE40:
12285 case FLASH_5720VENDOR_A_ST_M25PE40:
12286 case FLASH_5720VENDOR_A_ST_M45PE40:
12287 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12288 break;
12289 case FLASH_5720VENDOR_M_ST_M25PE80:
12290 case FLASH_5720VENDOR_M_ST_M45PE80:
12291 case FLASH_5720VENDOR_A_ST_M25PE80:
12292 case FLASH_5720VENDOR_A_ST_M45PE80:
12293 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12294 break;
12295 default:
12296 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12297 break;
12298 }
12299 break;
12300 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012301 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012302 return;
12303 }
12304
12305 tg3_nvram_get_pagesize(tp, nvcfg1);
12306 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012307 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012308}
12309
Linus Torvalds1da177e2005-04-16 15:20:36 -070012310/* Chips other than 5700/5701 use the NVRAM for fetching info. */
12311static void __devinit tg3_nvram_init(struct tg3 *tp)
12312{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012313 tw32_f(GRC_EEPROM_ADDR,
12314 (EEPROM_ADDR_FSM_RESET |
12315 (EEPROM_DEFAULT_CLOCK_PERIOD <<
12316 EEPROM_ADDR_CLKPERD_SHIFT)));
12317
Michael Chan9d57f012006-12-07 00:23:25 -080012318 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012319
12320 /* Enable seeprom accesses. */
12321 tw32_f(GRC_LOCAL_CTRL,
12322 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
12323 udelay(100);
12324
12325 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12326 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000012327 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012328
Michael Chanec41c7d2006-01-17 02:40:55 -080012329 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000012330 netdev_warn(tp->dev,
12331 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000012332 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080012333 return;
12334 }
Michael Chane6af3012005-04-21 17:12:05 -070012335 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012336
Matt Carlson989a9d22007-05-05 11:51:05 -070012337 tp->nvram_size = 0;
12338
Michael Chan361b4ac2005-04-21 17:11:21 -070012339 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12340 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080012341 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
12342 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070012343 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012344 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12345 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080012346 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012347 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
12348 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070012349 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12350 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000012351 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
12352 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080012353 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012354 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12355 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000012356 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012357 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
12358 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070012359 else
12360 tg3_get_nvram_info(tp);
12361
Matt Carlson989a9d22007-05-05 11:51:05 -070012362 if (tp->nvram_size == 0)
12363 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012364
Michael Chane6af3012005-04-21 17:12:05 -070012365 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080012366 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012367
12368 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000012369 tg3_flag_clear(tp, NVRAM);
12370 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012371
12372 tg3_get_eeprom_size(tp);
12373 }
12374}
12375
Linus Torvalds1da177e2005-04-16 15:20:36 -070012376static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
12377 u32 offset, u32 len, u8 *buf)
12378{
12379 int i, j, rc = 0;
12380 u32 val;
12381
12382 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012383 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012384 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012385
12386 addr = offset + i;
12387
12388 memcpy(&data, buf + i, 4);
12389
Matt Carlson62cedd12009-04-20 14:52:29 -070012390 /*
12391 * The SEEPROM interface expects the data to always be opposite
12392 * the native endian format. We accomplish this by reversing
12393 * all the operations that would have been performed on the
12394 * data from a call to tg3_nvram_read_be32().
12395 */
12396 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012397
12398 val = tr32(GRC_EEPROM_ADDR);
12399 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
12400
12401 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
12402 EEPROM_ADDR_READ);
12403 tw32(GRC_EEPROM_ADDR, val |
12404 (0 << EEPROM_ADDR_DEVID_SHIFT) |
12405 (addr & EEPROM_ADDR_ADDR_MASK) |
12406 EEPROM_ADDR_START |
12407 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012408
Michael Chan9d57f012006-12-07 00:23:25 -080012409 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012410 val = tr32(GRC_EEPROM_ADDR);
12411
12412 if (val & EEPROM_ADDR_COMPLETE)
12413 break;
Michael Chan9d57f012006-12-07 00:23:25 -080012414 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012415 }
12416 if (!(val & EEPROM_ADDR_COMPLETE)) {
12417 rc = -EBUSY;
12418 break;
12419 }
12420 }
12421
12422 return rc;
12423}
12424
12425/* offset and length are dword aligned */
12426static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
12427 u8 *buf)
12428{
12429 int ret = 0;
12430 u32 pagesize = tp->nvram_pagesize;
12431 u32 pagemask = pagesize - 1;
12432 u32 nvram_cmd;
12433 u8 *tmp;
12434
12435 tmp = kmalloc(pagesize, GFP_KERNEL);
12436 if (tmp == NULL)
12437 return -ENOMEM;
12438
12439 while (len) {
12440 int j;
Michael Chane6af3012005-04-21 17:12:05 -070012441 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012442
12443 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012444
Linus Torvalds1da177e2005-04-16 15:20:36 -070012445 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012446 ret = tg3_nvram_read_be32(tp, phy_addr + j,
12447 (__be32 *) (tmp + j));
12448 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012449 break;
12450 }
12451 if (ret)
12452 break;
12453
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012454 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012455 size = pagesize;
12456 if (len < size)
12457 size = len;
12458
12459 len -= size;
12460
12461 memcpy(tmp + page_off, buf, size);
12462
12463 offset = offset + (pagesize - page_off);
12464
Michael Chane6af3012005-04-21 17:12:05 -070012465 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012466
12467 /*
12468 * Before we can erase the flash page, we need
12469 * to issue a special "write enable" command.
12470 */
12471 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12472
12473 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12474 break;
12475
12476 /* Erase the target page */
12477 tw32(NVRAM_ADDR, phy_addr);
12478
12479 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
12480 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
12481
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012482 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012483 break;
12484
12485 /* Issue another write enable to start the write. */
12486 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12487
12488 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12489 break;
12490
12491 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012492 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012493
Al Virob9fc7dc2007-12-17 22:59:57 -080012494 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000012495
Al Virob9fc7dc2007-12-17 22:59:57 -080012496 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012497
12498 tw32(NVRAM_ADDR, phy_addr + j);
12499
12500 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
12501 NVRAM_CMD_WR;
12502
12503 if (j == 0)
12504 nvram_cmd |= NVRAM_CMD_FIRST;
12505 else if (j == (pagesize - 4))
12506 nvram_cmd |= NVRAM_CMD_LAST;
12507
12508 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12509 break;
12510 }
12511 if (ret)
12512 break;
12513 }
12514
12515 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12516 tg3_nvram_exec_cmd(tp, nvram_cmd);
12517
12518 kfree(tmp);
12519
12520 return ret;
12521}
12522
12523/* offset and length are dword aligned */
12524static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
12525 u8 *buf)
12526{
12527 int i, ret = 0;
12528
12529 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012530 u32 page_off, phy_addr, nvram_cmd;
12531 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012532
12533 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080012534 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012535
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012536 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012537
Michael Chan18201802006-03-20 22:29:15 -080012538 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012539
12540 tw32(NVRAM_ADDR, phy_addr);
12541
12542 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
12543
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012544 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012545 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070012546 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012547 nvram_cmd |= NVRAM_CMD_LAST;
12548
12549 if (i == (len - 4))
12550 nvram_cmd |= NVRAM_CMD_LAST;
12551
Matt Carlson321d32a2008-11-21 17:22:19 -080012552 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
Joe Perches63c3a662011-04-26 08:12:10 +000012553 !tg3_flag(tp, 5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070012554 (tp->nvram_jedecnum == JEDEC_ST) &&
12555 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012556
12557 if ((ret = tg3_nvram_exec_cmd(tp,
12558 NVRAM_CMD_WREN | NVRAM_CMD_GO |
12559 NVRAM_CMD_DONE)))
12560
12561 break;
12562 }
Joe Perches63c3a662011-04-26 08:12:10 +000012563 if (!tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012564 /* We always do complete word writes to eeprom. */
12565 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
12566 }
12567
12568 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12569 break;
12570 }
12571 return ret;
12572}
12573
12574/* offset and length are dword aligned */
12575static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
12576{
12577 int ret;
12578
Joe Perches63c3a662011-04-26 08:12:10 +000012579 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012580 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
12581 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012582 udelay(40);
12583 }
12584
Joe Perches63c3a662011-04-26 08:12:10 +000012585 if (!tg3_flag(tp, NVRAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012586 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a588792010-04-05 10:19:28 +000012587 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012588 u32 grc_mode;
12589
Michael Chanec41c7d2006-01-17 02:40:55 -080012590 ret = tg3_nvram_lock(tp);
12591 if (ret)
12592 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012593
Michael Chane6af3012005-04-21 17:12:05 -070012594 tg3_enable_nvram_access(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000012595 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012596 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012597
12598 grc_mode = tr32(GRC_MODE);
12599 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
12600
Joe Perches63c3a662011-04-26 08:12:10 +000012601 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012602 ret = tg3_nvram_write_block_buffered(tp, offset, len,
12603 buf);
Matt Carlson859a588792010-04-05 10:19:28 +000012604 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012605 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
12606 buf);
12607 }
12608
12609 grc_mode = tr32(GRC_MODE);
12610 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
12611
Michael Chane6af3012005-04-21 17:12:05 -070012612 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012613 tg3_nvram_unlock(tp);
12614 }
12615
Joe Perches63c3a662011-04-26 08:12:10 +000012616 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012617 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012618 udelay(40);
12619 }
12620
12621 return ret;
12622}
12623
12624struct subsys_tbl_ent {
12625 u16 subsys_vendor, subsys_devid;
12626 u32 phy_id;
12627};
12628
Matt Carlson24daf2b2010-02-17 15:17:02 +000012629static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012630 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012631 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012632 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012633 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012634 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012635 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012636 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012637 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12638 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
12639 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012640 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012641 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012642 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012643 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12644 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
12645 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012646 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012647 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012648 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012649 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012650 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012651 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012652 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012653
12654 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012655 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012656 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012657 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012658 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012659 { TG3PCI_SUBVENDOR_ID_3COM,
12660 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
12661 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012662 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012663 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012664 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012665
12666 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012667 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012668 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012669 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012670 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012671 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012672 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012673 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012674 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012675
12676 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012677 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012678 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012679 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012680 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012681 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12682 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
12683 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012684 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012685 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012686 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012687
12688 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012689 { TG3PCI_SUBVENDOR_ID_IBM,
12690 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012691};
12692
Matt Carlson24daf2b2010-02-17 15:17:02 +000012693static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012694{
12695 int i;
12696
12697 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
12698 if ((subsys_id_to_phy_id[i].subsys_vendor ==
12699 tp->pdev->subsystem_vendor) &&
12700 (subsys_id_to_phy_id[i].subsys_devid ==
12701 tp->pdev->subsystem_device))
12702 return &subsys_id_to_phy_id[i];
12703 }
12704 return NULL;
12705}
12706
Michael Chan7d0c41e2005-04-21 17:06:20 -070012707static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012708{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012709 u32 val;
Michael Chancaf636c72006-03-22 01:05:31 -080012710 u16 pmcsr;
12711
12712 /* On some early chips the SRAM cannot be accessed in D3hot state,
12713 * so need make sure we're in D0.
12714 */
12715 pci_read_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, &pmcsr);
12716 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
12717 pci_write_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, pmcsr);
12718 msleep(1);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012719
12720 /* Make sure register accesses (indirect or otherwise)
12721 * will function correctly.
12722 */
12723 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12724 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012725
David S. Millerf49639e2006-06-09 11:58:36 -070012726 /* The memory arbiter has to be enabled in order for SRAM accesses
12727 * to succeed. Normally on powerup the tg3 chip firmware will make
12728 * sure it is enabled, but other entities such as system netboot
12729 * code might disable it.
12730 */
12731 val = tr32(MEMARB_MODE);
12732 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
12733
Matt Carlson79eb6902010-02-17 15:17:03 +000012734 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012735 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12736
Gary Zambranoa85feb82007-05-05 11:52:19 -070012737 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000012738 tg3_flag_set(tp, EEPROM_WRITE_PROT);
12739 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080012740
Michael Chanb5d37722006-09-27 16:06:21 -070012741 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012742 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012743 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
12744 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080012745 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012746 val = tr32(VCPU_CFGSHDW);
12747 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000012748 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070012749 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Matt Carlson20232762008-12-21 20:18:56 -080012750 (val & VCPU_CFGSHDW_WOL_MAGPKT))
Joe Perches63c3a662011-04-26 08:12:10 +000012751 tg3_flag_set(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012752 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070012753 }
12754
Linus Torvalds1da177e2005-04-16 15:20:36 -070012755 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12756 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12757 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012758 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012759 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012760
12761 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12762 tp->nic_sram_data_cfg = nic_cfg;
12763
12764 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
12765 ver >>= NIC_SRAM_DATA_VER_SHIFT;
12766 if ((GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700) &&
12767 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) &&
12768 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703) &&
12769 (ver > 0) && (ver < 0x100))
12770 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
12771
Matt Carlsona9daf362008-05-25 23:49:44 -070012772 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
12773 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
12774
Linus Torvalds1da177e2005-04-16 15:20:36 -070012775 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
12776 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
12777 eeprom_phy_serdes = 1;
12778
12779 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
12780 if (nic_phy_id != 0) {
12781 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
12782 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
12783
12784 eeprom_phy_id = (id1 >> 16) << 10;
12785 eeprom_phy_id |= (id2 & 0xfc00) << 16;
12786 eeprom_phy_id |= (id2 & 0x03ff) << 0;
12787 } else
12788 eeprom_phy_id = 0;
12789
Michael Chan7d0c41e2005-04-21 17:06:20 -070012790 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070012791 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000012792 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012793 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000012794 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012795 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070012796 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070012797
Joe Perches63c3a662011-04-26 08:12:10 +000012798 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012799 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
12800 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070012801 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070012802 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
12803
12804 switch (led_cfg) {
12805 default:
12806 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
12807 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12808 break;
12809
12810 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
12811 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12812 break;
12813
12814 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
12815 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070012816
12817 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
12818 * read on some older 5700/5701 bootcode.
12819 */
12820 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
12821 ASIC_REV_5700 ||
12822 GET_ASIC_REV(tp->pci_chip_rev_id) ==
12823 ASIC_REV_5701)
12824 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12825
Linus Torvalds1da177e2005-04-16 15:20:36 -070012826 break;
12827
12828 case SHASTA_EXT_LED_SHARED:
12829 tp->led_ctrl = LED_CTRL_MODE_SHARED;
12830 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
12831 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
12832 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12833 LED_CTRL_MODE_PHY_2);
12834 break;
12835
12836 case SHASTA_EXT_LED_MAC:
12837 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
12838 break;
12839
12840 case SHASTA_EXT_LED_COMBO:
12841 tp->led_ctrl = LED_CTRL_MODE_COMBO;
12842 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
12843 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12844 LED_CTRL_MODE_PHY_2);
12845 break;
12846
Stephen Hemminger855e1112008-04-16 16:37:28 -070012847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012848
12849 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12850 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
12851 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
12852 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12853
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012854 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
12855 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080012856
Michael Chan9d26e212006-12-07 00:21:14 -080012857 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000012858 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080012859 if ((tp->pdev->subsystem_vendor ==
12860 PCI_VENDOR_ID_ARIMA) &&
12861 (tp->pdev->subsystem_device == 0x205a ||
12862 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000012863 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080012864 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000012865 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
12866 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080012867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012868
12869 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000012870 tg3_flag_set(tp, ENABLE_ASF);
12871 if (tg3_flag(tp, 5750_PLUS))
12872 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012873 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012874
12875 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000012876 tg3_flag(tp, 5750_PLUS))
12877 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012878
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012879 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070012880 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000012881 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012882
Joe Perches63c3a662011-04-26 08:12:10 +000012883 if (tg3_flag(tp, WOL_CAP) &&
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012884 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE))
Joe Perches63c3a662011-04-26 08:12:10 +000012885 tg3_flag_set(tp, WOL_ENABLE);
Matt Carlson0527ba32007-10-10 18:03:30 -070012886
Linus Torvalds1da177e2005-04-16 15:20:36 -070012887 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012888 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012889
12890 /* serdes signal pre-emphasis in register 0x590 set by */
12891 /* bootcode if bit 18 is set */
12892 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012893 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070012894
Joe Perches63c3a662011-04-26 08:12:10 +000012895 if ((tg3_flag(tp, 57765_PLUS) ||
12896 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
12897 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080012898 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012899 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080012900
Joe Perches63c3a662011-04-26 08:12:10 +000012901 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson8c69b1e2010-08-02 11:26:00 +000012902 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000012903 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070012904 u32 cfg3;
12905
12906 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
12907 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000012908 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070012909 }
Matt Carlsona9daf362008-05-25 23:49:44 -070012910
Matt Carlson14417062010-02-17 15:16:59 +000012911 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000012912 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070012913 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000012914 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070012915 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000012916 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012917 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012918done:
Joe Perches63c3a662011-04-26 08:12:10 +000012919 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000012920 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000012921 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000012922 else
12923 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012924}
12925
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012926static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
12927{
12928 int i;
12929 u32 val;
12930
12931 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
12932 tw32(OTP_CTRL, cmd);
12933
12934 /* Wait for up to 1 ms for command to execute. */
12935 for (i = 0; i < 100; i++) {
12936 val = tr32(OTP_STATUS);
12937 if (val & OTP_STATUS_CMD_DONE)
12938 break;
12939 udelay(10);
12940 }
12941
12942 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
12943}
12944
12945/* Read the gphy configuration from the OTP region of the chip. The gphy
12946 * configuration is a 32-bit value that straddles the alignment boundary.
12947 * We do two 32-bit reads and then shift and merge the results.
12948 */
12949static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
12950{
12951 u32 bhalf_otp, thalf_otp;
12952
12953 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
12954
12955 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
12956 return 0;
12957
12958 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
12959
12960 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12961 return 0;
12962
12963 thalf_otp = tr32(OTP_READ_DATA);
12964
12965 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
12966
12967 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12968 return 0;
12969
12970 bhalf_otp = tr32(OTP_READ_DATA);
12971
12972 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
12973}
12974
Matt Carlsone256f8a2011-03-09 16:58:24 +000012975static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
12976{
12977 u32 adv = ADVERTISED_Autoneg |
12978 ADVERTISED_Pause;
12979
12980 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
12981 adv |= ADVERTISED_1000baseT_Half |
12982 ADVERTISED_1000baseT_Full;
12983
12984 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
12985 adv |= ADVERTISED_100baseT_Half |
12986 ADVERTISED_100baseT_Full |
12987 ADVERTISED_10baseT_Half |
12988 ADVERTISED_10baseT_Full |
12989 ADVERTISED_TP;
12990 else
12991 adv |= ADVERTISED_FIBRE;
12992
12993 tp->link_config.advertising = adv;
12994 tp->link_config.speed = SPEED_INVALID;
12995 tp->link_config.duplex = DUPLEX_INVALID;
12996 tp->link_config.autoneg = AUTONEG_ENABLE;
12997 tp->link_config.active_speed = SPEED_INVALID;
12998 tp->link_config.active_duplex = DUPLEX_INVALID;
12999 tp->link_config.orig_speed = SPEED_INVALID;
13000 tp->link_config.orig_duplex = DUPLEX_INVALID;
13001 tp->link_config.orig_autoneg = AUTONEG_INVALID;
13002}
13003
Michael Chan7d0c41e2005-04-21 17:06:20 -070013004static int __devinit tg3_phy_probe(struct tg3 *tp)
13005{
13006 u32 hw_phy_id_1, hw_phy_id_2;
13007 u32 hw_phy_id, hw_phy_id_masked;
13008 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013009
Matt Carlsone256f8a2011-03-09 16:58:24 +000013010 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000013011 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000013012 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
13013
Joe Perches63c3a662011-04-26 08:12:10 +000013014 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013015 return tg3_phy_init(tp);
13016
Linus Torvalds1da177e2005-04-16 15:20:36 -070013017 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010013018 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013019 */
13020 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000013021 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000013022 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013023 } else {
13024 /* Now read the physical PHY_ID from the chip and verify
13025 * that it is sane. If it doesn't look good, we fall back
13026 * to either the hard-coded table based PHY_ID and failing
13027 * that the value found in the eeprom area.
13028 */
13029 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
13030 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
13031
13032 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
13033 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
13034 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
13035
Matt Carlson79eb6902010-02-17 15:17:03 +000013036 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013037 }
13038
Matt Carlson79eb6902010-02-17 15:17:03 +000013039 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013040 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000013041 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013042 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070013043 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013044 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013045 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000013046 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070013047 /* Do nothing, phy ID already set up in
13048 * tg3_get_eeprom_hw_cfg().
13049 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013050 } else {
13051 struct subsys_tbl_ent *p;
13052
13053 /* No eeprom signature? Try the hardcoded
13054 * subsys device table.
13055 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013056 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013057 if (!p)
13058 return -ENODEV;
13059
13060 tp->phy_id = p->phy_id;
13061 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000013062 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013063 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013064 }
13065 }
13066
Matt Carlsona6b68da2010-12-06 08:28:52 +000013067 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
13068 ((tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
13069 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
13070 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
13071 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000013072 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
13073
Matt Carlsone256f8a2011-03-09 16:58:24 +000013074 tg3_phy_init_link_config(tp);
13075
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013076 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013077 !tg3_flag(tp, ENABLE_APE) &&
13078 !tg3_flag(tp, ENABLE_ASF)) {
Michael Chan3600d912006-12-07 00:21:48 -080013079 u32 bmsr, adv_reg, tg3_ctrl, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013080
13081 tg3_readphy(tp, MII_BMSR, &bmsr);
13082 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
13083 (bmsr & BMSR_LSTATUS))
13084 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013085
Linus Torvalds1da177e2005-04-16 15:20:36 -070013086 err = tg3_phy_reset(tp);
13087 if (err)
13088 return err;
13089
13090 adv_reg = (ADVERTISE_10HALF | ADVERTISE_10FULL |
13091 ADVERTISE_100HALF | ADVERTISE_100FULL |
13092 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
13093 tg3_ctrl = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013094 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013095 tg3_ctrl = (MII_TG3_CTRL_ADV_1000_HALF |
13096 MII_TG3_CTRL_ADV_1000_FULL);
13097 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
13098 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
13099 tg3_ctrl |= (MII_TG3_CTRL_AS_MASTER |
13100 MII_TG3_CTRL_ENABLE_AS_MASTER);
13101 }
13102
Michael Chan3600d912006-12-07 00:21:48 -080013103 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
13104 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
13105 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
13106 if (!tg3_copper_is_advertising_all(tp, mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013107 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
13108
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013109 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013110 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
13111
13112 tg3_writephy(tp, MII_BMCR,
13113 BMCR_ANENABLE | BMCR_ANRESTART);
13114 }
13115 tg3_phy_set_wirespeed(tp);
13116
13117 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013118 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013119 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
13120 }
13121
13122skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000013123 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013124 err = tg3_init_5401phy_dsp(tp);
13125 if (err)
13126 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013127
Linus Torvalds1da177e2005-04-16 15:20:36 -070013128 err = tg3_init_5401phy_dsp(tp);
13129 }
13130
Linus Torvalds1da177e2005-04-16 15:20:36 -070013131 return err;
13132}
13133
Matt Carlson184b8902010-04-05 10:19:25 +000013134static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013135{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013136 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013137 unsigned int block_end, rosize, len;
Matt Carlson184b8902010-04-05 10:19:25 +000013138 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013139
Matt Carlsonc3e94502011-04-13 11:05:08 +000013140 vpd_data = (u8 *)tg3_vpd_readblock(tp);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013141 if (!vpd_data)
13142 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013143
Matt Carlson4181b2c2010-02-26 14:04:45 +000013144 i = pci_vpd_find_tag(vpd_data, 0, TG3_NVM_VPD_LEN,
13145 PCI_VPD_LRDT_RO_DATA);
13146 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013147 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013148
13149 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
13150 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
13151 i += PCI_VPD_LRDT_TAG_SIZE;
13152
13153 if (block_end > TG3_NVM_VPD_LEN)
13154 goto out_not_found;
13155
Matt Carlson184b8902010-04-05 10:19:25 +000013156 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13157 PCI_VPD_RO_KEYWORD_MFR_ID);
13158 if (j > 0) {
13159 len = pci_vpd_info_field_size(&vpd_data[j]);
13160
13161 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13162 if (j + len > block_end || len != 4 ||
13163 memcmp(&vpd_data[j], "1028", 4))
13164 goto partno;
13165
13166 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13167 PCI_VPD_RO_KEYWORD_VENDOR0);
13168 if (j < 0)
13169 goto partno;
13170
13171 len = pci_vpd_info_field_size(&vpd_data[j]);
13172
13173 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13174 if (j + len > block_end)
13175 goto partno;
13176
13177 memcpy(tp->fw_ver, &vpd_data[j], len);
13178 strncat(tp->fw_ver, " bc ", TG3_NVM_VPD_LEN - len - 1);
13179 }
13180
13181partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000013182 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13183 PCI_VPD_RO_KEYWORD_PARTNO);
13184 if (i < 0)
13185 goto out_not_found;
13186
13187 len = pci_vpd_info_field_size(&vpd_data[i]);
13188
13189 i += PCI_VPD_INFO_FLD_HDR_SIZE;
13190 if (len > TG3_BPN_SIZE ||
13191 (len + i) > TG3_NVM_VPD_LEN)
13192 goto out_not_found;
13193
13194 memcpy(tp->board_part_number, &vpd_data[i], len);
13195
Linus Torvalds1da177e2005-04-16 15:20:36 -070013196out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013197 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000013198 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013199 return;
13200
13201out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000013202 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13203 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
13204 strcpy(tp->board_part_number, "BCM5717");
13205 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
13206 strcpy(tp->board_part_number, "BCM5718");
13207 else
13208 goto nomatch;
13209 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
13210 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
13211 strcpy(tp->board_part_number, "BCM57780");
13212 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
13213 strcpy(tp->board_part_number, "BCM57760");
13214 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
13215 strcpy(tp->board_part_number, "BCM57790");
13216 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
13217 strcpy(tp->board_part_number, "BCM57788");
13218 else
13219 goto nomatch;
13220 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
13221 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
13222 strcpy(tp->board_part_number, "BCM57761");
13223 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
13224 strcpy(tp->board_part_number, "BCM57765");
13225 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
13226 strcpy(tp->board_part_number, "BCM57781");
13227 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
13228 strcpy(tp->board_part_number, "BCM57785");
13229 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
13230 strcpy(tp->board_part_number, "BCM57791");
13231 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13232 strcpy(tp->board_part_number, "BCM57795");
13233 else
13234 goto nomatch;
13235 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070013236 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000013237 } else {
13238nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070013239 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000013240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013241}
13242
Matt Carlson9c8a6202007-10-21 16:16:08 -070013243static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
13244{
13245 u32 val;
13246
Matt Carlsone4f34112009-02-25 14:25:00 +000013247 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013248 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013249 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013250 val != 0)
13251 return 0;
13252
13253 return 1;
13254}
13255
Matt Carlsonacd9c112009-02-25 14:26:33 +000013256static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13257{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013258 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000013259 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013260 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013261
13262 if (tg3_nvram_read(tp, 0xc, &offset) ||
13263 tg3_nvram_read(tp, 0x4, &start))
13264 return;
13265
13266 offset = tg3_nvram_logical_addr(tp, offset);
13267
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013268 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013269 return;
13270
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013271 if ((val & 0xfc000000) == 0x0c000000) {
13272 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013273 return;
13274
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013275 if (val == 0)
13276 newver = true;
13277 }
13278
Matt Carlson75f99362010-04-05 10:19:24 +000013279 dst_off = strlen(tp->fw_ver);
13280
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013281 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013282 if (TG3_VER_SIZE - dst_off < 16 ||
13283 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013284 return;
13285
13286 offset = offset + ver_offset - start;
13287 for (i = 0; i < 16; i += 4) {
13288 __be32 v;
13289 if (tg3_nvram_read_be32(tp, offset + i, &v))
13290 return;
13291
Matt Carlson75f99362010-04-05 10:19:24 +000013292 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013293 }
13294 } else {
13295 u32 major, minor;
13296
13297 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13298 return;
13299
13300 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13301 TG3_NVM_BCVER_MAJSFT;
13302 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013303 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13304 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013305 }
13306}
13307
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013308static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13309{
13310 u32 val, major, minor;
13311
13312 /* Use native endian representation */
13313 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13314 return;
13315
13316 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13317 TG3_NVM_HWSB_CFG1_MAJSFT;
13318 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13319 TG3_NVM_HWSB_CFG1_MINSFT;
13320
13321 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13322}
13323
Matt Carlsondfe00d72008-11-21 17:19:41 -080013324static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13325{
13326 u32 offset, major, minor, build;
13327
Matt Carlson75f99362010-04-05 10:19:24 +000013328 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013329
13330 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13331 return;
13332
13333 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13334 case TG3_EEPROM_SB_REVISION_0:
13335 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13336 break;
13337 case TG3_EEPROM_SB_REVISION_2:
13338 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13339 break;
13340 case TG3_EEPROM_SB_REVISION_3:
13341 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13342 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013343 case TG3_EEPROM_SB_REVISION_4:
13344 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13345 break;
13346 case TG3_EEPROM_SB_REVISION_5:
13347 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13348 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013349 case TG3_EEPROM_SB_REVISION_6:
13350 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13351 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013352 default:
13353 return;
13354 }
13355
Matt Carlsone4f34112009-02-25 14:25:00 +000013356 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013357 return;
13358
13359 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13360 TG3_EEPROM_SB_EDH_BLD_SHFT;
13361 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13362 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13363 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13364
13365 if (minor > 99 || build > 26)
13366 return;
13367
Matt Carlson75f99362010-04-05 10:19:24 +000013368 offset = strlen(tp->fw_ver);
13369 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13370 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013371
13372 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013373 offset = strlen(tp->fw_ver);
13374 if (offset < TG3_VER_SIZE - 1)
13375 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013376 }
13377}
13378
Matt Carlsonacd9c112009-02-25 14:26:33 +000013379static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013380{
13381 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013382 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013383
13384 for (offset = TG3_NVM_DIR_START;
13385 offset < TG3_NVM_DIR_END;
13386 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013387 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013388 return;
13389
13390 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13391 break;
13392 }
13393
13394 if (offset == TG3_NVM_DIR_END)
13395 return;
13396
Joe Perches63c3a662011-04-26 08:12:10 +000013397 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013398 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013399 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013400 return;
13401
Matt Carlsone4f34112009-02-25 14:25:00 +000013402 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013403 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013404 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013405 return;
13406
13407 offset += val - start;
13408
Matt Carlsonacd9c112009-02-25 14:26:33 +000013409 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013410
Matt Carlsonacd9c112009-02-25 14:26:33 +000013411 tp->fw_ver[vlen++] = ',';
13412 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013413
13414 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013415 __be32 v;
13416 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013417 return;
13418
Al Virob9fc7dc2007-12-17 22:59:57 -080013419 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013420
Matt Carlsonacd9c112009-02-25 14:26:33 +000013421 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13422 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013423 break;
13424 }
13425
Matt Carlsonacd9c112009-02-25 14:26:33 +000013426 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13427 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013428 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013429}
13430
Matt Carlson7fd76442009-02-25 14:27:20 +000013431static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13432{
13433 int vlen;
13434 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013435 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013436
Joe Perches63c3a662011-04-26 08:12:10 +000013437 if (!tg3_flag(tp, ENABLE_APE) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000013438 return;
13439
13440 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13441 if (apedata != APE_SEG_SIG_MAGIC)
13442 return;
13443
13444 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13445 if (!(apedata & APE_FW_STATUS_READY))
13446 return;
13447
13448 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13449
Matt Carlsondc6d0742010-09-15 08:59:55 +000013450 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
Joe Perches63c3a662011-04-26 08:12:10 +000013451 tg3_flag_set(tp, APE_HAS_NCSI);
Matt Carlsonecc79642010-08-02 11:26:01 +000013452 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013453 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013454 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013455 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013456
Matt Carlson7fd76442009-02-25 14:27:20 +000013457 vlen = strlen(tp->fw_ver);
13458
Matt Carlsonecc79642010-08-02 11:26:01 +000013459 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13460 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013461 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13462 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13463 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13464 (apedata & APE_FW_VERSION_BLDMSK));
13465}
13466
Matt Carlsonacd9c112009-02-25 14:26:33 +000013467static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13468{
13469 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013470 bool vpd_vers = false;
13471
13472 if (tp->fw_ver[0] != 0)
13473 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013474
Joe Perches63c3a662011-04-26 08:12:10 +000013475 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000013476 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013477 return;
13478 }
13479
Matt Carlsonacd9c112009-02-25 14:26:33 +000013480 if (tg3_nvram_read(tp, 0, &val))
13481 return;
13482
13483 if (val == TG3_EEPROM_MAGIC)
13484 tg3_read_bc_ver(tp);
13485 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13486 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013487 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13488 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013489 else
13490 return;
13491
Joe Perches63c3a662011-04-26 08:12:10 +000013492 if (!tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || vpd_vers)
Matt Carlson75f99362010-04-05 10:19:24 +000013493 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013494
13495 tg3_read_mgmtfw_ver(tp);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013496
Matt Carlson75f99362010-04-05 10:19:24 +000013497done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013498 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013499}
13500
Michael Chan7544b092007-05-05 13:08:32 -070013501static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
13502
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013503static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13504{
Joe Perches63c3a662011-04-26 08:12:10 +000013505 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013506 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000013507 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013508 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013509 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013510 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013511}
13512
Matt Carlson41434702011-03-09 16:58:22 +000013513static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013514 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13515 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13516 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13517 { },
13518};
13519
Linus Torvalds1da177e2005-04-16 15:20:36 -070013520static int __devinit tg3_get_invariants(struct tg3 *tp)
13521{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013522 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013523 u32 pci_state_reg, grc_misc_cfg;
13524 u32 val;
13525 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013526 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013527
Linus Torvalds1da177e2005-04-16 15:20:36 -070013528 /* Force memory write invalidate off. If we leave it on,
13529 * then on 5700_BX chips we have to enable a workaround.
13530 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13531 * to match the cacheline size. The Broadcom driver have this
13532 * workaround but turns MWI off all the times so never uses
13533 * it. This seems to suggest that the workaround is insufficient.
13534 */
13535 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13536 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13537 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13538
13539 /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
13540 * has the register indirect write enable bit set before
13541 * we try to access any of the MMIO registers. It is also
13542 * critical that the PCI-X hw workaround situation is decided
13543 * before that as well.
13544 */
13545 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13546 &misc_ctrl_reg);
13547
13548 tp->pci_chip_rev_id = (misc_ctrl_reg >>
13549 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070013550 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13551 u32 prod_id_asic_rev;
13552
Matt Carlson5001e2f2009-11-13 13:03:51 +000013553 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13554 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013555 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
13556 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013557 pci_read_config_dword(tp->pdev,
13558 TG3PCI_GEN2_PRODID_ASICREV,
13559 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000013560 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13561 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13562 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13563 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13564 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13565 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13566 pci_read_config_dword(tp->pdev,
13567 TG3PCI_GEN15_PRODID_ASICREV,
13568 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013569 else
13570 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
13571 &prod_id_asic_rev);
13572
Matt Carlson321d32a2008-11-21 17:22:19 -080013573 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070013574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013575
Michael Chanff645be2005-04-21 17:09:53 -070013576 /* Wrong chip ID in 5752 A0. This code can be removed later
13577 * as A0 is not in production.
13578 */
13579 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13580 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13581
Michael Chan68929142005-08-09 20:17:14 -070013582 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
13583 * we need to disable memory and use config. cycles
13584 * only to access all registers. The 5702/03 chips
13585 * can mistakenly decode the special cycles from the
13586 * ICH chipsets as memory write cycles, causing corruption
13587 * of register and memory space. Only certain ICH bridges
13588 * will drive special cycles with non-zero data during the
13589 * address phase which can fall within the 5703's address
13590 * range. This is not an ICH bug as the PCI spec allows
13591 * non-zero address during special cycles. However, only
13592 * these ICH bridges are known to drive non-zero addresses
13593 * during special cycles.
13594 *
13595 * Since special cycles do not cross PCI bridges, we only
13596 * enable this workaround if the 5703 is on the secondary
13597 * bus of these ICH bridges.
13598 */
13599 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
13600 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
13601 static struct tg3_dev_id {
13602 u32 vendor;
13603 u32 device;
13604 u32 rev;
13605 } ich_chipsets[] = {
13606 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
13607 PCI_ANY_ID },
13608 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
13609 PCI_ANY_ID },
13610 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
13611 0xa },
13612 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
13613 PCI_ANY_ID },
13614 { },
13615 };
13616 struct tg3_dev_id *pci_id = &ich_chipsets[0];
13617 struct pci_dev *bridge = NULL;
13618
13619 while (pci_id->vendor != 0) {
13620 bridge = pci_get_device(pci_id->vendor, pci_id->device,
13621 bridge);
13622 if (!bridge) {
13623 pci_id++;
13624 continue;
13625 }
13626 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070013627 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070013628 continue;
13629 }
13630 if (bridge->subordinate &&
13631 (bridge->subordinate->number ==
13632 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013633 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070013634 pci_dev_put(bridge);
13635 break;
13636 }
13637 }
13638 }
13639
Matt Carlson41588ba2008-04-19 18:12:33 -070013640 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
13641 static struct tg3_dev_id {
13642 u32 vendor;
13643 u32 device;
13644 } bridge_chipsets[] = {
13645 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
13646 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
13647 { },
13648 };
13649 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
13650 struct pci_dev *bridge = NULL;
13651
13652 while (pci_id->vendor != 0) {
13653 bridge = pci_get_device(pci_id->vendor,
13654 pci_id->device,
13655 bridge);
13656 if (!bridge) {
13657 pci_id++;
13658 continue;
13659 }
13660 if (bridge->subordinate &&
13661 (bridge->subordinate->number <=
13662 tp->pdev->bus->number) &&
13663 (bridge->subordinate->subordinate >=
13664 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013665 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070013666 pci_dev_put(bridge);
13667 break;
13668 }
13669 }
13670 }
13671
Michael Chan4a29cc22006-03-19 13:21:12 -080013672 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
13673 * DMA addresses > 40-bit. This bridge may have other additional
13674 * 57xx devices behind it in some 4-port NIC designs for example.
13675 * Any tg3 device found behind the bridge will also need the 40-bit
13676 * DMA workaround.
13677 */
Michael Chana4e2b342005-10-26 15:46:52 -070013678 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
13679 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Joe Perches63c3a662011-04-26 08:12:10 +000013680 tg3_flag_set(tp, 5780_CLASS);
13681 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070013682 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000013683 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080013684 struct pci_dev *bridge = NULL;
13685
13686 do {
13687 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
13688 PCI_DEVICE_ID_SERVERWORKS_EPB,
13689 bridge);
13690 if (bridge && bridge->subordinate &&
13691 (bridge->subordinate->number <=
13692 tp->pdev->bus->number) &&
13693 (bridge->subordinate->subordinate >=
13694 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013695 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080013696 pci_dev_put(bridge);
13697 break;
13698 }
13699 } while (bridge);
13700 }
Michael Chan4cf78e42005-07-25 12:29:19 -070013701
Linus Torvalds1da177e2005-04-16 15:20:36 -070013702 /* Initialize misc host control in PCI block. */
13703 tp->misc_host_ctrl |= (misc_ctrl_reg &
13704 MISC_HOST_CTRL_CHIPREV);
13705 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13706 tp->misc_host_ctrl);
13707
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013708 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
13709 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013710 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13711 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Michael Chan7544b092007-05-05 13:08:32 -070013712 tp->pdev_peer = tg3_find_peer(tp);
13713
Matt Carlsonc885e822010-08-02 11:25:57 +000013714 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013715 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13716 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000013717 tg3_flag_set(tp, 5717_PLUS);
Matt Carlson0a58d662011-04-05 14:22:45 +000013718
13719 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013720 tg3_flag(tp, 5717_PLUS))
13721 tg3_flag_set(tp, 57765_PLUS);
Matt Carlsonc885e822010-08-02 11:25:57 +000013722
Matt Carlson321d32a2008-11-21 17:22:19 -080013723 /* Intentionally exclude ASIC_REV_5906 */
13724 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad12006-03-20 22:27:35 -080013725 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013726 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013727 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013728 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013729 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013730 tg3_flag(tp, 57765_PLUS))
13731 tg3_flag_set(tp, 5755_PLUS);
Matt Carlson321d32a2008-11-21 17:22:19 -080013732
13733 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13734 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070013735 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013736 tg3_flag(tp, 5755_PLUS) ||
13737 tg3_flag(tp, 5780_CLASS))
13738 tg3_flag_set(tp, 5750_PLUS);
John W. Linville6708e5c2005-04-21 17:00:52 -070013739
John W. Linville1b440c562005-04-21 17:03:18 -070013740 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) ||
Joe Perches63c3a662011-04-26 08:12:10 +000013741 tg3_flag(tp, 5750_PLUS))
13742 tg3_flag_set(tp, 5705_PLUS);
John W. Linville1b440c562005-04-21 17:03:18 -070013743
Matt Carlson027455a2008-12-21 20:19:30 -080013744 /* 5700 B0 chips do not support checksumming correctly due
13745 * to hardware bugs.
13746 */
Michał Mirosławdc668912011-04-07 03:35:07 +000013747 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
13748 u32 features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013749
Joe Perches63c3a662011-04-26 08:12:10 +000013750 if (tg3_flag(tp, 5755_PLUS))
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013751 features |= NETIF_F_IPV6_CSUM;
13752 tp->dev->features |= features;
Michał Mirosławdc668912011-04-07 03:35:07 +000013753 tp->dev->hw_features |= features;
13754 tp->dev->vlan_features |= features;
Matt Carlson027455a2008-12-21 20:19:30 -080013755 }
13756
Matt Carlson507399f2009-11-13 13:03:37 +000013757 /* Determine TSO capabilities */
Matt Carlson2866d952011-02-10 20:06:46 -080013758 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlson4d163b72011-01-25 15:58:48 +000013759 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000013760 else if (tg3_flag(tp, 57765_PLUS))
13761 tg3_flag_set(tp, HW_TSO_3);
13762 else if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000013763 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000013764 tg3_flag_set(tp, HW_TSO_2);
13765 else if (tg3_flag(tp, 5750_PLUS)) {
13766 tg3_flag_set(tp, HW_TSO_1);
13767 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013768 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13769 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000013770 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013771 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13772 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13773 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000013774 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013775 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13776 tp->fw_needed = FIRMWARE_TG3TSO5;
13777 else
13778 tp->fw_needed = FIRMWARE_TG3TSO;
13779 }
13780
13781 tp->irq_max = 1;
13782
Joe Perches63c3a662011-04-26 08:12:10 +000013783 if (tg3_flag(tp, 5750_PLUS)) {
13784 tg3_flag_set(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070013785 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
13786 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
13787 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
13788 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
13789 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000013790 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070013791
Joe Perches63c3a662011-04-26 08:12:10 +000013792 if (tg3_flag(tp, 5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070013793 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000013794 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070013795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013796
Joe Perches63c3a662011-04-26 08:12:10 +000013797 if (tg3_flag(tp, 57765_PLUS)) {
13798 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000013799 tp->irq_max = TG3_IRQ_MAX_VECS;
13800 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013801 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000013802
Matt Carlson615774f2009-11-13 13:03:39 +000013803 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsona50d0792010-06-05 17:24:37 +000013804 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson615774f2009-11-13 13:03:39 +000013805 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000013806 tg3_flag_set(tp, SHORT_DMA_BUG);
13807 else if (!tg3_flag(tp, 5755_PLUS)) {
13808 tg3_flag_set(tp, 4G_DMA_BNDRY_BUG);
13809 tg3_flag_set(tp, 40BIT_DMA_LIMIT_BUG);
Matt Carlson0e1406d2009-11-02 12:33:33 +000013810 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013811
Joe Perches63c3a662011-04-26 08:12:10 +000013812 if (tg3_flag(tp, 5717_PLUS))
13813 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000013814
Joe Perches63c3a662011-04-26 08:12:10 +000013815 if (tg3_flag(tp, 57765_PLUS) &&
Matt Carlson2866d952011-02-10 20:06:46 -080013816 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
Joe Perches63c3a662011-04-26 08:12:10 +000013817 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000013818
Joe Perches63c3a662011-04-26 08:12:10 +000013819 if (!tg3_flag(tp, 5705_PLUS) ||
13820 tg3_flag(tp, 5780_CLASS) ||
13821 tg3_flag(tp, USE_JUMBO_BDFLAG))
13822 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070013823
Matt Carlson52f44902008-11-21 17:17:04 -080013824 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13825 &pci_state_reg);
13826
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013827 tp->pcie_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_EXP);
13828 if (tp->pcie_cap != 0) {
13829 u16 lnkctl;
13830
Joe Perches63c3a662011-04-26 08:12:10 +000013831 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013832
Matt Carlsoncf790032010-11-24 08:31:48 +000013833 tp->pcie_readrq = 4096;
Matt Carlsond78b59f2011-04-05 14:22:46 +000013834 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13835 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Matt Carlsonb4495ed2011-01-25 15:58:47 +000013836 tp->pcie_readrq = 2048;
Matt Carlsoncf790032010-11-24 08:31:48 +000013837
13838 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013839
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013840 pci_read_config_word(tp->pdev,
13841 tp->pcie_cap + PCI_EXP_LNKCTL,
13842 &lnkctl);
13843 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
13844 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000013845 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013846 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013847 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000013848 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
13849 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000013850 tg3_flag_set(tp, CLKREQ_BUG);
Matt Carlson614b0592010-01-20 16:58:02 +000013851 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000013852 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080013853 }
Matt Carlson52f44902008-11-21 17:17:04 -080013854 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Joe Perches63c3a662011-04-26 08:12:10 +000013855 tg3_flag_set(tp, PCI_EXPRESS);
13856 } else if (!tg3_flag(tp, 5705_PLUS) ||
13857 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080013858 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
13859 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000013860 dev_err(&tp->pdev->dev,
13861 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080013862 return -EIO;
13863 }
13864
13865 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000013866 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080013867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013868
Michael Chan399de502005-10-03 14:02:39 -070013869 /* If we have an AMD 762 or VIA K8T800 chipset, write
13870 * reordering to the mailbox registers done by the host
13871 * controller can cause major troubles. We read back from
13872 * every mailbox register write to force the writes to be
13873 * posted to the chip in order.
13874 */
Matt Carlson41434702011-03-09 16:58:22 +000013875 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013876 !tg3_flag(tp, PCI_EXPRESS))
13877 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070013878
Matt Carlson69fc4052008-12-21 20:19:57 -080013879 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
13880 &tp->pci_cacheline_sz);
13881 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13882 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013883 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13884 tp->pci_lat_timer < 64) {
13885 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080013886 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13887 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013888 }
13889
Matt Carlson52f44902008-11-21 17:17:04 -080013890 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
13891 /* 5700 BX chips need to have their TX producer index
13892 * mailboxes written twice to workaround a bug.
13893 */
Joe Perches63c3a662011-04-26 08:12:10 +000013894 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070013895
Matt Carlson52f44902008-11-21 17:17:04 -080013896 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013897 *
13898 * The workaround is to use indirect register accesses
13899 * for all chip writes not to mailbox registers.
13900 */
Joe Perches63c3a662011-04-26 08:12:10 +000013901 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013902 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013903
Joe Perches63c3a662011-04-26 08:12:10 +000013904 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013905
13906 /* The chip can have it's power management PCI config
13907 * space registers clobbered due to this bug.
13908 * So explicitly force the chip into D0 here.
13909 */
Matt Carlson9974a352007-10-07 23:27:28 -070013910 pci_read_config_dword(tp->pdev,
13911 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013912 &pm_reg);
13913 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
13914 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070013915 pci_write_config_dword(tp->pdev,
13916 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013917 pm_reg);
13918
13919 /* Also, force SERR#/PERR# in PCI command. */
13920 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13921 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
13922 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13923 }
13924 }
13925
Linus Torvalds1da177e2005-04-16 15:20:36 -070013926 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000013927 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013928 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000013929 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013930
13931 /* Chip-specific fixup from Broadcom driver */
13932 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
13933 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
13934 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
13935 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
13936 }
13937
Michael Chan1ee582d2005-08-09 20:16:46 -070013938 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070013939 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013940 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070013941 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070013942 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013943 tp->write32_tx_mbox = tg3_write32;
13944 tp->write32_rx_mbox = tg3_write32;
13945
13946 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000013947 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070013948 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013949 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013950 (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson98efd8a2007-05-05 12:47:25 -070013951 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
13952 /*
13953 * Back to back register writes can cause problems on these
13954 * chips, the workaround is to read back all reg writes
13955 * except those to mailbox regs.
13956 *
13957 * See tg3_write_indirect_reg32().
13958 */
Michael Chan1ee582d2005-08-09 20:16:46 -070013959 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013960 }
13961
Joe Perches63c3a662011-04-26 08:12:10 +000013962 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070013963 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000013964 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070013965 tp->write32_rx_mbox = tg3_write_flush_reg32;
13966 }
Michael Chan20094932005-08-09 20:16:32 -070013967
Joe Perches63c3a662011-04-26 08:12:10 +000013968 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070013969 tp->read32 = tg3_read_indirect_reg32;
13970 tp->write32 = tg3_write_indirect_reg32;
13971 tp->read32_mbox = tg3_read_indirect_mbox;
13972 tp->write32_mbox = tg3_write_indirect_mbox;
13973 tp->write32_tx_mbox = tg3_write_indirect_mbox;
13974 tp->write32_rx_mbox = tg3_write_indirect_mbox;
13975
13976 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070013977 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070013978
13979 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13980 pci_cmd &= ~PCI_COMMAND_MEMORY;
13981 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13982 }
Michael Chanb5d37722006-09-27 16:06:21 -070013983 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13984 tp->read32_mbox = tg3_read32_mbox_5906;
13985 tp->write32_mbox = tg3_write32_mbox_5906;
13986 tp->write32_tx_mbox = tg3_write32_mbox_5906;
13987 tp->write32_rx_mbox = tg3_write32_mbox_5906;
13988 }
Michael Chan68929142005-08-09 20:17:14 -070013989
Michael Chanbbadf502006-04-06 21:46:34 -070013990 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013991 (tg3_flag(tp, PCIX_MODE) &&
Michael Chanbbadf502006-04-06 21:46:34 -070013992 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070013993 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000013994 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070013995
Michael Chan7d0c41e2005-04-21 17:06:20 -070013996 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000013997 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070013998 * determined before calling tg3_set_power_state() so that
13999 * we know whether or not to switch out of Vaux power.
14000 * When the flag is set, it means that GPIO1 is used for eeprom
14001 * write protect and also implies that it is a LOM where GPIOs
14002 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014003 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070014004 tg3_get_eeprom_hw_cfg(tp);
14005
Joe Perches63c3a662011-04-26 08:12:10 +000014006 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070014007 /* Allow reads and writes to the
14008 * APE register and memory space.
14009 */
14010 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000014011 PCISTATE_ALLOW_APE_SHMEM_WR |
14012 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014013 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
14014 pci_state_reg);
14015 }
14016
Matt Carlson9936bcf2007-10-10 18:03:07 -070014017 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070014018 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014019 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014020 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014021 tg3_flag(tp, 57765_PLUS))
14022 tg3_flag_set(tp, CPMU_PRESENT);
Matt Carlsond30cdd22007-10-07 23:28:35 -070014023
Matt Carlsonbea8a632011-04-25 12:42:49 +000014024 /* Set up tp->grc_local_ctrl before calling tg3_power_up().
Michael Chan314fba32005-04-21 17:07:04 -070014025 * GPIO1 driven high will bring 5700's external PHY out of reset.
14026 * It is also used as eeprom write protect on LOMs.
14027 */
14028 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
14029 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
Joe Perches63c3a662011-04-26 08:12:10 +000014030 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070014031 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
14032 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070014033 /* Unused GPIO3 must be driven as output on 5752 because there
14034 * are no pull-up resistors on unused GPIO pins.
14035 */
14036 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
14037 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070014038
Matt Carlson321d32a2008-11-21 17:22:19 -080014039 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000014040 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
14041 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Michael Chanaf36e6b2006-03-23 01:28:06 -080014042 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
14043
Matt Carlson8d519ab2009-04-20 06:58:01 +000014044 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
14045 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014046 /* Turn off the debug UART. */
14047 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014048 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014049 /* Keep VMain power. */
14050 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
14051 GRC_LCLCTRL_GPIO_OUTPUT0;
14052 }
14053
Linus Torvalds1da177e2005-04-16 15:20:36 -070014054 /* Force the chip into D0. */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000014055 err = tg3_power_up(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014056 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014057 dev_err(&tp->pdev->dev, "Transition to D0 failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014058 return err;
14059 }
14060
Linus Torvalds1da177e2005-04-16 15:20:36 -070014061 /* Derive initial jumbo mode from MTU assigned in
14062 * ether_setup() via the alloc_etherdev() call
14063 */
Joe Perches63c3a662011-04-26 08:12:10 +000014064 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
14065 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014066
14067 /* Determine WakeOnLan speed to use. */
14068 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14069 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
14070 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
14071 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000014072 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014073 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014074 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014075 }
14076
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014077 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014078 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014079
Linus Torvalds1da177e2005-04-16 15:20:36 -070014080 /* A few boards don't want Ethernet@WireSpeed phy feature */
14081 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
14082 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
14083 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070014084 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014085 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
14086 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14087 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014088
14089 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
14090 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014091 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014092 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014093 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014094
Joe Perches63c3a662011-04-26 08:12:10 +000014095 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014096 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080014097 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014098 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014099 !tg3_flag(tp, 57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070014100 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070014101 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070014102 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
14103 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080014104 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
14105 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014106 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080014107 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014108 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080014109 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014110 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070014111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014112
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014113 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14114 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
14115 tp->phy_otp = tg3_read_otp_phycfg(tp);
14116 if (tp->phy_otp == 0)
14117 tp->phy_otp = TG3_OTP_DEFAULT;
14118 }
14119
Joe Perches63c3a662011-04-26 08:12:10 +000014120 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070014121 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
14122 else
14123 tp->mi_mode = MAC_MI_MODE_BASE;
14124
Linus Torvalds1da177e2005-04-16 15:20:36 -070014125 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014126 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
14127 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
14128 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
14129
Matt Carlson4d958472011-04-20 07:57:35 +000014130 /* Set these bits to enable statistics workaround. */
14131 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14132 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
14133 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
14134 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
14135 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
14136 }
14137
Matt Carlson321d32a2008-11-21 17:22:19 -080014138 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14139 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000014140 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070014141
Matt Carlson158d7ab2008-05-29 01:37:54 -070014142 err = tg3_mdio_init(tp);
14143 if (err)
14144 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014145
14146 /* Initialize data/descriptor byte/word swapping. */
14147 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000014148 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14149 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
14150 GRC_MODE_WORD_SWAP_B2HRX_DATA |
14151 GRC_MODE_B2HRX_ENABLE |
14152 GRC_MODE_HTX2B_ENABLE |
14153 GRC_MODE_HOST_STACKUP);
14154 else
14155 val &= GRC_MODE_HOST_STACKUP;
14156
Linus Torvalds1da177e2005-04-16 15:20:36 -070014157 tw32(GRC_MODE, val | tp->grc_mode);
14158
14159 tg3_switch_clocks(tp);
14160
14161 /* Clear this out for sanity. */
14162 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
14163
14164 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14165 &pci_state_reg);
14166 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014167 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014168 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
14169
14170 if (chiprevid == CHIPREV_ID_5701_A0 ||
14171 chiprevid == CHIPREV_ID_5701_B0 ||
14172 chiprevid == CHIPREV_ID_5701_B2 ||
14173 chiprevid == CHIPREV_ID_5701_B5) {
14174 void __iomem *sram_base;
14175
14176 /* Write some dummy words into the SRAM status block
14177 * area, see if it reads back correctly. If the return
14178 * value is bad, force enable the PCIX workaround.
14179 */
14180 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
14181
14182 writel(0x00000000, sram_base);
14183 writel(0x00000000, sram_base + 4);
14184 writel(0xffffffff, sram_base + 4);
14185 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000014186 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014187 }
14188 }
14189
14190 udelay(50);
14191 tg3_nvram_init(tp);
14192
14193 grc_misc_cfg = tr32(GRC_MISC_CFG);
14194 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
14195
Linus Torvalds1da177e2005-04-16 15:20:36 -070014196 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14197 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
14198 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000014199 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014200
Joe Perches63c3a662011-04-26 08:12:10 +000014201 if (!tg3_flag(tp, IS_5788) &&
David S. Millerfac9b832005-05-18 22:46:34 -070014202 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700))
Joe Perches63c3a662011-04-26 08:12:10 +000014203 tg3_flag_set(tp, TAGGED_STATUS);
14204 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070014205 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
14206 HOSTCC_MODE_CLRTICK_TXBD);
14207
14208 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
14209 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14210 tp->misc_host_ctrl);
14211 }
14212
Matt Carlson3bda1252008-08-15 14:08:22 -070014213 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000014214 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000014215 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070014216 else
14217 tp->mac_mode = TG3_DEF_MAC_MODE;
14218
Linus Torvalds1da177e2005-04-16 15:20:36 -070014219 /* these are limited to 10/100 only */
14220 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14221 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14222 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14223 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14224 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
14225 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
14226 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
14227 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14228 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080014229 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
14230 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014231 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000014232 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14233 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014234 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14235 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014236
14237 err = tg3_phy_probe(tp);
14238 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014239 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014240 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014241 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014242 }
14243
Matt Carlson184b8902010-04-05 10:19:25 +000014244 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080014245 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014246
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014247 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
14248 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014249 } else {
14250 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014251 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014252 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014253 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014254 }
14255
14256 /* 5700 {AX,BX} chips have a broken status block link
14257 * change bit implementation, so we must use the
14258 * status register in those cases.
14259 */
14260 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014261 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014262 else
Joe Perches63c3a662011-04-26 08:12:10 +000014263 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014264
14265 /* The led_ctrl is set during tg3_phy_probe, here we might
14266 * have to force the link status polling mechanism based
14267 * upon subsystem IDs.
14268 */
14269 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070014270 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014271 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
14272 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000014273 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014274 }
14275
14276 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014277 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000014278 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014279 else
Joe Perches63c3a662011-04-26 08:12:10 +000014280 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014281
Matt Carlsonbf933c82011-01-25 15:58:49 +000014282 tp->rx_offset = NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014283 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014284 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014285 tg3_flag(tp, PCIX_MODE)) {
Matt Carlsonbf933c82011-01-25 15:58:49 +000014286 tp->rx_offset = 0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014287#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000014288 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014289#endif
14290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014291
Matt Carlson2c49a442010-09-30 10:34:35 +000014292 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14293 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014294 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14295
Matt Carlson2c49a442010-09-30 10:34:35 +000014296 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070014297
14298 /* Increment the rx prod index on the rx std ring by at most
14299 * 8 for these chips to workaround hw errata.
14300 */
14301 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14302 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14303 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14304 tp->rx_std_max_post = 8;
14305
Joe Perches63c3a662011-04-26 08:12:10 +000014306 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070014307 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14308 PCIE_PWR_MGMT_L1_THRESH_MSK;
14309
Linus Torvalds1da177e2005-04-16 15:20:36 -070014310 return err;
14311}
14312
David S. Miller49b6e95f2007-03-29 01:38:42 -070014313#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014314static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14315{
14316 struct net_device *dev = tp->dev;
14317 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014318 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070014319 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014320 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014321
David S. Miller49b6e95f2007-03-29 01:38:42 -070014322 addr = of_get_property(dp, "local-mac-address", &len);
14323 if (addr && len == 6) {
14324 memcpy(dev->dev_addr, addr, 6);
14325 memcpy(dev->perm_addr, dev->dev_addr, 6);
14326 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014327 }
14328 return -ENODEV;
14329}
14330
14331static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14332{
14333 struct net_device *dev = tp->dev;
14334
14335 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070014336 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014337 return 0;
14338}
14339#endif
14340
14341static int __devinit tg3_get_device_address(struct tg3 *tp)
14342{
14343 struct net_device *dev = tp->dev;
14344 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080014345 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014346
David S. Miller49b6e95f2007-03-29 01:38:42 -070014347#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014348 if (!tg3_get_macaddr_sparc(tp))
14349 return 0;
14350#endif
14351
14352 mac_offset = 0x7c;
David S. Millerf49639e2006-06-09 11:58:36 -070014353 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
Joe Perches63c3a662011-04-26 08:12:10 +000014354 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014355 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14356 mac_offset = 0xcc;
14357 if (tg3_nvram_lock(tp))
14358 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14359 else
14360 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000014361 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlsona50d0792010-06-05 17:24:37 +000014362 if (PCI_FUNC(tp->pdev->devfn) & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014363 mac_offset = 0xcc;
Matt Carlsona50d0792010-06-05 17:24:37 +000014364 if (PCI_FUNC(tp->pdev->devfn) > 1)
14365 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014366 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014367 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014368
14369 /* First try to get it from MAC address mailbox. */
14370 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14371 if ((hi >> 16) == 0x484b) {
14372 dev->dev_addr[0] = (hi >> 8) & 0xff;
14373 dev->dev_addr[1] = (hi >> 0) & 0xff;
14374
14375 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14376 dev->dev_addr[2] = (lo >> 24) & 0xff;
14377 dev->dev_addr[3] = (lo >> 16) & 0xff;
14378 dev->dev_addr[4] = (lo >> 8) & 0xff;
14379 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014380
Michael Chan008652b2006-03-27 23:14:53 -080014381 /* Some old bootcode may report a 0 MAC address in SRAM */
14382 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14383 }
14384 if (!addr_ok) {
14385 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000014386 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000014387 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000014388 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070014389 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14390 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080014391 }
14392 /* Finally just fetch it out of the MAC control regs. */
14393 else {
14394 hi = tr32(MAC_ADDR_0_HIGH);
14395 lo = tr32(MAC_ADDR_0_LOW);
14396
14397 dev->dev_addr[5] = lo & 0xff;
14398 dev->dev_addr[4] = (lo >> 8) & 0xff;
14399 dev->dev_addr[3] = (lo >> 16) & 0xff;
14400 dev->dev_addr[2] = (lo >> 24) & 0xff;
14401 dev->dev_addr[1] = hi & 0xff;
14402 dev->dev_addr[0] = (hi >> 8) & 0xff;
14403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014404 }
14405
14406 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014407#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014408 if (!tg3_get_default_macaddr_sparc(tp))
14409 return 0;
14410#endif
14411 return -EINVAL;
14412 }
John W. Linville2ff43692005-09-12 14:44:20 -070014413 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014414 return 0;
14415}
14416
David S. Miller59e6b432005-05-18 22:50:10 -070014417#define BOUNDARY_SINGLE_CACHELINE 1
14418#define BOUNDARY_MULTI_CACHELINE 2
14419
14420static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14421{
14422 int cacheline_size;
14423 u8 byte;
14424 int goal;
14425
14426 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14427 if (byte == 0)
14428 cacheline_size = 1024;
14429 else
14430 cacheline_size = (int) byte * 4;
14431
14432 /* On 5703 and later chips, the boundary bits have no
14433 * effect.
14434 */
14435 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14436 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014437 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070014438 goto out;
14439
14440#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14441 goal = BOUNDARY_MULTI_CACHELINE;
14442#else
14443#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14444 goal = BOUNDARY_SINGLE_CACHELINE;
14445#else
14446 goal = 0;
14447#endif
14448#endif
14449
Joe Perches63c3a662011-04-26 08:12:10 +000014450 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014451 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14452 goto out;
14453 }
14454
David S. Miller59e6b432005-05-18 22:50:10 -070014455 if (!goal)
14456 goto out;
14457
14458 /* PCI controllers on most RISC systems tend to disconnect
14459 * when a device tries to burst across a cache-line boundary.
14460 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14461 *
14462 * Unfortunately, for PCI-E there are only limited
14463 * write-side controls for this, and thus for reads
14464 * we will still get the disconnects. We'll also waste
14465 * these PCI cycles for both read and write for chips
14466 * other than 5700 and 5701 which do not implement the
14467 * boundary bits.
14468 */
Joe Perches63c3a662011-04-26 08:12:10 +000014469 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014470 switch (cacheline_size) {
14471 case 16:
14472 case 32:
14473 case 64:
14474 case 128:
14475 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14476 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14477 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14478 } else {
14479 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14480 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14481 }
14482 break;
14483
14484 case 256:
14485 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14486 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14487 break;
14488
14489 default:
14490 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14491 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14492 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014493 }
Joe Perches63c3a662011-04-26 08:12:10 +000014494 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014495 switch (cacheline_size) {
14496 case 16:
14497 case 32:
14498 case 64:
14499 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14500 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14501 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14502 break;
14503 }
14504 /* fallthrough */
14505 case 128:
14506 default:
14507 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14508 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14509 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014510 }
David S. Miller59e6b432005-05-18 22:50:10 -070014511 } else {
14512 switch (cacheline_size) {
14513 case 16:
14514 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14515 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14516 DMA_RWCTRL_WRITE_BNDRY_16);
14517 break;
14518 }
14519 /* fallthrough */
14520 case 32:
14521 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14522 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14523 DMA_RWCTRL_WRITE_BNDRY_32);
14524 break;
14525 }
14526 /* fallthrough */
14527 case 64:
14528 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14529 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14530 DMA_RWCTRL_WRITE_BNDRY_64);
14531 break;
14532 }
14533 /* fallthrough */
14534 case 128:
14535 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14536 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14537 DMA_RWCTRL_WRITE_BNDRY_128);
14538 break;
14539 }
14540 /* fallthrough */
14541 case 256:
14542 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14543 DMA_RWCTRL_WRITE_BNDRY_256);
14544 break;
14545 case 512:
14546 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14547 DMA_RWCTRL_WRITE_BNDRY_512);
14548 break;
14549 case 1024:
14550 default:
14551 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14552 DMA_RWCTRL_WRITE_BNDRY_1024);
14553 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014554 }
David S. Miller59e6b432005-05-18 22:50:10 -070014555 }
14556
14557out:
14558 return val;
14559}
14560
Linus Torvalds1da177e2005-04-16 15:20:36 -070014561static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
14562{
14563 struct tg3_internal_buffer_desc test_desc;
14564 u32 sram_dma_descs;
14565 int i, ret;
14566
14567 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
14568
14569 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
14570 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
14571 tw32(RDMAC_STATUS, 0);
14572 tw32(WDMAC_STATUS, 0);
14573
14574 tw32(BUFMGR_MODE, 0);
14575 tw32(FTQ_RESET, 0);
14576
14577 test_desc.addr_hi = ((u64) buf_dma) >> 32;
14578 test_desc.addr_lo = buf_dma & 0xffffffff;
14579 test_desc.nic_mbuf = 0x00002100;
14580 test_desc.len = size;
14581
14582 /*
14583 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
14584 * the *second* time the tg3 driver was getting loaded after an
14585 * initial scan.
14586 *
14587 * Broadcom tells me:
14588 * ...the DMA engine is connected to the GRC block and a DMA
14589 * reset may affect the GRC block in some unpredictable way...
14590 * The behavior of resets to individual blocks has not been tested.
14591 *
14592 * Broadcom noted the GRC reset will also reset all sub-components.
14593 */
14594 if (to_device) {
14595 test_desc.cqid_sqid = (13 << 8) | 2;
14596
14597 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
14598 udelay(40);
14599 } else {
14600 test_desc.cqid_sqid = (16 << 8) | 7;
14601
14602 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
14603 udelay(40);
14604 }
14605 test_desc.flags = 0x00000005;
14606
14607 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
14608 u32 val;
14609
14610 val = *(((u32 *)&test_desc) + i);
14611 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
14612 sram_dma_descs + (i * sizeof(u32)));
14613 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
14614 }
14615 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
14616
Matt Carlson859a588792010-04-05 10:19:28 +000014617 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014618 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000014619 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014620 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014621
14622 ret = -ENODEV;
14623 for (i = 0; i < 40; i++) {
14624 u32 val;
14625
14626 if (to_device)
14627 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
14628 else
14629 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
14630 if ((val & 0xffff) == sram_dma_descs) {
14631 ret = 0;
14632 break;
14633 }
14634
14635 udelay(100);
14636 }
14637
14638 return ret;
14639}
14640
David S. Millerded73402005-05-23 13:59:47 -070014641#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070014642
Matt Carlson41434702011-03-09 16:58:22 +000014643static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080014644 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14645 { },
14646};
14647
Linus Torvalds1da177e2005-04-16 15:20:36 -070014648static int __devinit tg3_test_dma(struct tg3 *tp)
14649{
14650 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070014651 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014652 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014653
Matt Carlson4bae65c2010-11-24 08:31:52 +000014654 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
14655 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014656 if (!buf) {
14657 ret = -ENOMEM;
14658 goto out_nofree;
14659 }
14660
14661 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
14662 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
14663
David S. Miller59e6b432005-05-18 22:50:10 -070014664 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014665
Joe Perches63c3a662011-04-26 08:12:10 +000014666 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014667 goto out;
14668
Joe Perches63c3a662011-04-26 08:12:10 +000014669 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014670 /* DMA read watermark not used on PCIE */
14671 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000014672 } else if (!tg3_flag(tp, PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070014673 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14674 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014675 tp->dma_rwctrl |= 0x003f0000;
14676 else
14677 tp->dma_rwctrl |= 0x003f000f;
14678 } else {
14679 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14680 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
14681 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080014682 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014683
Michael Chan4a29cc22006-03-19 13:21:12 -080014684 /* If the 5704 is behind the EPB bridge, we can
14685 * do the less restrictive ONE_DMA workaround for
14686 * better performance.
14687 */
Joe Perches63c3a662011-04-26 08:12:10 +000014688 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Michael Chan4a29cc22006-03-19 13:21:12 -080014689 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14690 tp->dma_rwctrl |= 0x8000;
14691 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014692 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
14693
Michael Chan49afdeb2007-02-13 12:17:03 -080014694 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
14695 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070014696 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080014697 tp->dma_rwctrl |=
14698 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
14699 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
14700 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070014701 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
14702 /* 5780 always in PCIX mode */
14703 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070014704 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
14705 /* 5714 always in PCIX mode */
14706 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014707 } else {
14708 tp->dma_rwctrl |= 0x001b000f;
14709 }
14710 }
14711
14712 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14713 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14714 tp->dma_rwctrl &= 0xfffffff0;
14715
14716 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14717 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
14718 /* Remove this if it causes problems for some boards. */
14719 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
14720
14721 /* On 5700/5701 chips, we need to set this bit.
14722 * Otherwise the chip will issue cacheline transactions
14723 * to streamable DMA memory with not all the byte
14724 * enables turned on. This is an error on several
14725 * RISC PCI controllers, in particular sparc64.
14726 *
14727 * On 5703/5704 chips, this bit has been reassigned
14728 * a different meaning. In particular, it is used
14729 * on those chips to enable a PCI-X workaround.
14730 */
14731 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
14732 }
14733
14734 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14735
14736#if 0
14737 /* Unneeded, already done by tg3_get_invariants. */
14738 tg3_switch_clocks(tp);
14739#endif
14740
Linus Torvalds1da177e2005-04-16 15:20:36 -070014741 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14742 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
14743 goto out;
14744
David S. Miller59e6b432005-05-18 22:50:10 -070014745 /* It is best to perform DMA test with maximum write burst size
14746 * to expose the 5700/5701 write DMA bug.
14747 */
14748 saved_dma_rwctrl = tp->dma_rwctrl;
14749 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14750 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14751
Linus Torvalds1da177e2005-04-16 15:20:36 -070014752 while (1) {
14753 u32 *p = buf, i;
14754
14755 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
14756 p[i] = i;
14757
14758 /* Send the buffer to the chip. */
14759 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
14760 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000014761 dev_err(&tp->pdev->dev,
14762 "%s: Buffer write failed. err = %d\n",
14763 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014764 break;
14765 }
14766
14767#if 0
14768 /* validate data reached card RAM correctly. */
14769 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14770 u32 val;
14771 tg3_read_mem(tp, 0x2100 + (i*4), &val);
14772 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000014773 dev_err(&tp->pdev->dev,
14774 "%s: Buffer corrupted on device! "
14775 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014776 /* ret = -ENODEV here? */
14777 }
14778 p[i] = 0;
14779 }
14780#endif
14781 /* Now read it back. */
14782 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
14783 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014784 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
14785 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014786 break;
14787 }
14788
14789 /* Verify it. */
14790 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14791 if (p[i] == i)
14792 continue;
14793
David S. Miller59e6b432005-05-18 22:50:10 -070014794 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14795 DMA_RWCTRL_WRITE_BNDRY_16) {
14796 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014797 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
14798 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14799 break;
14800 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000014801 dev_err(&tp->pdev->dev,
14802 "%s: Buffer corrupted on read back! "
14803 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014804 ret = -ENODEV;
14805 goto out;
14806 }
14807 }
14808
14809 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
14810 /* Success. */
14811 ret = 0;
14812 break;
14813 }
14814 }
David S. Miller59e6b432005-05-18 22:50:10 -070014815 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14816 DMA_RWCTRL_WRITE_BNDRY_16) {
14817 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070014818 * now look for chipsets that are known to expose the
14819 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070014820 */
Matt Carlson41434702011-03-09 16:58:22 +000014821 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014822 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14823 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000014824 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014825 /* Safe to use the calculated DMA boundary. */
14826 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000014827 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070014828
David S. Miller59e6b432005-05-18 22:50:10 -070014829 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014831
14832out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000014833 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014834out_nofree:
14835 return ret;
14836}
14837
Linus Torvalds1da177e2005-04-16 15:20:36 -070014838static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
14839{
Joe Perches63c3a662011-04-26 08:12:10 +000014840 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000014841 tp->bufmgr_config.mbuf_read_dma_low_water =
14842 DEFAULT_MB_RDMA_LOW_WATER_5705;
14843 tp->bufmgr_config.mbuf_mac_rx_low_water =
14844 DEFAULT_MB_MACRX_LOW_WATER_57765;
14845 tp->bufmgr_config.mbuf_high_water =
14846 DEFAULT_MB_HIGH_WATER_57765;
14847
14848 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14849 DEFAULT_MB_RDMA_LOW_WATER_5705;
14850 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14851 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
14852 tp->bufmgr_config.mbuf_high_water_jumbo =
14853 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000014854 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070014855 tp->bufmgr_config.mbuf_read_dma_low_water =
14856 DEFAULT_MB_RDMA_LOW_WATER_5705;
14857 tp->bufmgr_config.mbuf_mac_rx_low_water =
14858 DEFAULT_MB_MACRX_LOW_WATER_5705;
14859 tp->bufmgr_config.mbuf_high_water =
14860 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070014861 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14862 tp->bufmgr_config.mbuf_mac_rx_low_water =
14863 DEFAULT_MB_MACRX_LOW_WATER_5906;
14864 tp->bufmgr_config.mbuf_high_water =
14865 DEFAULT_MB_HIGH_WATER_5906;
14866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014867
Michael Chanfdfec1722005-07-25 12:31:48 -070014868 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14869 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
14870 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14871 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
14872 tp->bufmgr_config.mbuf_high_water_jumbo =
14873 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
14874 } else {
14875 tp->bufmgr_config.mbuf_read_dma_low_water =
14876 DEFAULT_MB_RDMA_LOW_WATER;
14877 tp->bufmgr_config.mbuf_mac_rx_low_water =
14878 DEFAULT_MB_MACRX_LOW_WATER;
14879 tp->bufmgr_config.mbuf_high_water =
14880 DEFAULT_MB_HIGH_WATER;
14881
14882 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14883 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
14884 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14885 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
14886 tp->bufmgr_config.mbuf_high_water_jumbo =
14887 DEFAULT_MB_HIGH_WATER_JUMBO;
14888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014889
14890 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
14891 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
14892}
14893
14894static char * __devinit tg3_phy_string(struct tg3 *tp)
14895{
Matt Carlson79eb6902010-02-17 15:17:03 +000014896 switch (tp->phy_id & TG3_PHY_ID_MASK) {
14897 case TG3_PHY_ID_BCM5400: return "5400";
14898 case TG3_PHY_ID_BCM5401: return "5401";
14899 case TG3_PHY_ID_BCM5411: return "5411";
14900 case TG3_PHY_ID_BCM5701: return "5701";
14901 case TG3_PHY_ID_BCM5703: return "5703";
14902 case TG3_PHY_ID_BCM5704: return "5704";
14903 case TG3_PHY_ID_BCM5705: return "5705";
14904 case TG3_PHY_ID_BCM5750: return "5750";
14905 case TG3_PHY_ID_BCM5752: return "5752";
14906 case TG3_PHY_ID_BCM5714: return "5714";
14907 case TG3_PHY_ID_BCM5780: return "5780";
14908 case TG3_PHY_ID_BCM5755: return "5755";
14909 case TG3_PHY_ID_BCM5787: return "5787";
14910 case TG3_PHY_ID_BCM5784: return "5784";
14911 case TG3_PHY_ID_BCM5756: return "5722/5756";
14912 case TG3_PHY_ID_BCM5906: return "5906";
14913 case TG3_PHY_ID_BCM5761: return "5761";
14914 case TG3_PHY_ID_BCM5718C: return "5718C";
14915 case TG3_PHY_ID_BCM5718S: return "5718S";
14916 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000014917 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000014918 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000014919 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070014920 case 0: return "serdes";
14921 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070014922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014923}
14924
Michael Chanf9804dd2005-09-27 12:13:10 -070014925static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
14926{
Joe Perches63c3a662011-04-26 08:12:10 +000014927 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070014928 strcpy(str, "PCI Express");
14929 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000014930 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070014931 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
14932
14933 strcpy(str, "PCIX:");
14934
14935 if ((clock_ctrl == 7) ||
14936 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
14937 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
14938 strcat(str, "133MHz");
14939 else if (clock_ctrl == 0)
14940 strcat(str, "33MHz");
14941 else if (clock_ctrl == 2)
14942 strcat(str, "50MHz");
14943 else if (clock_ctrl == 4)
14944 strcat(str, "66MHz");
14945 else if (clock_ctrl == 6)
14946 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070014947 } else {
14948 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000014949 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070014950 strcat(str, "66MHz");
14951 else
14952 strcat(str, "33MHz");
14953 }
Joe Perches63c3a662011-04-26 08:12:10 +000014954 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070014955 strcat(str, ":32-bit");
14956 else
14957 strcat(str, ":64-bit");
14958 return str;
14959}
14960
Michael Chan8c2dc7e2005-12-19 16:26:02 -080014961static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014962{
14963 struct pci_dev *peer;
14964 unsigned int func, devnr = tp->pdev->devfn & ~7;
14965
14966 for (func = 0; func < 8; func++) {
14967 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14968 if (peer && peer != tp->pdev)
14969 break;
14970 pci_dev_put(peer);
14971 }
Michael Chan16fe9d72005-12-13 21:09:54 -080014972 /* 5704 can be configured in single-port mode, set peer to
14973 * tp->pdev in that case.
14974 */
14975 if (!peer) {
14976 peer = tp->pdev;
14977 return peer;
14978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014979
14980 /*
14981 * We don't need to keep the refcount elevated; there's no way
14982 * to remove one half of this device without removing the other
14983 */
14984 pci_dev_put(peer);
14985
14986 return peer;
14987}
14988
David S. Miller15f98502005-05-18 22:49:26 -070014989static void __devinit tg3_init_coal(struct tg3 *tp)
14990{
14991 struct ethtool_coalesce *ec = &tp->coal;
14992
14993 memset(ec, 0, sizeof(*ec));
14994 ec->cmd = ETHTOOL_GCOALESCE;
14995 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
14996 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
14997 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
14998 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
14999 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
15000 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
15001 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
15002 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
15003 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
15004
15005 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
15006 HOSTCC_MODE_CLRTICK_TXBD)) {
15007 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
15008 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
15009 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
15010 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
15011 }
Michael Chand244c892005-07-05 14:42:33 -070015012
Joe Perches63c3a662011-04-26 08:12:10 +000015013 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070015014 ec->rx_coalesce_usecs_irq = 0;
15015 ec->tx_coalesce_usecs_irq = 0;
15016 ec->stats_block_coalesce_usecs = 0;
15017 }
David S. Miller15f98502005-05-18 22:49:26 -070015018}
15019
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080015020static const struct net_device_ops tg3_netdev_ops = {
15021 .ndo_open = tg3_open,
15022 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080015023 .ndo_start_xmit = tg3_start_xmit,
Eric Dumazet511d2222010-07-07 20:44:24 +000015024 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -080015025 .ndo_validate_addr = eth_validate_addr,
15026 .ndo_set_multicast_list = tg3_set_rx_mode,
15027 .ndo_set_mac_address = tg3_set_mac_addr,
15028 .ndo_do_ioctl = tg3_ioctl,
15029 .ndo_tx_timeout = tg3_tx_timeout,
15030 .ndo_change_mtu = tg3_change_mtu,
Michał Mirosławdc668912011-04-07 03:35:07 +000015031 .ndo_fix_features = tg3_fix_features,
Stephen Hemminger00829822008-11-20 20:14:53 -080015032#ifdef CONFIG_NET_POLL_CONTROLLER
15033 .ndo_poll_controller = tg3_poll_controller,
15034#endif
15035};
15036
15037static const struct net_device_ops tg3_netdev_ops_dma_bug = {
15038 .ndo_open = tg3_open,
15039 .ndo_stop = tg3_close,
15040 .ndo_start_xmit = tg3_start_xmit_dma_bug,
Eric Dumazet511d2222010-07-07 20:44:24 +000015041 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080015042 .ndo_validate_addr = eth_validate_addr,
15043 .ndo_set_multicast_list = tg3_set_rx_mode,
15044 .ndo_set_mac_address = tg3_set_mac_addr,
15045 .ndo_do_ioctl = tg3_ioctl,
15046 .ndo_tx_timeout = tg3_tx_timeout,
15047 .ndo_change_mtu = tg3_change_mtu,
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080015048#ifdef CONFIG_NET_POLL_CONTROLLER
15049 .ndo_poll_controller = tg3_poll_controller,
15050#endif
15051};
15052
Linus Torvalds1da177e2005-04-16 15:20:36 -070015053static int __devinit tg3_init_one(struct pci_dev *pdev,
15054 const struct pci_device_id *ent)
15055{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015056 struct net_device *dev;
15057 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000015058 int i, err, pm_cap;
15059 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070015060 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080015061 u64 dma_mask, persist_dma_mask;
Michał Mirosławdc668912011-04-07 03:35:07 +000015062 u32 hw_features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015063
Joe Perches05dbe002010-02-17 19:44:19 +000015064 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015065
15066 err = pci_enable_device(pdev);
15067 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015068 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015069 return err;
15070 }
15071
Linus Torvalds1da177e2005-04-16 15:20:36 -070015072 err = pci_request_regions(pdev, DRV_MODULE_NAME);
15073 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015074 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015075 goto err_out_disable_pdev;
15076 }
15077
15078 pci_set_master(pdev);
15079
15080 /* Find power-management capability. */
15081 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
15082 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000015083 dev_err(&pdev->dev,
15084 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015085 err = -EIO;
15086 goto err_out_free_res;
15087 }
15088
Matt Carlsonfe5f5782009-09-01 13:09:39 +000015089 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015090 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000015091 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015092 err = -ENOMEM;
15093 goto err_out_free_res;
15094 }
15095
Linus Torvalds1da177e2005-04-16 15:20:36 -070015096 SET_NETDEV_DEV(dev, &pdev->dev);
15097
Linus Torvalds1da177e2005-04-16 15:20:36 -070015098 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015099
15100 tp = netdev_priv(dev);
15101 tp->pdev = pdev;
15102 tp->dev = dev;
15103 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015104 tp->rx_mode = TG3_DEF_RX_MODE;
15105 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070015106
Linus Torvalds1da177e2005-04-16 15:20:36 -070015107 if (tg3_debug > 0)
15108 tp->msg_enable = tg3_debug;
15109 else
15110 tp->msg_enable = TG3_DEF_MSG_ENABLE;
15111
15112 /* The word/byte swap controls here control register access byte
15113 * swapping. DMA data byte swapping is controlled in the GRC_MODE
15114 * setting below.
15115 */
15116 tp->misc_host_ctrl =
15117 MISC_HOST_CTRL_MASK_PCI_INT |
15118 MISC_HOST_CTRL_WORD_SWAP |
15119 MISC_HOST_CTRL_INDIR_ACCESS |
15120 MISC_HOST_CTRL_PCISTATE_RW;
15121
15122 /* The NONFRM (non-frame) byte/word swap controls take effect
15123 * on descriptor entries, anything which isn't packet data.
15124 *
15125 * The StrongARM chips on the board (one for tx, one for rx)
15126 * are running in big-endian mode.
15127 */
15128 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
15129 GRC_MODE_WSWAP_NONFRM_DATA);
15130#ifdef __BIG_ENDIAN
15131 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
15132#endif
15133 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015134 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000015135 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015136
Matt Carlsond5fe4882008-11-21 17:20:32 -080015137 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010015138 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015139 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015140 err = -ENOMEM;
15141 goto err_out_free_dev;
15142 }
15143
Linus Torvalds1da177e2005-04-16 15:20:36 -070015144 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
15145 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015146
Linus Torvalds1da177e2005-04-16 15:20:36 -070015147 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015148 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015149 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015150
15151 err = tg3_get_invariants(tp);
15152 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015153 dev_err(&pdev->dev,
15154 "Problem fetching invariants of chip, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015155 goto err_out_iounmap;
15156 }
15157
Joe Perches63c3a662011-04-26 08:12:10 +000015158 if (tg3_flag(tp, 5755_PLUS) && !tg3_flag(tp, 5717_PLUS))
Stephen Hemminger00829822008-11-20 20:14:53 -080015159 dev->netdev_ops = &tg3_netdev_ops;
15160 else
15161 dev->netdev_ops = &tg3_netdev_ops_dma_bug;
15162
15163
Michael Chan4a29cc22006-03-19 13:21:12 -080015164 /* The EPB bridge inside 5714, 5715, and 5780 and any
15165 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080015166 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
15167 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
15168 * do DMA address check in tg3_start_xmit().
15169 */
Joe Perches63c3a662011-04-26 08:12:10 +000015170 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070015171 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000015172 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070015173 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080015174#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070015175 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015176#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080015177 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070015178 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015179
15180 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070015181 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080015182 err = pci_set_dma_mask(pdev, dma_mask);
15183 if (!err) {
15184 dev->features |= NETIF_F_HIGHDMA;
15185 err = pci_set_consistent_dma_mask(pdev,
15186 persist_dma_mask);
15187 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015188 dev_err(&pdev->dev, "Unable to obtain 64 bit "
15189 "DMA for consistent allocations\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080015190 goto err_out_iounmap;
15191 }
15192 }
15193 }
Yang Hongyang284901a2009-04-06 19:01:15 -070015194 if (err || dma_mask == DMA_BIT_MASK(32)) {
15195 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080015196 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015197 dev_err(&pdev->dev,
15198 "No usable DMA configuration, aborting\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080015199 goto err_out_iounmap;
15200 }
15201 }
15202
Michael Chanfdfec1722005-07-25 12:31:48 -070015203 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015204
Matt Carlson507399f2009-11-13 13:03:37 +000015205 /* Selectively allow TSO based on operating conditions */
Joe Perches63c3a662011-04-26 08:12:10 +000015206 if ((tg3_flag(tp, HW_TSO_1) ||
15207 tg3_flag(tp, HW_TSO_2) ||
15208 tg3_flag(tp, HW_TSO_3)) ||
15209 (tp->fw_needed && !tg3_flag(tp, ENABLE_ASF)))
15210 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlson507399f2009-11-13 13:03:37 +000015211 else {
Joe Perches63c3a662011-04-26 08:12:10 +000015212 tg3_flag_clear(tp, TSO_CAPABLE);
15213 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000015214 tp->fw_needed = NULL;
15215 }
15216
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015217 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
Matt Carlson9e9fd122009-01-19 16:57:45 -080015218 tp->fw_needed = FIRMWARE_TG3;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015219
Michael Chan4e3a7aa2006-03-20 17:47:44 -080015220 /* TSO is on by default on chips that support hardware TSO.
15221 * Firmware TSO on older chips gives lower performance, so it
15222 * is off by default, but can be enabled using ethtool.
15223 */
Joe Perches63c3a662011-04-26 08:12:10 +000015224 if ((tg3_flag(tp, HW_TSO_1) ||
15225 tg3_flag(tp, HW_TSO_2) ||
15226 tg3_flag(tp, HW_TSO_3)) &&
Michał Mirosławdc668912011-04-07 03:35:07 +000015227 (dev->features & NETIF_F_IP_CSUM))
15228 hw_features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000015229 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Michał Mirosławdc668912011-04-07 03:35:07 +000015230 if (dev->features & NETIF_F_IPV6_CSUM)
15231 hw_features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000015232 if (tg3_flag(tp, HW_TSO_3) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000015233 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070015234 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15235 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Joe Perches63c3a662011-04-26 08:12:10 +000015236 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000015237 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
15238 hw_features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070015239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015240
Michał Mirosławdc668912011-04-07 03:35:07 +000015241 dev->hw_features |= hw_features;
15242 dev->features |= hw_features;
15243 dev->vlan_features |= hw_features;
15244
Linus Torvalds1da177e2005-04-16 15:20:36 -070015245 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015246 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015247 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015248 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015249 tp->rx_pending = 63;
15250 }
15251
Linus Torvalds1da177e2005-04-16 15:20:36 -070015252 err = tg3_get_device_address(tp);
15253 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015254 dev_err(&pdev->dev,
15255 "Could not obtain valid ethernet address, aborting\n");
Matt Carlson026a6c22009-12-03 08:36:24 +000015256 goto err_out_iounmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015257 }
15258
Joe Perches63c3a662011-04-26 08:12:10 +000015259 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson63532392008-11-03 16:49:57 -080015260 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
Al Viro79ea13c2008-01-24 02:06:46 -080015261 if (!tp->aperegs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015262 dev_err(&pdev->dev,
15263 "Cannot map APE registers, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015264 err = -ENOMEM;
Matt Carlson026a6c22009-12-03 08:36:24 +000015265 goto err_out_iounmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015266 }
15267
15268 tg3_ape_lock_init(tp);
Matt Carlson7fd76442009-02-25 14:27:20 +000015269
Joe Perches63c3a662011-04-26 08:12:10 +000015270 if (tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000015271 tg3_read_dash_ver(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015272 }
15273
Matt Carlsonc88864d2007-11-12 21:07:01 -080015274 /*
15275 * Reset chip in case UNDI or EFI driver did not shutdown
15276 * DMA self test will enable WDMAC and we'll see (spurious)
15277 * pending DMA on the PCI bus at that point.
15278 */
15279 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15280 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15281 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15282 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15283 }
15284
15285 err = tg3_test_dma(tp);
15286 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015287 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080015288 goto err_out_apeunmap;
15289 }
15290
Matt Carlson78f90dc2009-11-13 13:03:42 +000015291 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15292 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15293 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000015294 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000015295 struct tg3_napi *tnapi = &tp->napi[i];
15296
15297 tnapi->tp = tp;
15298 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15299
15300 tnapi->int_mbox = intmbx;
15301 if (i < 4)
15302 intmbx += 0x8;
15303 else
15304 intmbx += 0x4;
15305
15306 tnapi->consmbox = rcvmbx;
15307 tnapi->prodmbox = sndmbx;
15308
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015309 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015310 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015311 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000015312 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000015313
Joe Perches63c3a662011-04-26 08:12:10 +000015314 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000015315 break;
15316
15317 /*
15318 * If we support MSIX, we'll be using RSS. If we're using
15319 * RSS, the first vector only handles link interrupts and the
15320 * remaining vectors handle rx and tx interrupts. Reuse the
15321 * mailbox values for the next iteration. The values we setup
15322 * above are still useful for the single vectored mode.
15323 */
15324 if (!i)
15325 continue;
15326
15327 rcvmbx += 0x8;
15328
15329 if (sndmbx & 0x4)
15330 sndmbx -= 0x4;
15331 else
15332 sndmbx += 0xc;
15333 }
15334
Matt Carlsonc88864d2007-11-12 21:07:01 -080015335 tg3_init_coal(tp);
15336
Michael Chanc49a1562006-12-17 17:07:29 -080015337 pci_set_drvdata(pdev, dev);
15338
Linus Torvalds1da177e2005-04-16 15:20:36 -070015339 err = register_netdev(dev);
15340 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015341 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015342 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015343 }
15344
Joe Perches05dbe002010-02-17 19:44:19 +000015345 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15346 tp->board_part_number,
15347 tp->pci_chip_rev_id,
15348 tg3_bus_string(tp, str),
15349 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015350
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015351 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000015352 struct phy_device *phydev;
15353 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000015354 netdev_info(dev,
15355 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015356 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015357 } else {
15358 char *ethtype;
15359
15360 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15361 ethtype = "10/100Base-TX";
15362 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15363 ethtype = "1000Base-SX";
15364 else
15365 ethtype = "10/100/1000Base-T";
15366
Matt Carlson5129c3a2010-04-05 10:19:23 +000015367 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000015368 "(WireSpeed[%d], EEE[%d])\n",
15369 tg3_phy_string(tp), ethtype,
15370 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
15371 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015372 }
Matt Carlsondf59c942008-11-03 16:52:56 -080015373
Joe Perches05dbe002010-02-17 19:44:19 +000015374 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000015375 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015376 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015377 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015378 tg3_flag(tp, ENABLE_ASF) != 0,
15379 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000015380 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15381 tp->dma_rwctrl,
15382 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15383 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015384
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015385 pci_save_state(pdev);
15386
Linus Torvalds1da177e2005-04-16 15:20:36 -070015387 return 0;
15388
Matt Carlson0d3031d2007-10-10 18:02:43 -070015389err_out_apeunmap:
15390 if (tp->aperegs) {
15391 iounmap(tp->aperegs);
15392 tp->aperegs = NULL;
15393 }
15394
Linus Torvalds1da177e2005-04-16 15:20:36 -070015395err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070015396 if (tp->regs) {
15397 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015398 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015400
15401err_out_free_dev:
15402 free_netdev(dev);
15403
15404err_out_free_res:
15405 pci_release_regions(pdev);
15406
15407err_out_disable_pdev:
15408 pci_disable_device(pdev);
15409 pci_set_drvdata(pdev, NULL);
15410 return err;
15411}
15412
15413static void __devexit tg3_remove_one(struct pci_dev *pdev)
15414{
15415 struct net_device *dev = pci_get_drvdata(pdev);
15416
15417 if (dev) {
15418 struct tg3 *tp = netdev_priv(dev);
15419
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015420 if (tp->fw)
15421 release_firmware(tp->fw);
15422
Tejun Heo23f333a2010-12-12 16:45:14 +010015423 cancel_work_sync(&tp->reset_task);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015424
Joe Perches63c3a662011-04-26 08:12:10 +000015425 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015426 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015427 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015428 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015429
Linus Torvalds1da177e2005-04-16 15:20:36 -070015430 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015431 if (tp->aperegs) {
15432 iounmap(tp->aperegs);
15433 tp->aperegs = NULL;
15434 }
Michael Chan68929142005-08-09 20:17:14 -070015435 if (tp->regs) {
15436 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015437 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015439 free_netdev(dev);
15440 pci_release_regions(pdev);
15441 pci_disable_device(pdev);
15442 pci_set_drvdata(pdev, NULL);
15443 }
15444}
15445
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015446#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015447static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015448{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015449 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015450 struct net_device *dev = pci_get_drvdata(pdev);
15451 struct tg3 *tp = netdev_priv(dev);
15452 int err;
15453
15454 if (!netif_running(dev))
15455 return 0;
15456
Tejun Heo23f333a2010-12-12 16:45:14 +010015457 flush_work_sync(&tp->reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015458 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015459 tg3_netif_stop(tp);
15460
15461 del_timer_sync(&tp->timer);
15462
David S. Millerf47c11e2005-06-24 20:18:35 -070015463 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015464 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015465 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015466
15467 netif_device_detach(dev);
15468
David S. Millerf47c11e2005-06-24 20:18:35 -070015469 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015470 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000015471 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070015472 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015473
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015474 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015475 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015476 int err2;
15477
David S. Millerf47c11e2005-06-24 20:18:35 -070015478 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015479
Joe Perches63c3a662011-04-26 08:12:10 +000015480 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015481 err2 = tg3_restart_hw(tp, 1);
15482 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015483 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015484
15485 tp->timer.expires = jiffies + tp->timer_offset;
15486 add_timer(&tp->timer);
15487
15488 netif_device_attach(dev);
15489 tg3_netif_start(tp);
15490
Michael Chanb9ec6c12006-07-25 16:37:27 -070015491out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015492 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015493
15494 if (!err2)
15495 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015496 }
15497
15498 return err;
15499}
15500
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015501static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015502{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015503 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015504 struct net_device *dev = pci_get_drvdata(pdev);
15505 struct tg3 *tp = netdev_priv(dev);
15506 int err;
15507
15508 if (!netif_running(dev))
15509 return 0;
15510
Linus Torvalds1da177e2005-04-16 15:20:36 -070015511 netif_device_attach(dev);
15512
David S. Millerf47c11e2005-06-24 20:18:35 -070015513 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015514
Joe Perches63c3a662011-04-26 08:12:10 +000015515 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070015516 err = tg3_restart_hw(tp, 1);
15517 if (err)
15518 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015519
15520 tp->timer.expires = jiffies + tp->timer_offset;
15521 add_timer(&tp->timer);
15522
Linus Torvalds1da177e2005-04-16 15:20:36 -070015523 tg3_netif_start(tp);
15524
Michael Chanb9ec6c12006-07-25 16:37:27 -070015525out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015526 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015527
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015528 if (!err)
15529 tg3_phy_start(tp);
15530
Michael Chanb9ec6c12006-07-25 16:37:27 -070015531 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015532}
15533
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015534static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015535#define TG3_PM_OPS (&tg3_pm_ops)
15536
15537#else
15538
15539#define TG3_PM_OPS NULL
15540
15541#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015542
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015543/**
15544 * tg3_io_error_detected - called when PCI error is detected
15545 * @pdev: Pointer to PCI device
15546 * @state: The current pci connection state
15547 *
15548 * This function is called after a PCI bus error affecting
15549 * this device has been detected.
15550 */
15551static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
15552 pci_channel_state_t state)
15553{
15554 struct net_device *netdev = pci_get_drvdata(pdev);
15555 struct tg3 *tp = netdev_priv(netdev);
15556 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
15557
15558 netdev_info(netdev, "PCI I/O error detected\n");
15559
15560 rtnl_lock();
15561
15562 if (!netif_running(netdev))
15563 goto done;
15564
15565 tg3_phy_stop(tp);
15566
15567 tg3_netif_stop(tp);
15568
15569 del_timer_sync(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +000015570 tg3_flag_clear(tp, RESTART_TIMER);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015571
15572 /* Want to make sure that the reset task doesn't run */
15573 cancel_work_sync(&tp->reset_task);
Joe Perches63c3a662011-04-26 08:12:10 +000015574 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
15575 tg3_flag_clear(tp, RESTART_TIMER);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015576
15577 netif_device_detach(netdev);
15578
15579 /* Clean up software state, even if MMIO is blocked */
15580 tg3_full_lock(tp, 0);
15581 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
15582 tg3_full_unlock(tp);
15583
15584done:
15585 if (state == pci_channel_io_perm_failure)
15586 err = PCI_ERS_RESULT_DISCONNECT;
15587 else
15588 pci_disable_device(pdev);
15589
15590 rtnl_unlock();
15591
15592 return err;
15593}
15594
15595/**
15596 * tg3_io_slot_reset - called after the pci bus has been reset.
15597 * @pdev: Pointer to PCI device
15598 *
15599 * Restart the card from scratch, as if from a cold-boot.
15600 * At this point, the card has exprienced a hard reset,
15601 * followed by fixups by BIOS, and has its config space
15602 * set up identically to what it was at cold boot.
15603 */
15604static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
15605{
15606 struct net_device *netdev = pci_get_drvdata(pdev);
15607 struct tg3 *tp = netdev_priv(netdev);
15608 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
15609 int err;
15610
15611 rtnl_lock();
15612
15613 if (pci_enable_device(pdev)) {
15614 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
15615 goto done;
15616 }
15617
15618 pci_set_master(pdev);
15619 pci_restore_state(pdev);
15620 pci_save_state(pdev);
15621
15622 if (!netif_running(netdev)) {
15623 rc = PCI_ERS_RESULT_RECOVERED;
15624 goto done;
15625 }
15626
15627 err = tg3_power_up(tp);
15628 if (err) {
15629 netdev_err(netdev, "Failed to restore register access.\n");
15630 goto done;
15631 }
15632
15633 rc = PCI_ERS_RESULT_RECOVERED;
15634
15635done:
15636 rtnl_unlock();
15637
15638 return rc;
15639}
15640
15641/**
15642 * tg3_io_resume - called when traffic can start flowing again.
15643 * @pdev: Pointer to PCI device
15644 *
15645 * This callback is called when the error recovery driver tells
15646 * us that its OK to resume normal operation.
15647 */
15648static void tg3_io_resume(struct pci_dev *pdev)
15649{
15650 struct net_device *netdev = pci_get_drvdata(pdev);
15651 struct tg3 *tp = netdev_priv(netdev);
15652 int err;
15653
15654 rtnl_lock();
15655
15656 if (!netif_running(netdev))
15657 goto done;
15658
15659 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000015660 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015661 err = tg3_restart_hw(tp, 1);
15662 tg3_full_unlock(tp);
15663 if (err) {
15664 netdev_err(netdev, "Cannot restart hardware after reset.\n");
15665 goto done;
15666 }
15667
15668 netif_device_attach(netdev);
15669
15670 tp->timer.expires = jiffies + tp->timer_offset;
15671 add_timer(&tp->timer);
15672
15673 tg3_netif_start(tp);
15674
15675 tg3_phy_start(tp);
15676
15677done:
15678 rtnl_unlock();
15679}
15680
15681static struct pci_error_handlers tg3_err_handler = {
15682 .error_detected = tg3_io_error_detected,
15683 .slot_reset = tg3_io_slot_reset,
15684 .resume = tg3_io_resume
15685};
15686
Linus Torvalds1da177e2005-04-16 15:20:36 -070015687static struct pci_driver tg3_driver = {
15688 .name = DRV_MODULE_NAME,
15689 .id_table = tg3_pci_tbl,
15690 .probe = tg3_init_one,
15691 .remove = __devexit_p(tg3_remove_one),
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015692 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015693 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015694};
15695
15696static int __init tg3_init(void)
15697{
Jeff Garzik29917622006-08-19 17:48:59 -040015698 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015699}
15700
15701static void __exit tg3_cleanup(void)
15702{
15703 pci_unregister_driver(&tg3_driver);
15704}
15705
15706module_init(tg3_init);
15707module_exit(tg3_cleanup);