blob: 09fe0678443225a0953ebebe6d23441adc3e4e2e [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" },
367 { "rx_errors" },
368 { "rx_threshold_hit" },
369
370 { "dma_readq_full" },
371 { "dma_read_prioq_full" },
372 { "tx_comp_queue_full" },
373
374 { "ring_set_send_prod_index" },
375 { "ring_status_update" },
376 { "nic_irqs" },
377 { "nic_avoided_irqs" },
Matt Carlson4452d092011-05-19 12:12:51 +0000378 { "nic_tx_threshold_hit" },
379
380 { "mbuf_lwm_thresh_hit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381};
382
Matt Carlson48fa55a2011-04-13 11:05:06 +0000383#define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
384
385
Andreas Mohr50da8592006-08-14 23:54:30 -0700386static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700387 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000388} ethtool_test_keys[] = {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700389 { "nvram test (online) " },
390 { "link test (online) " },
391 { "register test (offline)" },
392 { "memory test (offline)" },
393 { "loopback test (offline)" },
394 { "interrupt test (offline)" },
395};
396
Matt Carlson48fa55a2011-04-13 11:05:06 +0000397#define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
398
399
Michael Chanb401e9e2005-12-19 16:27:04 -0800400static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
401{
402 writel(val, tp->regs + off);
403}
404
405static u32 tg3_read32(struct tg3 *tp, u32 off)
406{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000407 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800408}
409
Matt Carlson0d3031d2007-10-10 18:02:43 -0700410static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
411{
412 writel(val, tp->aperegs + off);
413}
414
415static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
416{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000417 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700418}
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
421{
Michael Chan68929142005-08-09 20:17:14 -0700422 unsigned long flags;
423
424 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700425 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
426 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700427 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700428}
429
430static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
431{
432 writel(val, tp->regs + off);
433 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
Michael Chan68929142005-08-09 20:17:14 -0700436static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
437{
438 unsigned long flags;
439 u32 val;
440
441 spin_lock_irqsave(&tp->indirect_lock, flags);
442 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
443 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
444 spin_unlock_irqrestore(&tp->indirect_lock, flags);
445 return val;
446}
447
448static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
449{
450 unsigned long flags;
451
452 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
453 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
454 TG3_64BIT_REG_LOW, val);
455 return;
456 }
Matt Carlson66711e62009-11-13 13:03:49 +0000457 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700458 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
459 TG3_64BIT_REG_LOW, val);
460 return;
461 }
462
463 spin_lock_irqsave(&tp->indirect_lock, flags);
464 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
465 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
466 spin_unlock_irqrestore(&tp->indirect_lock, flags);
467
468 /* In indirect mode when disabling interrupts, we also need
469 * to clear the interrupt bit in the GRC local ctrl register.
470 */
471 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
472 (val == 0x1)) {
473 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
474 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
475 }
476}
477
478static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
479{
480 unsigned long flags;
481 u32 val;
482
483 spin_lock_irqsave(&tp->indirect_lock, flags);
484 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
485 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
486 spin_unlock_irqrestore(&tp->indirect_lock, flags);
487 return val;
488}
489
Michael Chanb401e9e2005-12-19 16:27:04 -0800490/* usec_wait specifies the wait time in usec when writing to certain registers
491 * where it is unsafe to read back the register without some delay.
492 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
493 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
494 */
495static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Joe Perches63c3a662011-04-26 08:12:10 +0000497 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800498 /* Non-posted methods */
499 tp->write32(tp, off, val);
500 else {
501 /* Posted method */
502 tg3_write32(tp, off, val);
503 if (usec_wait)
504 udelay(usec_wait);
505 tp->read32(tp, off);
506 }
507 /* Wait again after the read for the posted method to guarantee that
508 * the wait time is met.
509 */
510 if (usec_wait)
511 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
Michael Chan09ee9292005-08-09 20:17:00 -0700514static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
515{
516 tp->write32_mbox(tp, off, val);
Joe Perches63c3a662011-04-26 08:12:10 +0000517 if (!tg3_flag(tp, MBOX_WRITE_REORDER) && !tg3_flag(tp, ICH_WORKAROUND))
Michael Chan68929142005-08-09 20:17:14 -0700518 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700519}
520
Michael Chan20094932005-08-09 20:16:32 -0700521static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 void __iomem *mbox = tp->regs + off;
524 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000525 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000527 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 readl(mbox);
529}
530
Michael Chanb5d37722006-09-27 16:06:21 -0700531static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
532{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000533 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700534}
535
536static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
537{
538 writel(val, tp->regs + off + GRCMBOX_BASE);
539}
540
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000541#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700542#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000543#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
544#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
545#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700546
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000547#define tw32(reg, val) tp->write32(tp, reg, val)
548#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
549#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
550#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
553{
Michael Chan68929142005-08-09 20:17:14 -0700554 unsigned long flags;
555
Michael Chanb5d37722006-09-27 16:06:21 -0700556 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
557 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
558 return;
559
Michael Chan68929142005-08-09 20:17:14 -0700560 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000561 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700562 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
563 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Michael Chanbbadf502006-04-06 21:46:34 -0700565 /* Always leave this as zero. */
566 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
567 } else {
568 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
569 tw32_f(TG3PCI_MEM_WIN_DATA, val);
570
571 /* Always leave this as zero. */
572 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
573 }
Michael Chan68929142005-08-09 20:17:14 -0700574 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
577static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
578{
Michael Chan68929142005-08-09 20:17:14 -0700579 unsigned long flags;
580
Michael Chanb5d37722006-09-27 16:06:21 -0700581 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
582 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
583 *val = 0;
584 return;
585 }
586
Michael Chan68929142005-08-09 20:17:14 -0700587 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000588 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700589 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
590 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Michael Chanbbadf502006-04-06 21:46:34 -0700592 /* Always leave this as zero. */
593 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
594 } else {
595 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
596 *val = tr32(TG3PCI_MEM_WIN_DATA);
597
598 /* Always leave this as zero. */
599 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
600 }
Michael Chan68929142005-08-09 20:17:14 -0700601 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603
Matt Carlson0d3031d2007-10-10 18:02:43 -0700604static void tg3_ape_lock_init(struct tg3 *tp)
605{
606 int i;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000607 u32 regbase;
608
609 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
610 regbase = TG3_APE_LOCK_GRANT;
611 else
612 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700613
614 /* Make sure the driver hasn't any stale locks. */
615 for (i = 0; i < 8; i++)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000616 tg3_ape_write32(tp, regbase + 4 * i, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700617}
618
619static int tg3_ape_lock(struct tg3 *tp, int locknum)
620{
621 int i, off;
622 int ret = 0;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000623 u32 status, req, gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700624
Joe Perches63c3a662011-04-26 08:12:10 +0000625 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700626 return 0;
627
628 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000629 case TG3_APE_LOCK_GRC:
630 case TG3_APE_LOCK_MEM:
631 break;
632 default:
633 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700634 }
635
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000636 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
637 req = TG3_APE_LOCK_REQ;
638 gnt = TG3_APE_LOCK_GRANT;
639 } else {
640 req = TG3_APE_PER_LOCK_REQ;
641 gnt = TG3_APE_PER_LOCK_GRANT;
642 }
643
Matt Carlson0d3031d2007-10-10 18:02:43 -0700644 off = 4 * locknum;
645
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000646 tg3_ape_write32(tp, req + off, APE_LOCK_REQ_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700647
648 /* Wait for up to 1 millisecond to acquire lock. */
649 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000650 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700651 if (status == APE_LOCK_GRANT_DRIVER)
652 break;
653 udelay(10);
654 }
655
656 if (status != APE_LOCK_GRANT_DRIVER) {
657 /* Revoke the lock request. */
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000658 tg3_ape_write32(tp, gnt + off,
Matt Carlson0d3031d2007-10-10 18:02:43 -0700659 APE_LOCK_GRANT_DRIVER);
660
661 ret = -EBUSY;
662 }
663
664 return ret;
665}
666
667static void tg3_ape_unlock(struct tg3 *tp, int locknum)
668{
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000669 u32 gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700670
Joe Perches63c3a662011-04-26 08:12:10 +0000671 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700672 return;
673
674 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000675 case TG3_APE_LOCK_GRC:
676 case TG3_APE_LOCK_MEM:
677 break;
678 default:
679 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700680 }
681
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000682 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
683 gnt = TG3_APE_LOCK_GRANT;
684 else
685 gnt = TG3_APE_PER_LOCK_GRANT;
686
687 tg3_ape_write32(tp, gnt + 4 * locknum, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700688}
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690static void tg3_disable_ints(struct tg3 *tp)
691{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000692 int i;
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 tw32(TG3PCI_MISC_HOST_CTRL,
695 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000696 for (i = 0; i < tp->irq_max; i++)
697 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700static void tg3_enable_ints(struct tg3 *tp)
701{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000702 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000703
Michael Chanbbe832c2005-06-24 20:20:04 -0700704 tp->irq_sync = 0;
705 wmb();
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 tw32(TG3PCI_MISC_HOST_CTRL,
708 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000709
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000710 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000711 for (i = 0; i < tp->irq_cnt; i++) {
712 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000713
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000714 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +0000715 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000716 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
717
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000718 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000719 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000720
721 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +0000722 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000723 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
724 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
725 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000726 tw32(HOSTCC_MODE, tp->coal_now);
727
728 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730
Matt Carlson17375d22009-08-28 14:02:18 +0000731static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700732{
Matt Carlson17375d22009-08-28 14:02:18 +0000733 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000734 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700735 unsigned int work_exists = 0;
736
737 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +0000738 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -0700739 if (sblk->status & SD_STATUS_LINK_CHG)
740 work_exists = 1;
741 }
742 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000743 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000744 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700745 work_exists = 1;
746
747 return work_exists;
748}
749
Matt Carlson17375d22009-08-28 14:02:18 +0000750/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700751 * similar to tg3_enable_ints, but it accurately determines whether there
752 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400753 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 */
Matt Carlson17375d22009-08-28 14:02:18 +0000755static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
Matt Carlson17375d22009-08-28 14:02:18 +0000757 struct tg3 *tp = tnapi->tp;
758
Matt Carlson898a56f2009-08-28 14:02:40 +0000759 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 mmiowb();
761
David S. Millerfac9b832005-05-18 22:46:34 -0700762 /* When doing tagged status, this work check is unnecessary.
763 * The last_tag we write above tells the chip which piece of
764 * work we've completed.
765 */
Joe Perches63c3a662011-04-26 08:12:10 +0000766 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700767 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000768 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771static void tg3_switch_clocks(struct tg3 *tp)
772{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000773 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 u32 orig_clock_ctrl;
775
Joe Perches63c3a662011-04-26 08:12:10 +0000776 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700777 return;
778
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000779 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 orig_clock_ctrl = clock_ctrl;
782 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
783 CLOCK_CTRL_CLKRUN_OENABLE |
784 0x1f);
785 tp->pci_clock_ctrl = clock_ctrl;
786
Joe Perches63c3a662011-04-26 08:12:10 +0000787 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800789 tw32_wait_f(TG3PCI_CLOCK_CTRL,
790 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
792 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800793 tw32_wait_f(TG3PCI_CLOCK_CTRL,
794 clock_ctrl |
795 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
796 40);
797 tw32_wait_f(TG3PCI_CLOCK_CTRL,
798 clock_ctrl | (CLOCK_CTRL_ALTCLK),
799 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800801 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802}
803
804#define PHY_BUSY_LOOPS 5000
805
806static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
807{
808 u32 frame_val;
809 unsigned int loops;
810 int ret;
811
812 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
813 tw32_f(MAC_MI_MODE,
814 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
815 udelay(80);
816 }
817
818 *val = 0x0;
819
Matt Carlson882e9792009-09-01 13:21:36 +0000820 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 MI_COM_PHY_ADDR_MASK);
822 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
823 MI_COM_REG_ADDR_MASK);
824 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 tw32_f(MAC_MI_COM, frame_val);
827
828 loops = PHY_BUSY_LOOPS;
829 while (loops != 0) {
830 udelay(10);
831 frame_val = tr32(MAC_MI_COM);
832
833 if ((frame_val & MI_COM_BUSY) == 0) {
834 udelay(5);
835 frame_val = tr32(MAC_MI_COM);
836 break;
837 }
838 loops -= 1;
839 }
840
841 ret = -EBUSY;
842 if (loops != 0) {
843 *val = frame_val & MI_COM_DATA_MASK;
844 ret = 0;
845 }
846
847 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
848 tw32_f(MAC_MI_MODE, tp->mi_mode);
849 udelay(80);
850 }
851
852 return ret;
853}
854
855static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
856{
857 u32 frame_val;
858 unsigned int loops;
859 int ret;
860
Matt Carlsonf07e9af2010-08-02 11:26:07 +0000861 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Michael Chanb5d37722006-09-27 16:06:21 -0700862 (reg == MII_TG3_CTRL || reg == MII_TG3_AUX_CTRL))
863 return 0;
864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
866 tw32_f(MAC_MI_MODE,
867 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
868 udelay(80);
869 }
870
Matt Carlson882e9792009-09-01 13:21:36 +0000871 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 MI_COM_PHY_ADDR_MASK);
873 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
874 MI_COM_REG_ADDR_MASK);
875 frame_val |= (val & MI_COM_DATA_MASK);
876 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 tw32_f(MAC_MI_COM, frame_val);
879
880 loops = PHY_BUSY_LOOPS;
881 while (loops != 0) {
882 udelay(10);
883 frame_val = tr32(MAC_MI_COM);
884 if ((frame_val & MI_COM_BUSY) == 0) {
885 udelay(5);
886 frame_val = tr32(MAC_MI_COM);
887 break;
888 }
889 loops -= 1;
890 }
891
892 ret = -EBUSY;
893 if (loops != 0)
894 ret = 0;
895
896 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
897 tw32_f(MAC_MI_MODE, tp->mi_mode);
898 udelay(80);
899 }
900
901 return ret;
902}
903
Matt Carlsonb0988c12011-04-20 07:57:39 +0000904static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
905{
906 int err;
907
908 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
909 if (err)
910 goto done;
911
912 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
913 if (err)
914 goto done;
915
916 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
917 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
918 if (err)
919 goto done;
920
921 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
922
923done:
924 return err;
925}
926
927static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
928{
929 int err;
930
931 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
932 if (err)
933 goto done;
934
935 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
936 if (err)
937 goto done;
938
939 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
940 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
941 if (err)
942 goto done;
943
944 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
945
946done:
947 return err;
948}
949
950static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
951{
952 int err;
953
954 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
955 if (!err)
956 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
957
958 return err;
959}
960
961static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
962{
963 int err;
964
965 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
966 if (!err)
967 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
968
969 return err;
970}
971
Matt Carlson15ee95c2011-04-20 07:57:40 +0000972static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
973{
974 int err;
975
976 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
977 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
978 MII_TG3_AUXCTL_SHDWSEL_MISC);
979 if (!err)
980 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
981
982 return err;
983}
984
Matt Carlsonb4bd2922011-04-20 07:57:41 +0000985static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
986{
987 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
988 set |= MII_TG3_AUXCTL_MISC_WREN;
989
990 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
991}
992
Matt Carlson1d36ba42011-04-20 07:57:42 +0000993#define TG3_PHY_AUXCTL_SMDSP_ENABLE(tp) \
994 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
995 MII_TG3_AUXCTL_ACTL_SMDSP_ENA | \
996 MII_TG3_AUXCTL_ACTL_TX_6DB)
997
998#define TG3_PHY_AUXCTL_SMDSP_DISABLE(tp) \
999 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1000 MII_TG3_AUXCTL_ACTL_TX_6DB);
1001
Matt Carlson95e28692008-05-25 23:44:14 -07001002static int tg3_bmcr_reset(struct tg3 *tp)
1003{
1004 u32 phy_control;
1005 int limit, err;
1006
1007 /* OK, reset it, and poll the BMCR_RESET bit until it
1008 * clears or we time out.
1009 */
1010 phy_control = BMCR_RESET;
1011 err = tg3_writephy(tp, MII_BMCR, phy_control);
1012 if (err != 0)
1013 return -EBUSY;
1014
1015 limit = 5000;
1016 while (limit--) {
1017 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1018 if (err != 0)
1019 return -EBUSY;
1020
1021 if ((phy_control & BMCR_RESET) == 0) {
1022 udelay(40);
1023 break;
1024 }
1025 udelay(10);
1026 }
Roel Kluind4675b52009-02-12 16:33:27 -08001027 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001028 return -EBUSY;
1029
1030 return 0;
1031}
1032
Matt Carlson158d7ab2008-05-29 01:37:54 -07001033static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1034{
Francois Romieu3d165432009-01-19 16:56:50 -08001035 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001036 u32 val;
1037
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001038 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001039
1040 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001041 val = -EIO;
1042
1043 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001044
1045 return val;
1046}
1047
1048static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1049{
Francois Romieu3d165432009-01-19 16:56:50 -08001050 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001051 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001052
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001053 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001054
1055 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001056 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001057
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001058 spin_unlock_bh(&tp->lock);
1059
1060 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001061}
1062
1063static int tg3_mdio_reset(struct mii_bus *bp)
1064{
1065 return 0;
1066}
1067
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001068static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001069{
1070 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001071 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001072
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001073 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001074 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001075 case PHY_ID_BCM50610:
1076 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001077 val = MAC_PHYCFG2_50610_LED_MODES;
1078 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001079 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001080 val = MAC_PHYCFG2_AC131_LED_MODES;
1081 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001082 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001083 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1084 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001085 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001086 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1087 break;
1088 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001089 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001090 }
1091
1092 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1093 tw32(MAC_PHYCFG2, val);
1094
1095 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001096 val &= ~(MAC_PHYCFG1_RGMII_INT |
1097 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1098 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001099 tw32(MAC_PHYCFG1, val);
1100
1101 return;
1102 }
1103
Joe Perches63c3a662011-04-26 08:12:10 +00001104 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001105 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1106 MAC_PHYCFG2_FMODE_MASK_MASK |
1107 MAC_PHYCFG2_GMODE_MASK_MASK |
1108 MAC_PHYCFG2_ACT_MASK_MASK |
1109 MAC_PHYCFG2_QUAL_MASK_MASK |
1110 MAC_PHYCFG2_INBAND_ENABLE;
1111
1112 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001113
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001114 val = tr32(MAC_PHYCFG1);
1115 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1116 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001117 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1118 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001119 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001120 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001121 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1122 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001123 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1124 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1125 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001126
Matt Carlsona9daf362008-05-25 23:49:44 -07001127 val = tr32(MAC_EXT_RGMII_MODE);
1128 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1129 MAC_RGMII_MODE_RX_QUALITY |
1130 MAC_RGMII_MODE_RX_ACTIVITY |
1131 MAC_RGMII_MODE_RX_ENG_DET |
1132 MAC_RGMII_MODE_TX_ENABLE |
1133 MAC_RGMII_MODE_TX_LOWPWR |
1134 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001135 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1136 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001137 val |= MAC_RGMII_MODE_RX_INT_B |
1138 MAC_RGMII_MODE_RX_QUALITY |
1139 MAC_RGMII_MODE_RX_ACTIVITY |
1140 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001141 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001142 val |= MAC_RGMII_MODE_TX_ENABLE |
1143 MAC_RGMII_MODE_TX_LOWPWR |
1144 MAC_RGMII_MODE_TX_RESET;
1145 }
1146 tw32(MAC_EXT_RGMII_MODE, val);
1147}
1148
Matt Carlson158d7ab2008-05-29 01:37:54 -07001149static void tg3_mdio_start(struct tg3 *tp)
1150{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001151 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1152 tw32_f(MAC_MI_MODE, tp->mi_mode);
1153 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001154
Joe Perches63c3a662011-04-26 08:12:10 +00001155 if (tg3_flag(tp, MDIOBUS_INITED) &&
Matt Carlson9ea48182010-02-17 15:17:01 +00001156 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1157 tg3_mdio_config_5785(tp);
1158}
1159
1160static int tg3_mdio_init(struct tg3 *tp)
1161{
1162 int i;
1163 u32 reg;
1164 struct phy_device *phydev;
1165
Joe Perches63c3a662011-04-26 08:12:10 +00001166 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001167 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001168
Matt Carlson9c7df912010-06-05 17:24:36 +00001169 tp->phy_addr = PCI_FUNC(tp->pdev->devfn) + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001170
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001171 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1172 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1173 else
1174 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1175 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001176 if (is_serdes)
1177 tp->phy_addr += 7;
1178 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001179 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001180
Matt Carlson158d7ab2008-05-29 01:37:54 -07001181 tg3_mdio_start(tp);
1182
Joe Perches63c3a662011-04-26 08:12:10 +00001183 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001184 return 0;
1185
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001186 tp->mdio_bus = mdiobus_alloc();
1187 if (tp->mdio_bus == NULL)
1188 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001189
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001190 tp->mdio_bus->name = "tg3 mdio bus";
1191 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001192 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001193 tp->mdio_bus->priv = tp;
1194 tp->mdio_bus->parent = &tp->pdev->dev;
1195 tp->mdio_bus->read = &tg3_mdio_read;
1196 tp->mdio_bus->write = &tg3_mdio_write;
1197 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001198 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001199 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001200
1201 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001202 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001203
1204 /* The bus registration will look for all the PHYs on the mdio bus.
1205 * Unfortunately, it does not ensure the PHY is powered up before
1206 * accessing the PHY ID registers. A chip reset is the
1207 * quickest way to bring the device back to an operational state..
1208 */
1209 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1210 tg3_bmcr_reset(tp);
1211
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001212 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001213 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001214 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001215 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001216 return i;
1217 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001218
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001219 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001220
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001221 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001222 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001223 mdiobus_unregister(tp->mdio_bus);
1224 mdiobus_free(tp->mdio_bus);
1225 return -ENODEV;
1226 }
1227
1228 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001229 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001230 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001231 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001232 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001233 case PHY_ID_BCM50610:
1234 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001235 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001236 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001237 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001238 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001239 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001240 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001241 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001242 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001243 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001244 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001245 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001246 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001247 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001248 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001249 case PHY_ID_RTL8201E:
1250 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001251 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001252 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001253 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001254 break;
1255 }
1256
Joe Perches63c3a662011-04-26 08:12:10 +00001257 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001258
1259 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1260 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001261
1262 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001263}
1264
1265static void tg3_mdio_fini(struct tg3 *tp)
1266{
Joe Perches63c3a662011-04-26 08:12:10 +00001267 if (tg3_flag(tp, MDIOBUS_INITED)) {
1268 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001269 mdiobus_unregister(tp->mdio_bus);
1270 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001271 }
1272}
1273
Matt Carlson95e28692008-05-25 23:44:14 -07001274/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001275static inline void tg3_generate_fw_event(struct tg3 *tp)
1276{
1277 u32 val;
1278
1279 val = tr32(GRC_RX_CPU_EVENT);
1280 val |= GRC_RX_CPU_DRIVER_EVENT;
1281 tw32_f(GRC_RX_CPU_EVENT, val);
1282
1283 tp->last_event_jiffies = jiffies;
1284}
1285
1286#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1287
1288/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001289static void tg3_wait_for_event_ack(struct tg3 *tp)
1290{
1291 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001292 unsigned int delay_cnt;
1293 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001294
Matt Carlson4ba526c2008-08-15 14:10:04 -07001295 /* If enough time has passed, no wait is necessary. */
1296 time_remain = (long)(tp->last_event_jiffies + 1 +
1297 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1298 (long)jiffies;
1299 if (time_remain < 0)
1300 return;
1301
1302 /* Check if we can shorten the wait time. */
1303 delay_cnt = jiffies_to_usecs(time_remain);
1304 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1305 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1306 delay_cnt = (delay_cnt >> 3) + 1;
1307
1308 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001309 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1310 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001311 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001312 }
1313}
1314
1315/* tp->lock is held. */
1316static void tg3_ump_link_report(struct tg3 *tp)
1317{
1318 u32 reg;
1319 u32 val;
1320
Joe Perches63c3a662011-04-26 08:12:10 +00001321 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson95e28692008-05-25 23:44:14 -07001322 return;
1323
1324 tg3_wait_for_event_ack(tp);
1325
1326 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1327
1328 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1329
1330 val = 0;
1331 if (!tg3_readphy(tp, MII_BMCR, &reg))
1332 val = reg << 16;
1333 if (!tg3_readphy(tp, MII_BMSR, &reg))
1334 val |= (reg & 0xffff);
1335 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1336
1337 val = 0;
1338 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1339 val = reg << 16;
1340 if (!tg3_readphy(tp, MII_LPA, &reg))
1341 val |= (reg & 0xffff);
1342 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1343
1344 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001345 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001346 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1347 val = reg << 16;
1348 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1349 val |= (reg & 0xffff);
1350 }
1351 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1352
1353 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1354 val = reg << 16;
1355 else
1356 val = 0;
1357 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1358
Matt Carlson4ba526c2008-08-15 14:10:04 -07001359 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001360}
1361
1362static void tg3_link_report(struct tg3 *tp)
1363{
1364 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001365 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001366 tg3_ump_link_report(tp);
1367 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001368 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1369 (tp->link_config.active_speed == SPEED_1000 ?
1370 1000 :
1371 (tp->link_config.active_speed == SPEED_100 ?
1372 100 : 10)),
1373 (tp->link_config.active_duplex == DUPLEX_FULL ?
1374 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001375
Joe Perches05dbe002010-02-17 19:44:19 +00001376 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1377 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1378 "on" : "off",
1379 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1380 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001381
1382 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1383 netdev_info(tp->dev, "EEE is %s\n",
1384 tp->setlpicnt ? "enabled" : "disabled");
1385
Matt Carlson95e28692008-05-25 23:44:14 -07001386 tg3_ump_link_report(tp);
1387 }
1388}
1389
1390static u16 tg3_advert_flowctrl_1000T(u8 flow_ctrl)
1391{
1392 u16 miireg;
1393
Steve Glendinninge18ce342008-12-16 02:00:00 -08001394 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001395 miireg = ADVERTISE_PAUSE_CAP;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001396 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001397 miireg = ADVERTISE_PAUSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001398 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001399 miireg = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1400 else
1401 miireg = 0;
1402
1403 return miireg;
1404}
1405
1406static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1407{
1408 u16 miireg;
1409
Steve Glendinninge18ce342008-12-16 02:00:00 -08001410 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001411 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001412 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001413 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001414 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001415 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1416 else
1417 miireg = 0;
1418
1419 return miireg;
1420}
1421
Matt Carlson95e28692008-05-25 23:44:14 -07001422static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1423{
1424 u8 cap = 0;
1425
1426 if (lcladv & ADVERTISE_1000XPAUSE) {
1427 if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1428 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001429 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001430 else if (rmtadv & LPA_1000XPAUSE_ASYM)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001431 cap = FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001432 } else {
1433 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001434 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001435 }
1436 } else if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1437 if ((rmtadv & LPA_1000XPAUSE) && (rmtadv & LPA_1000XPAUSE_ASYM))
Steve Glendinninge18ce342008-12-16 02:00:00 -08001438 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001439 }
1440
1441 return cap;
1442}
1443
Matt Carlsonf51f3562008-05-25 23:45:08 -07001444static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001445{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001446 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001447 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001448 u32 old_rx_mode = tp->rx_mode;
1449 u32 old_tx_mode = tp->tx_mode;
1450
Joe Perches63c3a662011-04-26 08:12:10 +00001451 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001452 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001453 else
1454 autoneg = tp->link_config.autoneg;
1455
Joe Perches63c3a662011-04-26 08:12:10 +00001456 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001457 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001458 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001459 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001460 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001461 } else
1462 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001463
Matt Carlsonf51f3562008-05-25 23:45:08 -07001464 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001465
Steve Glendinninge18ce342008-12-16 02:00:00 -08001466 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001467 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1468 else
1469 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1470
Matt Carlsonf51f3562008-05-25 23:45:08 -07001471 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001472 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001473
Steve Glendinninge18ce342008-12-16 02:00:00 -08001474 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001475 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1476 else
1477 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1478
Matt Carlsonf51f3562008-05-25 23:45:08 -07001479 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001480 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001481}
1482
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001483static void tg3_adjust_link(struct net_device *dev)
1484{
1485 u8 oldflowctrl, linkmesg = 0;
1486 u32 mac_mode, lcl_adv, rmt_adv;
1487 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001488 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001489
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001490 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001491
1492 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1493 MAC_MODE_HALF_DUPLEX);
1494
1495 oldflowctrl = tp->link_config.active_flowctrl;
1496
1497 if (phydev->link) {
1498 lcl_adv = 0;
1499 rmt_adv = 0;
1500
1501 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1502 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001503 else if (phydev->speed == SPEED_1000 ||
1504 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001505 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001506 else
1507 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001508
1509 if (phydev->duplex == DUPLEX_HALF)
1510 mac_mode |= MAC_MODE_HALF_DUPLEX;
1511 else {
1512 lcl_adv = tg3_advert_flowctrl_1000T(
1513 tp->link_config.flowctrl);
1514
1515 if (phydev->pause)
1516 rmt_adv = LPA_PAUSE_CAP;
1517 if (phydev->asym_pause)
1518 rmt_adv |= LPA_PAUSE_ASYM;
1519 }
1520
1521 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1522 } else
1523 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1524
1525 if (mac_mode != tp->mac_mode) {
1526 tp->mac_mode = mac_mode;
1527 tw32_f(MAC_MODE, tp->mac_mode);
1528 udelay(40);
1529 }
1530
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001531 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1532 if (phydev->speed == SPEED_10)
1533 tw32(MAC_MI_STAT,
1534 MAC_MI_STAT_10MBPS_MODE |
1535 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1536 else
1537 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1538 }
1539
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001540 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1541 tw32(MAC_TX_LENGTHS,
1542 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1543 (6 << TX_LENGTHS_IPG_SHIFT) |
1544 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1545 else
1546 tw32(MAC_TX_LENGTHS,
1547 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1548 (6 << TX_LENGTHS_IPG_SHIFT) |
1549 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1550
1551 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1552 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1553 phydev->speed != tp->link_config.active_speed ||
1554 phydev->duplex != tp->link_config.active_duplex ||
1555 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001556 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001557
1558 tp->link_config.active_speed = phydev->speed;
1559 tp->link_config.active_duplex = phydev->duplex;
1560
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001561 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001562
1563 if (linkmesg)
1564 tg3_link_report(tp);
1565}
1566
1567static int tg3_phy_init(struct tg3 *tp)
1568{
1569 struct phy_device *phydev;
1570
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001571 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001572 return 0;
1573
1574 /* Bring the PHY back to a known state. */
1575 tg3_bmcr_reset(tp);
1576
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001577 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001578
1579 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08001580 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001581 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001582 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001583 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001584 return PTR_ERR(phydev);
1585 }
1586
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001587 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001588 switch (phydev->interface) {
1589 case PHY_INTERFACE_MODE_GMII:
1590 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001591 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08001592 phydev->supported &= (PHY_GBIT_FEATURES |
1593 SUPPORTED_Pause |
1594 SUPPORTED_Asym_Pause);
1595 break;
1596 }
1597 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001598 case PHY_INTERFACE_MODE_MII:
1599 phydev->supported &= (PHY_BASIC_FEATURES |
1600 SUPPORTED_Pause |
1601 SUPPORTED_Asym_Pause);
1602 break;
1603 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001604 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001605 return -EINVAL;
1606 }
1607
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001608 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001609
1610 phydev->advertising = phydev->supported;
1611
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001612 return 0;
1613}
1614
1615static void tg3_phy_start(struct tg3 *tp)
1616{
1617 struct phy_device *phydev;
1618
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001619 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001620 return;
1621
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001622 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001623
Matt Carlson80096062010-08-02 11:26:06 +00001624 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
1625 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001626 phydev->speed = tp->link_config.orig_speed;
1627 phydev->duplex = tp->link_config.orig_duplex;
1628 phydev->autoneg = tp->link_config.orig_autoneg;
1629 phydev->advertising = tp->link_config.orig_advertising;
1630 }
1631
1632 phy_start(phydev);
1633
1634 phy_start_aneg(phydev);
1635}
1636
1637static void tg3_phy_stop(struct tg3 *tp)
1638{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001639 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001640 return;
1641
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001642 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001643}
1644
1645static void tg3_phy_fini(struct tg3 *tp)
1646{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001647 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001648 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001649 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001650 }
1651}
1652
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001653static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1654{
1655 u32 phytest;
1656
1657 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1658 u32 phy;
1659
1660 tg3_writephy(tp, MII_TG3_FET_TEST,
1661 phytest | MII_TG3_FET_SHADOW_EN);
1662 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1663 if (enable)
1664 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1665 else
1666 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1667 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1668 }
1669 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1670 }
1671}
1672
Matt Carlson6833c042008-11-21 17:18:59 -08001673static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1674{
1675 u32 reg;
1676
Joe Perches63c3a662011-04-26 08:12:10 +00001677 if (!tg3_flag(tp, 5705_PLUS) ||
1678 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001679 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001680 return;
1681
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001682 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001683 tg3_phy_fet_toggle_apd(tp, enable);
1684 return;
1685 }
1686
Matt Carlson6833c042008-11-21 17:18:59 -08001687 reg = MII_TG3_MISC_SHDW_WREN |
1688 MII_TG3_MISC_SHDW_SCR5_SEL |
1689 MII_TG3_MISC_SHDW_SCR5_LPED |
1690 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1691 MII_TG3_MISC_SHDW_SCR5_SDTL |
1692 MII_TG3_MISC_SHDW_SCR5_C125OE;
1693 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1694 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1695
1696 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1697
1698
1699 reg = MII_TG3_MISC_SHDW_WREN |
1700 MII_TG3_MISC_SHDW_APD_SEL |
1701 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1702 if (enable)
1703 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1704
1705 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1706}
1707
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001708static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
1709{
1710 u32 phy;
1711
Joe Perches63c3a662011-04-26 08:12:10 +00001712 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001713 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001714 return;
1715
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001716 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001717 u32 ephy;
1718
Matt Carlson535ef6e2009-08-25 10:09:36 +00001719 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
1720 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
1721
1722 tg3_writephy(tp, MII_TG3_FET_TEST,
1723 ephy | MII_TG3_FET_SHADOW_EN);
1724 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001725 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00001726 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001727 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00001728 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1729 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001730 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00001731 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001732 }
1733 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00001734 int ret;
1735
1736 ret = tg3_phy_auxctl_read(tp,
1737 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
1738 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001739 if (enable)
1740 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1741 else
1742 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001743 tg3_phy_auxctl_write(tp,
1744 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001745 }
1746 }
1747}
1748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749static void tg3_phy_set_wirespeed(struct tg3 *tp)
1750{
Matt Carlson15ee95c2011-04-20 07:57:40 +00001751 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 u32 val;
1753
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001754 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 return;
1756
Matt Carlson15ee95c2011-04-20 07:57:40 +00001757 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
1758 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001759 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
1760 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761}
1762
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001763static void tg3_phy_apply_otp(struct tg3 *tp)
1764{
1765 u32 otp, phy;
1766
1767 if (!tp->phy_otp)
1768 return;
1769
1770 otp = tp->phy_otp;
1771
Matt Carlson1d36ba42011-04-20 07:57:42 +00001772 if (TG3_PHY_AUXCTL_SMDSP_ENABLE(tp))
1773 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001774
1775 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
1776 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
1777 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
1778
1779 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
1780 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
1781 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
1782
1783 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
1784 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
1785 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
1786
1787 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
1788 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
1789
1790 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
1791 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
1792
1793 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
1794 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
1795 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
1796
Matt Carlson1d36ba42011-04-20 07:57:42 +00001797 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001798}
1799
Matt Carlson52b02d02010-10-14 10:37:41 +00001800static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
1801{
1802 u32 val;
1803
1804 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
1805 return;
1806
1807 tp->setlpicnt = 0;
1808
1809 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
1810 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00001811 tp->link_config.active_duplex == DUPLEX_FULL &&
1812 (tp->link_config.active_speed == SPEED_100 ||
1813 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00001814 u32 eeectl;
1815
1816 if (tp->link_config.active_speed == SPEED_1000)
1817 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
1818 else
1819 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
1820
1821 tw32(TG3_CPMU_EEE_CTRL, eeectl);
1822
Matt Carlson3110f5f52010-12-06 08:28:50 +00001823 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
1824 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00001825
Matt Carlsonb0c59432011-05-19 12:12:48 +00001826 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
1827 val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
Matt Carlson52b02d02010-10-14 10:37:41 +00001828 tp->setlpicnt = 2;
1829 }
1830
1831 if (!tp->setlpicnt) {
1832 val = tr32(TG3_CPMU_EEE_MODE);
1833 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
1834 }
1835}
1836
Matt Carlsonb0c59432011-05-19 12:12:48 +00001837static void tg3_phy_eee_enable(struct tg3 *tp)
1838{
1839 u32 val;
1840
1841 if (tp->link_config.active_speed == SPEED_1000 &&
1842 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
1843 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
1844 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) &&
1845 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
1846 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0003);
1847 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
1848 }
1849
1850 val = tr32(TG3_CPMU_EEE_MODE);
1851 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
1852}
1853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854static int tg3_wait_macro_done(struct tg3 *tp)
1855{
1856 int limit = 100;
1857
1858 while (limit--) {
1859 u32 tmp32;
1860
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001861 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 if ((tmp32 & 0x1000) == 0)
1863 break;
1864 }
1865 }
Roel Kluind4675b52009-02-12 16:33:27 -08001866 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 return -EBUSY;
1868
1869 return 0;
1870}
1871
1872static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
1873{
1874 static const u32 test_pat[4][6] = {
1875 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
1876 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
1877 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
1878 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
1879 };
1880 int chan;
1881
1882 for (chan = 0; chan < 4; chan++) {
1883 int i;
1884
1885 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1886 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001887 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
1889 for (i = 0; i < 6; i++)
1890 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
1891 test_pat[chan][i]);
1892
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001893 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 if (tg3_wait_macro_done(tp)) {
1895 *resetp = 1;
1896 return -EBUSY;
1897 }
1898
1899 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1900 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001901 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 if (tg3_wait_macro_done(tp)) {
1903 *resetp = 1;
1904 return -EBUSY;
1905 }
1906
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001907 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 if (tg3_wait_macro_done(tp)) {
1909 *resetp = 1;
1910 return -EBUSY;
1911 }
1912
1913 for (i = 0; i < 6; i += 2) {
1914 u32 low, high;
1915
1916 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
1917 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
1918 tg3_wait_macro_done(tp)) {
1919 *resetp = 1;
1920 return -EBUSY;
1921 }
1922 low &= 0x7fff;
1923 high &= 0x000f;
1924 if (low != test_pat[chan][i] ||
1925 high != test_pat[chan][i+1]) {
1926 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
1927 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
1928 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
1929
1930 return -EBUSY;
1931 }
1932 }
1933 }
1934
1935 return 0;
1936}
1937
1938static int tg3_phy_reset_chanpat(struct tg3 *tp)
1939{
1940 int chan;
1941
1942 for (chan = 0; chan < 4; chan++) {
1943 int i;
1944
1945 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1946 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001947 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 for (i = 0; i < 6; i++)
1949 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001950 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 if (tg3_wait_macro_done(tp))
1952 return -EBUSY;
1953 }
1954
1955 return 0;
1956}
1957
1958static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
1959{
1960 u32 reg32, phy9_orig;
1961 int retries, do_phy_reset, err;
1962
1963 retries = 10;
1964 do_phy_reset = 1;
1965 do {
1966 if (do_phy_reset) {
1967 err = tg3_bmcr_reset(tp);
1968 if (err)
1969 return err;
1970 do_phy_reset = 0;
1971 }
1972
1973 /* Disable transmitter and interrupt. */
1974 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
1975 continue;
1976
1977 reg32 |= 0x3000;
1978 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1979
1980 /* Set full-duplex, 1000 mbps. */
1981 tg3_writephy(tp, MII_BMCR,
1982 BMCR_FULLDPLX | TG3_BMCR_SPEED1000);
1983
1984 /* Set to master mode. */
1985 if (tg3_readphy(tp, MII_TG3_CTRL, &phy9_orig))
1986 continue;
1987
1988 tg3_writephy(tp, MII_TG3_CTRL,
1989 (MII_TG3_CTRL_AS_MASTER |
1990 MII_TG3_CTRL_ENABLE_AS_MASTER));
1991
Matt Carlson1d36ba42011-04-20 07:57:42 +00001992 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
1993 if (err)
1994 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
1996 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00001997 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
1999 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2000 if (!err)
2001 break;
2002 } while (--retries);
2003
2004 err = tg3_phy_reset_chanpat(tp);
2005 if (err)
2006 return err;
2007
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002008 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
2010 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002011 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Matt Carlson1d36ba42011-04-20 07:57:42 +00002013 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
2015 tg3_writephy(tp, MII_TG3_CTRL, phy9_orig);
2016
2017 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2018 reg32 &= ~0x3000;
2019 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2020 } else if (!err)
2021 err = -EBUSY;
2022
2023 return err;
2024}
2025
2026/* This will reset the tigon3 PHY if there is no valid
2027 * link unless the FORCE argument is non-zero.
2028 */
2029static int tg3_phy_reset(struct tg3 *tp)
2030{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002031 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 int err;
2033
Michael Chan60189dd2006-12-17 17:08:07 -08002034 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002035 val = tr32(GRC_MISC_CFG);
2036 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2037 udelay(40);
2038 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002039 err = tg3_readphy(tp, MII_BMSR, &val);
2040 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 if (err != 0)
2042 return -EBUSY;
2043
Michael Chanc8e1e822006-04-29 18:55:17 -07002044 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
2045 netif_carrier_off(tp->dev);
2046 tg3_link_report(tp);
2047 }
2048
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2050 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2051 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2052 err = tg3_phy_reset_5703_4_5(tp);
2053 if (err)
2054 return err;
2055 goto out;
2056 }
2057
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002058 cpmuctrl = 0;
2059 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2060 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2061 cpmuctrl = tr32(TG3_CPMU_CTRL);
2062 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2063 tw32(TG3_CPMU_CTRL,
2064 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2065 }
2066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 err = tg3_bmcr_reset(tp);
2068 if (err)
2069 return err;
2070
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002071 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002072 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2073 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002074
2075 tw32(TG3_CPMU_CTRL, cpmuctrl);
2076 }
2077
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002078 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2079 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002080 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2081 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2082 CPMU_LSPD_1000MB_MACCLK_12_5) {
2083 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2084 udelay(40);
2085 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2086 }
2087 }
2088
Joe Perches63c3a662011-04-26 08:12:10 +00002089 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002090 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002091 return 0;
2092
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002093 tg3_phy_apply_otp(tp);
2094
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002095 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002096 tg3_phy_toggle_apd(tp, true);
2097 else
2098 tg3_phy_toggle_apd(tp, false);
2099
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002101 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
2102 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002103 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2104 tg3_phydsp_write(tp, 0x000a, 0x0323);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002105 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
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_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002109 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2110 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002112
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002113 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002114 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2115 tg3_phydsp_write(tp, 0x000a, 0x310b);
2116 tg3_phydsp_write(tp, 0x201f, 0x9506);
2117 tg3_phydsp_write(tp, 0x401f, 0x14e2);
2118 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2119 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002120 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002121 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2122 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2123 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2124 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2125 tg3_writephy(tp, MII_TG3_TEST1,
2126 MII_TG3_TEST1_TRIM_EN | 0x4);
2127 } else
2128 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2129
2130 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2131 }
Michael Chanc424cb22006-04-29 18:56:34 -07002132 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002133
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 /* Set Extended packet length bit (bit 14) on all chips that */
2135 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002136 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002138 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002139 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002141 err = tg3_phy_auxctl_read(tp,
2142 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2143 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002144 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2145 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 }
2147
2148 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2149 * jumbo frames transmission.
2150 */
Joe Perches63c3a662011-04-26 08:12:10 +00002151 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002152 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002153 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002154 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 }
2156
Michael Chan715116a2006-09-27 16:09:25 -07002157 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002158 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002159 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002160 }
2161
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002162 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 tg3_phy_set_wirespeed(tp);
2164 return 0;
2165}
2166
2167static void tg3_frob_aux_power(struct tg3 *tp)
2168{
Matt Carlson683644b2011-03-09 16:58:23 +00002169 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
Matt Carlson334355a2010-01-20 16:58:10 +00002171 /* The GPIOs do something completely different on 57765. */
Joe Perches63c3a662011-04-26 08:12:10 +00002172 if (!tg3_flag(tp, IS_NIC) ||
Matt Carlsona50d0792010-06-05 17:24:37 +00002173 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson334355a2010-01-20 16:58:10 +00002174 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 return;
2176
Matt Carlson683644b2011-03-09 16:58:23 +00002177 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2178 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +00002179 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2180 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) &&
Matt Carlson683644b2011-03-09 16:58:23 +00002181 tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002182 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002184 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002185
Michael Chanbc1c7562006-03-20 17:48:03 -08002186 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002187 if (dev_peer) {
2188 struct tg3 *tp_peer = netdev_priv(dev_peer);
2189
Joe Perches63c3a662011-04-26 08:12:10 +00002190 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002191 return;
2192
Joe Perches63c3a662011-04-26 08:12:10 +00002193 if (tg3_flag(tp_peer, WOL_ENABLE) ||
2194 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002195 need_vaux = true;
2196 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Joe Perches63c3a662011-04-26 08:12:10 +00002199 if (tg3_flag(tp, WOL_ENABLE) || tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002200 need_vaux = true;
2201
2202 if (need_vaux) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2204 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002205 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2206 (GRC_LCLCTRL_GPIO_OE0 |
2207 GRC_LCLCTRL_GPIO_OE1 |
2208 GRC_LCLCTRL_GPIO_OE2 |
2209 GRC_LCLCTRL_GPIO_OUTPUT0 |
2210 GRC_LCLCTRL_GPIO_OUTPUT1),
2211 100);
Matt Carlson8d519ab2009-04-20 06:58:01 +00002212 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2213 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -07002214 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2215 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2216 GRC_LCLCTRL_GPIO_OE1 |
2217 GRC_LCLCTRL_GPIO_OE2 |
2218 GRC_LCLCTRL_GPIO_OUTPUT0 |
2219 GRC_LCLCTRL_GPIO_OUTPUT1 |
2220 tp->grc_local_ctrl;
2221 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2222
2223 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2224 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2225
2226 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2227 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 } else {
2229 u32 no_gpio2;
Michael Chandc56b7d2005-12-19 16:26:28 -08002230 u32 grc_local_ctrl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
Michael Chandc56b7d2005-12-19 16:26:28 -08002232 /* Workaround to prevent overdrawing Amps. */
2233 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2234 ASIC_REV_5714) {
2235 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chanb401e9e2005-12-19 16:27:04 -08002236 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2237 grc_local_ctrl, 100);
Michael Chandc56b7d2005-12-19 16:26:28 -08002238 }
2239
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 /* On 5753 and variants, GPIO2 cannot be used. */
2241 no_gpio2 = tp->nic_sram_data_cfg &
2242 NIC_SRAM_DATA_CFG_NO_GPIO2;
2243
Michael Chandc56b7d2005-12-19 16:26:28 -08002244 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 GRC_LCLCTRL_GPIO_OE1 |
2246 GRC_LCLCTRL_GPIO_OE2 |
2247 GRC_LCLCTRL_GPIO_OUTPUT1 |
2248 GRC_LCLCTRL_GPIO_OUTPUT2;
2249 if (no_gpio2) {
2250 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2251 GRC_LCLCTRL_GPIO_OUTPUT2);
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 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2257
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 if (!no_gpio2) {
2262 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chanb401e9e2005-12-19 16:27:04 -08002263 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2264 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 }
2266 }
2267 } else {
2268 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
2269 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002270 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2271 (GRC_LCLCTRL_GPIO_OE1 |
2272 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
Michael Chanb401e9e2005-12-19 16:27:04 -08002274 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2275 GRC_LCLCTRL_GPIO_OE1, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
Michael Chanb401e9e2005-12-19 16:27:04 -08002277 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2278 (GRC_LCLCTRL_GPIO_OE1 |
2279 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 }
2281 }
2282}
2283
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002284static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2285{
2286 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2287 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002288 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002289 if (speed != SPEED_10)
2290 return 1;
2291 } else if (speed == SPEED_10)
2292 return 1;
2293
2294 return 0;
2295}
2296
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297static int tg3_setup_phy(struct tg3 *, int);
2298
2299#define RESET_KIND_SHUTDOWN 0
2300#define RESET_KIND_INIT 1
2301#define RESET_KIND_SUSPEND 2
2302
2303static void tg3_write_sig_post_reset(struct tg3 *, int);
2304static int tg3_halt_cpu(struct tg3 *, u32);
2305
Matt Carlson0a459aa2008-11-03 16:54:15 -08002306static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002307{
Matt Carlsonce057f02007-11-12 21:08:03 -08002308 u32 val;
2309
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002310 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002311 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2312 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2313 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2314
2315 sg_dig_ctrl |=
2316 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2317 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2318 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2319 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002320 return;
Michael Chan51297242007-02-13 12:17:57 -08002321 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002322
Michael Chan60189dd2006-12-17 17:08:07 -08002323 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002324 tg3_bmcr_reset(tp);
2325 val = tr32(GRC_MISC_CFG);
2326 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2327 udelay(40);
2328 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002329 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002330 u32 phytest;
2331 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2332 u32 phy;
2333
2334 tg3_writephy(tp, MII_ADVERTISE, 0);
2335 tg3_writephy(tp, MII_BMCR,
2336 BMCR_ANENABLE | BMCR_ANRESTART);
2337
2338 tg3_writephy(tp, MII_TG3_FET_TEST,
2339 phytest | MII_TG3_FET_SHADOW_EN);
2340 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2341 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2342 tg3_writephy(tp,
2343 MII_TG3_FET_SHDW_AUXMODE4,
2344 phy);
2345 }
2346 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2347 }
2348 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002349 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002350 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2351 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002352
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002353 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2354 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2355 MII_TG3_AUXCTL_PCTL_VREG_11V;
2356 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07002357 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002358
Michael Chan15c3b692006-03-22 01:06:52 -08002359 /* The PHY should not be powered down on some chips because
2360 * of bugs.
2361 */
2362 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2363 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2364 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002365 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Michael Chan15c3b692006-03-22 01:06:52 -08002366 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002367
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002368 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2369 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002370 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2371 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2372 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2373 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2374 }
2375
Michael Chan15c3b692006-03-22 01:06:52 -08002376 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2377}
2378
Matt Carlson3f007892008-11-03 16:51:36 -08002379/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002380static int tg3_nvram_lock(struct tg3 *tp)
2381{
Joe Perches63c3a662011-04-26 08:12:10 +00002382 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002383 int i;
2384
2385 if (tp->nvram_lock_cnt == 0) {
2386 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2387 for (i = 0; i < 8000; i++) {
2388 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2389 break;
2390 udelay(20);
2391 }
2392 if (i == 8000) {
2393 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2394 return -ENODEV;
2395 }
2396 }
2397 tp->nvram_lock_cnt++;
2398 }
2399 return 0;
2400}
2401
2402/* tp->lock is held. */
2403static void tg3_nvram_unlock(struct tg3 *tp)
2404{
Joe Perches63c3a662011-04-26 08:12:10 +00002405 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002406 if (tp->nvram_lock_cnt > 0)
2407 tp->nvram_lock_cnt--;
2408 if (tp->nvram_lock_cnt == 0)
2409 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2410 }
2411}
2412
2413/* tp->lock is held. */
2414static void tg3_enable_nvram_access(struct tg3 *tp)
2415{
Joe Perches63c3a662011-04-26 08:12:10 +00002416 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002417 u32 nvaccess = tr32(NVRAM_ACCESS);
2418
2419 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2420 }
2421}
2422
2423/* tp->lock is held. */
2424static void tg3_disable_nvram_access(struct tg3 *tp)
2425{
Joe Perches63c3a662011-04-26 08:12:10 +00002426 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002427 u32 nvaccess = tr32(NVRAM_ACCESS);
2428
2429 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2430 }
2431}
2432
2433static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2434 u32 offset, u32 *val)
2435{
2436 u32 tmp;
2437 int i;
2438
2439 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2440 return -EINVAL;
2441
2442 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2443 EEPROM_ADDR_DEVID_MASK |
2444 EEPROM_ADDR_READ);
2445 tw32(GRC_EEPROM_ADDR,
2446 tmp |
2447 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2448 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2449 EEPROM_ADDR_ADDR_MASK) |
2450 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2451
2452 for (i = 0; i < 1000; i++) {
2453 tmp = tr32(GRC_EEPROM_ADDR);
2454
2455 if (tmp & EEPROM_ADDR_COMPLETE)
2456 break;
2457 msleep(1);
2458 }
2459 if (!(tmp & EEPROM_ADDR_COMPLETE))
2460 return -EBUSY;
2461
Matt Carlson62cedd12009-04-20 14:52:29 -07002462 tmp = tr32(GRC_EEPROM_DATA);
2463
2464 /*
2465 * The data will always be opposite the native endian
2466 * format. Perform a blind byteswap to compensate.
2467 */
2468 *val = swab32(tmp);
2469
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002470 return 0;
2471}
2472
2473#define NVRAM_CMD_TIMEOUT 10000
2474
2475static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2476{
2477 int i;
2478
2479 tw32(NVRAM_CMD, nvram_cmd);
2480 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2481 udelay(10);
2482 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2483 udelay(10);
2484 break;
2485 }
2486 }
2487
2488 if (i == NVRAM_CMD_TIMEOUT)
2489 return -EBUSY;
2490
2491 return 0;
2492}
2493
2494static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2495{
Joe Perches63c3a662011-04-26 08:12:10 +00002496 if (tg3_flag(tp, NVRAM) &&
2497 tg3_flag(tp, NVRAM_BUFFERED) &&
2498 tg3_flag(tp, FLASH) &&
2499 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002500 (tp->nvram_jedecnum == JEDEC_ATMEL))
2501
2502 addr = ((addr / tp->nvram_pagesize) <<
2503 ATMEL_AT45DB0X1B_PAGE_POS) +
2504 (addr % tp->nvram_pagesize);
2505
2506 return addr;
2507}
2508
2509static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2510{
Joe Perches63c3a662011-04-26 08:12:10 +00002511 if (tg3_flag(tp, NVRAM) &&
2512 tg3_flag(tp, NVRAM_BUFFERED) &&
2513 tg3_flag(tp, FLASH) &&
2514 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002515 (tp->nvram_jedecnum == JEDEC_ATMEL))
2516
2517 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2518 tp->nvram_pagesize) +
2519 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2520
2521 return addr;
2522}
2523
Matt Carlsone4f34112009-02-25 14:25:00 +00002524/* NOTE: Data read in from NVRAM is byteswapped according to
2525 * the byteswapping settings for all other register accesses.
2526 * tg3 devices are BE devices, so on a BE machine, the data
2527 * returned will be exactly as it is seen in NVRAM. On a LE
2528 * machine, the 32-bit value will be byteswapped.
2529 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002530static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2531{
2532 int ret;
2533
Joe Perches63c3a662011-04-26 08:12:10 +00002534 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002535 return tg3_nvram_read_using_eeprom(tp, offset, val);
2536
2537 offset = tg3_nvram_phys_addr(tp, offset);
2538
2539 if (offset > NVRAM_ADDR_MSK)
2540 return -EINVAL;
2541
2542 ret = tg3_nvram_lock(tp);
2543 if (ret)
2544 return ret;
2545
2546 tg3_enable_nvram_access(tp);
2547
2548 tw32(NVRAM_ADDR, offset);
2549 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2550 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2551
2552 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002553 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002554
2555 tg3_disable_nvram_access(tp);
2556
2557 tg3_nvram_unlock(tp);
2558
2559 return ret;
2560}
2561
Matt Carlsona9dc5292009-02-25 14:25:30 +00002562/* Ensures NVRAM data is in bytestream format. */
2563static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002564{
2565 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002566 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002567 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002568 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002569 return res;
2570}
2571
2572/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08002573static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2574{
2575 u32 addr_high, addr_low;
2576 int i;
2577
2578 addr_high = ((tp->dev->dev_addr[0] << 8) |
2579 tp->dev->dev_addr[1]);
2580 addr_low = ((tp->dev->dev_addr[2] << 24) |
2581 (tp->dev->dev_addr[3] << 16) |
2582 (tp->dev->dev_addr[4] << 8) |
2583 (tp->dev->dev_addr[5] << 0));
2584 for (i = 0; i < 4; i++) {
2585 if (i == 1 && skip_mac_1)
2586 continue;
2587 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2588 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2589 }
2590
2591 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2592 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2593 for (i = 0; i < 12; i++) {
2594 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2595 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2596 }
2597 }
2598
2599 addr_high = (tp->dev->dev_addr[0] +
2600 tp->dev->dev_addr[1] +
2601 tp->dev->dev_addr[2] +
2602 tp->dev->dev_addr[3] +
2603 tp->dev->dev_addr[4] +
2604 tp->dev->dev_addr[5]) &
2605 TX_BACKOFF_SEED_MASK;
2606 tw32(MAC_TX_BACKOFF_SEED, addr_high);
2607}
2608
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002609static void tg3_enable_register_access(struct tg3 *tp)
2610{
2611 /*
2612 * Make sure register accesses (indirect or otherwise) will function
2613 * correctly.
2614 */
2615 pci_write_config_dword(tp->pdev,
2616 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
2617}
2618
2619static int tg3_power_up(struct tg3 *tp)
2620{
2621 tg3_enable_register_access(tp);
2622
2623 pci_set_power_state(tp->pdev, PCI_D0);
2624
2625 /* Switch out of Vaux if it is a NIC */
Joe Perches63c3a662011-04-26 08:12:10 +00002626 if (tg3_flag(tp, IS_NIC))
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002627 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl, 100);
2628
2629 return 0;
2630}
2631
2632static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633{
2634 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002635 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002637 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002638
2639 /* Restore the CLKREQ setting. */
Joe Perches63c3a662011-04-26 08:12:10 +00002640 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002641 u16 lnkctl;
2642
2643 pci_read_config_word(tp->pdev,
2644 tp->pcie_cap + PCI_EXP_LNKCTL,
2645 &lnkctl);
2646 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2647 pci_write_config_word(tp->pdev,
2648 tp->pcie_cap + PCI_EXP_LNKCTL,
2649 lnkctl);
2650 }
2651
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2653 tw32(TG3PCI_MISC_HOST_CTRL,
2654 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2655
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002656 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00002657 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002658
Joe Perches63c3a662011-04-26 08:12:10 +00002659 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002660 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002661 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00002662 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002663 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002664 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002665
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002666 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002667
Matt Carlson80096062010-08-02 11:26:06 +00002668 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002669
2670 tp->link_config.orig_speed = phydev->speed;
2671 tp->link_config.orig_duplex = phydev->duplex;
2672 tp->link_config.orig_autoneg = phydev->autoneg;
2673 tp->link_config.orig_advertising = phydev->advertising;
2674
2675 advertising = ADVERTISED_TP |
2676 ADVERTISED_Pause |
2677 ADVERTISED_Autoneg |
2678 ADVERTISED_10baseT_Half;
2679
Joe Perches63c3a662011-04-26 08:12:10 +00002680 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
2681 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002682 advertising |=
2683 ADVERTISED_100baseT_Half |
2684 ADVERTISED_100baseT_Full |
2685 ADVERTISED_10baseT_Full;
2686 else
2687 advertising |= ADVERTISED_10baseT_Full;
2688 }
2689
2690 phydev->advertising = advertising;
2691
2692 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002693
2694 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00002695 if (phyid != PHY_ID_BCMAC131) {
2696 phyid &= PHY_BCM_OUI_MASK;
2697 if (phyid == PHY_BCM_OUI_1 ||
2698 phyid == PHY_BCM_OUI_2 ||
2699 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08002700 do_low_power = true;
2701 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002702 }
Matt Carlsondd477002008-05-25 23:45:58 -07002703 } else {
Matt Carlson20232762008-12-21 20:18:56 -08002704 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002705
Matt Carlson80096062010-08-02 11:26:06 +00002706 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
2707 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07002708 tp->link_config.orig_speed = tp->link_config.speed;
2709 tp->link_config.orig_duplex = tp->link_config.duplex;
2710 tp->link_config.orig_autoneg = tp->link_config.autoneg;
2711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002713 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Matt Carlsondd477002008-05-25 23:45:58 -07002714 tp->link_config.speed = SPEED_10;
2715 tp->link_config.duplex = DUPLEX_HALF;
2716 tp->link_config.autoneg = AUTONEG_ENABLE;
2717 tg3_setup_phy(tp, 0);
2718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 }
2720
Michael Chanb5d37722006-09-27 16:06:21 -07002721 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2722 u32 val;
2723
2724 val = tr32(GRC_VCPU_EXT_CTRL);
2725 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00002726 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08002727 int i;
2728 u32 val;
2729
2730 for (i = 0; i < 200; i++) {
2731 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2732 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2733 break;
2734 msleep(1);
2735 }
2736 }
Joe Perches63c3a662011-04-26 08:12:10 +00002737 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07002738 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2739 WOL_DRV_STATE_SHUTDOWN |
2740 WOL_DRV_WOL |
2741 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08002742
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002743 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 u32 mac_mode;
2745
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002746 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002747 if (do_low_power &&
2748 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
2749 tg3_phy_auxctl_write(tp,
2750 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
2751 MII_TG3_AUXCTL_PCTL_WOL_EN |
2752 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2753 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07002754 udelay(40);
2755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002757 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07002758 mac_mode = MAC_MODE_PORT_MODE_GMII;
2759 else
2760 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002762 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2763 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2764 ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00002765 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002766 SPEED_100 : SPEED_10;
2767 if (tg3_5700_link_polarity(tp, speed))
2768 mac_mode |= MAC_MODE_LINK_POLARITY;
2769 else
2770 mac_mode &= ~MAC_MODE_LINK_POLARITY;
2771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 } else {
2773 mac_mode = MAC_MODE_PORT_MODE_TBI;
2774 }
2775
Joe Perches63c3a662011-04-26 08:12:10 +00002776 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 tw32(MAC_LED_CTRL, tp->led_ctrl);
2778
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002779 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00002780 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
2781 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002782 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
Joe Perches63c3a662011-04-26 08:12:10 +00002784 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00002785 mac_mode |= MAC_MODE_APE_TX_EN |
2786 MAC_MODE_APE_RX_EN |
2787 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07002788
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 tw32_f(MAC_MODE, mac_mode);
2790 udelay(100);
2791
2792 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2793 udelay(10);
2794 }
2795
Joe Perches63c3a662011-04-26 08:12:10 +00002796 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2798 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2799 u32 base_val;
2800
2801 base_val = tp->pci_clock_ctrl;
2802 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2803 CLOCK_CTRL_TXCLK_DISABLE);
2804
Michael Chanb401e9e2005-12-19 16:27:04 -08002805 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
2806 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00002807 } else if (tg3_flag(tp, 5780_CLASS) ||
2808 tg3_flag(tp, CPMU_PRESENT) ||
Michael Chand7b0a852007-02-13 12:17:38 -08002809 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)) {
Michael Chan4cf78e42005-07-25 12:29:19 -07002810 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00002811 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 u32 newbits1, newbits2;
2813
2814 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2815 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2816 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
2817 CLOCK_CTRL_TXCLK_DISABLE |
2818 CLOCK_CTRL_ALTCLK);
2819 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00002820 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 newbits1 = CLOCK_CTRL_625_CORE;
2822 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
2823 } else {
2824 newbits1 = CLOCK_CTRL_ALTCLK;
2825 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2826 }
2827
Michael Chanb401e9e2005-12-19 16:27:04 -08002828 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
2829 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
Michael Chanb401e9e2005-12-19 16:27:04 -08002831 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
2832 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
Joe Perches63c3a662011-04-26 08:12:10 +00002834 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 u32 newbits3;
2836
2837 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2838 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2839 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
2840 CLOCK_CTRL_TXCLK_DISABLE |
2841 CLOCK_CTRL_44MHZ_CORE);
2842 } else {
2843 newbits3 = CLOCK_CTRL_44MHZ_CORE;
2844 }
2845
Michael Chanb401e9e2005-12-19 16:27:04 -08002846 tw32_wait_f(TG3PCI_CLOCK_CTRL,
2847 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 }
2849 }
2850
Joe Perches63c3a662011-04-26 08:12:10 +00002851 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08002852 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08002853
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 tg3_frob_aux_power(tp);
2855
2856 /* Workaround for unstable PLL clock */
2857 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
2858 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
2859 u32 val = tr32(0x7d00);
2860
2861 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
2862 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00002863 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08002864 int err;
2865
2866 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08002868 if (!err)
2869 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08002870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 }
2872
Michael Chanbbadf502006-04-06 21:46:34 -07002873 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
2874
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 return 0;
2876}
2877
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002878static void tg3_power_down(struct tg3 *tp)
2879{
2880 tg3_power_down_prepare(tp);
2881
Joe Perches63c3a662011-04-26 08:12:10 +00002882 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002883 pci_set_power_state(tp->pdev, PCI_D3hot);
2884}
2885
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
2887{
2888 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
2889 case MII_TG3_AUX_STAT_10HALF:
2890 *speed = SPEED_10;
2891 *duplex = DUPLEX_HALF;
2892 break;
2893
2894 case MII_TG3_AUX_STAT_10FULL:
2895 *speed = SPEED_10;
2896 *duplex = DUPLEX_FULL;
2897 break;
2898
2899 case MII_TG3_AUX_STAT_100HALF:
2900 *speed = SPEED_100;
2901 *duplex = DUPLEX_HALF;
2902 break;
2903
2904 case MII_TG3_AUX_STAT_100FULL:
2905 *speed = SPEED_100;
2906 *duplex = DUPLEX_FULL;
2907 break;
2908
2909 case MII_TG3_AUX_STAT_1000HALF:
2910 *speed = SPEED_1000;
2911 *duplex = DUPLEX_HALF;
2912 break;
2913
2914 case MII_TG3_AUX_STAT_1000FULL:
2915 *speed = SPEED_1000;
2916 *duplex = DUPLEX_FULL;
2917 break;
2918
2919 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002920 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07002921 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
2922 SPEED_10;
2923 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
2924 DUPLEX_HALF;
2925 break;
2926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 *speed = SPEED_INVALID;
2928 *duplex = DUPLEX_INVALID;
2929 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931}
2932
Matt Carlson42b64a42011-05-19 12:12:49 +00002933static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934{
Matt Carlson42b64a42011-05-19 12:12:49 +00002935 int err = 0;
2936 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
Matt Carlson42b64a42011-05-19 12:12:49 +00002938 new_adv = ADVERTISE_CSMA;
2939 if (advertise & ADVERTISED_10baseT_Half)
2940 new_adv |= ADVERTISE_10HALF;
2941 if (advertise & ADVERTISED_10baseT_Full)
2942 new_adv |= ADVERTISE_10FULL;
2943 if (advertise & ADVERTISED_100baseT_Half)
2944 new_adv |= ADVERTISE_100HALF;
2945 if (advertise & ADVERTISED_100baseT_Full)
2946 new_adv |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
Matt Carlson42b64a42011-05-19 12:12:49 +00002948 new_adv |= tg3_advert_flowctrl_1000T(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949
Matt Carlson42b64a42011-05-19 12:12:49 +00002950 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
2951 if (err)
2952 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
Matt Carlson42b64a42011-05-19 12:12:49 +00002954 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
2955 goto done;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002956
Matt Carlson42b64a42011-05-19 12:12:49 +00002957 new_adv = 0;
2958 if (advertise & ADVERTISED_1000baseT_Half)
2959 new_adv |= MII_TG3_CTRL_ADV_1000_HALF;
2960 if (advertise & ADVERTISED_1000baseT_Full)
2961 new_adv |= MII_TG3_CTRL_ADV_1000_FULL;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002962
Matt Carlson42b64a42011-05-19 12:12:49 +00002963 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2964 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
2965 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2966 MII_TG3_CTRL_ENABLE_AS_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
Matt Carlson42b64a42011-05-19 12:12:49 +00002968 err = tg3_writephy(tp, MII_TG3_CTRL, new_adv);
2969 if (err)
2970 goto done;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002971
Matt Carlson42b64a42011-05-19 12:12:49 +00002972 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2973 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
Matt Carlson42b64a42011-05-19 12:12:49 +00002975 tw32(TG3_CPMU_EEE_MODE,
2976 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002977
Matt Carlson42b64a42011-05-19 12:12:49 +00002978 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
2979 if (!err) {
2980 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00002981
Matt Carlson21a00ab2011-01-25 15:58:55 +00002982 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
2983 case ASIC_REV_5717:
2984 case ASIC_REV_57765:
2985 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
2986 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
2987 MII_TG3_DSP_CH34TP2_HIBW01);
2988 /* Fall through */
2989 case ASIC_REV_5719:
2990 val = MII_TG3_DSP_TAP26_ALNOKO |
2991 MII_TG3_DSP_TAP26_RMRXSTO |
2992 MII_TG3_DSP_TAP26_OPCSINPT;
2993 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
2994 }
Matt Carlson52b02d02010-10-14 10:37:41 +00002995
Matt Carlsona6b68da2010-12-06 08:28:52 +00002996 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00002997 /* Advertise 100-BaseTX EEE ability */
2998 if (advertise & ADVERTISED_100baseT_Full)
2999 val |= MDIO_AN_EEE_ADV_100TX;
3000 /* Advertise 1000-BaseT EEE ability */
3001 if (advertise & ADVERTISED_1000baseT_Full)
3002 val |= MDIO_AN_EEE_ADV_1000T;
3003 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlson52b02d02010-10-14 10:37:41 +00003004
Matt Carlson42b64a42011-05-19 12:12:49 +00003005 err2 = TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
3006 if (!err)
3007 err = err2;
3008 }
3009
3010done:
3011 return err;
3012}
3013
3014static void tg3_phy_copper_begin(struct tg3 *tp)
3015{
3016 u32 new_adv;
3017 int i;
3018
3019 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
3020 new_adv = ADVERTISED_10baseT_Half |
3021 ADVERTISED_10baseT_Full;
3022 if (tg3_flag(tp, WOL_SPEED_100MB))
3023 new_adv |= ADVERTISED_100baseT_Half |
3024 ADVERTISED_100baseT_Full;
3025
3026 tg3_phy_autoneg_cfg(tp, new_adv,
3027 FLOW_CTRL_TX | FLOW_CTRL_RX);
3028 } else if (tp->link_config.speed == SPEED_INVALID) {
3029 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
3030 tp->link_config.advertising &=
3031 ~(ADVERTISED_1000baseT_Half |
3032 ADVERTISED_1000baseT_Full);
3033
3034 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
3035 tp->link_config.flowctrl);
3036 } else {
3037 /* Asking for a specific link mode. */
3038 if (tp->link_config.speed == SPEED_1000) {
3039 if (tp->link_config.duplex == DUPLEX_FULL)
3040 new_adv = ADVERTISED_1000baseT_Full;
3041 else
3042 new_adv = ADVERTISED_1000baseT_Half;
3043 } else if (tp->link_config.speed == SPEED_100) {
3044 if (tp->link_config.duplex == DUPLEX_FULL)
3045 new_adv = ADVERTISED_100baseT_Full;
3046 else
3047 new_adv = ADVERTISED_100baseT_Half;
3048 } else {
3049 if (tp->link_config.duplex == DUPLEX_FULL)
3050 new_adv = ADVERTISED_10baseT_Full;
3051 else
3052 new_adv = ADVERTISED_10baseT_Half;
3053 }
3054
3055 tg3_phy_autoneg_cfg(tp, new_adv,
3056 tp->link_config.flowctrl);
Matt Carlson52b02d02010-10-14 10:37:41 +00003057 }
3058
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
3060 tp->link_config.speed != SPEED_INVALID) {
3061 u32 bmcr, orig_bmcr;
3062
3063 tp->link_config.active_speed = tp->link_config.speed;
3064 tp->link_config.active_duplex = tp->link_config.duplex;
3065
3066 bmcr = 0;
3067 switch (tp->link_config.speed) {
3068 default:
3069 case SPEED_10:
3070 break;
3071
3072 case SPEED_100:
3073 bmcr |= BMCR_SPEED100;
3074 break;
3075
3076 case SPEED_1000:
3077 bmcr |= TG3_BMCR_SPEED1000;
3078 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080
3081 if (tp->link_config.duplex == DUPLEX_FULL)
3082 bmcr |= BMCR_FULLDPLX;
3083
3084 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3085 (bmcr != orig_bmcr)) {
3086 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3087 for (i = 0; i < 1500; i++) {
3088 u32 tmp;
3089
3090 udelay(10);
3091 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3092 tg3_readphy(tp, MII_BMSR, &tmp))
3093 continue;
3094 if (!(tmp & BMSR_LSTATUS)) {
3095 udelay(40);
3096 break;
3097 }
3098 }
3099 tg3_writephy(tp, MII_BMCR, bmcr);
3100 udelay(40);
3101 }
3102 } else {
3103 tg3_writephy(tp, MII_BMCR,
3104 BMCR_ANENABLE | BMCR_ANRESTART);
3105 }
3106}
3107
3108static int tg3_init_5401phy_dsp(struct tg3 *tp)
3109{
3110 int err;
3111
3112 /* Turn off tap power management. */
3113 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003114 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003116 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3117 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3118 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3119 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3120 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121
3122 udelay(40);
3123
3124 return err;
3125}
3126
Michael Chan3600d912006-12-07 00:21:48 -08003127static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128{
Michael Chan3600d912006-12-07 00:21:48 -08003129 u32 adv_reg, all_mask = 0;
3130
3131 if (mask & ADVERTISED_10baseT_Half)
3132 all_mask |= ADVERTISE_10HALF;
3133 if (mask & ADVERTISED_10baseT_Full)
3134 all_mask |= ADVERTISE_10FULL;
3135 if (mask & ADVERTISED_100baseT_Half)
3136 all_mask |= ADVERTISE_100HALF;
3137 if (mask & ADVERTISED_100baseT_Full)
3138 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139
3140 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
3141 return 0;
3142
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 if ((adv_reg & all_mask) != all_mask)
3144 return 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003145 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 u32 tg3_ctrl;
3147
Michael Chan3600d912006-12-07 00:21:48 -08003148 all_mask = 0;
3149 if (mask & ADVERTISED_1000baseT_Half)
3150 all_mask |= ADVERTISE_1000HALF;
3151 if (mask & ADVERTISED_1000baseT_Full)
3152 all_mask |= ADVERTISE_1000FULL;
3153
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 if (tg3_readphy(tp, MII_TG3_CTRL, &tg3_ctrl))
3155 return 0;
3156
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 if ((tg3_ctrl & all_mask) != all_mask)
3158 return 0;
3159 }
3160 return 1;
3161}
3162
Matt Carlsonef167e22007-12-20 20:10:01 -08003163static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
3164{
3165 u32 curadv, reqadv;
3166
3167 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3168 return 1;
3169
3170 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3171 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3172
3173 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3174 if (curadv != reqadv)
3175 return 0;
3176
Joe Perches63c3a662011-04-26 08:12:10 +00003177 if (tg3_flag(tp, PAUSE_AUTONEG))
Matt Carlsonef167e22007-12-20 20:10:01 -08003178 tg3_readphy(tp, MII_LPA, rmtadv);
3179 } else {
3180 /* Reprogram the advertisement register, even if it
3181 * does not affect the current link. If the link
3182 * gets renegotiated in the future, we can save an
3183 * additional renegotiation cycle by advertising
3184 * it correctly in the first place.
3185 */
3186 if (curadv != reqadv) {
3187 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3188 ADVERTISE_PAUSE_ASYM);
3189 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3190 }
3191 }
3192
3193 return 1;
3194}
3195
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3197{
3198 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003199 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08003200 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 u16 current_speed;
3202 u8 current_duplex;
3203 int i, err;
3204
3205 tw32(MAC_EVENT, 0);
3206
3207 tw32_f(MAC_STATUS,
3208 (MAC_STATUS_SYNC_CHANGED |
3209 MAC_STATUS_CFG_CHANGED |
3210 MAC_STATUS_MI_COMPLETION |
3211 MAC_STATUS_LNKSTATE_CHANGED));
3212 udelay(40);
3213
Matt Carlson8ef21422008-05-02 16:47:53 -07003214 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3215 tw32_f(MAC_MI_MODE,
3216 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3217 udelay(80);
3218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003220 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
3222 /* Some third-party PHYs need to be reset on link going
3223 * down.
3224 */
3225 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3226 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3227 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3228 netif_carrier_ok(tp->dev)) {
3229 tg3_readphy(tp, MII_BMSR, &bmsr);
3230 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3231 !(bmsr & BMSR_LSTATUS))
3232 force_reset = 1;
3233 }
3234 if (force_reset)
3235 tg3_phy_reset(tp);
3236
Matt Carlson79eb6902010-02-17 15:17:03 +00003237 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 tg3_readphy(tp, MII_BMSR, &bmsr);
3239 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00003240 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 bmsr = 0;
3242
3243 if (!(bmsr & BMSR_LSTATUS)) {
3244 err = tg3_init_5401phy_dsp(tp);
3245 if (err)
3246 return err;
3247
3248 tg3_readphy(tp, MII_BMSR, &bmsr);
3249 for (i = 0; i < 1000; i++) {
3250 udelay(10);
3251 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3252 (bmsr & BMSR_LSTATUS)) {
3253 udelay(40);
3254 break;
3255 }
3256 }
3257
Matt Carlson79eb6902010-02-17 15:17:03 +00003258 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3259 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 !(bmsr & BMSR_LSTATUS) &&
3261 tp->link_config.active_speed == SPEED_1000) {
3262 err = tg3_phy_reset(tp);
3263 if (!err)
3264 err = tg3_init_5401phy_dsp(tp);
3265 if (err)
3266 return err;
3267 }
3268 }
3269 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3270 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3271 /* 5701 {A0,B0} CRC bug workaround */
3272 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00003273 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3274 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
3275 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 }
3277
3278 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003279 tg3_readphy(tp, MII_TG3_ISTAT, &val);
3280 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003282 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003284 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3286
3287 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3288 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3289 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3290 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3291 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3292 else
3293 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3294 }
3295
3296 current_link_up = 0;
3297 current_speed = SPEED_INVALID;
3298 current_duplex = DUPLEX_INVALID;
3299
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003300 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00003301 err = tg3_phy_auxctl_read(tp,
3302 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3303 &val);
3304 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003305 tg3_phy_auxctl_write(tp,
3306 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3307 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 goto relink;
3309 }
3310 }
3311
3312 bmsr = 0;
3313 for (i = 0; i < 100; i++) {
3314 tg3_readphy(tp, MII_BMSR, &bmsr);
3315 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3316 (bmsr & BMSR_LSTATUS))
3317 break;
3318 udelay(40);
3319 }
3320
3321 if (bmsr & BMSR_LSTATUS) {
3322 u32 aux_stat, bmcr;
3323
3324 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3325 for (i = 0; i < 2000; i++) {
3326 udelay(10);
3327 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3328 aux_stat)
3329 break;
3330 }
3331
3332 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3333 &current_speed,
3334 &current_duplex);
3335
3336 bmcr = 0;
3337 for (i = 0; i < 200; i++) {
3338 tg3_readphy(tp, MII_BMCR, &bmcr);
3339 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3340 continue;
3341 if (bmcr && bmcr != 0x7fff)
3342 break;
3343 udelay(10);
3344 }
3345
Matt Carlsonef167e22007-12-20 20:10:01 -08003346 lcl_adv = 0;
3347 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348
Matt Carlsonef167e22007-12-20 20:10:01 -08003349 tp->link_config.active_speed = current_speed;
3350 tp->link_config.active_duplex = current_duplex;
3351
3352 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3353 if ((bmcr & BMCR_ANENABLE) &&
3354 tg3_copper_is_advertising_all(tp,
3355 tp->link_config.advertising)) {
3356 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3357 &rmt_adv))
3358 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 }
3360 } else {
3361 if (!(bmcr & BMCR_ANENABLE) &&
3362 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003363 tp->link_config.duplex == current_duplex &&
3364 tp->link_config.flowctrl ==
3365 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 }
3368 }
3369
Matt Carlsonef167e22007-12-20 20:10:01 -08003370 if (current_link_up == 1 &&
3371 tp->link_config.active_duplex == DUPLEX_FULL)
3372 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 }
3374
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375relink:
Matt Carlson80096062010-08-02 11:26:06 +00003376 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 tg3_phy_copper_begin(tp);
3378
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003379 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00003380 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
3381 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 current_link_up = 1;
3383 }
3384
3385 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3386 if (current_link_up == 1) {
3387 if (tp->link_config.active_speed == SPEED_100 ||
3388 tp->link_config.active_speed == SPEED_10)
3389 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3390 else
3391 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003392 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003393 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3394 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3396
3397 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3398 if (tp->link_config.active_duplex == DUPLEX_HALF)
3399 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3400
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003402 if (current_link_up == 1 &&
3403 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003405 else
3406 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 }
3408
3409 /* ??? Without this setting Netgear GA302T PHY does not
3410 * ??? send/receive packets...
3411 */
Matt Carlson79eb6902010-02-17 15:17:03 +00003412 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3414 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3415 tw32_f(MAC_MI_MODE, tp->mi_mode);
3416 udelay(80);
3417 }
3418
3419 tw32_f(MAC_MODE, tp->mac_mode);
3420 udelay(40);
3421
Matt Carlson52b02d02010-10-14 10:37:41 +00003422 tg3_phy_eee_adjust(tp, current_link_up);
3423
Joe Perches63c3a662011-04-26 08:12:10 +00003424 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 /* Polled via timer. */
3426 tw32_f(MAC_EVENT, 0);
3427 } else {
3428 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3429 }
3430 udelay(40);
3431
3432 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3433 current_link_up == 1 &&
3434 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00003435 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 udelay(120);
3437 tw32_f(MAC_STATUS,
3438 (MAC_STATUS_SYNC_CHANGED |
3439 MAC_STATUS_CFG_CHANGED));
3440 udelay(40);
3441 tg3_write_mem(tp,
3442 NIC_SRAM_FIRMWARE_MBOX,
3443 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3444 }
3445
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003446 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00003447 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003448 u16 oldlnkctl, newlnkctl;
3449
3450 pci_read_config_word(tp->pdev,
3451 tp->pcie_cap + PCI_EXP_LNKCTL,
3452 &oldlnkctl);
3453 if (tp->link_config.active_speed == SPEED_100 ||
3454 tp->link_config.active_speed == SPEED_10)
3455 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3456 else
3457 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3458 if (newlnkctl != oldlnkctl)
3459 pci_write_config_word(tp->pdev,
3460 tp->pcie_cap + PCI_EXP_LNKCTL,
3461 newlnkctl);
3462 }
3463
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 if (current_link_up != netif_carrier_ok(tp->dev)) {
3465 if (current_link_up)
3466 netif_carrier_on(tp->dev);
3467 else
3468 netif_carrier_off(tp->dev);
3469 tg3_link_report(tp);
3470 }
3471
3472 return 0;
3473}
3474
3475struct tg3_fiber_aneginfo {
3476 int state;
3477#define ANEG_STATE_UNKNOWN 0
3478#define ANEG_STATE_AN_ENABLE 1
3479#define ANEG_STATE_RESTART_INIT 2
3480#define ANEG_STATE_RESTART 3
3481#define ANEG_STATE_DISABLE_LINK_OK 4
3482#define ANEG_STATE_ABILITY_DETECT_INIT 5
3483#define ANEG_STATE_ABILITY_DETECT 6
3484#define ANEG_STATE_ACK_DETECT_INIT 7
3485#define ANEG_STATE_ACK_DETECT 8
3486#define ANEG_STATE_COMPLETE_ACK_INIT 9
3487#define ANEG_STATE_COMPLETE_ACK 10
3488#define ANEG_STATE_IDLE_DETECT_INIT 11
3489#define ANEG_STATE_IDLE_DETECT 12
3490#define ANEG_STATE_LINK_OK 13
3491#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3492#define ANEG_STATE_NEXT_PAGE_WAIT 15
3493
3494 u32 flags;
3495#define MR_AN_ENABLE 0x00000001
3496#define MR_RESTART_AN 0x00000002
3497#define MR_AN_COMPLETE 0x00000004
3498#define MR_PAGE_RX 0x00000008
3499#define MR_NP_LOADED 0x00000010
3500#define MR_TOGGLE_TX 0x00000020
3501#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3502#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3503#define MR_LP_ADV_SYM_PAUSE 0x00000100
3504#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3505#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3506#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3507#define MR_LP_ADV_NEXT_PAGE 0x00001000
3508#define MR_TOGGLE_RX 0x00002000
3509#define MR_NP_RX 0x00004000
3510
3511#define MR_LINK_OK 0x80000000
3512
3513 unsigned long link_time, cur_time;
3514
3515 u32 ability_match_cfg;
3516 int ability_match_count;
3517
3518 char ability_match, idle_match, ack_match;
3519
3520 u32 txconfig, rxconfig;
3521#define ANEG_CFG_NP 0x00000080
3522#define ANEG_CFG_ACK 0x00000040
3523#define ANEG_CFG_RF2 0x00000020
3524#define ANEG_CFG_RF1 0x00000010
3525#define ANEG_CFG_PS2 0x00000001
3526#define ANEG_CFG_PS1 0x00008000
3527#define ANEG_CFG_HD 0x00004000
3528#define ANEG_CFG_FD 0x00002000
3529#define ANEG_CFG_INVAL 0x00001f06
3530
3531};
3532#define ANEG_OK 0
3533#define ANEG_DONE 1
3534#define ANEG_TIMER_ENAB 2
3535#define ANEG_FAILED -1
3536
3537#define ANEG_STATE_SETTLE_TIME 10000
3538
3539static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3540 struct tg3_fiber_aneginfo *ap)
3541{
Matt Carlson5be73b42007-12-20 20:09:29 -08003542 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 unsigned long delta;
3544 u32 rx_cfg_reg;
3545 int ret;
3546
3547 if (ap->state == ANEG_STATE_UNKNOWN) {
3548 ap->rxconfig = 0;
3549 ap->link_time = 0;
3550 ap->cur_time = 0;
3551 ap->ability_match_cfg = 0;
3552 ap->ability_match_count = 0;
3553 ap->ability_match = 0;
3554 ap->idle_match = 0;
3555 ap->ack_match = 0;
3556 }
3557 ap->cur_time++;
3558
3559 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3560 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3561
3562 if (rx_cfg_reg != ap->ability_match_cfg) {
3563 ap->ability_match_cfg = rx_cfg_reg;
3564 ap->ability_match = 0;
3565 ap->ability_match_count = 0;
3566 } else {
3567 if (++ap->ability_match_count > 1) {
3568 ap->ability_match = 1;
3569 ap->ability_match_cfg = rx_cfg_reg;
3570 }
3571 }
3572 if (rx_cfg_reg & ANEG_CFG_ACK)
3573 ap->ack_match = 1;
3574 else
3575 ap->ack_match = 0;
3576
3577 ap->idle_match = 0;
3578 } else {
3579 ap->idle_match = 1;
3580 ap->ability_match_cfg = 0;
3581 ap->ability_match_count = 0;
3582 ap->ability_match = 0;
3583 ap->ack_match = 0;
3584
3585 rx_cfg_reg = 0;
3586 }
3587
3588 ap->rxconfig = rx_cfg_reg;
3589 ret = ANEG_OK;
3590
Matt Carlson33f401a2010-04-05 10:19:27 +00003591 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 case ANEG_STATE_UNKNOWN:
3593 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3594 ap->state = ANEG_STATE_AN_ENABLE;
3595
3596 /* fallthru */
3597 case ANEG_STATE_AN_ENABLE:
3598 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3599 if (ap->flags & MR_AN_ENABLE) {
3600 ap->link_time = 0;
3601 ap->cur_time = 0;
3602 ap->ability_match_cfg = 0;
3603 ap->ability_match_count = 0;
3604 ap->ability_match = 0;
3605 ap->idle_match = 0;
3606 ap->ack_match = 0;
3607
3608 ap->state = ANEG_STATE_RESTART_INIT;
3609 } else {
3610 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3611 }
3612 break;
3613
3614 case ANEG_STATE_RESTART_INIT:
3615 ap->link_time = ap->cur_time;
3616 ap->flags &= ~(MR_NP_LOADED);
3617 ap->txconfig = 0;
3618 tw32(MAC_TX_AUTO_NEG, 0);
3619 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3620 tw32_f(MAC_MODE, tp->mac_mode);
3621 udelay(40);
3622
3623 ret = ANEG_TIMER_ENAB;
3624 ap->state = ANEG_STATE_RESTART;
3625
3626 /* fallthru */
3627 case ANEG_STATE_RESTART:
3628 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00003629 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00003631 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 break;
3634
3635 case ANEG_STATE_DISABLE_LINK_OK:
3636 ret = ANEG_DONE;
3637 break;
3638
3639 case ANEG_STATE_ABILITY_DETECT_INIT:
3640 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003641 ap->txconfig = ANEG_CFG_FD;
3642 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3643 if (flowctrl & ADVERTISE_1000XPAUSE)
3644 ap->txconfig |= ANEG_CFG_PS1;
3645 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3646 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3648 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3649 tw32_f(MAC_MODE, tp->mac_mode);
3650 udelay(40);
3651
3652 ap->state = ANEG_STATE_ABILITY_DETECT;
3653 break;
3654
3655 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00003656 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 break;
3659
3660 case ANEG_STATE_ACK_DETECT_INIT:
3661 ap->txconfig |= ANEG_CFG_ACK;
3662 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3663 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3664 tw32_f(MAC_MODE, tp->mac_mode);
3665 udelay(40);
3666
3667 ap->state = ANEG_STATE_ACK_DETECT;
3668
3669 /* fallthru */
3670 case ANEG_STATE_ACK_DETECT:
3671 if (ap->ack_match != 0) {
3672 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3673 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3674 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3675 } else {
3676 ap->state = ANEG_STATE_AN_ENABLE;
3677 }
3678 } else if (ap->ability_match != 0 &&
3679 ap->rxconfig == 0) {
3680 ap->state = ANEG_STATE_AN_ENABLE;
3681 }
3682 break;
3683
3684 case ANEG_STATE_COMPLETE_ACK_INIT:
3685 if (ap->rxconfig & ANEG_CFG_INVAL) {
3686 ret = ANEG_FAILED;
3687 break;
3688 }
3689 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3690 MR_LP_ADV_HALF_DUPLEX |
3691 MR_LP_ADV_SYM_PAUSE |
3692 MR_LP_ADV_ASYM_PAUSE |
3693 MR_LP_ADV_REMOTE_FAULT1 |
3694 MR_LP_ADV_REMOTE_FAULT2 |
3695 MR_LP_ADV_NEXT_PAGE |
3696 MR_TOGGLE_RX |
3697 MR_NP_RX);
3698 if (ap->rxconfig & ANEG_CFG_FD)
3699 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3700 if (ap->rxconfig & ANEG_CFG_HD)
3701 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3702 if (ap->rxconfig & ANEG_CFG_PS1)
3703 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3704 if (ap->rxconfig & ANEG_CFG_PS2)
3705 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3706 if (ap->rxconfig & ANEG_CFG_RF1)
3707 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3708 if (ap->rxconfig & ANEG_CFG_RF2)
3709 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3710 if (ap->rxconfig & ANEG_CFG_NP)
3711 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3712
3713 ap->link_time = ap->cur_time;
3714
3715 ap->flags ^= (MR_TOGGLE_TX);
3716 if (ap->rxconfig & 0x0008)
3717 ap->flags |= MR_TOGGLE_RX;
3718 if (ap->rxconfig & ANEG_CFG_NP)
3719 ap->flags |= MR_NP_RX;
3720 ap->flags |= MR_PAGE_RX;
3721
3722 ap->state = ANEG_STATE_COMPLETE_ACK;
3723 ret = ANEG_TIMER_ENAB;
3724 break;
3725
3726 case ANEG_STATE_COMPLETE_ACK:
3727 if (ap->ability_match != 0 &&
3728 ap->rxconfig == 0) {
3729 ap->state = ANEG_STATE_AN_ENABLE;
3730 break;
3731 }
3732 delta = ap->cur_time - ap->link_time;
3733 if (delta > ANEG_STATE_SETTLE_TIME) {
3734 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3735 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3736 } else {
3737 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3738 !(ap->flags & MR_NP_RX)) {
3739 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3740 } else {
3741 ret = ANEG_FAILED;
3742 }
3743 }
3744 }
3745 break;
3746
3747 case ANEG_STATE_IDLE_DETECT_INIT:
3748 ap->link_time = ap->cur_time;
3749 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3750 tw32_f(MAC_MODE, tp->mac_mode);
3751 udelay(40);
3752
3753 ap->state = ANEG_STATE_IDLE_DETECT;
3754 ret = ANEG_TIMER_ENAB;
3755 break;
3756
3757 case ANEG_STATE_IDLE_DETECT:
3758 if (ap->ability_match != 0 &&
3759 ap->rxconfig == 0) {
3760 ap->state = ANEG_STATE_AN_ENABLE;
3761 break;
3762 }
3763 delta = ap->cur_time - ap->link_time;
3764 if (delta > ANEG_STATE_SETTLE_TIME) {
3765 /* XXX another gem from the Broadcom driver :( */
3766 ap->state = ANEG_STATE_LINK_OK;
3767 }
3768 break;
3769
3770 case ANEG_STATE_LINK_OK:
3771 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3772 ret = ANEG_DONE;
3773 break;
3774
3775 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3776 /* ??? unimplemented */
3777 break;
3778
3779 case ANEG_STATE_NEXT_PAGE_WAIT:
3780 /* ??? unimplemented */
3781 break;
3782
3783 default:
3784 ret = ANEG_FAILED;
3785 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787
3788 return ret;
3789}
3790
Matt Carlson5be73b42007-12-20 20:09:29 -08003791static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792{
3793 int res = 0;
3794 struct tg3_fiber_aneginfo aninfo;
3795 int status = ANEG_FAILED;
3796 unsigned int tick;
3797 u32 tmp;
3798
3799 tw32_f(MAC_TX_AUTO_NEG, 0);
3800
3801 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3802 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3803 udelay(40);
3804
3805 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3806 udelay(40);
3807
3808 memset(&aninfo, 0, sizeof(aninfo));
3809 aninfo.flags |= MR_AN_ENABLE;
3810 aninfo.state = ANEG_STATE_UNKNOWN;
3811 aninfo.cur_time = 0;
3812 tick = 0;
3813 while (++tick < 195000) {
3814 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3815 if (status == ANEG_DONE || status == ANEG_FAILED)
3816 break;
3817
3818 udelay(1);
3819 }
3820
3821 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3822 tw32_f(MAC_MODE, tp->mac_mode);
3823 udelay(40);
3824
Matt Carlson5be73b42007-12-20 20:09:29 -08003825 *txflags = aninfo.txconfig;
3826 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827
3828 if (status == ANEG_DONE &&
3829 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3830 MR_LP_ADV_FULL_DUPLEX)))
3831 res = 1;
3832
3833 return res;
3834}
3835
3836static void tg3_init_bcm8002(struct tg3 *tp)
3837{
3838 u32 mac_status = tr32(MAC_STATUS);
3839 int i;
3840
3841 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00003842 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 !(mac_status & MAC_STATUS_PCS_SYNCED))
3844 return;
3845
3846 /* Set PLL lock range. */
3847 tg3_writephy(tp, 0x16, 0x8007);
3848
3849 /* SW reset */
3850 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
3851
3852 /* Wait for reset to complete. */
3853 /* XXX schedule_timeout() ... */
3854 for (i = 0; i < 500; i++)
3855 udelay(10);
3856
3857 /* Config mode; select PMA/Ch 1 regs. */
3858 tg3_writephy(tp, 0x10, 0x8411);
3859
3860 /* Enable auto-lock and comdet, select txclk for tx. */
3861 tg3_writephy(tp, 0x11, 0x0a10);
3862
3863 tg3_writephy(tp, 0x18, 0x00a0);
3864 tg3_writephy(tp, 0x16, 0x41ff);
3865
3866 /* Assert and deassert POR. */
3867 tg3_writephy(tp, 0x13, 0x0400);
3868 udelay(40);
3869 tg3_writephy(tp, 0x13, 0x0000);
3870
3871 tg3_writephy(tp, 0x11, 0x0a50);
3872 udelay(40);
3873 tg3_writephy(tp, 0x11, 0x0a10);
3874
3875 /* Wait for signal to stabilize */
3876 /* XXX schedule_timeout() ... */
3877 for (i = 0; i < 15000; i++)
3878 udelay(10);
3879
3880 /* Deselect the channel register so we can read the PHYID
3881 * later.
3882 */
3883 tg3_writephy(tp, 0x10, 0x8011);
3884}
3885
3886static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
3887{
Matt Carlson82cd3d12007-12-20 20:09:00 -08003888 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 u32 sg_dig_ctrl, sg_dig_status;
3890 u32 serdes_cfg, expected_sg_dig_ctrl;
3891 int workaround, port_a;
3892 int current_link_up;
3893
3894 serdes_cfg = 0;
3895 expected_sg_dig_ctrl = 0;
3896 workaround = 0;
3897 port_a = 1;
3898 current_link_up = 0;
3899
3900 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
3901 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
3902 workaround = 1;
3903 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
3904 port_a = 0;
3905
3906 /* preserve bits 0-11,13,14 for signal pre-emphasis */
3907 /* preserve bits 20-23 for voltage regulator */
3908 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
3909 }
3910
3911 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3912
3913 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003914 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 if (workaround) {
3916 u32 val = serdes_cfg;
3917
3918 if (port_a)
3919 val |= 0xc010000;
3920 else
3921 val |= 0x4010000;
3922 tw32_f(MAC_SERDES_CFG, val);
3923 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003924
3925 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 }
3927 if (mac_status & MAC_STATUS_PCS_SYNCED) {
3928 tg3_setup_flow_control(tp, 0, 0);
3929 current_link_up = 1;
3930 }
3931 goto out;
3932 }
3933
3934 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003935 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936
Matt Carlson82cd3d12007-12-20 20:09:00 -08003937 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3938 if (flowctrl & ADVERTISE_1000XPAUSE)
3939 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
3940 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3941 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942
3943 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003944 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07003945 tp->serdes_counter &&
3946 ((mac_status & (MAC_STATUS_PCS_SYNCED |
3947 MAC_STATUS_RCVD_CFG)) ==
3948 MAC_STATUS_PCS_SYNCED)) {
3949 tp->serdes_counter--;
3950 current_link_up = 1;
3951 goto out;
3952 }
3953restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 if (workaround)
3955 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003956 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 udelay(5);
3958 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
3959
Michael Chan3d3ebe72006-09-27 15:59:15 -07003960 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003961 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
3963 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003964 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 mac_status = tr32(MAC_STATUS);
3966
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003967 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08003969 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970
Matt Carlson82cd3d12007-12-20 20:09:00 -08003971 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
3972 local_adv |= ADVERTISE_1000XPAUSE;
3973 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
3974 local_adv |= ADVERTISE_1000XPSE_ASYM;
3975
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003976 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003977 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003978 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003979 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980
3981 tg3_setup_flow_control(tp, local_adv, remote_adv);
3982 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003983 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003984 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003985 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003986 if (tp->serdes_counter)
3987 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 else {
3989 if (workaround) {
3990 u32 val = serdes_cfg;
3991
3992 if (port_a)
3993 val |= 0xc010000;
3994 else
3995 val |= 0x4010000;
3996
3997 tw32_f(MAC_SERDES_CFG, val);
3998 }
3999
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004000 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 udelay(40);
4002
4003 /* Link parallel detection - link is up */
4004 /* only if we have PCS_SYNC and not */
4005 /* receiving config code words */
4006 mac_status = tr32(MAC_STATUS);
4007 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
4008 !(mac_status & MAC_STATUS_RCVD_CFG)) {
4009 tg3_setup_flow_control(tp, 0, 0);
4010 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004011 tp->phy_flags |=
4012 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004013 tp->serdes_counter =
4014 SERDES_PARALLEL_DET_TIMEOUT;
4015 } else
4016 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 }
4018 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07004019 } else {
4020 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004021 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 }
4023
4024out:
4025 return current_link_up;
4026}
4027
4028static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
4029{
4030 int current_link_up = 0;
4031
Michael Chan5cf64b8a2007-05-05 12:11:21 -07004032 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034
4035 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08004036 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004038
Matt Carlson5be73b42007-12-20 20:09:29 -08004039 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4040 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041
Matt Carlson5be73b42007-12-20 20:09:29 -08004042 if (txflags & ANEG_CFG_PS1)
4043 local_adv |= ADVERTISE_1000XPAUSE;
4044 if (txflags & ANEG_CFG_PS2)
4045 local_adv |= ADVERTISE_1000XPSE_ASYM;
4046
4047 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4048 remote_adv |= LPA_1000XPAUSE;
4049 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4050 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051
4052 tg3_setup_flow_control(tp, local_adv, remote_adv);
4053
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 current_link_up = 1;
4055 }
4056 for (i = 0; i < 30; i++) {
4057 udelay(20);
4058 tw32_f(MAC_STATUS,
4059 (MAC_STATUS_SYNC_CHANGED |
4060 MAC_STATUS_CFG_CHANGED));
4061 udelay(40);
4062 if ((tr32(MAC_STATUS) &
4063 (MAC_STATUS_SYNC_CHANGED |
4064 MAC_STATUS_CFG_CHANGED)) == 0)
4065 break;
4066 }
4067
4068 mac_status = tr32(MAC_STATUS);
4069 if (current_link_up == 0 &&
4070 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4071 !(mac_status & MAC_STATUS_RCVD_CFG))
4072 current_link_up = 1;
4073 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004074 tg3_setup_flow_control(tp, 0, 0);
4075
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 /* Forcing 1000FD link up. */
4077 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078
4079 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4080 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004081
4082 tw32_f(MAC_MODE, tp->mac_mode);
4083 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 }
4085
4086out:
4087 return current_link_up;
4088}
4089
4090static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4091{
4092 u32 orig_pause_cfg;
4093 u16 orig_active_speed;
4094 u8 orig_active_duplex;
4095 u32 mac_status;
4096 int current_link_up;
4097 int i;
4098
Matt Carlson8d018622007-12-20 20:05:44 -08004099 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 orig_active_speed = tp->link_config.active_speed;
4101 orig_active_duplex = tp->link_config.active_duplex;
4102
Joe Perches63c3a662011-04-26 08:12:10 +00004103 if (!tg3_flag(tp, HW_AUTONEG) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 netif_carrier_ok(tp->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004105 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 mac_status = tr32(MAC_STATUS);
4107 mac_status &= (MAC_STATUS_PCS_SYNCED |
4108 MAC_STATUS_SIGNAL_DET |
4109 MAC_STATUS_CFG_CHANGED |
4110 MAC_STATUS_RCVD_CFG);
4111 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4112 MAC_STATUS_SIGNAL_DET)) {
4113 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4114 MAC_STATUS_CFG_CHANGED));
4115 return 0;
4116 }
4117 }
4118
4119 tw32_f(MAC_TX_AUTO_NEG, 0);
4120
4121 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4122 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
4123 tw32_f(MAC_MODE, tp->mac_mode);
4124 udelay(40);
4125
Matt Carlson79eb6902010-02-17 15:17:03 +00004126 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 tg3_init_bcm8002(tp);
4128
4129 /* Enable link change event even when serdes polling. */
4130 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4131 udelay(40);
4132
4133 current_link_up = 0;
4134 mac_status = tr32(MAC_STATUS);
4135
Joe Perches63c3a662011-04-26 08:12:10 +00004136 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4138 else
4139 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4140
Matt Carlson898a56f2009-08-28 14:02:40 +00004141 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00004143 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144
4145 for (i = 0; i < 100; i++) {
4146 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4147 MAC_STATUS_CFG_CHANGED));
4148 udelay(5);
4149 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07004150 MAC_STATUS_CFG_CHANGED |
4151 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 break;
4153 }
4154
4155 mac_status = tr32(MAC_STATUS);
4156 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4157 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004158 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4159 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 tw32_f(MAC_MODE, (tp->mac_mode |
4161 MAC_MODE_SEND_CONFIGS));
4162 udelay(1);
4163 tw32_f(MAC_MODE, tp->mac_mode);
4164 }
4165 }
4166
4167 if (current_link_up == 1) {
4168 tp->link_config.active_speed = SPEED_1000;
4169 tp->link_config.active_duplex = DUPLEX_FULL;
4170 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4171 LED_CTRL_LNKLED_OVERRIDE |
4172 LED_CTRL_1000MBPS_ON));
4173 } else {
4174 tp->link_config.active_speed = SPEED_INVALID;
4175 tp->link_config.active_duplex = DUPLEX_INVALID;
4176 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4177 LED_CTRL_LNKLED_OVERRIDE |
4178 LED_CTRL_TRAFFIC_OVERRIDE));
4179 }
4180
4181 if (current_link_up != netif_carrier_ok(tp->dev)) {
4182 if (current_link_up)
4183 netif_carrier_on(tp->dev);
4184 else
4185 netif_carrier_off(tp->dev);
4186 tg3_link_report(tp);
4187 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004188 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 if (orig_pause_cfg != now_pause_cfg ||
4190 orig_active_speed != tp->link_config.active_speed ||
4191 orig_active_duplex != tp->link_config.active_duplex)
4192 tg3_link_report(tp);
4193 }
4194
4195 return 0;
4196}
4197
Michael Chan747e8f82005-07-25 12:33:22 -07004198static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4199{
4200 int current_link_up, err = 0;
4201 u32 bmsr, bmcr;
4202 u16 current_speed;
4203 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004204 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004205
4206 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4207 tw32_f(MAC_MODE, tp->mac_mode);
4208 udelay(40);
4209
4210 tw32(MAC_EVENT, 0);
4211
4212 tw32_f(MAC_STATUS,
4213 (MAC_STATUS_SYNC_CHANGED |
4214 MAC_STATUS_CFG_CHANGED |
4215 MAC_STATUS_MI_COMPLETION |
4216 MAC_STATUS_LNKSTATE_CHANGED));
4217 udelay(40);
4218
4219 if (force_reset)
4220 tg3_phy_reset(tp);
4221
4222 current_link_up = 0;
4223 current_speed = SPEED_INVALID;
4224 current_duplex = DUPLEX_INVALID;
4225
4226 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4227 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004228 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4229 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4230 bmsr |= BMSR_LSTATUS;
4231 else
4232 bmsr &= ~BMSR_LSTATUS;
4233 }
Michael Chan747e8f82005-07-25 12:33:22 -07004234
4235 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4236
4237 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004238 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004239 /* do nothing, just check for link up at the end */
4240 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4241 u32 adv, new_adv;
4242
4243 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4244 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4245 ADVERTISE_1000XPAUSE |
4246 ADVERTISE_1000XPSE_ASYM |
4247 ADVERTISE_SLCT);
4248
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004249 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004250
4251 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4252 new_adv |= ADVERTISE_1000XHALF;
4253 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4254 new_adv |= ADVERTISE_1000XFULL;
4255
4256 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4257 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4258 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4259 tg3_writephy(tp, MII_BMCR, bmcr);
4260
4261 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004262 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004263 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004264
4265 return err;
4266 }
4267 } else {
4268 u32 new_bmcr;
4269
4270 bmcr &= ~BMCR_SPEED1000;
4271 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4272
4273 if (tp->link_config.duplex == DUPLEX_FULL)
4274 new_bmcr |= BMCR_FULLDPLX;
4275
4276 if (new_bmcr != bmcr) {
4277 /* BMCR_SPEED1000 is a reserved bit that needs
4278 * to be set on write.
4279 */
4280 new_bmcr |= BMCR_SPEED1000;
4281
4282 /* Force a linkdown */
4283 if (netif_carrier_ok(tp->dev)) {
4284 u32 adv;
4285
4286 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4287 adv &= ~(ADVERTISE_1000XFULL |
4288 ADVERTISE_1000XHALF |
4289 ADVERTISE_SLCT);
4290 tg3_writephy(tp, MII_ADVERTISE, adv);
4291 tg3_writephy(tp, MII_BMCR, bmcr |
4292 BMCR_ANRESTART |
4293 BMCR_ANENABLE);
4294 udelay(10);
4295 netif_carrier_off(tp->dev);
4296 }
4297 tg3_writephy(tp, MII_BMCR, new_bmcr);
4298 bmcr = new_bmcr;
4299 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4300 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004301 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4302 ASIC_REV_5714) {
4303 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4304 bmsr |= BMSR_LSTATUS;
4305 else
4306 bmsr &= ~BMSR_LSTATUS;
4307 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004308 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004309 }
4310 }
4311
4312 if (bmsr & BMSR_LSTATUS) {
4313 current_speed = SPEED_1000;
4314 current_link_up = 1;
4315 if (bmcr & BMCR_FULLDPLX)
4316 current_duplex = DUPLEX_FULL;
4317 else
4318 current_duplex = DUPLEX_HALF;
4319
Matt Carlsonef167e22007-12-20 20:10:01 -08004320 local_adv = 0;
4321 remote_adv = 0;
4322
Michael Chan747e8f82005-07-25 12:33:22 -07004323 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004324 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004325
4326 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4327 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4328 common = local_adv & remote_adv;
4329 if (common & (ADVERTISE_1000XHALF |
4330 ADVERTISE_1000XFULL)) {
4331 if (common & ADVERTISE_1000XFULL)
4332 current_duplex = DUPLEX_FULL;
4333 else
4334 current_duplex = DUPLEX_HALF;
Joe Perches63c3a662011-04-26 08:12:10 +00004335 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00004336 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00004337 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07004338 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00004339 }
Michael Chan747e8f82005-07-25 12:33:22 -07004340 }
4341 }
4342
Matt Carlsonef167e22007-12-20 20:10:01 -08004343 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4344 tg3_setup_flow_control(tp, local_adv, remote_adv);
4345
Michael Chan747e8f82005-07-25 12:33:22 -07004346 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4347 if (tp->link_config.active_duplex == DUPLEX_HALF)
4348 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4349
4350 tw32_f(MAC_MODE, tp->mac_mode);
4351 udelay(40);
4352
4353 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4354
4355 tp->link_config.active_speed = current_speed;
4356 tp->link_config.active_duplex = current_duplex;
4357
4358 if (current_link_up != netif_carrier_ok(tp->dev)) {
4359 if (current_link_up)
4360 netif_carrier_on(tp->dev);
4361 else {
4362 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004363 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004364 }
4365 tg3_link_report(tp);
4366 }
4367 return err;
4368}
4369
4370static void tg3_serdes_parallel_detect(struct tg3 *tp)
4371{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004372 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004373 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004374 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004375 return;
4376 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004377
Michael Chan747e8f82005-07-25 12:33:22 -07004378 if (!netif_carrier_ok(tp->dev) &&
4379 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4380 u32 bmcr;
4381
4382 tg3_readphy(tp, MII_BMCR, &bmcr);
4383 if (bmcr & BMCR_ANENABLE) {
4384 u32 phy1, phy2;
4385
4386 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004387 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
4388 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07004389
4390 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004391 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4392 MII_TG3_DSP_EXP1_INT_STAT);
4393 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
4394 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004395
4396 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4397 /* We have signal detect and not receiving
4398 * config code words, link is up by parallel
4399 * detection.
4400 */
4401
4402 bmcr &= ~BMCR_ANENABLE;
4403 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4404 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004405 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004406 }
4407 }
Matt Carlson859a588792010-04-05 10:19:28 +00004408 } else if (netif_carrier_ok(tp->dev) &&
4409 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004410 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004411 u32 phy2;
4412
4413 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004414 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4415 MII_TG3_DSP_EXP1_INT_STAT);
4416 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004417 if (phy2 & 0x20) {
4418 u32 bmcr;
4419
4420 /* Config code words received, turn on autoneg. */
4421 tg3_readphy(tp, MII_BMCR, &bmcr);
4422 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4423
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004424 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004425
4426 }
4427 }
4428}
4429
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4431{
Matt Carlsonf2096f92011-04-05 14:22:48 +00004432 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 int err;
4434
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004435 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004437 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07004438 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00004439 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441
Matt Carlsonbcb37f62008-11-03 16:52:09 -08004442 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00004443 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08004444
4445 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4446 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4447 scale = 65;
4448 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4449 scale = 6;
4450 else
4451 scale = 12;
4452
4453 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4454 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4455 tw32(GRC_MISC_CFG, val);
4456 }
4457
Matt Carlsonf2096f92011-04-05 14:22:48 +00004458 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4459 (6 << TX_LENGTHS_IPG_SHIFT);
4460 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
4461 val |= tr32(MAC_TX_LENGTHS) &
4462 (TX_LENGTHS_JMB_FRM_LEN_MSK |
4463 TX_LENGTHS_CNT_DWN_VAL_MSK);
4464
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 if (tp->link_config.active_speed == SPEED_1000 &&
4466 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00004467 tw32(MAC_TX_LENGTHS, val |
4468 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00004470 tw32(MAC_TX_LENGTHS, val |
4471 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472
Joe Perches63c3a662011-04-26 08:12:10 +00004473 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 if (netif_carrier_ok(tp->dev)) {
4475 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07004476 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 } else {
4478 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4479 }
4480 }
4481
Joe Perches63c3a662011-04-26 08:12:10 +00004482 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00004483 val = tr32(PCIE_PWR_MGMT_THRESH);
Matt Carlson8ed5d972007-05-07 00:25:49 -07004484 if (!netif_carrier_ok(tp->dev))
4485 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4486 tp->pwrmgmt_thresh;
4487 else
4488 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4489 tw32(PCIE_PWR_MGMT_THRESH, val);
4490 }
4491
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 return err;
4493}
4494
Matt Carlson66cfd1b2010-09-30 10:34:30 +00004495static inline int tg3_irq_sync(struct tg3 *tp)
4496{
4497 return tp->irq_sync;
4498}
4499
Matt Carlson97bd8e42011-04-13 11:05:04 +00004500static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
4501{
4502 int i;
4503
4504 dst = (u32 *)((u8 *)dst + off);
4505 for (i = 0; i < len; i += sizeof(u32))
4506 *dst++ = tr32(off + i);
4507}
4508
4509static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
4510{
4511 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
4512 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
4513 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
4514 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
4515 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
4516 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
4517 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
4518 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
4519 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
4520 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
4521 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
4522 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
4523 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
4524 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
4525 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
4526 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
4527 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
4528 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
4529 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
4530
Joe Perches63c3a662011-04-26 08:12:10 +00004531 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00004532 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
4533
4534 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
4535 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
4536 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
4537 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
4538 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
4539 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
4540 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
4541 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
4542
Joe Perches63c3a662011-04-26 08:12:10 +00004543 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00004544 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
4545 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
4546 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
4547 }
4548
4549 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
4550 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
4551 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
4552 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
4553 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
4554
Joe Perches63c3a662011-04-26 08:12:10 +00004555 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00004556 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
4557}
4558
4559static void tg3_dump_state(struct tg3 *tp)
4560{
4561 int i;
4562 u32 *regs;
4563
4564 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
4565 if (!regs) {
4566 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
4567 return;
4568 }
4569
Joe Perches63c3a662011-04-26 08:12:10 +00004570 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00004571 /* Read up to but not including private PCI registers */
4572 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
4573 regs[i / sizeof(u32)] = tr32(i);
4574 } else
4575 tg3_dump_legacy_regs(tp, regs);
4576
4577 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
4578 if (!regs[i + 0] && !regs[i + 1] &&
4579 !regs[i + 2] && !regs[i + 3])
4580 continue;
4581
4582 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
4583 i * 4,
4584 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
4585 }
4586
4587 kfree(regs);
4588
4589 for (i = 0; i < tp->irq_cnt; i++) {
4590 struct tg3_napi *tnapi = &tp->napi[i];
4591
4592 /* SW status block */
4593 netdev_err(tp->dev,
4594 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
4595 i,
4596 tnapi->hw_status->status,
4597 tnapi->hw_status->status_tag,
4598 tnapi->hw_status->rx_jumbo_consumer,
4599 tnapi->hw_status->rx_consumer,
4600 tnapi->hw_status->rx_mini_consumer,
4601 tnapi->hw_status->idx[0].rx_producer,
4602 tnapi->hw_status->idx[0].tx_consumer);
4603
4604 netdev_err(tp->dev,
4605 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
4606 i,
4607 tnapi->last_tag, tnapi->last_irq_tag,
4608 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
4609 tnapi->rx_rcb_ptr,
4610 tnapi->prodring.rx_std_prod_idx,
4611 tnapi->prodring.rx_std_cons_idx,
4612 tnapi->prodring.rx_jmb_prod_idx,
4613 tnapi->prodring.rx_jmb_cons_idx);
4614 }
4615}
4616
Michael Chandf3e6542006-05-26 17:48:07 -07004617/* This is called whenever we suspect that the system chipset is re-
4618 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4619 * is bogus tx completions. We try to recover by setting the
4620 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4621 * in the workqueue.
4622 */
4623static void tg3_tx_recover(struct tg3 *tp)
4624{
Joe Perches63c3a662011-04-26 08:12:10 +00004625 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07004626 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4627
Matt Carlson5129c3a2010-04-05 10:19:23 +00004628 netdev_warn(tp->dev,
4629 "The system may be re-ordering memory-mapped I/O "
4630 "cycles to the network device, attempting to recover. "
4631 "Please report the problem to the driver maintainer "
4632 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07004633
4634 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00004635 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07004636 spin_unlock(&tp->lock);
4637}
4638
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004639static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07004640{
Matt Carlsonf65aac12010-08-02 11:26:03 +00004641 /* Tell compiler to fetch tx indices from memory. */
4642 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004643 return tnapi->tx_pending -
4644 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07004645}
4646
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647/* Tigon3 never reports partial packet sends. So we do not
4648 * need special logic to handle SKBs that have not had all
4649 * of their frags sent yet, like SunGEM does.
4650 */
Matt Carlson17375d22009-08-28 14:02:18 +00004651static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652{
Matt Carlson17375d22009-08-28 14:02:18 +00004653 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004654 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004655 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004656 struct netdev_queue *txq;
4657 int index = tnapi - tp->napi;
4658
Joe Perches63c3a662011-04-26 08:12:10 +00004659 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004660 index--;
4661
4662 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663
4664 while (sw_idx != hw_idx) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00004665 struct ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004667 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668
Michael Chandf3e6542006-05-26 17:48:07 -07004669 if (unlikely(skb == NULL)) {
4670 tg3_tx_recover(tp);
4671 return;
4672 }
4673
Alexander Duyckf4188d82009-12-02 16:48:38 +00004674 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004675 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004676 skb_headlen(skb),
4677 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678
4679 ri->skb = NULL;
4680
4681 sw_idx = NEXT_TX(sw_idx);
4682
4683 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004684 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004685 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4686 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00004687
4688 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004689 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004690 skb_shinfo(skb)->frags[i].size,
4691 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 sw_idx = NEXT_TX(sw_idx);
4693 }
4694
David S. Millerf47c11e2005-06-24 20:18:35 -07004695 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004696
4697 if (unlikely(tx_bug)) {
4698 tg3_tx_recover(tp);
4699 return;
4700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 }
4702
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004703 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704
Michael Chan1b2a7202006-08-07 21:46:02 -07004705 /* Need to make the tx_cons update visible to tg3_start_xmit()
4706 * before checking for netif_queue_stopped(). Without the
4707 * memory barrier, there is a small possibility that tg3_start_xmit()
4708 * will miss it and cause the queue to be stopped forever.
4709 */
4710 smp_mb();
4711
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004712 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004713 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004714 __netif_tx_lock(txq, smp_processor_id());
4715 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004716 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004717 netif_tx_wake_queue(txq);
4718 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07004719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720}
4721
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004722static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
4723{
4724 if (!ri->skb)
4725 return;
4726
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004727 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004728 map_sz, PCI_DMA_FROMDEVICE);
4729 dev_kfree_skb_any(ri->skb);
4730 ri->skb = NULL;
4731}
4732
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733/* Returns size of skb allocated or < 0 on error.
4734 *
4735 * We only need to fill in the address because the other members
4736 * of the RX descriptor are invariant, see tg3_init_rings.
4737 *
4738 * Note the purposeful assymetry of cpu vs. chip accesses. For
4739 * posting buffers we only dirty the first cache line of the RX
4740 * descriptor (containing the address). Whereas for the RX status
4741 * buffers the cpu only reads the last cacheline of the RX descriptor
4742 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4743 */
Matt Carlson86b21e52009-11-13 13:03:45 +00004744static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00004745 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746{
4747 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00004748 struct ring_info *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 struct sk_buff *skb;
4750 dma_addr_t mapping;
4751 int skb_size, dest_idx;
4752
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 switch (opaque_key) {
4754 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004755 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00004756 desc = &tpr->rx_std[dest_idx];
4757 map = &tpr->rx_std_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004758 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 break;
4760
4761 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004762 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004763 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004764 map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004765 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 break;
4767
4768 default:
4769 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771
4772 /* Do not overwrite any of the map or rp information
4773 * until we are sure we can commit to a new buffer.
4774 *
4775 * Callers depend upon this behavior and assume that
4776 * we leave everything unchanged if we fail.
4777 */
Matt Carlson287be122009-08-28 13:58:46 +00004778 skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 if (skb == NULL)
4780 return -ENOMEM;
4781
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 skb_reserve(skb, tp->rx_offset);
4783
Matt Carlson287be122009-08-28 13:58:46 +00004784 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00004786 if (pci_dma_mapping_error(tp->pdev, mapping)) {
4787 dev_kfree_skb(skb);
4788 return -EIO;
4789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790
4791 map->skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004792 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 desc->addr_hi = ((u64)mapping >> 32);
4795 desc->addr_lo = ((u64)mapping & 0xffffffff);
4796
4797 return skb_size;
4798}
4799
4800/* We only need to move over in the address because the other
4801 * members of the RX descriptor are invariant. See notes above
4802 * tg3_alloc_rx_skb for full details.
4803 */
Matt Carlsona3896162009-11-13 13:03:44 +00004804static void tg3_recycle_rx(struct tg3_napi *tnapi,
4805 struct tg3_rx_prodring_set *dpr,
4806 u32 opaque_key, int src_idx,
4807 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808{
Matt Carlson17375d22009-08-28 14:02:18 +00004809 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4811 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004812 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004813 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814
4815 switch (opaque_key) {
4816 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004817 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004818 dest_desc = &dpr->rx_std[dest_idx];
4819 dest_map = &dpr->rx_std_buffers[dest_idx];
4820 src_desc = &spr->rx_std[src_idx];
4821 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 break;
4823
4824 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004825 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004826 dest_desc = &dpr->rx_jmb[dest_idx].std;
4827 dest_map = &dpr->rx_jmb_buffers[dest_idx];
4828 src_desc = &spr->rx_jmb[src_idx].std;
4829 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 break;
4831
4832 default:
4833 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835
4836 dest_map->skb = src_map->skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004837 dma_unmap_addr_set(dest_map, mapping,
4838 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 dest_desc->addr_hi = src_desc->addr_hi;
4840 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00004841
4842 /* Ensure that the update to the skb happens after the physical
4843 * addresses have been transferred to the new BD location.
4844 */
4845 smp_wmb();
4846
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 src_map->skb = NULL;
4848}
4849
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850/* The RX ring scheme is composed of multiple rings which post fresh
4851 * buffers to the chip, and one special ring the chip uses to report
4852 * status back to the host.
4853 *
4854 * The special ring reports the status of received packets to the
4855 * host. The chip does not write into the original descriptor the
4856 * RX buffer was obtained from. The chip simply takes the original
4857 * descriptor as provided by the host, updates the status and length
4858 * field, then writes this into the next status ring entry.
4859 *
4860 * Each ring the host uses to post buffers to the chip is described
4861 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
4862 * it is first placed into the on-chip ram. When the packet's length
4863 * is known, it walks down the TG3_BDINFO entries to select the ring.
4864 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
4865 * which is within the range of the new packet's length is chosen.
4866 *
4867 * The "separate ring for rx status" scheme may sound queer, but it makes
4868 * sense from a cache coherency perspective. If only the host writes
4869 * to the buffer post rings, and only the chip writes to the rx status
4870 * rings, then cache lines never move beyond shared-modified state.
4871 * If both the host and chip were to write into the same ring, cache line
4872 * eviction could occur since both entities want it in an exclusive state.
4873 */
Matt Carlson17375d22009-08-28 14:02:18 +00004874static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875{
Matt Carlson17375d22009-08-28 14:02:18 +00004876 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07004877 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004878 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00004879 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07004880 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004882 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004884 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 /*
4886 * We need to order the read of hw_idx and the read of
4887 * the opaque cookie.
4888 */
4889 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 work_mask = 0;
4891 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004892 std_prod_idx = tpr->rx_std_prod_idx;
4893 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00004895 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00004896 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 unsigned int len;
4898 struct sk_buff *skb;
4899 dma_addr_t dma_addr;
4900 u32 opaque_key, desc_idx, *post_ptr;
4901
4902 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
4903 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
4904 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004905 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004906 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004907 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004908 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07004909 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004911 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004912 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004913 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004914 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00004915 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917
4918 work_mask |= opaque_key;
4919
4920 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
4921 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
4922 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00004923 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 desc_idx, *post_ptr);
4925 drop_it_no_recycle:
4926 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00004927 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 goto next_pkt;
4929 }
4930
Matt Carlsonad829262008-11-21 17:16:16 -08004931 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
4932 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933
Matt Carlsond2757fc2010-04-12 06:58:27 +00004934 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 int skb_size;
4936
Matt Carlson86b21e52009-11-13 13:03:45 +00004937 skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00004938 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 if (skb_size < 0)
4940 goto drop_it;
4941
Matt Carlson287be122009-08-28 13:58:46 +00004942 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 PCI_DMA_FROMDEVICE);
4944
Matt Carlson61e800c2010-02-17 15:16:54 +00004945 /* Ensure that the update to the skb happens
4946 * after the usage of the old DMA mapping.
4947 */
4948 smp_wmb();
4949
4950 ri->skb = NULL;
4951
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 skb_put(skb, len);
4953 } else {
4954 struct sk_buff *copy_skb;
4955
Matt Carlsona3896162009-11-13 13:03:44 +00004956 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 desc_idx, *post_ptr);
4958
Matt Carlsonbf933c82011-01-25 15:58:49 +00004959 copy_skb = netdev_alloc_skb(tp->dev, len +
Matt Carlson9dc7a112010-04-12 06:58:28 +00004960 TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 if (copy_skb == NULL)
4962 goto drop_it_no_recycle;
4963
Matt Carlsonbf933c82011-01-25 15:58:49 +00004964 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 skb_put(copy_skb, len);
4966 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03004967 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
4969
4970 /* We'll reuse the original ring buffer. */
4971 skb = copy_skb;
4972 }
4973
Michał Mirosławdc668912011-04-07 03:35:07 +00004974 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
4976 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
4977 >> RXD_TCPCSUM_SHIFT) == 0xffff))
4978 skb->ip_summed = CHECKSUM_UNNECESSARY;
4979 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07004980 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981
4982 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004983
4984 if (len > (tp->dev->mtu + ETH_HLEN) &&
4985 skb->protocol != htons(ETH_P_8021Q)) {
4986 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00004987 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004988 }
4989
Matt Carlson9dc7a112010-04-12 06:58:28 +00004990 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00004991 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
4992 __vlan_hwaccel_put_tag(skb,
4993 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00004994
Matt Carlsonbf933c82011-01-25 15:58:49 +00004995 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 received++;
4998 budget--;
4999
5000next_pkt:
5001 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07005002
5003 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005004 tpr->rx_std_prod_idx = std_prod_idx &
5005 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00005006 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5007 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07005008 work_mask &= ~RXD_OPAQUE_RING_STD;
5009 rx_std_posted = 0;
5010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07005012 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00005013 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07005014
5015 /* Refresh hw_idx to see if there is new work */
5016 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005017 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07005018 rmb();
5019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 }
5021
5022 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00005023 tnapi->rx_rcb_ptr = sw_idx;
5024 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025
5026 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00005027 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005028 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005029 tpr->rx_std_prod_idx = std_prod_idx &
5030 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005031 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5032 tpr->rx_std_prod_idx);
5033 }
5034 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005035 tpr->rx_jmb_prod_idx = jmb_prod_idx &
5036 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005037 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5038 tpr->rx_jmb_prod_idx);
5039 }
5040 mmiowb();
5041 } else if (work_mask) {
5042 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
5043 * updated before the producer indices can be updated.
5044 */
5045 smp_wmb();
5046
Matt Carlson2c49a442010-09-30 10:34:35 +00005047 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
5048 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005049
Matt Carlsone4af1af2010-02-12 14:47:05 +00005050 if (tnapi != &tp->napi[1])
5051 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053
5054 return received;
5055}
5056
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005057static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00005060 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005061 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
5062
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 if (sblk->status & SD_STATUS_LINK_CHG) {
5064 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005065 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07005066 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005067 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07005068 tw32_f(MAC_STATUS,
5069 (MAC_STATUS_SYNC_CHANGED |
5070 MAC_STATUS_CFG_CHANGED |
5071 MAC_STATUS_MI_COMPLETION |
5072 MAC_STATUS_LNKSTATE_CHANGED));
5073 udelay(40);
5074 } else
5075 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07005076 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 }
5078 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005079}
5080
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005081static int tg3_rx_prodring_xfer(struct tg3 *tp,
5082 struct tg3_rx_prodring_set *dpr,
5083 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005084{
5085 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005086 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005087
5088 while (1) {
5089 src_prod_idx = spr->rx_std_prod_idx;
5090
5091 /* Make sure updates to the rx_std_buffers[] entries and the
5092 * standard producer index are seen in the correct order.
5093 */
5094 smp_rmb();
5095
5096 if (spr->rx_std_cons_idx == src_prod_idx)
5097 break;
5098
5099 if (spr->rx_std_cons_idx < src_prod_idx)
5100 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
5101 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005102 cpycnt = tp->rx_std_ring_mask + 1 -
5103 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005104
Matt Carlson2c49a442010-09-30 10:34:35 +00005105 cpycnt = min(cpycnt,
5106 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005107
5108 si = spr->rx_std_cons_idx;
5109 di = dpr->rx_std_prod_idx;
5110
Matt Carlsone92967b2010-02-12 14:47:06 +00005111 for (i = di; i < di + cpycnt; i++) {
5112 if (dpr->rx_std_buffers[i].skb) {
5113 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005114 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005115 break;
5116 }
5117 }
5118
5119 if (!cpycnt)
5120 break;
5121
5122 /* Ensure that updates to the rx_std_buffers ring and the
5123 * shadowed hardware producer ring from tg3_recycle_skb() are
5124 * ordered correctly WRT the skb check above.
5125 */
5126 smp_rmb();
5127
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005128 memcpy(&dpr->rx_std_buffers[di],
5129 &spr->rx_std_buffers[si],
5130 cpycnt * sizeof(struct ring_info));
5131
5132 for (i = 0; i < cpycnt; i++, di++, si++) {
5133 struct tg3_rx_buffer_desc *sbd, *dbd;
5134 sbd = &spr->rx_std[si];
5135 dbd = &dpr->rx_std[di];
5136 dbd->addr_hi = sbd->addr_hi;
5137 dbd->addr_lo = sbd->addr_lo;
5138 }
5139
Matt Carlson2c49a442010-09-30 10:34:35 +00005140 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
5141 tp->rx_std_ring_mask;
5142 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
5143 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005144 }
5145
5146 while (1) {
5147 src_prod_idx = spr->rx_jmb_prod_idx;
5148
5149 /* Make sure updates to the rx_jmb_buffers[] entries and
5150 * the jumbo producer index are seen in the correct order.
5151 */
5152 smp_rmb();
5153
5154 if (spr->rx_jmb_cons_idx == src_prod_idx)
5155 break;
5156
5157 if (spr->rx_jmb_cons_idx < src_prod_idx)
5158 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
5159 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005160 cpycnt = tp->rx_jmb_ring_mask + 1 -
5161 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005162
5163 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00005164 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005165
5166 si = spr->rx_jmb_cons_idx;
5167 di = dpr->rx_jmb_prod_idx;
5168
Matt Carlsone92967b2010-02-12 14:47:06 +00005169 for (i = di; i < di + cpycnt; i++) {
5170 if (dpr->rx_jmb_buffers[i].skb) {
5171 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005172 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005173 break;
5174 }
5175 }
5176
5177 if (!cpycnt)
5178 break;
5179
5180 /* Ensure that updates to the rx_jmb_buffers ring and the
5181 * shadowed hardware producer ring from tg3_recycle_skb() are
5182 * ordered correctly WRT the skb check above.
5183 */
5184 smp_rmb();
5185
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005186 memcpy(&dpr->rx_jmb_buffers[di],
5187 &spr->rx_jmb_buffers[si],
5188 cpycnt * sizeof(struct ring_info));
5189
5190 for (i = 0; i < cpycnt; i++, di++, si++) {
5191 struct tg3_rx_buffer_desc *sbd, *dbd;
5192 sbd = &spr->rx_jmb[si].std;
5193 dbd = &dpr->rx_jmb[di].std;
5194 dbd->addr_hi = sbd->addr_hi;
5195 dbd->addr_lo = sbd->addr_lo;
5196 }
5197
Matt Carlson2c49a442010-09-30 10:34:35 +00005198 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
5199 tp->rx_jmb_ring_mask;
5200 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
5201 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005202 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005203
5204 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005205}
5206
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005207static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
5208{
5209 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210
5211 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005212 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00005213 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00005214 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07005215 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 }
5217
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 /* run RX thread, within the bounds set by NAPI.
5219 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005220 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005222 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00005223 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224
Joe Perches63c3a662011-04-26 08:12:10 +00005225 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005226 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005227 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00005228 u32 std_prod_idx = dpr->rx_std_prod_idx;
5229 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005230
Matt Carlsone4af1af2010-02-12 14:47:05 +00005231 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005232 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00005233 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005234
5235 wmb();
5236
Matt Carlsone4af1af2010-02-12 14:47:05 +00005237 if (std_prod_idx != dpr->rx_std_prod_idx)
5238 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5239 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005240
Matt Carlsone4af1af2010-02-12 14:47:05 +00005241 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5242 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5243 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005244
5245 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005246
5247 if (err)
5248 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005249 }
5250
David S. Miller6f535762007-10-11 18:08:29 -07005251 return work_done;
5252}
David S. Millerf7383c22005-05-18 22:50:53 -07005253
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005254static int tg3_poll_msix(struct napi_struct *napi, int budget)
5255{
5256 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5257 struct tg3 *tp = tnapi->tp;
5258 int work_done = 0;
5259 struct tg3_hw_status *sblk = tnapi->hw_status;
5260
5261 while (1) {
5262 work_done = tg3_poll_work(tnapi, work_done, budget);
5263
Joe Perches63c3a662011-04-26 08:12:10 +00005264 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005265 goto tx_recovery;
5266
5267 if (unlikely(work_done >= budget))
5268 break;
5269
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005270 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005271 * to tell the hw how much work has been processed,
5272 * so we must read it before checking for more work.
5273 */
5274 tnapi->last_tag = sblk->status_tag;
5275 tnapi->last_irq_tag = tnapi->last_tag;
5276 rmb();
5277
5278 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005279 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5280 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005281 napi_complete(napi);
5282 /* Reenable interrupts. */
5283 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5284 mmiowb();
5285 break;
5286 }
5287 }
5288
5289 return work_done;
5290
5291tx_recovery:
5292 /* work_done is guaranteed to be less than budget. */
5293 napi_complete(napi);
5294 schedule_work(&tp->reset_task);
5295 return work_done;
5296}
5297
Matt Carlsone64de4e2011-04-13 11:05:05 +00005298static void tg3_process_error(struct tg3 *tp)
5299{
5300 u32 val;
5301 bool real_error = false;
5302
Joe Perches63c3a662011-04-26 08:12:10 +00005303 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00005304 return;
5305
5306 /* Check Flow Attention register */
5307 val = tr32(HOSTCC_FLOW_ATTN);
5308 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
5309 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
5310 real_error = true;
5311 }
5312
5313 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
5314 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
5315 real_error = true;
5316 }
5317
5318 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
5319 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
5320 real_error = true;
5321 }
5322
5323 if (!real_error)
5324 return;
5325
5326 tg3_dump_state(tp);
5327
Joe Perches63c3a662011-04-26 08:12:10 +00005328 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsone64de4e2011-04-13 11:05:05 +00005329 schedule_work(&tp->reset_task);
5330}
5331
David S. Miller6f535762007-10-11 18:08:29 -07005332static int tg3_poll(struct napi_struct *napi, int budget)
5333{
Matt Carlson8ef04422009-08-28 14:01:37 +00005334 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5335 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07005336 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00005337 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07005338
5339 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00005340 if (sblk->status & SD_STATUS_ERROR)
5341 tg3_process_error(tp);
5342
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005343 tg3_poll_link(tp);
5344
Matt Carlson17375d22009-08-28 14:02:18 +00005345 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07005346
Joe Perches63c3a662011-04-26 08:12:10 +00005347 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07005348 goto tx_recovery;
5349
5350 if (unlikely(work_done >= budget))
5351 break;
5352
Joe Perches63c3a662011-04-26 08:12:10 +00005353 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00005354 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07005355 * to tell the hw how much work has been processed,
5356 * so we must read it before checking for more work.
5357 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005358 tnapi->last_tag = sblk->status_tag;
5359 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07005360 rmb();
5361 } else
5362 sblk->status &= ~SD_STATUS_UPDATED;
5363
Matt Carlson17375d22009-08-28 14:02:18 +00005364 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005365 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00005366 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005367 break;
5368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 }
5370
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005371 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07005372
5373tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07005374 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08005375 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07005376 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07005377 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378}
5379
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005380static void tg3_napi_disable(struct tg3 *tp)
5381{
5382 int i;
5383
5384 for (i = tp->irq_cnt - 1; i >= 0; i--)
5385 napi_disable(&tp->napi[i].napi);
5386}
5387
5388static void tg3_napi_enable(struct tg3 *tp)
5389{
5390 int i;
5391
5392 for (i = 0; i < tp->irq_cnt; i++)
5393 napi_enable(&tp->napi[i].napi);
5394}
5395
5396static void tg3_napi_init(struct tg3 *tp)
5397{
5398 int i;
5399
5400 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
5401 for (i = 1; i < tp->irq_cnt; i++)
5402 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
5403}
5404
5405static void tg3_napi_fini(struct tg3 *tp)
5406{
5407 int i;
5408
5409 for (i = 0; i < tp->irq_cnt; i++)
5410 netif_napi_del(&tp->napi[i].napi);
5411}
5412
5413static inline void tg3_netif_stop(struct tg3 *tp)
5414{
5415 tp->dev->trans_start = jiffies; /* prevent tx timeout */
5416 tg3_napi_disable(tp);
5417 netif_tx_disable(tp->dev);
5418}
5419
5420static inline void tg3_netif_start(struct tg3 *tp)
5421{
5422 /* NOTE: unconditional netif_tx_wake_all_queues is only
5423 * appropriate so long as all callers are assured to
5424 * have free tx slots (such as after tg3_init_hw)
5425 */
5426 netif_tx_wake_all_queues(tp->dev);
5427
5428 tg3_napi_enable(tp);
5429 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
5430 tg3_enable_ints(tp);
5431}
5432
David S. Millerf47c11e2005-06-24 20:18:35 -07005433static void tg3_irq_quiesce(struct tg3 *tp)
5434{
Matt Carlson4f125f42009-09-01 12:55:02 +00005435 int i;
5436
David S. Millerf47c11e2005-06-24 20:18:35 -07005437 BUG_ON(tp->irq_sync);
5438
5439 tp->irq_sync = 1;
5440 smp_mb();
5441
Matt Carlson4f125f42009-09-01 12:55:02 +00005442 for (i = 0; i < tp->irq_cnt; i++)
5443 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07005444}
5445
David S. Millerf47c11e2005-06-24 20:18:35 -07005446/* Fully shutdown all tg3 driver activity elsewhere in the system.
5447 * If irq_sync is non-zero, then the IRQ handler must be synchronized
5448 * with as well. Most of the time, this is not necessary except when
5449 * shutting down the device.
5450 */
5451static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
5452{
Michael Chan46966542007-07-11 19:47:19 -07005453 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07005454 if (irq_sync)
5455 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005456}
5457
5458static inline void tg3_full_unlock(struct tg3 *tp)
5459{
David S. Millerf47c11e2005-06-24 20:18:35 -07005460 spin_unlock_bh(&tp->lock);
5461}
5462
Michael Chanfcfa0a32006-03-20 22:28:41 -08005463/* One-shot MSI handler - Chip automatically disables interrupt
5464 * after sending MSI so driver doesn't have to do it.
5465 */
David Howells7d12e782006-10-05 14:55:46 +01005466static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08005467{
Matt Carlson09943a12009-08-28 14:01:57 +00005468 struct tg3_napi *tnapi = dev_id;
5469 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08005470
Matt Carlson898a56f2009-08-28 14:02:40 +00005471 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005472 if (tnapi->rx_rcb)
5473 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005474
5475 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005476 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005477
5478 return IRQ_HANDLED;
5479}
5480
Michael Chan88b06bc22005-04-21 17:13:25 -07005481/* MSI ISR - No need to check for interrupt sharing and no need to
5482 * flush status block and interrupt mailbox. PCI ordering rules
5483 * guarantee that MSI will arrive after the status block.
5484 */
David Howells7d12e782006-10-05 14:55:46 +01005485static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07005486{
Matt Carlson09943a12009-08-28 14:01:57 +00005487 struct tg3_napi *tnapi = dev_id;
5488 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07005489
Matt Carlson898a56f2009-08-28 14:02:40 +00005490 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005491 if (tnapi->rx_rcb)
5492 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07005493 /*
David S. Millerfac9b832005-05-18 22:46:34 -07005494 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07005495 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07005496 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07005497 * NIC to stop sending us irqs, engaging "in-intr-handler"
5498 * event coalescing.
5499 */
5500 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07005501 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005502 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07005503
Michael Chan88b06bc22005-04-21 17:13:25 -07005504 return IRQ_RETVAL(1);
5505}
5506
David Howells7d12e782006-10-05 14:55:46 +01005507static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508{
Matt Carlson09943a12009-08-28 14:01:57 +00005509 struct tg3_napi *tnapi = dev_id;
5510 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005511 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512 unsigned int handled = 1;
5513
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 /* In INTx mode, it is possible for the interrupt to arrive at
5515 * the CPU before the status block posted prior to the interrupt.
5516 * Reading the PCI State register will confirm whether the
5517 * interrupt is ours and will flush the status block.
5518 */
Michael Chand18edcb2007-03-24 20:57:11 -07005519 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00005520 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07005521 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5522 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005523 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07005524 }
Michael Chand18edcb2007-03-24 20:57:11 -07005525 }
5526
5527 /*
5528 * Writing any value to intr-mbox-0 clears PCI INTA# and
5529 * chip-internal interrupt pending events.
5530 * Writing non-zero to intr-mbox-0 additional tells the
5531 * NIC to stop sending us irqs, engaging "in-intr-handler"
5532 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005533 *
5534 * Flush the mailbox to de-assert the IRQ immediately to prevent
5535 * spurious interrupts. The flush impacts performance but
5536 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005537 */
Michael Chanc04cb342007-05-07 00:26:15 -07005538 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07005539 if (tg3_irq_sync(tp))
5540 goto out;
5541 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00005542 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00005543 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00005544 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07005545 } else {
5546 /* No work, shared interrupt perhaps? re-enable
5547 * interrupts, and flush that PCI write
5548 */
5549 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
5550 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07005551 }
David S. Millerf47c11e2005-06-24 20:18:35 -07005552out:
David S. Millerfac9b832005-05-18 22:46:34 -07005553 return IRQ_RETVAL(handled);
5554}
5555
David Howells7d12e782006-10-05 14:55:46 +01005556static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07005557{
Matt Carlson09943a12009-08-28 14:01:57 +00005558 struct tg3_napi *tnapi = dev_id;
5559 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005560 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07005561 unsigned int handled = 1;
5562
David S. Millerfac9b832005-05-18 22:46:34 -07005563 /* In INTx mode, it is possible for the interrupt to arrive at
5564 * the CPU before the status block posted prior to the interrupt.
5565 * Reading the PCI State register will confirm whether the
5566 * interrupt is ours and will flush the status block.
5567 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005568 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00005569 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07005570 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5571 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005572 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 }
Michael Chand18edcb2007-03-24 20:57:11 -07005574 }
5575
5576 /*
5577 * writing any value to intr-mbox-0 clears PCI INTA# and
5578 * chip-internal interrupt pending events.
5579 * writing non-zero to intr-mbox-0 additional tells the
5580 * NIC to stop sending us irqs, engaging "in-intr-handler"
5581 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005582 *
5583 * Flush the mailbox to de-assert the IRQ immediately to prevent
5584 * spurious interrupts. The flush impacts performance but
5585 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005586 */
Michael Chanc04cb342007-05-07 00:26:15 -07005587 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00005588
5589 /*
5590 * In a shared interrupt configuration, sometimes other devices'
5591 * interrupts will scream. We record the current status tag here
5592 * so that the above check can report that the screaming interrupts
5593 * are unhandled. Eventually they will be silenced.
5594 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005595 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00005596
Michael Chand18edcb2007-03-24 20:57:11 -07005597 if (tg3_irq_sync(tp))
5598 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00005599
Matt Carlson72334482009-08-28 14:03:01 +00005600 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00005601
Matt Carlson09943a12009-08-28 14:01:57 +00005602 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00005603
David S. Millerf47c11e2005-06-24 20:18:35 -07005604out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 return IRQ_RETVAL(handled);
5606}
5607
Michael Chan79381092005-04-21 17:13:59 -07005608/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01005609static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07005610{
Matt Carlson09943a12009-08-28 14:01:57 +00005611 struct tg3_napi *tnapi = dev_id;
5612 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005613 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07005614
Michael Chanf9804dd2005-09-27 12:13:10 -07005615 if ((sblk->status & SD_STATUS_UPDATED) ||
5616 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07005617 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07005618 return IRQ_RETVAL(1);
5619 }
5620 return IRQ_RETVAL(0);
5621}
5622
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07005623static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07005624static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625
Michael Chanb9ec6c12006-07-25 16:37:27 -07005626/* Restart hardware after configuration changes, self-test, etc.
5627 * Invoked with tp->lock held.
5628 */
5629static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07005630 __releases(tp->lock)
5631 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005632{
5633 int err;
5634
5635 err = tg3_init_hw(tp, reset_phy);
5636 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00005637 netdev_err(tp->dev,
5638 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07005639 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
5640 tg3_full_unlock(tp);
5641 del_timer_sync(&tp->timer);
5642 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00005643 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005644 dev_close(tp->dev);
5645 tg3_full_lock(tp, 0);
5646 }
5647 return err;
5648}
5649
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650#ifdef CONFIG_NET_POLL_CONTROLLER
5651static void tg3_poll_controller(struct net_device *dev)
5652{
Matt Carlson4f125f42009-09-01 12:55:02 +00005653 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07005654 struct tg3 *tp = netdev_priv(dev);
5655
Matt Carlson4f125f42009-09-01 12:55:02 +00005656 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00005657 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658}
5659#endif
5660
David Howellsc4028952006-11-22 14:57:56 +00005661static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662{
David Howellsc4028952006-11-22 14:57:56 +00005663 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005664 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 unsigned int restart_timer;
5666
Michael Chan7faa0062006-02-02 17:29:28 -08005667 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08005668
5669 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08005670 tg3_full_unlock(tp);
5671 return;
5672 }
5673
5674 tg3_full_unlock(tp);
5675
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005676 tg3_phy_stop(tp);
5677
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 tg3_netif_stop(tp);
5679
David S. Millerf47c11e2005-06-24 20:18:35 -07005680 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681
Joe Perches63c3a662011-04-26 08:12:10 +00005682 restart_timer = tg3_flag(tp, RESTART_TIMER);
5683 tg3_flag_clear(tp, RESTART_TIMER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684
Joe Perches63c3a662011-04-26 08:12:10 +00005685 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
Michael Chandf3e6542006-05-26 17:48:07 -07005686 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5687 tp->write32_rx_mbox = tg3_write_flush_reg32;
Joe Perches63c3a662011-04-26 08:12:10 +00005688 tg3_flag_set(tp, MBOX_WRITE_REORDER);
5689 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005690 }
5691
Michael Chan944d9802005-05-29 14:57:48 -07005692 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005693 err = tg3_init_hw(tp, 1);
5694 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005695 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696
5697 tg3_netif_start(tp);
5698
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 if (restart_timer)
5700 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08005701
Michael Chanb9ec6c12006-07-25 16:37:27 -07005702out:
Michael Chan7faa0062006-02-02 17:29:28 -08005703 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005704
5705 if (!err)
5706 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707}
5708
5709static void tg3_tx_timeout(struct net_device *dev)
5710{
5711 struct tg3 *tp = netdev_priv(dev);
5712
Michael Chanb0408752007-02-13 12:18:30 -08005713 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00005714 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00005715 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08005716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717
5718 schedule_work(&tp->reset_task);
5719}
5720
Michael Chanc58ec932005-09-17 00:46:27 -07005721/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5722static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5723{
5724 u32 base = (u32) mapping & 0xffffffff;
5725
Eric Dumazet807540b2010-09-23 05:40:09 +00005726 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07005727}
5728
Michael Chan72f2afb2006-03-06 19:28:35 -08005729/* Test for DMA addresses > 40-bit */
5730static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5731 int len)
5732{
5733#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00005734 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00005735 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08005736 return 0;
5737#else
5738 return 0;
5739#endif
5740}
5741
Matt Carlson2ffcc982011-05-19 12:12:44 +00005742static void tg3_set_txd(struct tg3_napi *tnapi, int entry,
5743 dma_addr_t mapping, int len, u32 flags,
5744 u32 mss_and_is_end)
5745{
5746 struct tg3_tx_buffer_desc *txd = &tnapi->tx_ring[entry];
5747 int is_end = (mss_and_is_end & 0x1);
5748 u32 mss = (mss_and_is_end >> 1);
5749 u32 vlan_tag = 0;
5750
5751 if (is_end)
5752 flags |= TXD_FLAG_END;
5753 if (flags & TXD_FLAG_VLAN) {
5754 vlan_tag = flags >> 16;
5755 flags &= 0xffff;
5756 }
5757 vlan_tag |= (mss << TXD_MSS_SHIFT);
5758
5759 txd->addr_hi = ((u64) mapping >> 32);
5760 txd->addr_lo = ((u64) mapping & 0xffffffff);
5761 txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
5762 txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
5763}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764
Matt Carlson432aa7e2011-05-19 12:12:45 +00005765static void tg3_skb_error_unmap(struct tg3_napi *tnapi,
5766 struct sk_buff *skb, int last)
5767{
5768 int i;
5769 u32 entry = tnapi->tx_prod;
5770 struct ring_info *txb = &tnapi->tx_buffers[entry];
5771
5772 pci_unmap_single(tnapi->tp->pdev,
5773 dma_unmap_addr(txb, mapping),
5774 skb_headlen(skb),
5775 PCI_DMA_TODEVICE);
5776 for (i = 0; i <= last; i++) {
5777 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5778
5779 entry = NEXT_TX(entry);
5780 txb = &tnapi->tx_buffers[entry];
5781
5782 pci_unmap_page(tnapi->tp->pdev,
5783 dma_unmap_addr(txb, mapping),
5784 frag->size, PCI_DMA_TODEVICE);
5785 }
5786}
5787
Michael Chan72f2afb2006-03-06 19:28:35 -08005788/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005789static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
Matt Carlson432aa7e2011-05-19 12:12:45 +00005790 struct sk_buff *skb,
5791 u32 base_flags, u32 mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792{
Matt Carlson24f4efd2009-11-13 13:03:35 +00005793 struct tg3 *tp = tnapi->tp;
Matt Carlson41588ba2008-04-19 18:12:33 -07005794 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07005795 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00005796 u32 entry = tnapi->tx_prod;
5797 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798
Matt Carlson41588ba2008-04-19 18:12:33 -07005799 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
5800 new_skb = skb_copy(skb, GFP_ATOMIC);
5801 else {
5802 int more_headroom = 4 - ((unsigned long)skb->data & 3);
5803
5804 new_skb = skb_copy_expand(skb,
5805 skb_headroom(skb) + more_headroom,
5806 skb_tailroom(skb), GFP_ATOMIC);
5807 }
5808
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07005810 ret = -1;
5811 } else {
5812 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00005813 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
5814 PCI_DMA_TODEVICE);
5815 /* Make sure the mapping succeeded */
5816 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
5817 ret = -1;
5818 dev_kfree_skb(new_skb);
David S. Miller90079ce2008-09-11 04:52:51 -07005819
Michael Chanc58ec932005-09-17 00:46:27 -07005820 /* Make sure new skb does not cross any 4G boundaries.
5821 * Drop the packet if it does.
5822 */
Joe Perches63c3a662011-04-26 08:12:10 +00005823 } else if (tg3_flag(tp, 4G_DMA_BNDRY_BUG) &&
5824 tg3_4g_overflow_test(new_addr, new_skb->len)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00005825 pci_unmap_single(tp->pdev, new_addr, new_skb->len,
5826 PCI_DMA_TODEVICE);
Michael Chanc58ec932005-09-17 00:46:27 -07005827 ret = -1;
5828 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07005829 } else {
Matt Carlson432aa7e2011-05-19 12:12:45 +00005830 tnapi->tx_buffers[entry].skb = new_skb;
5831 dma_unmap_addr_set(&tnapi->tx_buffers[entry],
5832 mapping, new_addr);
5833
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005834 tg3_set_txd(tnapi, entry, new_addr, new_skb->len,
Michael Chanc58ec932005-09-17 00:46:27 -07005835 base_flags, 1 | (mss << 1));
Michael Chanc58ec932005-09-17 00:46:27 -07005836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837 }
5838
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839 dev_kfree_skb(skb);
5840
Michael Chanc58ec932005-09-17 00:46:27 -07005841 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842}
5843
Matt Carlson2ffcc982011-05-19 12:12:44 +00005844static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07005845
5846/* Use GSO to workaround a rare TSO bug that may be triggered when the
5847 * TSO header is greater than 80 bytes.
5848 */
5849static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
5850{
5851 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005852 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07005853
5854 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005855 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07005856 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00005857
5858 /* netif_tx_stop_queue() must be done before checking
5859 * checking tx index in tg3_tx_avail() below, because in
5860 * tg3_tx(), we update tx index before checking for
5861 * netif_tx_queue_stopped().
5862 */
5863 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005864 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08005865 return NETDEV_TX_BUSY;
5866
5867 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005868 }
5869
5870 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07005871 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07005872 goto tg3_tso_bug_end;
5873
5874 do {
5875 nskb = segs;
5876 segs = segs->next;
5877 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00005878 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005879 } while (segs);
5880
5881tg3_tso_bug_end:
5882 dev_kfree_skb(skb);
5883
5884 return NETDEV_TX_OK;
5885}
Michael Chan52c0fd82006-06-29 20:15:54 -07005886
Michael Chan5a6f3072006-03-20 22:28:05 -08005887/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00005888 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08005889 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00005890static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08005891{
5892 struct tg3 *tp = netdev_priv(dev);
Michael Chan5a6f3072006-03-20 22:28:05 -08005893 u32 len, entry, base_flags, mss;
Matt Carlson432aa7e2011-05-19 12:12:45 +00005894 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07005895 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005896 struct tg3_napi *tnapi;
5897 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00005898 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005899
Matt Carlson24f4efd2009-11-13 13:03:35 +00005900 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5901 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00005902 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00005903 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904
Michael Chan00b70502006-06-17 21:58:45 -07005905 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005906 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07005907 * interrupt. Furthermore, IRQ processing runs lockless so we have
5908 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005910 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00005911 if (!netif_tx_queue_stopped(txq)) {
5912 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005913
5914 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00005915 netdev_err(dev,
5916 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918 return NETDEV_TX_BUSY;
5919 }
5920
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005921 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07005923 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005925
Matt Carlsonbe98da62010-07-11 09:31:46 +00005926 mss = skb_shinfo(skb)->gso_size;
5927 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005928 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00005929 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930
5931 if (skb_header_cloned(skb) &&
5932 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5933 dev_kfree_skb(skb);
5934 goto out_unlock;
5935 }
5936
Matt Carlson34195c32010-07-11 09:31:42 +00005937 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005938 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939
Matt Carlson02e96082010-09-15 08:59:59 +00005940 if (skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00005941 hdr_len = skb_headlen(skb) - ETH_HLEN;
5942 } else {
5943 u32 ip_tcp_len;
5944
5945 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
5946 hdr_len = ip_tcp_len + tcp_opt_len;
5947
5948 iph->check = 0;
5949 iph->tot_len = htons(mss + hdr_len);
5950 }
5951
Michael Chan52c0fd82006-06-29 20:15:54 -07005952 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00005953 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00005954 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07005955
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5957 TXD_FLAG_CPU_POST_DMA);
5958
Joe Perches63c3a662011-04-26 08:12:10 +00005959 if (tg3_flag(tp, HW_TSO_1) ||
5960 tg3_flag(tp, HW_TSO_2) ||
5961 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005962 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005964 } else
5965 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
5966 iph->daddr, 0,
5967 IPPROTO_TCP,
5968 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969
Joe Perches63c3a662011-04-26 08:12:10 +00005970 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00005971 mss |= (hdr_len & 0xc) << 12;
5972 if (hdr_len & 0x10)
5973 base_flags |= 0x00000010;
5974 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00005975 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005976 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00005977 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005978 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005979 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980 int tsflags;
5981
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005982 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983 mss |= (tsflags << 11);
5984 }
5985 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005986 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987 int tsflags;
5988
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005989 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990 base_flags |= tsflags << 12;
5991 }
5992 }
5993 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00005994
Jesse Grosseab6d182010-10-20 13:56:03 +00005995 if (vlan_tx_tag_present(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996 base_flags |= (TXD_FLAG_VLAN |
5997 (vlan_tx_tag_get(skb) << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998
Joe Perches63c3a662011-04-26 08:12:10 +00005999 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
Matt Carlson8fc2f992010-12-06 08:28:49 +00006000 !mss && skb->len > VLAN_ETH_FRAME_LEN)
Matt Carlson615774f2009-11-13 13:03:39 +00006001 base_flags |= TXD_FLAG_JMB_PKT;
6002
Alexander Duyckf4188d82009-12-02 16:48:38 +00006003 len = skb_headlen(skb);
6004
6005 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
6006 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07006007 dev_kfree_skb(skb);
6008 goto out_unlock;
6009 }
6010
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006011 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006012 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013
6014 would_hit_hwbug = 0;
6015
Joe Perches63c3a662011-04-26 08:12:10 +00006016 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006017 would_hit_hwbug = 1;
6018
Joe Perches63c3a662011-04-26 08:12:10 +00006019 if (tg3_flag(tp, 4G_DMA_BNDRY_BUG) &&
Matt Carlson0e1406d2009-11-02 12:33:33 +00006020 tg3_4g_overflow_test(mapping, len))
Matt Carlson41588ba2008-04-19 18:12:33 -07006021 would_hit_hwbug = 1;
Matt Carlson0e1406d2009-11-02 12:33:33 +00006022
Joe Perches63c3a662011-04-26 08:12:10 +00006023 if (tg3_flag(tp, 40BIT_DMA_LIMIT_BUG) &&
Matt Carlson0e1406d2009-11-02 12:33:33 +00006024 tg3_40bit_overflow_test(tp, mapping, len))
6025 would_hit_hwbug = 1;
6026
Joe Perches63c3a662011-04-26 08:12:10 +00006027 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07006028 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006030 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
6032
6033 entry = NEXT_TX(entry);
6034
6035 /* Now loop through additional data fragments, and queue them. */
6036 if (skb_shinfo(skb)->nr_frags > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037 last = skb_shinfo(skb)->nr_frags - 1;
6038 for (i = 0; i <= last; i++) {
6039 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6040
6041 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006042 mapping = pci_map_page(tp->pdev,
6043 frag->page,
6044 frag->page_offset,
6045 len, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006047 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006048 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006049 mapping);
6050 if (pci_dma_mapping_error(tp->pdev, mapping))
6051 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052
Joe Perches63c3a662011-04-26 08:12:10 +00006053 if (tg3_flag(tp, SHORT_DMA_BUG) &&
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006054 len <= 8)
6055 would_hit_hwbug = 1;
6056
Joe Perches63c3a662011-04-26 08:12:10 +00006057 if (tg3_flag(tp, 4G_DMA_BNDRY_BUG) &&
Matt Carlson0e1406d2009-11-02 12:33:33 +00006058 tg3_4g_overflow_test(mapping, len))
Michael Chanc58ec932005-09-17 00:46:27 -07006059 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060
Joe Perches63c3a662011-04-26 08:12:10 +00006061 if (tg3_flag(tp, 40BIT_DMA_LIMIT_BUG) &&
Matt Carlson0e1406d2009-11-02 12:33:33 +00006062 tg3_40bit_overflow_test(tp, mapping, len))
Michael Chan72f2afb2006-03-06 19:28:35 -08006063 would_hit_hwbug = 1;
6064
Joe Perches63c3a662011-04-26 08:12:10 +00006065 if (tg3_flag(tp, HW_TSO_1) ||
6066 tg3_flag(tp, HW_TSO_2) ||
6067 tg3_flag(tp, HW_TSO_3))
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006068 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069 base_flags, (i == last)|(mss << 1));
6070 else
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006071 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 base_flags, (i == last));
6073
6074 entry = NEXT_TX(entry);
6075 }
6076 }
6077
6078 if (would_hit_hwbug) {
Matt Carlson432aa7e2011-05-19 12:12:45 +00006079 tg3_skb_error_unmap(tnapi, skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080
6081 /* If the workaround fails due to memory/mapping
6082 * failure, silently drop this packet.
6083 */
Matt Carlson432aa7e2011-05-19 12:12:45 +00006084 if (tigon3_dma_hwbug_workaround(tnapi, skb, base_flags, mss))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085 goto out_unlock;
6086
Matt Carlson432aa7e2011-05-19 12:12:45 +00006087 entry = NEXT_TX(tnapi->tx_prod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088 }
6089
6090 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006091 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006093 tnapi->tx_prod = entry;
6094 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006095 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006096
6097 /* netif_tx_stop_queue() must be done before checking
6098 * checking tx index in tg3_tx_avail() below, because in
6099 * tg3_tx(), we update tx index before checking for
6100 * netif_tx_queue_stopped().
6101 */
6102 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006103 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006104 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106
6107out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00006108 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109
6110 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006111
6112dma_error:
Matt Carlson432aa7e2011-05-19 12:12:45 +00006113 tg3_skb_error_unmap(tnapi, skb, i);
Alexander Duyckf4188d82009-12-02 16:48:38 +00006114 dev_kfree_skb(skb);
Matt Carlson432aa7e2011-05-19 12:12:45 +00006115 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006116 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117}
6118
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006119static void tg3_set_loopback(struct net_device *dev, u32 features)
6120{
6121 struct tg3 *tp = netdev_priv(dev);
6122
6123 if (features & NETIF_F_LOOPBACK) {
6124 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
6125 return;
6126
6127 /*
6128 * Clear MAC_MODE_HALF_DUPLEX or you won't get packets back in
6129 * loopback mode if Half-Duplex mode was negotiated earlier.
6130 */
6131 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
6132
6133 /* Enable internal MAC loopback mode */
6134 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
6135 spin_lock_bh(&tp->lock);
6136 tw32(MAC_MODE, tp->mac_mode);
6137 netif_carrier_on(tp->dev);
6138 spin_unlock_bh(&tp->lock);
6139 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
6140 } else {
6141 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
6142 return;
6143
6144 /* Disable internal MAC loopback mode */
6145 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
6146 spin_lock_bh(&tp->lock);
6147 tw32(MAC_MODE, tp->mac_mode);
6148 /* Force link status check */
6149 tg3_setup_phy(tp, 1);
6150 spin_unlock_bh(&tp->lock);
6151 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
6152 }
6153}
6154
Michał Mirosławdc668912011-04-07 03:35:07 +00006155static u32 tg3_fix_features(struct net_device *dev, u32 features)
6156{
6157 struct tg3 *tp = netdev_priv(dev);
6158
Joe Perches63c3a662011-04-26 08:12:10 +00006159 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00006160 features &= ~NETIF_F_ALL_TSO;
6161
6162 return features;
6163}
6164
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006165static int tg3_set_features(struct net_device *dev, u32 features)
6166{
6167 u32 changed = dev->features ^ features;
6168
6169 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
6170 tg3_set_loopback(dev, features);
6171
6172 return 0;
6173}
6174
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
6176 int new_mtu)
6177{
6178 dev->mtu = new_mtu;
6179
Michael Chanef7f5ec2005-07-25 12:32:25 -07006180 if (new_mtu > ETH_DATA_LEN) {
Joe Perches63c3a662011-04-26 08:12:10 +00006181 if (tg3_flag(tp, 5780_CLASS)) {
Michał Mirosławdc668912011-04-07 03:35:07 +00006182 netdev_update_features(dev);
Joe Perches63c3a662011-04-26 08:12:10 +00006183 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson859a588792010-04-05 10:19:28 +00006184 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00006185 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Matt Carlson859a588792010-04-05 10:19:28 +00006186 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07006187 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00006188 if (tg3_flag(tp, 5780_CLASS)) {
6189 tg3_flag_set(tp, TSO_CAPABLE);
Michał Mirosławdc668912011-04-07 03:35:07 +00006190 netdev_update_features(dev);
6191 }
Joe Perches63c3a662011-04-26 08:12:10 +00006192 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
Michael Chanef7f5ec2005-07-25 12:32:25 -07006193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194}
6195
6196static int tg3_change_mtu(struct net_device *dev, int new_mtu)
6197{
6198 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07006199 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200
6201 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
6202 return -EINVAL;
6203
6204 if (!netif_running(dev)) {
6205 /* We'll just catch it later when the
6206 * device is up'd.
6207 */
6208 tg3_set_mtu(dev, tp, new_mtu);
6209 return 0;
6210 }
6211
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006212 tg3_phy_stop(tp);
6213
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006215
6216 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217
Michael Chan944d9802005-05-29 14:57:48 -07006218 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219
6220 tg3_set_mtu(dev, tp, new_mtu);
6221
Michael Chanb9ec6c12006-07-25 16:37:27 -07006222 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223
Michael Chanb9ec6c12006-07-25 16:37:27 -07006224 if (!err)
6225 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226
David S. Millerf47c11e2005-06-24 20:18:35 -07006227 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006229 if (!err)
6230 tg3_phy_start(tp);
6231
Michael Chanb9ec6c12006-07-25 16:37:27 -07006232 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233}
6234
Matt Carlson21f581a2009-08-28 14:00:25 +00006235static void tg3_rx_prodring_free(struct tg3 *tp,
6236 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238 int i;
6239
Matt Carlson8fea32b2010-09-15 08:59:58 +00006240 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006241 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006242 i = (i + 1) & tp->rx_std_ring_mask)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006243 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6244 tp->rx_pkt_map_sz);
6245
Joe Perches63c3a662011-04-26 08:12:10 +00006246 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006247 for (i = tpr->rx_jmb_cons_idx;
6248 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006249 i = (i + 1) & tp->rx_jmb_ring_mask) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006250 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6251 TG3_RX_JMB_MAP_SZ);
6252 }
6253 }
6254
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006255 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257
Matt Carlson2c49a442010-09-30 10:34:35 +00006258 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006259 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6260 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261
Joe Perches63c3a662011-04-26 08:12:10 +00006262 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006263 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006264 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6265 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006266 }
6267}
6268
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006269/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006270 *
6271 * The chip has been shut down and the driver detached from
6272 * the networking, so no interrupts or new tx packets will
6273 * end up in the driver. tp->{tx,}lock are held and thus
6274 * we may not sleep.
6275 */
Matt Carlson21f581a2009-08-28 14:00:25 +00006276static int tg3_rx_prodring_alloc(struct tg3 *tp,
6277 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006278{
Matt Carlson287be122009-08-28 13:58:46 +00006279 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006281 tpr->rx_std_cons_idx = 0;
6282 tpr->rx_std_prod_idx = 0;
6283 tpr->rx_jmb_cons_idx = 0;
6284 tpr->rx_jmb_prod_idx = 0;
6285
Matt Carlson8fea32b2010-09-15 08:59:58 +00006286 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006287 memset(&tpr->rx_std_buffers[0], 0,
6288 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00006289 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006290 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00006291 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006292 goto done;
6293 }
6294
Linus Torvalds1da177e2005-04-16 15:20:36 -07006295 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00006296 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297
Matt Carlson287be122009-08-28 13:58:46 +00006298 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00006299 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00006300 tp->dev->mtu > ETH_DATA_LEN)
6301 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
6302 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07006303
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304 /* Initialize invariants of the rings, we only set this
6305 * stuff once. This works because the card does not
6306 * write into the rx buffer posting rings.
6307 */
Matt Carlson2c49a442010-09-30 10:34:35 +00006308 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309 struct tg3_rx_buffer_desc *rxd;
6310
Matt Carlson21f581a2009-08-28 14:00:25 +00006311 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00006312 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
6314 rxd->opaque = (RXD_OPAQUE_RING_STD |
6315 (i << RXD_OPAQUE_INDEX_SHIFT));
6316 }
6317
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006318 /* Now allocate fresh SKBs for each rx ring. */
6319 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson86b21e52009-11-13 13:03:45 +00006320 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006321 netdev_warn(tp->dev,
6322 "Using a smaller RX standard ring. Only "
6323 "%d out of %d buffers were allocated "
6324 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006325 if (i == 0)
6326 goto initfail;
6327 tp->rx_pending = i;
6328 break;
6329 }
6330 }
6331
Joe Perches63c3a662011-04-26 08:12:10 +00006332 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006333 goto done;
6334
Matt Carlson2c49a442010-09-30 10:34:35 +00006335 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006336
Joe Perches63c3a662011-04-26 08:12:10 +00006337 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00006338 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339
Matt Carlson2c49a442010-09-30 10:34:35 +00006340 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00006341 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006342
Matt Carlson0d86df82010-02-17 15:17:00 +00006343 rxd = &tpr->rx_jmb[i].std;
6344 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
6345 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
6346 RXD_FLAG_JUMBO;
6347 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
6348 (i << RXD_OPAQUE_INDEX_SHIFT));
6349 }
6350
6351 for (i = 0; i < tp->rx_jumbo_pending; i++) {
6352 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006353 netdev_warn(tp->dev,
6354 "Using a smaller RX jumbo ring. Only %d "
6355 "out of %d buffers were allocated "
6356 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00006357 if (i == 0)
6358 goto initfail;
6359 tp->rx_jumbo_pending = i;
6360 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361 }
6362 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006363
6364done:
Michael Chan32d8c572006-07-25 16:38:29 -07006365 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006366
6367initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00006368 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006369 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370}
6371
Matt Carlson21f581a2009-08-28 14:00:25 +00006372static void tg3_rx_prodring_fini(struct tg3 *tp,
6373 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374{
Matt Carlson21f581a2009-08-28 14:00:25 +00006375 kfree(tpr->rx_std_buffers);
6376 tpr->rx_std_buffers = NULL;
6377 kfree(tpr->rx_jmb_buffers);
6378 tpr->rx_jmb_buffers = NULL;
6379 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006380 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
6381 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006382 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383 }
Matt Carlson21f581a2009-08-28 14:00:25 +00006384 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006385 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
6386 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006387 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006389}
6390
Matt Carlson21f581a2009-08-28 14:00:25 +00006391static int tg3_rx_prodring_init(struct tg3 *tp,
6392 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006393{
Matt Carlson2c49a442010-09-30 10:34:35 +00006394 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
6395 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006396 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006397 return -ENOMEM;
6398
Matt Carlson4bae65c2010-11-24 08:31:52 +00006399 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
6400 TG3_RX_STD_RING_BYTES(tp),
6401 &tpr->rx_std_mapping,
6402 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006403 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006404 goto err_out;
6405
Joe Perches63c3a662011-04-26 08:12:10 +00006406 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006407 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00006408 GFP_KERNEL);
6409 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006410 goto err_out;
6411
Matt Carlson4bae65c2010-11-24 08:31:52 +00006412 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
6413 TG3_RX_JMB_RING_BYTES(tp),
6414 &tpr->rx_jmb_mapping,
6415 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006416 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006417 goto err_out;
6418 }
6419
6420 return 0;
6421
6422err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00006423 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006424 return -ENOMEM;
6425}
6426
6427/* Free up pending packets in all rx/tx rings.
6428 *
6429 * The chip has been shut down and the driver detached from
6430 * the networking, so no interrupts or new tx packets will
6431 * end up in the driver. tp->{tx,}lock is not held and we are not
6432 * in an interrupt context and thus may sleep.
6433 */
6434static void tg3_free_rings(struct tg3 *tp)
6435{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006436 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006437
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006438 for (j = 0; j < tp->irq_cnt; j++) {
6439 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006440
Matt Carlson8fea32b2010-09-15 08:59:58 +00006441 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00006442
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006443 if (!tnapi->tx_buffers)
6444 continue;
6445
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006446 for (i = 0; i < TG3_TX_RING_SIZE; ) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006447 struct ring_info *txp;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006448 struct sk_buff *skb;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006449 unsigned int k;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006450
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006451 txp = &tnapi->tx_buffers[i];
6452 skb = txp->skb;
6453
6454 if (skb == NULL) {
6455 i++;
6456 continue;
6457 }
6458
Alexander Duyckf4188d82009-12-02 16:48:38 +00006459 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006460 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006461 skb_headlen(skb),
6462 PCI_DMA_TODEVICE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006463 txp->skb = NULL;
6464
Alexander Duyckf4188d82009-12-02 16:48:38 +00006465 i++;
6466
6467 for (k = 0; k < skb_shinfo(skb)->nr_frags; k++) {
6468 txp = &tnapi->tx_buffers[i & (TG3_TX_RING_SIZE - 1)];
6469 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006470 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006471 skb_shinfo(skb)->frags[k].size,
6472 PCI_DMA_TODEVICE);
6473 i++;
6474 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006475
6476 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006477 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006478 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006479}
6480
6481/* Initialize tx/rx rings for packet processing.
6482 *
6483 * The chip has been shut down and the driver detached from
6484 * the networking, so no interrupts or new tx packets will
6485 * end up in the driver. tp->{tx,}lock are held and thus
6486 * we may not sleep.
6487 */
6488static int tg3_init_rings(struct tg3 *tp)
6489{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006490 int i;
Matt Carlson72334482009-08-28 14:03:01 +00006491
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006492 /* Free up all the SKBs. */
6493 tg3_free_rings(tp);
6494
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006495 for (i = 0; i < tp->irq_cnt; i++) {
6496 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006497
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006498 tnapi->last_tag = 0;
6499 tnapi->last_irq_tag = 0;
6500 tnapi->hw_status->status = 0;
6501 tnapi->hw_status->status_tag = 0;
6502 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6503
6504 tnapi->tx_prod = 0;
6505 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006506 if (tnapi->tx_ring)
6507 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006508
6509 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006510 if (tnapi->rx_rcb)
6511 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006512
Matt Carlson8fea32b2010-09-15 08:59:58 +00006513 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00006514 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006515 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006516 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006517 }
Matt Carlson72334482009-08-28 14:03:01 +00006518
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006519 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006520}
6521
6522/*
6523 * Must not be invoked with interrupt sources disabled and
6524 * the hardware shutdown down.
6525 */
6526static void tg3_free_consistent(struct tg3 *tp)
6527{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006528 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006529
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006530 for (i = 0; i < tp->irq_cnt; i++) {
6531 struct tg3_napi *tnapi = &tp->napi[i];
6532
6533 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006534 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006535 tnapi->tx_ring, tnapi->tx_desc_mapping);
6536 tnapi->tx_ring = NULL;
6537 }
6538
6539 kfree(tnapi->tx_buffers);
6540 tnapi->tx_buffers = NULL;
6541
6542 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006543 dma_free_coherent(&tp->pdev->dev,
6544 TG3_RX_RCB_RING_BYTES(tp),
6545 tnapi->rx_rcb,
6546 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006547 tnapi->rx_rcb = NULL;
6548 }
6549
Matt Carlson8fea32b2010-09-15 08:59:58 +00006550 tg3_rx_prodring_fini(tp, &tnapi->prodring);
6551
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006552 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006553 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
6554 tnapi->hw_status,
6555 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006556 tnapi->hw_status = NULL;
6557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006559
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006561 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
6562 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006563 tp->hw_stats = NULL;
6564 }
6565}
6566
6567/*
6568 * Must not be invoked with interrupt sources disabled and
6569 * the hardware shutdown down. Can sleep.
6570 */
6571static int tg3_alloc_consistent(struct tg3 *tp)
6572{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006573 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006574
Matt Carlson4bae65c2010-11-24 08:31:52 +00006575 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
6576 sizeof(struct tg3_hw_stats),
6577 &tp->stats_mapping,
6578 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579 if (!tp->hw_stats)
6580 goto err_out;
6581
Linus Torvalds1da177e2005-04-16 15:20:36 -07006582 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6583
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006584 for (i = 0; i < tp->irq_cnt; i++) {
6585 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006586 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006587
Matt Carlson4bae65c2010-11-24 08:31:52 +00006588 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
6589 TG3_HW_STATUS_SIZE,
6590 &tnapi->status_mapping,
6591 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006592 if (!tnapi->hw_status)
6593 goto err_out;
6594
6595 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006596 sblk = tnapi->hw_status;
6597
Matt Carlson8fea32b2010-09-15 08:59:58 +00006598 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
6599 goto err_out;
6600
Matt Carlson19cfaec2009-12-03 08:36:20 +00006601 /* If multivector TSS is enabled, vector 0 does not handle
6602 * tx interrupts. Don't allocate any resources for it.
6603 */
Joe Perches63c3a662011-04-26 08:12:10 +00006604 if ((!i && !tg3_flag(tp, ENABLE_TSS)) ||
6605 (i && tg3_flag(tp, ENABLE_TSS))) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00006606 tnapi->tx_buffers = kzalloc(sizeof(struct ring_info) *
6607 TG3_TX_RING_SIZE,
6608 GFP_KERNEL);
6609 if (!tnapi->tx_buffers)
6610 goto err_out;
6611
Matt Carlson4bae65c2010-11-24 08:31:52 +00006612 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
6613 TG3_TX_RING_BYTES,
6614 &tnapi->tx_desc_mapping,
6615 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00006616 if (!tnapi->tx_ring)
6617 goto err_out;
6618 }
6619
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006620 /*
6621 * When RSS is enabled, the status block format changes
6622 * slightly. The "rx_jumbo_consumer", "reserved",
6623 * and "rx_mini_consumer" members get mapped to the
6624 * other three rx return ring producer indexes.
6625 */
6626 switch (i) {
6627 default:
6628 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
6629 break;
6630 case 2:
6631 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
6632 break;
6633 case 3:
6634 tnapi->rx_rcb_prod_idx = &sblk->reserved;
6635 break;
6636 case 4:
6637 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
6638 break;
6639 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006640
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006641 /*
6642 * If multivector RSS is enabled, vector 0 does not handle
6643 * rx or tx interrupts. Don't allocate any resources for it.
6644 */
Joe Perches63c3a662011-04-26 08:12:10 +00006645 if (!i && tg3_flag(tp, ENABLE_RSS))
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006646 continue;
6647
Matt Carlson4bae65c2010-11-24 08:31:52 +00006648 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
6649 TG3_RX_RCB_RING_BYTES(tp),
6650 &tnapi->rx_rcb_mapping,
6651 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006652 if (!tnapi->rx_rcb)
6653 goto err_out;
6654
6655 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006656 }
6657
Linus Torvalds1da177e2005-04-16 15:20:36 -07006658 return 0;
6659
6660err_out:
6661 tg3_free_consistent(tp);
6662 return -ENOMEM;
6663}
6664
6665#define MAX_WAIT_CNT 1000
6666
6667/* To stop a block, clear the enable bit and poll till it
6668 * clears. tp->lock is held.
6669 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006670static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671{
6672 unsigned int i;
6673 u32 val;
6674
Joe Perches63c3a662011-04-26 08:12:10 +00006675 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006676 switch (ofs) {
6677 case RCVLSC_MODE:
6678 case DMAC_MODE:
6679 case MBFREE_MODE:
6680 case BUFMGR_MODE:
6681 case MEMARB_MODE:
6682 /* We can't enable/disable these bits of the
6683 * 5705/5750, just say success.
6684 */
6685 return 0;
6686
6687 default:
6688 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006690 }
6691
6692 val = tr32(ofs);
6693 val &= ~enable_bit;
6694 tw32_f(ofs, val);
6695
6696 for (i = 0; i < MAX_WAIT_CNT; i++) {
6697 udelay(100);
6698 val = tr32(ofs);
6699 if ((val & enable_bit) == 0)
6700 break;
6701 }
6702
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006703 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00006704 dev_err(&tp->pdev->dev,
6705 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
6706 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006707 return -ENODEV;
6708 }
6709
6710 return 0;
6711}
6712
6713/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006714static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715{
6716 int i, err;
6717
6718 tg3_disable_ints(tp);
6719
6720 tp->rx_mode &= ~RX_MODE_ENABLE;
6721 tw32_f(MAC_RX_MODE, tp->rx_mode);
6722 udelay(10);
6723
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006724 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
6725 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
6726 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
6727 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
6728 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
6729 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006730
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006731 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
6732 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
6733 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
6734 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
6735 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
6736 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
6737 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006738
6739 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
6740 tw32_f(MAC_MODE, tp->mac_mode);
6741 udelay(40);
6742
6743 tp->tx_mode &= ~TX_MODE_ENABLE;
6744 tw32_f(MAC_TX_MODE, tp->tx_mode);
6745
6746 for (i = 0; i < MAX_WAIT_CNT; i++) {
6747 udelay(100);
6748 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
6749 break;
6750 }
6751 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00006752 dev_err(&tp->pdev->dev,
6753 "%s timed out, TX_MODE_ENABLE will not clear "
6754 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07006755 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756 }
6757
Michael Chane6de8ad2005-05-05 14:42:41 -07006758 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006759 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
6760 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006761
6762 tw32(FTQ_RESET, 0xffffffff);
6763 tw32(FTQ_RESET, 0x00000000);
6764
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006765 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
6766 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006768 for (i = 0; i < tp->irq_cnt; i++) {
6769 struct tg3_napi *tnapi = &tp->napi[i];
6770 if (tnapi->hw_status)
6771 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773 if (tp->hw_stats)
6774 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6775
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776 return err;
6777}
6778
Matt Carlson0d3031d2007-10-10 18:02:43 -07006779static void tg3_ape_send_event(struct tg3 *tp, u32 event)
6780{
6781 int i;
6782 u32 apedata;
6783
Matt Carlsondc6d0742010-09-15 08:59:55 +00006784 /* NCSI does not support APE events */
Joe Perches63c3a662011-04-26 08:12:10 +00006785 if (tg3_flag(tp, APE_HAS_NCSI))
Matt Carlsondc6d0742010-09-15 08:59:55 +00006786 return;
6787
Matt Carlson0d3031d2007-10-10 18:02:43 -07006788 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
6789 if (apedata != APE_SEG_SIG_MAGIC)
6790 return;
6791
6792 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07006793 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006794 return;
6795
6796 /* Wait for up to 1 millisecond for APE to service previous event. */
6797 for (i = 0; i < 10; i++) {
6798 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
6799 return;
6800
6801 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
6802
6803 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6804 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
6805 event | APE_EVENT_STATUS_EVENT_PENDING);
6806
6807 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
6808
6809 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6810 break;
6811
6812 udelay(100);
6813 }
6814
6815 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6816 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
6817}
6818
6819static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
6820{
6821 u32 event;
6822 u32 apedata;
6823
Joe Perches63c3a662011-04-26 08:12:10 +00006824 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006825 return;
6826
6827 switch (kind) {
Matt Carlson33f401a2010-04-05 10:19:27 +00006828 case RESET_KIND_INIT:
6829 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
6830 APE_HOST_SEG_SIG_MAGIC);
6831 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
6832 APE_HOST_SEG_LEN_MAGIC);
6833 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
6834 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
6835 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
Matt Carlson6867c842010-07-11 09:31:44 +00006836 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
Matt Carlson33f401a2010-04-05 10:19:27 +00006837 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
6838 APE_HOST_BEHAV_NO_PHYLOCK);
Matt Carlsondc6d0742010-09-15 08:59:55 +00006839 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
6840 TG3_APE_HOST_DRVR_STATE_START);
Matt Carlson0d3031d2007-10-10 18:02:43 -07006841
Matt Carlson33f401a2010-04-05 10:19:27 +00006842 event = APE_EVENT_STATUS_STATE_START;
6843 break;
6844 case RESET_KIND_SHUTDOWN:
6845 /* With the interface we are currently using,
6846 * APE does not track driver state. Wiping
6847 * out the HOST SEGMENT SIGNATURE forces
6848 * the APE to assume OS absent status.
6849 */
6850 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
Matt Carlsonb2aee152008-11-03 16:51:11 -08006851
Matt Carlsondc6d0742010-09-15 08:59:55 +00006852 if (device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00006853 tg3_flag(tp, WOL_ENABLE)) {
Matt Carlsondc6d0742010-09-15 08:59:55 +00006854 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
6855 TG3_APE_HOST_WOL_SPEED_AUTO);
6856 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
6857 } else
6858 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
6859
6860 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
6861
Matt Carlson33f401a2010-04-05 10:19:27 +00006862 event = APE_EVENT_STATUS_STATE_UNLOAD;
6863 break;
6864 case RESET_KIND_SUSPEND:
6865 event = APE_EVENT_STATUS_STATE_SUSPEND;
6866 break;
6867 default:
6868 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006869 }
6870
6871 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
6872
6873 tg3_ape_send_event(tp, event);
6874}
6875
Michael Chane6af3012005-04-21 17:12:05 -07006876/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
6878{
David S. Millerf49639e2006-06-09 11:58:36 -07006879 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
6880 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006881
Joe Perches63c3a662011-04-26 08:12:10 +00006882 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006883 switch (kind) {
6884 case RESET_KIND_INIT:
6885 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6886 DRV_STATE_START);
6887 break;
6888
6889 case RESET_KIND_SHUTDOWN:
6890 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6891 DRV_STATE_UNLOAD);
6892 break;
6893
6894 case RESET_KIND_SUSPEND:
6895 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6896 DRV_STATE_SUSPEND);
6897 break;
6898
6899 default:
6900 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006902 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006903
6904 if (kind == RESET_KIND_INIT ||
6905 kind == RESET_KIND_SUSPEND)
6906 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006907}
6908
6909/* tp->lock is held. */
6910static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
6911{
Joe Perches63c3a662011-04-26 08:12:10 +00006912 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006913 switch (kind) {
6914 case RESET_KIND_INIT:
6915 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6916 DRV_STATE_START_DONE);
6917 break;
6918
6919 case RESET_KIND_SHUTDOWN:
6920 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6921 DRV_STATE_UNLOAD_DONE);
6922 break;
6923
6924 default:
6925 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006927 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006928
6929 if (kind == RESET_KIND_SHUTDOWN)
6930 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006931}
6932
6933/* tp->lock is held. */
6934static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
6935{
Joe Perches63c3a662011-04-26 08:12:10 +00006936 if (tg3_flag(tp, ENABLE_ASF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006937 switch (kind) {
6938 case RESET_KIND_INIT:
6939 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6940 DRV_STATE_START);
6941 break;
6942
6943 case RESET_KIND_SHUTDOWN:
6944 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6945 DRV_STATE_UNLOAD);
6946 break;
6947
6948 case RESET_KIND_SUSPEND:
6949 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6950 DRV_STATE_SUSPEND);
6951 break;
6952
6953 default:
6954 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006956 }
6957}
6958
Michael Chan7a6f4362006-09-27 16:03:31 -07006959static int tg3_poll_fw(struct tg3 *tp)
6960{
6961 int i;
6962 u32 val;
6963
Michael Chanb5d37722006-09-27 16:06:21 -07006964 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08006965 /* Wait up to 20ms for init done. */
6966 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07006967 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
6968 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08006969 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07006970 }
6971 return -ENODEV;
6972 }
6973
Michael Chan7a6f4362006-09-27 16:03:31 -07006974 /* Wait for firmware initialization to complete. */
6975 for (i = 0; i < 100000; i++) {
6976 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
6977 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
6978 break;
6979 udelay(10);
6980 }
6981
6982 /* Chip might not be fitted with firmware. Some Sun onboard
6983 * parts are configured like that. So don't signal the timeout
6984 * of the above loop as an error, but do report the lack of
6985 * running firmware once.
6986 */
Joe Perches63c3a662011-04-26 08:12:10 +00006987 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
6988 tg3_flag_set(tp, NO_FWARE_REPORTED);
Michael Chan7a6f4362006-09-27 16:03:31 -07006989
Joe Perches05dbe002010-02-17 19:44:19 +00006990 netdev_info(tp->dev, "No firmware running\n");
Michael Chan7a6f4362006-09-27 16:03:31 -07006991 }
6992
Matt Carlson6b10c162010-02-12 14:47:08 +00006993 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
6994 /* The 57765 A0 needs a little more
6995 * time to do some important work.
6996 */
6997 mdelay(10);
6998 }
6999
Michael Chan7a6f4362006-09-27 16:03:31 -07007000 return 0;
7001}
7002
Michael Chanee6a99b2007-07-18 21:49:10 -07007003/* Save PCI command register before chip reset */
7004static void tg3_save_pci_state(struct tg3 *tp)
7005{
Matt Carlson8a6eac92007-10-21 16:17:55 -07007006 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007007}
7008
7009/* Restore PCI state after chip reset */
7010static void tg3_restore_pci_state(struct tg3 *tp)
7011{
7012 u32 val;
7013
7014 /* Re-enable indirect register accesses. */
7015 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7016 tp->misc_host_ctrl);
7017
7018 /* Set MAX PCI retry to zero. */
7019 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7020 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007021 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07007022 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007023 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00007024 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007025 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00007026 PCISTATE_ALLOW_APE_SHMEM_WR |
7027 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07007028 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7029
Matt Carlson8a6eac92007-10-21 16:17:55 -07007030 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007031
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007032 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
Joe Perches63c3a662011-04-26 08:12:10 +00007033 if (tg3_flag(tp, PCI_EXPRESS))
Matt Carlsoncf790032010-11-24 08:31:48 +00007034 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007035 else {
7036 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7037 tp->pci_cacheline_sz);
7038 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7039 tp->pci_lat_timer);
7040 }
Michael Chan114342f2007-10-15 02:12:26 -07007041 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08007042
Michael Chanee6a99b2007-07-18 21:49:10 -07007043 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00007044 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07007045 u16 pcix_cmd;
7046
7047 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7048 &pcix_cmd);
7049 pcix_cmd &= ~PCI_X_CMD_ERO;
7050 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7051 pcix_cmd);
7052 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007053
Joe Perches63c3a662011-04-26 08:12:10 +00007054 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007055
7056 /* Chip reset on 5780 will reset MSI enable bit,
7057 * so need to restore it.
7058 */
Joe Perches63c3a662011-04-26 08:12:10 +00007059 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007060 u16 ctrl;
7061
7062 pci_read_config_word(tp->pdev,
7063 tp->msi_cap + PCI_MSI_FLAGS,
7064 &ctrl);
7065 pci_write_config_word(tp->pdev,
7066 tp->msi_cap + PCI_MSI_FLAGS,
7067 ctrl | PCI_MSI_FLAGS_ENABLE);
7068 val = tr32(MSGINT_MODE);
7069 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7070 }
7071 }
7072}
7073
Linus Torvalds1da177e2005-04-16 15:20:36 -07007074static void tg3_stop_fw(struct tg3 *);
7075
7076/* tp->lock is held. */
7077static int tg3_chip_reset(struct tg3 *tp)
7078{
7079 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007080 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007081 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007082
David S. Millerf49639e2006-06-09 11:58:36 -07007083 tg3_nvram_lock(tp);
7084
Matt Carlson77b483f2008-08-15 14:07:24 -07007085 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7086
David S. Millerf49639e2006-06-09 11:58:36 -07007087 /* No matching tg3_nvram_unlock() after this because
7088 * chip reset below will undo the nvram lock.
7089 */
7090 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007091
Michael Chanee6a99b2007-07-18 21:49:10 -07007092 /* GRC_MISC_CFG core clock reset will clear the memory
7093 * enable bit in PCI register 4 and the MSI enable bit
7094 * on some chips, so we save relevant registers here.
7095 */
7096 tg3_save_pci_state(tp);
7097
Michael Chand9ab5ad12006-03-20 22:27:35 -08007098 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00007099 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08007100 tw32(GRC_FASTBOOT_PC, 0);
7101
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102 /*
7103 * We must avoid the readl() that normally takes place.
7104 * It locks machines, causes machine checks, and other
7105 * fun things. So, temporarily disable the 5701
7106 * hardware workaround, while we do the reset.
7107 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007108 write_op = tp->write32;
7109 if (write_op == tg3_write_flush_reg32)
7110 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007111
Michael Chand18edcb2007-03-24 20:57:11 -07007112 /* Prevent the irq handler from reading or writing PCI registers
7113 * during chip reset when the memory enable bit in the PCI command
7114 * register may be cleared. The chip does not generate interrupt
7115 * at this time, but the irq handler may still be called due to irq
7116 * sharing or irqpoll.
7117 */
Joe Perches63c3a662011-04-26 08:12:10 +00007118 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007119 for (i = 0; i < tp->irq_cnt; i++) {
7120 struct tg3_napi *tnapi = &tp->napi[i];
7121 if (tnapi->hw_status) {
7122 tnapi->hw_status->status = 0;
7123 tnapi->hw_status->status_tag = 0;
7124 }
7125 tnapi->last_tag = 0;
7126 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007127 }
Michael Chand18edcb2007-03-24 20:57:11 -07007128 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007129
7130 for (i = 0; i < tp->irq_cnt; i++)
7131 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007132
Matt Carlson255ca312009-08-25 10:07:27 +00007133 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7134 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7135 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7136 }
7137
Linus Torvalds1da177e2005-04-16 15:20:36 -07007138 /* do the reset */
7139 val = GRC_MISC_CFG_CORECLK_RESET;
7140
Joe Perches63c3a662011-04-26 08:12:10 +00007141 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00007142 /* Force PCIe 1.0a mode */
7143 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007144 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00007145 tr32(TG3_PCIE_PHY_TSTCTL) ==
7146 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7147 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7148
Linus Torvalds1da177e2005-04-16 15:20:36 -07007149 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7150 tw32(GRC_MISC_CFG, (1 << 29));
7151 val |= (1 << 29);
7152 }
7153 }
7154
Michael Chanb5d37722006-09-27 16:06:21 -07007155 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7156 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7157 tw32(GRC_VCPU_EXT_CTRL,
7158 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7159 }
7160
Matt Carlsonf37500d2010-08-02 11:25:59 +00007161 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00007162 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007163 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007164
Linus Torvalds1da177e2005-04-16 15:20:36 -07007165 tw32(GRC_MISC_CFG, val);
7166
Michael Chan1ee582d2005-08-09 20:16:46 -07007167 /* restore 5701 hardware bug workaround write method */
7168 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007169
7170 /* Unfortunately, we have to delay before the PCI read back.
7171 * Some 575X chips even will not respond to a PCI cfg access
7172 * when the reset command is given to the chip.
7173 *
7174 * How do these hardware designers expect things to work
7175 * properly if the PCI write is posted for a long period
7176 * of time? It is always necessary to have some method by
7177 * which a register read back can occur to push the write
7178 * out which does the reset.
7179 *
7180 * For most tg3 variants the trick below was working.
7181 * Ho hum...
7182 */
7183 udelay(120);
7184
7185 /* Flush PCI posted writes. The normal MMIO registers
7186 * are inaccessible at this time so this is the only
7187 * way to make this reliably (actually, this is no longer
7188 * the case, see above). I tried to use indirect
7189 * register read/write but this upset some 5701 variants.
7190 */
7191 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7192
7193 udelay(120);
7194
Joe Perches63c3a662011-04-26 08:12:10 +00007195 if (tg3_flag(tp, PCI_EXPRESS) && tp->pcie_cap) {
Matt Carlsone7126992009-08-25 10:08:16 +00007196 u16 val16;
7197
Linus Torvalds1da177e2005-04-16 15:20:36 -07007198 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7199 int i;
7200 u32 cfg_val;
7201
7202 /* Wait for link training to complete. */
7203 for (i = 0; i < 5000; i++)
7204 udelay(100);
7205
7206 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7207 pci_write_config_dword(tp->pdev, 0xc4,
7208 cfg_val | (1 << 15));
7209 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007210
Matt Carlsone7126992009-08-25 10:08:16 +00007211 /* Clear the "no snoop" and "relaxed ordering" bits. */
7212 pci_read_config_word(tp->pdev,
7213 tp->pcie_cap + PCI_EXP_DEVCTL,
7214 &val16);
7215 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7216 PCI_EXP_DEVCTL_NOSNOOP_EN);
7217 /*
7218 * Older PCIe devices only support the 128 byte
7219 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007220 */
Joe Perches63c3a662011-04-26 08:12:10 +00007221 if (!tg3_flag(tp, CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007222 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007223 pci_write_config_word(tp->pdev,
7224 tp->pcie_cap + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007225 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007226
Matt Carlsoncf790032010-11-24 08:31:48 +00007227 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007228
7229 /* Clear error status */
7230 pci_write_config_word(tp->pdev,
7231 tp->pcie_cap + PCI_EXP_DEVSTA,
7232 PCI_EXP_DEVSTA_CED |
7233 PCI_EXP_DEVSTA_NFED |
7234 PCI_EXP_DEVSTA_FED |
7235 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007236 }
7237
Michael Chanee6a99b2007-07-18 21:49:10 -07007238 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007239
Joe Perches63c3a662011-04-26 08:12:10 +00007240 tg3_flag_clear(tp, CHIP_RESETTING);
7241 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07007242
Michael Chanee6a99b2007-07-18 21:49:10 -07007243 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007244 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07007245 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007246 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007247
7248 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7249 tg3_stop_fw(tp);
7250 tw32(0x5000, 0x400);
7251 }
7252
7253 tw32(GRC_MODE, tp->grc_mode);
7254
7255 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007256 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007257
7258 tw32(0xc4, val | (1 << 15));
7259 }
7260
7261 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7262 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7263 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7264 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7265 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7266 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7267 }
7268
Joe Perches63c3a662011-04-26 08:12:10 +00007269 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007270 tp->mac_mode = MAC_MODE_APE_TX_EN |
7271 MAC_MODE_APE_RX_EN |
7272 MAC_MODE_TDE_ENABLE;
7273
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007274 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007275 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
7276 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007277 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007278 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7279 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007280 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007281 val = 0;
7282
7283 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007284 udelay(40);
7285
Matt Carlson77b483f2008-08-15 14:07:24 -07007286 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7287
Michael Chan7a6f4362006-09-27 16:03:31 -07007288 err = tg3_poll_fw(tp);
7289 if (err)
7290 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007291
Matt Carlson0a9140c2009-08-28 12:27:50 +00007292 tg3_mdio_start(tp);
7293
Joe Perches63c3a662011-04-26 08:12:10 +00007294 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007295 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7296 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007297 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007298 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007299
7300 tw32(0x7c00, val | (1 << 25));
7301 }
7302
Matt Carlsond78b59f2011-04-05 14:22:46 +00007303 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
7304 val = tr32(TG3_CPMU_CLCK_ORIDE);
7305 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
7306 }
7307
Linus Torvalds1da177e2005-04-16 15:20:36 -07007308 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00007309 tg3_flag_clear(tp, ENABLE_ASF);
7310 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007311 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7312 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7313 u32 nic_cfg;
7314
7315 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7316 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00007317 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007318 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00007319 if (tg3_flag(tp, 5750_PLUS))
7320 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007321 }
7322 }
7323
7324 return 0;
7325}
7326
7327/* tp->lock is held. */
7328static void tg3_stop_fw(struct tg3 *tp)
7329{
Joe Perches63c3a662011-04-26 08:12:10 +00007330 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07007331 /* Wait for RX cpu to ACK the previous event. */
7332 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007333
7334 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007335
7336 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007337
Matt Carlson7c5026a2008-05-02 16:49:29 -07007338 /* Wait for RX cpu to ACK this event. */
7339 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007340 }
7341}
7342
7343/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007344static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007345{
7346 int err;
7347
7348 tg3_stop_fw(tp);
7349
Michael Chan944d9802005-05-29 14:57:48 -07007350 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007351
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007352 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007353 err = tg3_chip_reset(tp);
7354
Matt Carlsondaba2a62009-04-20 06:58:52 +00007355 __tg3_set_mac_addr(tp, 0);
7356
Michael Chan944d9802005-05-29 14:57:48 -07007357 tg3_write_sig_legacy(tp, kind);
7358 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007359
7360 if (err)
7361 return err;
7362
7363 return 0;
7364}
7365
Linus Torvalds1da177e2005-04-16 15:20:36 -07007366#define RX_CPU_SCRATCH_BASE 0x30000
7367#define RX_CPU_SCRATCH_SIZE 0x04000
7368#define TX_CPU_SCRATCH_BASE 0x34000
7369#define TX_CPU_SCRATCH_SIZE 0x04000
7370
7371/* tp->lock is held. */
7372static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
7373{
7374 int i;
7375
Joe Perches63c3a662011-04-26 08:12:10 +00007376 BUG_ON(offset == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007377
Michael Chanb5d37722006-09-27 16:06:21 -07007378 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7379 u32 val = tr32(GRC_VCPU_EXT_CTRL);
7380
7381 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
7382 return 0;
7383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007384 if (offset == RX_CPU_BASE) {
7385 for (i = 0; i < 10000; i++) {
7386 tw32(offset + CPU_STATE, 0xffffffff);
7387 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7388 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7389 break;
7390 }
7391
7392 tw32(offset + CPU_STATE, 0xffffffff);
7393 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
7394 udelay(10);
7395 } else {
7396 for (i = 0; i < 10000; i++) {
7397 tw32(offset + CPU_STATE, 0xffffffff);
7398 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7399 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7400 break;
7401 }
7402 }
7403
7404 if (i >= 10000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007405 netdev_err(tp->dev, "%s timed out, %s CPU\n",
7406 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007407 return -ENODEV;
7408 }
Michael Chanec41c7d2006-01-17 02:40:55 -08007409
7410 /* Clear firmware's nvram arbitration. */
Joe Perches63c3a662011-04-26 08:12:10 +00007411 if (tg3_flag(tp, NVRAM))
Michael Chanec41c7d2006-01-17 02:40:55 -08007412 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007413 return 0;
7414}
7415
7416struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007417 unsigned int fw_base;
7418 unsigned int fw_len;
7419 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007420};
7421
7422/* tp->lock is held. */
7423static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
7424 int cpu_scratch_size, struct fw_info *info)
7425{
Michael Chanec41c7d2006-01-17 02:40:55 -08007426 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007427 void (*write_op)(struct tg3 *, u32, u32);
7428
Joe Perches63c3a662011-04-26 08:12:10 +00007429 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007430 netdev_err(tp->dev,
7431 "%s: Trying to load TX cpu firmware which is 5705\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007432 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007433 return -EINVAL;
7434 }
7435
Joe Perches63c3a662011-04-26 08:12:10 +00007436 if (tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007437 write_op = tg3_write_mem;
7438 else
7439 write_op = tg3_write_indirect_reg32;
7440
Michael Chan1b628152005-05-29 14:59:49 -07007441 /* It is possible that bootcode is still loading at this point.
7442 * Get the nvram lock first before halting the cpu.
7443 */
Michael Chanec41c7d2006-01-17 02:40:55 -08007444 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007445 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08007446 if (!lock_err)
7447 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007448 if (err)
7449 goto out;
7450
7451 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
7452 write_op(tp, cpu_scratch_base + i, 0);
7453 tw32(cpu_base + CPU_STATE, 0xffffffff);
7454 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007455 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007456 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007457 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07007458 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007459 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007460
7461 err = 0;
7462
7463out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007464 return err;
7465}
7466
7467/* tp->lock is held. */
7468static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
7469{
7470 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007471 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007472 int err, i;
7473
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007474 fw_data = (void *)tp->fw->data;
7475
7476 /* Firmware blob starts with version numbers, followed by
7477 start address and length. We are setting complete length.
7478 length = end_address_of_bss - start_address_of_text.
7479 Remainder is the blob to be loaded contiguously
7480 from start address. */
7481
7482 info.fw_base = be32_to_cpu(fw_data[1]);
7483 info.fw_len = tp->fw->size - 12;
7484 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007485
7486 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
7487 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
7488 &info);
7489 if (err)
7490 return err;
7491
7492 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
7493 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
7494 &info);
7495 if (err)
7496 return err;
7497
7498 /* Now startup only the RX cpu. */
7499 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007500 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007501
7502 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007503 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007504 break;
7505 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7506 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007507 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007508 udelay(1000);
7509 }
7510 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007511 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
7512 "should be %08x\n", __func__,
Joe Perches05dbe002010-02-17 19:44:19 +00007513 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007514 return -ENODEV;
7515 }
7516 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7517 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
7518
7519 return 0;
7520}
7521
Linus Torvalds1da177e2005-04-16 15:20:36 -07007522/* tp->lock is held. */
7523static int tg3_load_tso_firmware(struct tg3 *tp)
7524{
7525 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007526 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007527 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
7528 int err, i;
7529
Joe Perches63c3a662011-04-26 08:12:10 +00007530 if (tg3_flag(tp, HW_TSO_1) ||
7531 tg3_flag(tp, HW_TSO_2) ||
7532 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007533 return 0;
7534
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007535 fw_data = (void *)tp->fw->data;
7536
7537 /* Firmware blob starts with version numbers, followed by
7538 start address and length. We are setting complete length.
7539 length = end_address_of_bss - start_address_of_text.
7540 Remainder is the blob to be loaded contiguously
7541 from start address. */
7542
7543 info.fw_base = be32_to_cpu(fw_data[1]);
7544 cpu_scratch_size = tp->fw_len;
7545 info.fw_len = tp->fw->size - 12;
7546 info.fw_data = &fw_data[3];
7547
Linus Torvalds1da177e2005-04-16 15:20:36 -07007548 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007549 cpu_base = RX_CPU_BASE;
7550 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007551 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007552 cpu_base = TX_CPU_BASE;
7553 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
7554 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
7555 }
7556
7557 err = tg3_load_firmware_cpu(tp, cpu_base,
7558 cpu_scratch_base, cpu_scratch_size,
7559 &info);
7560 if (err)
7561 return err;
7562
7563 /* Now startup the cpu. */
7564 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007565 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007566
7567 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007568 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007569 break;
7570 tw32(cpu_base + CPU_STATE, 0xffffffff);
7571 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007572 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007573 udelay(1000);
7574 }
7575 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007576 netdev_err(tp->dev,
7577 "%s fails to set CPU PC, is %08x should be %08x\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007578 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007579 return -ENODEV;
7580 }
7581 tw32(cpu_base + CPU_STATE, 0xffffffff);
7582 tw32_f(cpu_base + CPU_MODE, 0x00000000);
7583 return 0;
7584}
7585
Linus Torvalds1da177e2005-04-16 15:20:36 -07007586
Linus Torvalds1da177e2005-04-16 15:20:36 -07007587static int tg3_set_mac_addr(struct net_device *dev, void *p)
7588{
7589 struct tg3 *tp = netdev_priv(dev);
7590 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007591 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007592
Michael Chanf9804dd2005-09-27 12:13:10 -07007593 if (!is_valid_ether_addr(addr->sa_data))
7594 return -EINVAL;
7595
Linus Torvalds1da177e2005-04-16 15:20:36 -07007596 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7597
Michael Chane75f7c92006-03-20 21:33:26 -08007598 if (!netif_running(dev))
7599 return 0;
7600
Joe Perches63c3a662011-04-26 08:12:10 +00007601 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007602 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007603
Michael Chan986e0ae2007-05-05 12:10:20 -07007604 addr0_high = tr32(MAC_ADDR_0_HIGH);
7605 addr0_low = tr32(MAC_ADDR_0_LOW);
7606 addr1_high = tr32(MAC_ADDR_1_HIGH);
7607 addr1_low = tr32(MAC_ADDR_1_LOW);
7608
7609 /* Skip MAC addr 1 if ASF is using it. */
7610 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7611 !(addr1_high == 0 && addr1_low == 0))
7612 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007613 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007614 spin_lock_bh(&tp->lock);
7615 __tg3_set_mac_addr(tp, skip_mac_1);
7616 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007617
Michael Chanb9ec6c12006-07-25 16:37:27 -07007618 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007619}
7620
7621/* tp->lock is held. */
7622static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7623 dma_addr_t mapping, u32 maxlen_flags,
7624 u32 nic_addr)
7625{
7626 tg3_write_mem(tp,
7627 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7628 ((u64) mapping >> 32));
7629 tg3_write_mem(tp,
7630 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7631 ((u64) mapping & 0xffffffff));
7632 tg3_write_mem(tp,
7633 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7634 maxlen_flags);
7635
Joe Perches63c3a662011-04-26 08:12:10 +00007636 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007637 tg3_write_mem(tp,
7638 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7639 nic_addr);
7640}
7641
7642static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007643static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007644{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007645 int i;
7646
Joe Perches63c3a662011-04-26 08:12:10 +00007647 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007648 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7649 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7650 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007651 } else {
7652 tw32(HOSTCC_TXCOL_TICKS, 0);
7653 tw32(HOSTCC_TXMAX_FRAMES, 0);
7654 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007655 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007656
Joe Perches63c3a662011-04-26 08:12:10 +00007657 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007658 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
7659 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
7660 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
7661 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007662 tw32(HOSTCC_RXCOL_TICKS, 0);
7663 tw32(HOSTCC_RXMAX_FRAMES, 0);
7664 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07007665 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007666
Joe Perches63c3a662011-04-26 08:12:10 +00007667 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07007668 u32 val = ec->stats_block_coalesce_usecs;
7669
Matt Carlsonb6080e12009-09-01 13:12:00 +00007670 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
7671 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
7672
David S. Miller15f98502005-05-18 22:49:26 -07007673 if (!netif_carrier_ok(tp->dev))
7674 val = 0;
7675
7676 tw32(HOSTCC_STAT_COAL_TICKS, val);
7677 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007678
7679 for (i = 0; i < tp->irq_cnt - 1; i++) {
7680 u32 reg;
7681
7682 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
7683 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007684 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
7685 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007686 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
7687 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007688
Joe Perches63c3a662011-04-26 08:12:10 +00007689 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007690 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
7691 tw32(reg, ec->tx_coalesce_usecs);
7692 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
7693 tw32(reg, ec->tx_max_coalesced_frames);
7694 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
7695 tw32(reg, ec->tx_max_coalesced_frames_irq);
7696 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007697 }
7698
7699 for (; i < tp->irq_max - 1; i++) {
7700 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007701 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007702 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007703
Joe Perches63c3a662011-04-26 08:12:10 +00007704 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007705 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
7706 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
7707 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7708 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007709 }
David S. Miller15f98502005-05-18 22:49:26 -07007710}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007711
7712/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00007713static void tg3_rings_reset(struct tg3 *tp)
7714{
7715 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007716 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007717 struct tg3_napi *tnapi = &tp->napi[0];
7718
7719 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00007720 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007721 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00007722 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00007723 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlsonb703df62009-12-03 08:36:21 +00007724 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
7725 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007726 else
7727 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7728
7729 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7730 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
7731 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
7732 BDINFO_FLAGS_DISABLED);
7733
7734
7735 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00007736 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007737 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00007738 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007739 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007740 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7741 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00007742 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
7743 else
7744 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7745
7746 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7747 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
7748 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
7749 BDINFO_FLAGS_DISABLED);
7750
7751 /* Disable interrupts */
7752 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
7753
7754 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00007755 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00007756 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007757 tp->napi[i].tx_prod = 0;
7758 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007759 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00007760 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007761 tw32_rx_mbox(tp->napi[i].consmbox, 0);
7762 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
7763 }
Joe Perches63c3a662011-04-26 08:12:10 +00007764 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00007765 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007766 } else {
7767 tp->napi[0].tx_prod = 0;
7768 tp->napi[0].tx_cons = 0;
7769 tw32_mailbox(tp->napi[0].prodmbox, 0);
7770 tw32_rx_mbox(tp->napi[0].consmbox, 0);
7771 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007772
7773 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00007774 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00007775 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
7776 for (i = 0; i < 16; i++)
7777 tw32_tx_mbox(mbox + i * 8, 0);
7778 }
7779
7780 txrcb = NIC_SRAM_SEND_RCB;
7781 rxrcb = NIC_SRAM_RCV_RET_RCB;
7782
7783 /* Clear status block in ram. */
7784 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7785
7786 /* Set status block DMA address */
7787 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7788 ((u64) tnapi->status_mapping >> 32));
7789 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7790 ((u64) tnapi->status_mapping & 0xffffffff));
7791
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007792 if (tnapi->tx_ring) {
7793 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7794 (TG3_TX_RING_SIZE <<
7795 BDINFO_FLAGS_MAXLEN_SHIFT),
7796 NIC_SRAM_TX_BUFFER_DESC);
7797 txrcb += TG3_BDINFO_SIZE;
7798 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007799
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007800 if (tnapi->rx_rcb) {
7801 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007802 (tp->rx_ret_ring_mask + 1) <<
7803 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007804 rxrcb += TG3_BDINFO_SIZE;
7805 }
7806
7807 stblk = HOSTCC_STATBLCK_RING1;
7808
7809 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
7810 u64 mapping = (u64)tnapi->status_mapping;
7811 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
7812 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
7813
7814 /* Clear status block in ram. */
7815 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7816
Matt Carlson19cfaec2009-12-03 08:36:20 +00007817 if (tnapi->tx_ring) {
7818 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7819 (TG3_TX_RING_SIZE <<
7820 BDINFO_FLAGS_MAXLEN_SHIFT),
7821 NIC_SRAM_TX_BUFFER_DESC);
7822 txrcb += TG3_BDINFO_SIZE;
7823 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007824
7825 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007826 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007827 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7828
7829 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007830 rxrcb += TG3_BDINFO_SIZE;
7831 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007832}
7833
Matt Carlsoneb07a942011-04-20 07:57:36 +00007834static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
7835{
7836 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
7837
Joe Perches63c3a662011-04-26 08:12:10 +00007838 if (!tg3_flag(tp, 5750_PLUS) ||
7839 tg3_flag(tp, 5780_CLASS) ||
Matt Carlsoneb07a942011-04-20 07:57:36 +00007840 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
7841 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
7842 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
7843 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7844 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
7845 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
7846 else
7847 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
7848
7849 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
7850 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
7851
7852 val = min(nic_rep_thresh, host_rep_thresh);
7853 tw32(RCVBDI_STD_THRESH, val);
7854
Joe Perches63c3a662011-04-26 08:12:10 +00007855 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00007856 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
7857
Joe Perches63c3a662011-04-26 08:12:10 +00007858 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00007859 return;
7860
Joe Perches63c3a662011-04-26 08:12:10 +00007861 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00007862 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
7863 else
7864 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5717;
7865
7866 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
7867
7868 val = min(bdcache_maxcnt / 2, host_rep_thresh);
7869 tw32(RCVBDI_JUMBO_THRESH, val);
7870
Joe Perches63c3a662011-04-26 08:12:10 +00007871 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00007872 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
7873}
7874
Matt Carlson2d31eca2009-09-01 12:53:31 +00007875/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007876static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007877{
7878 u32 val, rdmac_mode;
7879 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00007880 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007881
7882 tg3_disable_ints(tp);
7883
7884 tg3_stop_fw(tp);
7885
7886 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
7887
Joe Perches63c3a662011-04-26 08:12:10 +00007888 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07007889 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007890
Matt Carlson699c0192010-12-06 08:28:51 +00007891 /* Enable MAC control of LPI */
7892 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
7893 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
7894 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
7895 TG3_CPMU_EEE_LNKIDL_UART_IDL);
7896
7897 tw32_f(TG3_CPMU_EEE_CTRL,
7898 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
7899
Matt Carlsona386b902010-12-06 08:28:53 +00007900 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
7901 TG3_CPMU_EEEMD_LPI_IN_TX |
7902 TG3_CPMU_EEEMD_LPI_IN_RX |
7903 TG3_CPMU_EEEMD_EEE_ENABLE;
7904
7905 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
7906 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
7907
Joe Perches63c3a662011-04-26 08:12:10 +00007908 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00007909 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
7910
7911 tw32_f(TG3_CPMU_EEE_MODE, val);
7912
7913 tw32_f(TG3_CPMU_EEE_DBTMR1,
7914 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
7915 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
7916
7917 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00007918 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00007919 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00007920 }
7921
Matt Carlson603f1172010-02-12 14:47:10 +00007922 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08007923 tg3_phy_reset(tp);
7924
Linus Torvalds1da177e2005-04-16 15:20:36 -07007925 err = tg3_chip_reset(tp);
7926 if (err)
7927 return err;
7928
7929 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
7930
Matt Carlsonbcb37f62008-11-03 16:52:09 -08007931 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007932 val = tr32(TG3_CPMU_CTRL);
7933 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
7934 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08007935
7936 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7937 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7938 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7939 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
7940
7941 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
7942 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
7943 val |= CPMU_LNK_AWARE_MACCLK_6_25;
7944 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
7945
7946 val = tr32(TG3_CPMU_HST_ACC);
7947 val &= ~CPMU_HST_ACC_MACCLK_MASK;
7948 val |= CPMU_HST_ACC_MACCLK_6_25;
7949 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07007950 }
7951
Matt Carlson33466d92009-04-20 06:57:41 +00007952 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7953 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
7954 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
7955 PCIE_PWR_MGMT_L1_THRESH_4MS;
7956 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00007957
7958 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
7959 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
7960
7961 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00007962
Matt Carlsonf40386c2009-11-02 14:24:02 +00007963 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7964 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00007965 }
7966
Joe Perches63c3a662011-04-26 08:12:10 +00007967 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00007968 u32 grc_mode = tr32(GRC_MODE);
7969
7970 /* Access the lower 1K of PL PCIE block registers. */
7971 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7972 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
7973
7974 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
7975 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
7976 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
7977
7978 tw32(GRC_MODE, grc_mode);
7979 }
7980
Matt Carlson5093eed2010-11-24 08:31:45 +00007981 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
7982 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
7983 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00007984
Matt Carlson5093eed2010-11-24 08:31:45 +00007985 /* Access the lower 1K of PL PCIE block registers. */
7986 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7987 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00007988
Matt Carlson5093eed2010-11-24 08:31:45 +00007989 val = tr32(TG3_PCIE_TLDLPL_PORT +
7990 TG3_PCIE_PL_LO_PHYCTL5);
7991 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
7992 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00007993
Matt Carlson5093eed2010-11-24 08:31:45 +00007994 tw32(GRC_MODE, grc_mode);
7995 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00007996
Matt Carlson1ff30a52011-05-19 12:12:46 +00007997 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
7998 u32 grc_mode = tr32(GRC_MODE);
7999
8000 /* Access the lower 1K of DL PCIE block registers. */
8001 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8002 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
8003
8004 val = tr32(TG3_PCIE_TLDLPL_PORT +
8005 TG3_PCIE_DL_LO_FTSMAX);
8006 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
8007 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
8008 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
8009
8010 tw32(GRC_MODE, grc_mode);
8011 }
8012
Matt Carlsona977dbe2010-04-12 06:58:26 +00008013 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8014 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8015 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8016 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008017 }
8018
Linus Torvalds1da177e2005-04-16 15:20:36 -07008019 /* This works around an issue with Athlon chipsets on
8020 * B3 tigon3 silicon. This bit has no effect on any
8021 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008022 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008023 */
Joe Perches63c3a662011-04-26 08:12:10 +00008024 if (!tg3_flag(tp, CPMU_PRESENT)) {
8025 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07008026 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8027 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008029
8030 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008031 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008032 val = tr32(TG3PCI_PCISTATE);
8033 val |= PCISTATE_RETRY_SAME_DMA;
8034 tw32(TG3PCI_PCISTATE, val);
8035 }
8036
Joe Perches63c3a662011-04-26 08:12:10 +00008037 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07008038 /* Allow reads and writes to the
8039 * APE register and memory space.
8040 */
8041 val = tr32(TG3PCI_PCISTATE);
8042 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008043 PCISTATE_ALLOW_APE_SHMEM_WR |
8044 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008045 tw32(TG3PCI_PCISTATE, val);
8046 }
8047
Linus Torvalds1da177e2005-04-16 15:20:36 -07008048 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8049 /* Enable some hw fixes. */
8050 val = tr32(TG3PCI_MSI_DATA);
8051 val |= (1 << 26) | (1 << 28) | (1 << 29);
8052 tw32(TG3PCI_MSI_DATA, val);
8053 }
8054
8055 /* Descriptor ring init may make accesses to the
8056 * NIC SRAM area to setup the TX descriptors, so we
8057 * can only do this after the hardware has been
8058 * successfully reset.
8059 */
Michael Chan32d8c572006-07-25 16:38:29 -07008060 err = tg3_init_rings(tp);
8061 if (err)
8062 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008063
Joe Perches63c3a662011-04-26 08:12:10 +00008064 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008065 val = tr32(TG3PCI_DMA_RW_CTRL) &
8066 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008067 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8068 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson0aebff42011-04-25 12:42:45 +00008069 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765 &&
8070 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8071 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008072 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8073 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8074 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008075 /* This value is determined during the probe time DMA
8076 * engine test, tg3_test_dma.
8077 */
8078 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008080
8081 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8082 GRC_MODE_4X_NIC_SEND_RINGS |
8083 GRC_MODE_NO_TX_PHDR_CSUM |
8084 GRC_MODE_NO_RX_PHDR_CSUM);
8085 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07008086
8087 /* Pseudo-header checksum is done by hardware logic and not
8088 * the offload processers, so make the chip do the pseudo-
8089 * header checksums on receive. For transmit it is more
8090 * convenient to do the pseudo-header checksum in software
8091 * as Linux does that on transmit for us in all cases.
8092 */
8093 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008094
8095 tw32(GRC_MODE,
8096 tp->grc_mode |
8097 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8098
8099 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8100 val = tr32(GRC_MISC_CFG);
8101 val &= ~0xff;
8102 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8103 tw32(GRC_MISC_CFG, val);
8104
8105 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00008106 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008107 /* Do nothing. */
8108 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8109 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8110 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8111 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8112 else
8113 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8114 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8115 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00008116 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008117 int fw_len;
8118
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008119 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008120 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8121 tw32(BUFMGR_MB_POOL_ADDR,
8122 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8123 tw32(BUFMGR_MB_POOL_SIZE,
8124 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008126
Michael Chan0f893dc2005-07-25 12:30:38 -07008127 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008128 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8129 tp->bufmgr_config.mbuf_read_dma_low_water);
8130 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8131 tp->bufmgr_config.mbuf_mac_rx_low_water);
8132 tw32(BUFMGR_MB_HIGH_WATER,
8133 tp->bufmgr_config.mbuf_high_water);
8134 } else {
8135 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8136 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8137 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8138 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8139 tw32(BUFMGR_MB_HIGH_WATER,
8140 tp->bufmgr_config.mbuf_high_water_jumbo);
8141 }
8142 tw32(BUFMGR_DMA_LOW_WATER,
8143 tp->bufmgr_config.dma_low_water);
8144 tw32(BUFMGR_DMA_HIGH_WATER,
8145 tp->bufmgr_config.dma_high_water);
8146
Matt Carlsond309a462010-09-30 10:34:31 +00008147 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8148 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8149 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00008150 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8151 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
8152 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
8153 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00008154 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008155 for (i = 0; i < 2000; i++) {
8156 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8157 break;
8158 udelay(10);
8159 }
8160 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008161 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008162 return -ENODEV;
8163 }
8164
Matt Carlsoneb07a942011-04-20 07:57:36 +00008165 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8166 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07008167
Matt Carlsoneb07a942011-04-20 07:57:36 +00008168 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008169
8170 /* Initialize TG3_BDINFO's at:
8171 * RCVDBDI_STD_BD: standard eth size rx ring
8172 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8173 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8174 *
8175 * like so:
8176 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8177 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8178 * ring attribute flags
8179 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8180 *
8181 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8182 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8183 *
8184 * The size of each ring is fixed in the firmware, but the location is
8185 * configurable.
8186 */
8187 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008188 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008189 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008190 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00008191 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00008192 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8193 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008194
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008195 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00008196 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008197 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8198 BDINFO_FLAGS_DISABLED);
8199
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008200 /* Program the jumbo buffer descriptor ring control
8201 * blocks on those devices that have them.
8202 */
Matt Carlsonbb18bb92011-03-09 16:58:19 +00008203 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008204 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008205
Joe Perches63c3a662011-04-26 08:12:10 +00008206 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008207 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008208 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008209 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008210 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008211 val = TG3_RX_JMB_RING_SIZE(tp) <<
8212 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008213 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008214 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00008215 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Matt Carlsona50d0792010-06-05 17:24:37 +00008216 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson87668d32009-11-13 13:03:34 +00008217 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8218 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008219 } else {
8220 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8221 BDINFO_FLAGS_DISABLED);
8222 }
8223
Joe Perches63c3a662011-04-26 08:12:10 +00008224 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008225 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsonde9f5232011-04-05 14:22:43 +00008226 val = TG3_RX_STD_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008227 else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008228 val = TG3_RX_STD_MAX_SIZE_5717;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008229 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8230 val |= (TG3_RX_STD_DMA_SZ << 2);
8231 } else
Matt Carlson04380d402010-04-12 06:58:29 +00008232 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008233 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008234 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008235
8236 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008237
Matt Carlson411da642009-11-13 13:03:46 +00008238 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00008239 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008240
Joe Perches63c3a662011-04-26 08:12:10 +00008241 tpr->rx_jmb_prod_idx =
8242 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00008243 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008244
Matt Carlson2d31eca2009-09-01 12:53:31 +00008245 tg3_rings_reset(tp);
8246
Linus Torvalds1da177e2005-04-16 15:20:36 -07008247 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008248 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008249
8250 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008251 tw32(MAC_RX_MTU_SIZE,
8252 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008253
8254 /* The slot time is changed by tg3_setup_phy if we
8255 * run at gigabit with half duplex.
8256 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00008257 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8258 (6 << TX_LENGTHS_IPG_SHIFT) |
8259 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8260
8261 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8262 val |= tr32(MAC_TX_LENGTHS) &
8263 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8264 TX_LENGTHS_CNT_DWN_VAL_MSK);
8265
8266 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008267
8268 /* Receive rules. */
8269 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8270 tw32(RCVLPC_CONFIG, 0x0181);
8271
8272 /* Calculate RDMAC_MODE setting early, we need it to determine
8273 * the RCVLPC_STATE_ENABLE mask.
8274 */
8275 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8276 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8277 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8278 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8279 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008280
Matt Carlsondeabaac2010-11-24 08:31:50 +00008281 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008282 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8283
Matt Carlson57e69832008-05-25 23:48:31 -07008284 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008285 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8286 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008287 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8288 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8289 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8290
Matt Carlsonc5908932011-03-09 16:58:25 +00008291 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8292 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008293 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008294 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008295 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8296 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008297 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008298 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8299 }
8300 }
8301
Joe Perches63c3a662011-04-26 08:12:10 +00008302 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07008303 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8304
Joe Perches63c3a662011-04-26 08:12:10 +00008305 if (tg3_flag(tp, HW_TSO_1) ||
8306 tg3_flag(tp, HW_TSO_2) ||
8307 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08008308 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8309
Matt Carlson108a6c12011-05-19 12:12:47 +00008310 if (tg3_flag(tp, 57765_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00008311 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008312 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8313 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008314
Matt Carlsonf2096f92011-04-05 14:22:48 +00008315 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8316 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8317
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008318 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8319 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8320 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8321 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008322 tg3_flag(tp, 57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008323 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00008324 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8325 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008326 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8327 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8328 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8329 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8330 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8331 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008332 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008333 tw32(TG3_RDMA_RSRVCTRL_REG,
8334 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8335 }
8336
Matt Carlsond78b59f2011-04-05 14:22:46 +00008337 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8338 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00008339 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8340 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8341 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8342 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8343 }
8344
Linus Torvalds1da177e2005-04-16 15:20:36 -07008345 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00008346 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07008347 val = tr32(RCVLPC_STATS_ENABLE);
8348 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8349 tw32(RCVLPC_STATS_ENABLE, val);
8350 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008351 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008352 val = tr32(RCVLPC_STATS_ENABLE);
8353 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8354 tw32(RCVLPC_STATS_ENABLE, val);
8355 } else {
8356 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8357 }
8358 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8359 tw32(SNDDATAI_STATSENAB, 0xffffff);
8360 tw32(SNDDATAI_STATSCTRL,
8361 (SNDDATAI_SCTRL_ENABLE |
8362 SNDDATAI_SCTRL_FASTUPD));
8363
8364 /* Setup host coalescing engine. */
8365 tw32(HOSTCC_MODE, 0);
8366 for (i = 0; i < 2000; i++) {
8367 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8368 break;
8369 udelay(10);
8370 }
8371
Michael Chand244c892005-07-05 14:42:33 -07008372 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008373
Joe Perches63c3a662011-04-26 08:12:10 +00008374 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008375 /* Status/statistics block address. See tg3_timer,
8376 * the tg3_periodic_fetch_stats call there, and
8377 * tg3_get_stats to see how this works for 5705/5750 chips.
8378 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008379 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8380 ((u64) tp->stats_mapping >> 32));
8381 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8382 ((u64) tp->stats_mapping & 0xffffffff));
8383 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008384
Linus Torvalds1da177e2005-04-16 15:20:36 -07008385 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008386
8387 /* Clear statistics and status block memory areas */
8388 for (i = NIC_SRAM_STATS_BLK;
8389 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8390 i += sizeof(u32)) {
8391 tg3_write_mem(tp, i, 0);
8392 udelay(40);
8393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008394 }
8395
8396 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8397
8398 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8399 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008400 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008401 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8402
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008403 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8404 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07008405 /* reset to prevent losing 1st rx packet intermittently */
8406 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8407 udelay(10);
8408 }
8409
Joe Perches63c3a662011-04-26 08:12:10 +00008410 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008411 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -07008412 else
8413 tp->mac_mode = 0;
8414 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07008415 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008416 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008417 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008418 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8419 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008420 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8421 udelay(40);
8422
Michael Chan314fba32005-04-21 17:07:04 -07008423 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00008424 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008425 * register to preserve the GPIO settings for LOMs. The GPIOs,
8426 * whether used as inputs or outputs, are set by boot code after
8427 * reset.
8428 */
Joe Perches63c3a662011-04-26 08:12:10 +00008429 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008430 u32 gpio_mask;
8431
Michael Chan9d26e212006-12-07 00:21:14 -08008432 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8433 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8434 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008435
8436 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8437 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8438 GRC_LCLCTRL_GPIO_OUTPUT3;
8439
Michael Chanaf36e6b2006-03-23 01:28:06 -08008440 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8441 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8442
Gary Zambranoaaf84462007-05-05 11:51:45 -07008443 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008444 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8445
8446 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00008447 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08008448 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8449 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008451 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8452 udelay(100);
8453
Joe Perches63c3a662011-04-26 08:12:10 +00008454 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008455 val = tr32(MSGINT_MODE);
8456 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
8457 tw32(MSGINT_MODE, val);
8458 }
8459
Joe Perches63c3a662011-04-26 08:12:10 +00008460 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008461 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8462 udelay(40);
8463 }
8464
8465 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8466 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8467 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8468 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8469 WDMAC_MODE_LNGREAD_ENAB);
8470
Matt Carlsonc5908932011-03-09 16:58:25 +00008471 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8472 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008473 if (tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008474 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8475 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8476 /* nothing */
8477 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008478 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008479 val |= WDMAC_MODE_RX_ACCEL;
8480 }
8481 }
8482
Michael Chand9ab5ad12006-03-20 22:27:35 -08008483 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00008484 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07008485 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -08008486
Matt Carlson788a0352009-11-02 14:26:03 +00008487 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8488 val |= WDMAC_MODE_BURST_ALL_DATA;
8489
Linus Torvalds1da177e2005-04-16 15:20:36 -07008490 tw32_f(WDMAC_MODE, val);
8491 udelay(40);
8492
Joe Perches63c3a662011-04-26 08:12:10 +00008493 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008494 u16 pcix_cmd;
8495
8496 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8497 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008498 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008499 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8500 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008501 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008502 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8503 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008504 }
Matt Carlson9974a352007-10-07 23:27:28 -07008505 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8506 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008507 }
8508
8509 tw32_f(RDMAC_MODE, rdmac_mode);
8510 udelay(40);
8511
8512 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008513 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008514 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008515
8516 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8517 tw32(SNDDATAC_MODE,
8518 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8519 else
8520 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8521
Linus Torvalds1da177e2005-04-16 15:20:36 -07008522 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8523 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008524 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00008525 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008526 val |= RCVDBDI_MODE_LRG_RING_SZ;
8527 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008528 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008529 if (tg3_flag(tp, HW_TSO_1) ||
8530 tg3_flag(tp, HW_TSO_2) ||
8531 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008532 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008533 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008534 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008535 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8536 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008537 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8538
8539 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8540 err = tg3_load_5701_a0_firmware_fix(tp);
8541 if (err)
8542 return err;
8543 }
8544
Joe Perches63c3a662011-04-26 08:12:10 +00008545 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008546 err = tg3_load_tso_firmware(tp);
8547 if (err)
8548 return err;
8549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008550
8551 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008552
Joe Perches63c3a662011-04-26 08:12:10 +00008553 if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsonb1d05212010-06-05 17:24:31 +00008554 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8555 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008556
8557 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8558 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
8559 tp->tx_mode &= ~val;
8560 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
8561 }
8562
Linus Torvalds1da177e2005-04-16 15:20:36 -07008563 tw32_f(MAC_TX_MODE, tp->tx_mode);
8564 udelay(100);
8565
Joe Perches63c3a662011-04-26 08:12:10 +00008566 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008567 u32 reg = MAC_RSS_INDIR_TBL_0;
8568 u8 *ent = (u8 *)&val;
8569
8570 /* Setup the indirection table */
8571 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8572 int idx = i % sizeof(val);
8573
Matt Carlson5efeeea2010-07-11 09:31:40 +00008574 ent[idx] = i % (tp->irq_cnt - 1);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008575 if (idx == sizeof(val) - 1) {
8576 tw32(reg, val);
8577 reg += 4;
8578 }
8579 }
8580
8581 /* Setup the "secret" hash key. */
8582 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8583 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8584 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8585 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8586 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8587 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8588 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8589 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8590 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8591 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8592 }
8593
Linus Torvalds1da177e2005-04-16 15:20:36 -07008594 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008595 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08008596 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8597
Joe Perches63c3a662011-04-26 08:12:10 +00008598 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008599 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8600 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8601 RX_MODE_RSS_IPV6_HASH_EN |
8602 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8603 RX_MODE_RSS_IPV4_HASH_EN |
8604 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8605
Linus Torvalds1da177e2005-04-16 15:20:36 -07008606 tw32_f(MAC_RX_MODE, tp->rx_mode);
8607 udelay(10);
8608
Linus Torvalds1da177e2005-04-16 15:20:36 -07008609 tw32(MAC_LED_CTRL, tp->led_ctrl);
8610
8611 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008612 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008613 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8614 udelay(10);
8615 }
8616 tw32_f(MAC_RX_MODE, tp->rx_mode);
8617 udelay(10);
8618
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008619 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008620 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008621 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008622 /* Set drive transmission level to 1.2V */
8623 /* only if the signal pre-emphasis bit is not set */
8624 val = tr32(MAC_SERDES_CFG);
8625 val &= 0xfffff000;
8626 val |= 0x880;
8627 tw32(MAC_SERDES_CFG, val);
8628 }
8629 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8630 tw32(MAC_SERDES_CFG, 0x616000);
8631 }
8632
8633 /* Prevent chip from dropping frames when flow control
8634 * is enabled.
8635 */
Matt Carlson666bc832010-01-20 16:58:03 +00008636 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8637 val = 1;
8638 else
8639 val = 2;
8640 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008641
8642 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008643 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008644 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00008645 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008646 }
8647
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008648 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Michael Chand4d2c552006-03-20 17:47:20 -08008649 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)) {
8650 u32 tmp;
8651
8652 tmp = tr32(SERDES_RX_CTRL);
8653 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
8654 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
8655 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
8656 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8657 }
8658
Joe Perches63c3a662011-04-26 08:12:10 +00008659 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson80096062010-08-02 11:26:06 +00008660 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
8661 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07008662 tp->link_config.speed = tp->link_config.orig_speed;
8663 tp->link_config.duplex = tp->link_config.orig_duplex;
8664 tp->link_config.autoneg = tp->link_config.orig_autoneg;
8665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008666
Matt Carlsondd477002008-05-25 23:45:58 -07008667 err = tg3_setup_phy(tp, 0);
8668 if (err)
8669 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008670
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008671 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
8672 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07008673 u32 tmp;
8674
8675 /* Clear CRC stats. */
8676 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
8677 tg3_writephy(tp, MII_TG3_TEST1,
8678 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00008679 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07008680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008681 }
8682 }
8683
8684 __tg3_set_rx_mode(tp->dev);
8685
8686 /* Initialize receive rules. */
8687 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
8688 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
8689 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
8690 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
8691
Joe Perches63c3a662011-04-26 08:12:10 +00008692 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008693 limit = 8;
8694 else
8695 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008696 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008697 limit -= 4;
8698 switch (limit) {
8699 case 16:
8700 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
8701 case 15:
8702 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
8703 case 14:
8704 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
8705 case 13:
8706 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
8707 case 12:
8708 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
8709 case 11:
8710 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
8711 case 10:
8712 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
8713 case 9:
8714 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
8715 case 8:
8716 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
8717 case 7:
8718 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
8719 case 6:
8720 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
8721 case 5:
8722 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
8723 case 4:
8724 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
8725 case 3:
8726 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
8727 case 2:
8728 case 1:
8729
8730 default:
8731 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008733
Joe Perches63c3a662011-04-26 08:12:10 +00008734 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07008735 /* Write our heartbeat update interval to APE. */
8736 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
8737 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07008738
Linus Torvalds1da177e2005-04-16 15:20:36 -07008739 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
8740
Linus Torvalds1da177e2005-04-16 15:20:36 -07008741 return 0;
8742}
8743
8744/* Called at device open time to get the chip ready for
8745 * packet processing. Invoked with tp->lock held.
8746 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008747static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008748{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008749 tg3_switch_clocks(tp);
8750
8751 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
8752
Matt Carlson2f751b62008-08-04 23:17:34 -07008753 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008754}
8755
8756#define TG3_STAT_ADD32(PSTAT, REG) \
8757do { u32 __val = tr32(REG); \
8758 (PSTAT)->low += __val; \
8759 if ((PSTAT)->low < __val) \
8760 (PSTAT)->high += 1; \
8761} while (0)
8762
8763static void tg3_periodic_fetch_stats(struct tg3 *tp)
8764{
8765 struct tg3_hw_stats *sp = tp->hw_stats;
8766
8767 if (!netif_carrier_ok(tp->dev))
8768 return;
8769
8770 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
8771 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
8772 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
8773 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
8774 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
8775 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
8776 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
8777 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
8778 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
8779 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
8780 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
8781 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
8782 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
8783
8784 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
8785 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
8786 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
8787 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
8788 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
8789 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
8790 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
8791 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
8792 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
8793 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
8794 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
8795 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
8796 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
8797 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07008798
8799 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson4d958472011-04-20 07:57:35 +00008800 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717) {
8801 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
8802 } else {
8803 u32 val = tr32(HOSTCC_FLOW_ATTN);
8804 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
8805 if (val) {
8806 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
8807 sp->rx_discards.low += val;
8808 if (sp->rx_discards.low < val)
8809 sp->rx_discards.high += 1;
8810 }
8811 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
8812 }
Michael Chan463d3052006-05-22 16:36:27 -07008813 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008814}
8815
8816static void tg3_timer(unsigned long __opaque)
8817{
8818 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008819
Michael Chanf475f162006-03-27 23:20:14 -08008820 if (tp->irq_sync)
8821 goto restart_timer;
8822
David S. Millerf47c11e2005-06-24 20:18:35 -07008823 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008824
Joe Perches63c3a662011-04-26 08:12:10 +00008825 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -07008826 /* All of this garbage is because when using non-tagged
8827 * IRQ status the mailbox/status_block protocol the chip
8828 * uses with the cpu is race prone.
8829 */
Matt Carlson898a56f2009-08-28 14:02:40 +00008830 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07008831 tw32(GRC_LOCAL_CTRL,
8832 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
8833 } else {
8834 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008835 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07008836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008837
David S. Millerfac9b832005-05-18 22:46:34 -07008838 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +00008839 tg3_flag_set(tp, RESTART_TIMER);
David S. Millerf47c11e2005-06-24 20:18:35 -07008840 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07008841 schedule_work(&tp->reset_task);
8842 return;
8843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008844 }
8845
Linus Torvalds1da177e2005-04-16 15:20:36 -07008846 /* This part only runs once per second. */
8847 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00008848 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -07008849 tg3_periodic_fetch_stats(tp);
8850
Matt Carlsonb0c59432011-05-19 12:12:48 +00008851 if (tp->setlpicnt && !--tp->setlpicnt)
8852 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +00008853
Joe Perches63c3a662011-04-26 08:12:10 +00008854 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008855 u32 mac_stat;
8856 int phy_event;
8857
8858 mac_stat = tr32(MAC_STATUS);
8859
8860 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008861 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008862 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
8863 phy_event = 1;
8864 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
8865 phy_event = 1;
8866
8867 if (phy_event)
8868 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +00008869 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008870 u32 mac_stat = tr32(MAC_STATUS);
8871 int need_setup = 0;
8872
8873 if (netif_carrier_ok(tp->dev) &&
8874 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
8875 need_setup = 1;
8876 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00008877 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008878 (mac_stat & (MAC_STATUS_PCS_SYNCED |
8879 MAC_STATUS_SIGNAL_DET))) {
8880 need_setup = 1;
8881 }
8882 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07008883 if (!tp->serdes_counter) {
8884 tw32_f(MAC_MODE,
8885 (tp->mac_mode &
8886 ~MAC_MODE_PORT_MODE_MASK));
8887 udelay(40);
8888 tw32_f(MAC_MODE, tp->mac_mode);
8889 udelay(40);
8890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008891 tg3_setup_phy(tp, 0);
8892 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008893 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008894 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07008895 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00008896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008897
8898 tp->timer_counter = tp->timer_multiplier;
8899 }
8900
Michael Chan130b8e42006-09-27 16:00:40 -07008901 /* Heartbeat is only sent once every 2 seconds.
8902 *
8903 * The heartbeat is to tell the ASF firmware that the host
8904 * driver is still alive. In the event that the OS crashes,
8905 * ASF needs to reset the hardware to free up the FIFO space
8906 * that may be filled with rx packets destined for the host.
8907 * If the FIFO is full, ASF will no longer function properly.
8908 *
8909 * Unintended resets have been reported on real time kernels
8910 * where the timer doesn't run on time. Netpoll will also have
8911 * same problem.
8912 *
8913 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
8914 * to check the ring condition when the heartbeat is expiring
8915 * before doing the reset. This will prevent most unintended
8916 * resets.
8917 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008918 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00008919 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07008920 tg3_wait_for_event_ack(tp);
8921
Michael Chanbbadf502006-04-06 21:46:34 -07008922 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07008923 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07008924 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00008925 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
8926 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008927
8928 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008929 }
8930 tp->asf_counter = tp->asf_multiplier;
8931 }
8932
David S. Millerf47c11e2005-06-24 20:18:35 -07008933 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008934
Michael Chanf475f162006-03-27 23:20:14 -08008935restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07008936 tp->timer.expires = jiffies + tp->timer_offset;
8937 add_timer(&tp->timer);
8938}
8939
Matt Carlson4f125f42009-09-01 12:55:02 +00008940static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08008941{
David Howells7d12e782006-10-05 14:55:46 +01008942 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008943 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00008944 char *name;
8945 struct tg3_napi *tnapi = &tp->napi[irq_num];
8946
8947 if (tp->irq_cnt == 1)
8948 name = tp->dev->name;
8949 else {
8950 name = &tnapi->irq_lbl[0];
8951 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
8952 name[IFNAMSIZ-1] = 0;
8953 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008954
Joe Perches63c3a662011-04-26 08:12:10 +00008955 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08008956 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +00008957 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -08008958 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00008959 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008960 } else {
8961 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +00008962 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -08008963 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00008964 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008965 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008966
8967 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008968}
8969
Michael Chan79381092005-04-21 17:13:59 -07008970static int tg3_test_interrupt(struct tg3 *tp)
8971{
Matt Carlson09943a12009-08-28 14:01:57 +00008972 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07008973 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07008974 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008975 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07008976
Michael Chand4bc3922005-05-29 14:59:20 -07008977 if (!netif_running(dev))
8978 return -ENODEV;
8979
Michael Chan79381092005-04-21 17:13:59 -07008980 tg3_disable_ints(tp);
8981
Matt Carlson4f125f42009-09-01 12:55:02 +00008982 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008983
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008984 /*
8985 * Turn off MSI one shot mode. Otherwise this test has no
8986 * observable way to know whether the interrupt was delivered.
8987 */
Joe Perches63c3a662011-04-26 08:12:10 +00008988 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008989 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
8990 tw32(MSGINT_MODE, val);
8991 }
8992
Matt Carlson4f125f42009-09-01 12:55:02 +00008993 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00008994 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008995 if (err)
8996 return err;
8997
Matt Carlson898a56f2009-08-28 14:02:40 +00008998 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07008999 tg3_enable_ints(tp);
9000
9001 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009002 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07009003
9004 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07009005 u32 int_mbox, misc_host_ctrl;
9006
Matt Carlson898a56f2009-08-28 14:02:40 +00009007 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07009008 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
9009
9010 if ((int_mbox != 0) ||
9011 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
9012 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07009013 break;
Michael Chanb16250e2006-09-27 16:10:14 -07009014 }
9015
Michael Chan79381092005-04-21 17:13:59 -07009016 msleep(10);
9017 }
9018
9019 tg3_disable_ints(tp);
9020
Matt Carlson4f125f42009-09-01 12:55:02 +00009021 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009022
Matt Carlson4f125f42009-09-01 12:55:02 +00009023 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009024
9025 if (err)
9026 return err;
9027
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009028 if (intr_ok) {
9029 /* Reenable MSI one shot mode. */
Joe Perches63c3a662011-04-26 08:12:10 +00009030 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009031 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
9032 tw32(MSGINT_MODE, val);
9033 }
Michael Chan79381092005-04-21 17:13:59 -07009034 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009035 }
Michael Chan79381092005-04-21 17:13:59 -07009036
9037 return -EIO;
9038}
9039
9040/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
9041 * successfully restored
9042 */
9043static int tg3_test_msi(struct tg3 *tp)
9044{
Michael Chan79381092005-04-21 17:13:59 -07009045 int err;
9046 u16 pci_cmd;
9047
Joe Perches63c3a662011-04-26 08:12:10 +00009048 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -07009049 return 0;
9050
9051 /* Turn off SERR reporting in case MSI terminates with Master
9052 * Abort.
9053 */
9054 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
9055 pci_write_config_word(tp->pdev, PCI_COMMAND,
9056 pci_cmd & ~PCI_COMMAND_SERR);
9057
9058 err = tg3_test_interrupt(tp);
9059
9060 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
9061
9062 if (!err)
9063 return 0;
9064
9065 /* other failures */
9066 if (err != -EIO)
9067 return err;
9068
9069 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009070 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
9071 "to INTx mode. Please report this failure to the PCI "
9072 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07009073
Matt Carlson4f125f42009-09-01 12:55:02 +00009074 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00009075
Michael Chan79381092005-04-21 17:13:59 -07009076 pci_disable_msi(tp->pdev);
9077
Joe Perches63c3a662011-04-26 08:12:10 +00009078 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +00009079 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07009080
Matt Carlson4f125f42009-09-01 12:55:02 +00009081 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009082 if (err)
9083 return err;
9084
9085 /* Need to reset the chip because the MSI cycle may have terminated
9086 * with Master Abort.
9087 */
David S. Millerf47c11e2005-06-24 20:18:35 -07009088 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009089
Michael Chan944d9802005-05-29 14:57:48 -07009090 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009091 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009092
David S. Millerf47c11e2005-06-24 20:18:35 -07009093 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009094
9095 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00009096 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07009097
9098 return err;
9099}
9100
Matt Carlson9e9fd122009-01-19 16:57:45 -08009101static int tg3_request_firmware(struct tg3 *tp)
9102{
9103 const __be32 *fw_data;
9104
9105 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009106 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
9107 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009108 return -ENOENT;
9109 }
9110
9111 fw_data = (void *)tp->fw->data;
9112
9113 /* Firmware blob starts with version numbers, followed by
9114 * start address and _full_ length including BSS sections
9115 * (which must be longer than the actual data, of course
9116 */
9117
9118 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9119 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009120 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9121 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009122 release_firmware(tp->fw);
9123 tp->fw = NULL;
9124 return -EINVAL;
9125 }
9126
9127 /* We no longer need firmware; we have it. */
9128 tp->fw_needed = NULL;
9129 return 0;
9130}
9131
Matt Carlson679563f2009-09-01 12:55:46 +00009132static bool tg3_enable_msix(struct tg3 *tp)
9133{
9134 int i, rc, cpus = num_online_cpus();
9135 struct msix_entry msix_ent[tp->irq_max];
9136
9137 if (cpus == 1)
9138 /* Just fallback to the simpler MSI mode. */
9139 return false;
9140
9141 /*
9142 * We want as many rx rings enabled as there are cpus.
9143 * The first MSIX vector only deals with link interrupts, etc,
9144 * so we add one to the number of vectors we are requesting.
9145 */
9146 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
9147
9148 for (i = 0; i < tp->irq_max; i++) {
9149 msix_ent[i].entry = i;
9150 msix_ent[i].vector = 0;
9151 }
9152
9153 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009154 if (rc < 0) {
9155 return false;
9156 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009157 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9158 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009159 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9160 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009161 tp->irq_cnt = rc;
9162 }
9163
9164 for (i = 0; i < tp->irq_max; i++)
9165 tp->napi[i].irq_vec = msix_ent[i].vector;
9166
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009167 netif_set_real_num_tx_queues(tp->dev, 1);
9168 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9169 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9170 pci_disable_msix(tp->pdev);
9171 return false;
9172 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009173
9174 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +00009175 tg3_flag_set(tp, ENABLE_RSS);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009176
9177 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9178 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Joe Perches63c3a662011-04-26 08:12:10 +00009179 tg3_flag_set(tp, ENABLE_TSS);
Matt Carlsonb92b9042010-11-24 08:31:51 +00009180 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9181 }
9182 }
Matt Carlson2430b032010-06-05 17:24:34 +00009183
Matt Carlson679563f2009-09-01 12:55:46 +00009184 return true;
9185}
9186
Matt Carlson07b01732009-08-28 14:01:15 +00009187static void tg3_ints_init(struct tg3 *tp)
9188{
Joe Perches63c3a662011-04-26 08:12:10 +00009189 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
9190 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009191 /* All MSI supporting chips should support tagged
9192 * status. Assert that this is the case.
9193 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009194 netdev_warn(tp->dev,
9195 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009196 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009197 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009198
Joe Perches63c3a662011-04-26 08:12:10 +00009199 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
9200 tg3_flag_set(tp, USING_MSIX);
9201 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
9202 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +00009203
Joe Perches63c3a662011-04-26 08:12:10 +00009204 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009205 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +00009206 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009207 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson679563f2009-09-01 12:55:46 +00009208 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9209 }
9210defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +00009211 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009212 tp->irq_cnt = 1;
9213 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009214 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009215 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009216 }
Matt Carlson07b01732009-08-28 14:01:15 +00009217}
9218
9219static void tg3_ints_fini(struct tg3 *tp)
9220{
Joe Perches63c3a662011-04-26 08:12:10 +00009221 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +00009222 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009223 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +00009224 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009225 tg3_flag_clear(tp, USING_MSI);
9226 tg3_flag_clear(tp, USING_MSIX);
9227 tg3_flag_clear(tp, ENABLE_RSS);
9228 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009229}
9230
Linus Torvalds1da177e2005-04-16 15:20:36 -07009231static int tg3_open(struct net_device *dev)
9232{
9233 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009234 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009235
Matt Carlson9e9fd122009-01-19 16:57:45 -08009236 if (tp->fw_needed) {
9237 err = tg3_request_firmware(tp);
9238 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9239 if (err)
9240 return err;
9241 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009242 netdev_warn(tp->dev, "TSO capability disabled\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009243 tg3_flag_clear(tp, TSO_CAPABLE);
9244 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009245 netdev_notice(tp->dev, "TSO capability restored\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009246 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009247 }
9248 }
9249
Michael Chanc49a1562006-12-17 17:07:29 -08009250 netif_carrier_off(tp->dev);
9251
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009252 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -07009253 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009254 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009255
9256 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009257
Linus Torvalds1da177e2005-04-16 15:20:36 -07009258 tg3_disable_ints(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009259 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009260
David S. Millerf47c11e2005-06-24 20:18:35 -07009261 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009262
Matt Carlson679563f2009-09-01 12:55:46 +00009263 /*
9264 * Setup interrupts first so we know how
9265 * many NAPI resources to allocate
9266 */
9267 tg3_ints_init(tp);
9268
Linus Torvalds1da177e2005-04-16 15:20:36 -07009269 /* The placement of this call is tied
9270 * to the setup and use of Host TX descriptors.
9271 */
9272 err = tg3_alloc_consistent(tp);
9273 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009274 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009275
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009276 tg3_napi_init(tp);
9277
Matt Carlsonfed97812009-09-01 13:10:19 +00009278 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07009279
Matt Carlson4f125f42009-09-01 12:55:02 +00009280 for (i = 0; i < tp->irq_cnt; i++) {
9281 struct tg3_napi *tnapi = &tp->napi[i];
9282 err = tg3_request_irq(tp, i);
9283 if (err) {
9284 for (i--; i >= 0; i--)
9285 free_irq(tnapi->irq_vec, tnapi);
9286 break;
9287 }
9288 }
Matt Carlson07b01732009-08-28 14:01:15 +00009289
9290 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009291 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00009292
David S. Millerf47c11e2005-06-24 20:18:35 -07009293 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009294
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009295 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009296 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07009297 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009298 tg3_free_rings(tp);
9299 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00009300 if (tg3_flag(tp, TAGGED_STATUS))
David S. Millerfac9b832005-05-18 22:46:34 -07009301 tp->timer_offset = HZ;
9302 else
9303 tp->timer_offset = HZ / 10;
9304
9305 BUG_ON(tp->timer_offset > HZ);
9306 tp->timer_counter = tp->timer_multiplier =
9307 (HZ / tp->timer_offset);
9308 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07009309 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009310
9311 init_timer(&tp->timer);
9312 tp->timer.expires = jiffies + tp->timer_offset;
9313 tp->timer.data = (unsigned long) tp;
9314 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009315 }
9316
David S. Millerf47c11e2005-06-24 20:18:35 -07009317 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009318
Matt Carlson07b01732009-08-28 14:01:15 +00009319 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009320 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009321
Joe Perches63c3a662011-04-26 08:12:10 +00009322 if (tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -07009323 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07009324
Michael Chan79381092005-04-21 17:13:59 -07009325 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009326 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07009327 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07009328 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07009329 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009330
Matt Carlson679563f2009-09-01 12:55:46 +00009331 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07009332 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009333
Joe Perches63c3a662011-04-26 08:12:10 +00009334 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009335 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009336
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009337 tw32(PCIE_TRANSACTION_CFG,
9338 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009339 }
Michael Chan79381092005-04-21 17:13:59 -07009340 }
9341
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009342 tg3_phy_start(tp);
9343
David S. Millerf47c11e2005-06-24 20:18:35 -07009344 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009345
Michael Chan79381092005-04-21 17:13:59 -07009346 add_timer(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +00009347 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009348 tg3_enable_ints(tp);
9349
David S. Millerf47c11e2005-06-24 20:18:35 -07009350 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009351
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009352 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009353
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00009354 /*
9355 * Reset loopback feature if it was turned on while the device was down
9356 * make sure that it's installed properly now.
9357 */
9358 if (dev->features & NETIF_F_LOOPBACK)
9359 tg3_set_loopback(dev, dev->features);
9360
Linus Torvalds1da177e2005-04-16 15:20:36 -07009361 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00009362
Matt Carlson679563f2009-09-01 12:55:46 +00009363err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00009364 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9365 struct tg3_napi *tnapi = &tp->napi[i];
9366 free_irq(tnapi->irq_vec, tnapi);
9367 }
Matt Carlson07b01732009-08-28 14:01:15 +00009368
Matt Carlson679563f2009-09-01 12:55:46 +00009369err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00009370 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009371 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009372 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00009373
9374err_out1:
9375 tg3_ints_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009376 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009377}
9378
Eric Dumazet511d2222010-07-07 20:44:24 +00009379static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
9380 struct rtnl_link_stats64 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009381static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
9382
9383static int tg3_close(struct net_device *dev)
9384{
Matt Carlson4f125f42009-09-01 12:55:02 +00009385 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009386 struct tg3 *tp = netdev_priv(dev);
9387
Matt Carlsonfed97812009-09-01 13:10:19 +00009388 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07009389 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08009390
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009391 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009392
9393 del_timer_sync(&tp->timer);
9394
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009395 tg3_phy_stop(tp);
9396
David S. Millerf47c11e2005-06-24 20:18:35 -07009397 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009398
9399 tg3_disable_ints(tp);
9400
Michael Chan944d9802005-05-29 14:57:48 -07009401 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009402 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009403 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009404
David S. Millerf47c11e2005-06-24 20:18:35 -07009405 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009406
Matt Carlson4f125f42009-09-01 12:55:02 +00009407 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9408 struct tg3_napi *tnapi = &tp->napi[i];
9409 free_irq(tnapi->irq_vec, tnapi);
9410 }
Matt Carlson07b01732009-08-28 14:01:15 +00009411
9412 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009413
Eric Dumazet511d2222010-07-07 20:44:24 +00009414 tg3_get_stats64(tp->dev, &tp->net_stats_prev);
9415
Linus Torvalds1da177e2005-04-16 15:20:36 -07009416 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9417 sizeof(tp->estats_prev));
9418
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009419 tg3_napi_fini(tp);
9420
Linus Torvalds1da177e2005-04-16 15:20:36 -07009421 tg3_free_consistent(tp);
9422
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009423 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -08009424
9425 netif_carrier_off(tp->dev);
9426
Linus Torvalds1da177e2005-04-16 15:20:36 -07009427 return 0;
9428}
9429
Eric Dumazet511d2222010-07-07 20:44:24 +00009430static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -07009431{
9432 return ((u64)val->high << 32) | ((u64)val->low);
9433}
9434
Eric Dumazet511d2222010-07-07 20:44:24 +00009435static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009436{
9437 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9438
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009439 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009440 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9441 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009442 u32 val;
9443
David S. Millerf47c11e2005-06-24 20:18:35 -07009444 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009445 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9446 tg3_writephy(tp, MII_TG3_TEST1,
9447 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009448 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009449 } else
9450 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009451 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009452
9453 tp->phy_crc_errors += val;
9454
9455 return tp->phy_crc_errors;
9456 }
9457
9458 return get_stat64(&hw_stats->rx_fcs_errors);
9459}
9460
9461#define ESTAT_ADD(member) \
9462 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +00009463 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009464
9465static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9466{
9467 struct tg3_ethtool_stats *estats = &tp->estats;
9468 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9469 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9470
9471 if (!hw_stats)
9472 return old_estats;
9473
9474 ESTAT_ADD(rx_octets);
9475 ESTAT_ADD(rx_fragments);
9476 ESTAT_ADD(rx_ucast_packets);
9477 ESTAT_ADD(rx_mcast_packets);
9478 ESTAT_ADD(rx_bcast_packets);
9479 ESTAT_ADD(rx_fcs_errors);
9480 ESTAT_ADD(rx_align_errors);
9481 ESTAT_ADD(rx_xon_pause_rcvd);
9482 ESTAT_ADD(rx_xoff_pause_rcvd);
9483 ESTAT_ADD(rx_mac_ctrl_rcvd);
9484 ESTAT_ADD(rx_xoff_entered);
9485 ESTAT_ADD(rx_frame_too_long_errors);
9486 ESTAT_ADD(rx_jabbers);
9487 ESTAT_ADD(rx_undersize_packets);
9488 ESTAT_ADD(rx_in_length_errors);
9489 ESTAT_ADD(rx_out_length_errors);
9490 ESTAT_ADD(rx_64_or_less_octet_packets);
9491 ESTAT_ADD(rx_65_to_127_octet_packets);
9492 ESTAT_ADD(rx_128_to_255_octet_packets);
9493 ESTAT_ADD(rx_256_to_511_octet_packets);
9494 ESTAT_ADD(rx_512_to_1023_octet_packets);
9495 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9496 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9497 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9498 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9499 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9500
9501 ESTAT_ADD(tx_octets);
9502 ESTAT_ADD(tx_collisions);
9503 ESTAT_ADD(tx_xon_sent);
9504 ESTAT_ADD(tx_xoff_sent);
9505 ESTAT_ADD(tx_flow_control);
9506 ESTAT_ADD(tx_mac_errors);
9507 ESTAT_ADD(tx_single_collisions);
9508 ESTAT_ADD(tx_mult_collisions);
9509 ESTAT_ADD(tx_deferred);
9510 ESTAT_ADD(tx_excessive_collisions);
9511 ESTAT_ADD(tx_late_collisions);
9512 ESTAT_ADD(tx_collide_2times);
9513 ESTAT_ADD(tx_collide_3times);
9514 ESTAT_ADD(tx_collide_4times);
9515 ESTAT_ADD(tx_collide_5times);
9516 ESTAT_ADD(tx_collide_6times);
9517 ESTAT_ADD(tx_collide_7times);
9518 ESTAT_ADD(tx_collide_8times);
9519 ESTAT_ADD(tx_collide_9times);
9520 ESTAT_ADD(tx_collide_10times);
9521 ESTAT_ADD(tx_collide_11times);
9522 ESTAT_ADD(tx_collide_12times);
9523 ESTAT_ADD(tx_collide_13times);
9524 ESTAT_ADD(tx_collide_14times);
9525 ESTAT_ADD(tx_collide_15times);
9526 ESTAT_ADD(tx_ucast_packets);
9527 ESTAT_ADD(tx_mcast_packets);
9528 ESTAT_ADD(tx_bcast_packets);
9529 ESTAT_ADD(tx_carrier_sense_errors);
9530 ESTAT_ADD(tx_discards);
9531 ESTAT_ADD(tx_errors);
9532
9533 ESTAT_ADD(dma_writeq_full);
9534 ESTAT_ADD(dma_write_prioq_full);
9535 ESTAT_ADD(rxbds_empty);
9536 ESTAT_ADD(rx_discards);
9537 ESTAT_ADD(rx_errors);
9538 ESTAT_ADD(rx_threshold_hit);
9539
9540 ESTAT_ADD(dma_readq_full);
9541 ESTAT_ADD(dma_read_prioq_full);
9542 ESTAT_ADD(tx_comp_queue_full);
9543
9544 ESTAT_ADD(ring_set_send_prod_index);
9545 ESTAT_ADD(ring_status_update);
9546 ESTAT_ADD(nic_irqs);
9547 ESTAT_ADD(nic_avoided_irqs);
9548 ESTAT_ADD(nic_tx_threshold_hit);
9549
Matt Carlson4452d092011-05-19 12:12:51 +00009550 ESTAT_ADD(mbuf_lwm_thresh_hit);
9551
Linus Torvalds1da177e2005-04-16 15:20:36 -07009552 return estats;
9553}
9554
Eric Dumazet511d2222010-07-07 20:44:24 +00009555static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9556 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009557{
9558 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +00009559 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009560 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9561
9562 if (!hw_stats)
9563 return old_stats;
9564
9565 stats->rx_packets = old_stats->rx_packets +
9566 get_stat64(&hw_stats->rx_ucast_packets) +
9567 get_stat64(&hw_stats->rx_mcast_packets) +
9568 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009569
Linus Torvalds1da177e2005-04-16 15:20:36 -07009570 stats->tx_packets = old_stats->tx_packets +
9571 get_stat64(&hw_stats->tx_ucast_packets) +
9572 get_stat64(&hw_stats->tx_mcast_packets) +
9573 get_stat64(&hw_stats->tx_bcast_packets);
9574
9575 stats->rx_bytes = old_stats->rx_bytes +
9576 get_stat64(&hw_stats->rx_octets);
9577 stats->tx_bytes = old_stats->tx_bytes +
9578 get_stat64(&hw_stats->tx_octets);
9579
9580 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009581 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009582 stats->tx_errors = old_stats->tx_errors +
9583 get_stat64(&hw_stats->tx_errors) +
9584 get_stat64(&hw_stats->tx_mac_errors) +
9585 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9586 get_stat64(&hw_stats->tx_discards);
9587
9588 stats->multicast = old_stats->multicast +
9589 get_stat64(&hw_stats->rx_mcast_packets);
9590 stats->collisions = old_stats->collisions +
9591 get_stat64(&hw_stats->tx_collisions);
9592
9593 stats->rx_length_errors = old_stats->rx_length_errors +
9594 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9595 get_stat64(&hw_stats->rx_undersize_packets);
9596
9597 stats->rx_over_errors = old_stats->rx_over_errors +
9598 get_stat64(&hw_stats->rxbds_empty);
9599 stats->rx_frame_errors = old_stats->rx_frame_errors +
9600 get_stat64(&hw_stats->rx_align_errors);
9601 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9602 get_stat64(&hw_stats->tx_discards);
9603 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
9604 get_stat64(&hw_stats->tx_carrier_sense_errors);
9605
9606 stats->rx_crc_errors = old_stats->rx_crc_errors +
9607 calc_crc_errors(tp);
9608
John W. Linville4f63b872005-09-12 14:43:18 -07009609 stats->rx_missed_errors = old_stats->rx_missed_errors +
9610 get_stat64(&hw_stats->rx_discards);
9611
Eric Dumazetb0057c52010-10-10 19:55:52 +00009612 stats->rx_dropped = tp->rx_dropped;
9613
Linus Torvalds1da177e2005-04-16 15:20:36 -07009614 return stats;
9615}
9616
9617static inline u32 calc_crc(unsigned char *buf, int len)
9618{
9619 u32 reg;
9620 u32 tmp;
9621 int j, k;
9622
9623 reg = 0xffffffff;
9624
9625 for (j = 0; j < len; j++) {
9626 reg ^= buf[j];
9627
9628 for (k = 0; k < 8; k++) {
9629 tmp = reg & 0x01;
9630
9631 reg >>= 1;
9632
Matt Carlson859a588792010-04-05 10:19:28 +00009633 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009634 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009635 }
9636 }
9637
9638 return ~reg;
9639}
9640
9641static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9642{
9643 /* accept or reject all multicast frames */
9644 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9645 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9646 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9647 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9648}
9649
9650static void __tg3_set_rx_mode(struct net_device *dev)
9651{
9652 struct tg3 *tp = netdev_priv(dev);
9653 u32 rx_mode;
9654
9655 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9656 RX_MODE_KEEP_VLAN_TAG);
9657
Matt Carlsonbf933c82011-01-25 15:58:49 +00009658#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009659 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9660 * flag clear.
9661 */
Joe Perches63c3a662011-04-26 08:12:10 +00009662 if (!tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009663 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9664#endif
9665
9666 if (dev->flags & IFF_PROMISC) {
9667 /* Promiscuous mode. */
9668 rx_mode |= RX_MODE_PROMISC;
9669 } else if (dev->flags & IFF_ALLMULTI) {
9670 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009671 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00009672 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009673 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009674 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009675 } else {
9676 /* Accept one or more multicast(s). */
Jiri Pirko22bedad32010-04-01 21:22:57 +00009677 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009678 u32 mc_filter[4] = { 0, };
9679 u32 regidx;
9680 u32 bit;
9681 u32 crc;
9682
Jiri Pirko22bedad32010-04-01 21:22:57 +00009683 netdev_for_each_mc_addr(ha, dev) {
9684 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009685 bit = ~crc & 0x7f;
9686 regidx = (bit & 0x60) >> 5;
9687 bit &= 0x1f;
9688 mc_filter[regidx] |= (1 << bit);
9689 }
9690
9691 tw32(MAC_HASH_REG_0, mc_filter[0]);
9692 tw32(MAC_HASH_REG_1, mc_filter[1]);
9693 tw32(MAC_HASH_REG_2, mc_filter[2]);
9694 tw32(MAC_HASH_REG_3, mc_filter[3]);
9695 }
9696
9697 if (rx_mode != tp->rx_mode) {
9698 tp->rx_mode = rx_mode;
9699 tw32_f(MAC_RX_MODE, rx_mode);
9700 udelay(10);
9701 }
9702}
9703
9704static void tg3_set_rx_mode(struct net_device *dev)
9705{
9706 struct tg3 *tp = netdev_priv(dev);
9707
Michael Chane75f7c92006-03-20 21:33:26 -08009708 if (!netif_running(dev))
9709 return;
9710
David S. Millerf47c11e2005-06-24 20:18:35 -07009711 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009712 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -07009713 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009714}
9715
Linus Torvalds1da177e2005-04-16 15:20:36 -07009716static int tg3_get_regs_len(struct net_device *dev)
9717{
Matt Carlson97bd8e42011-04-13 11:05:04 +00009718 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009719}
9720
9721static void tg3_get_regs(struct net_device *dev,
9722 struct ethtool_regs *regs, void *_p)
9723{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009724 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009725
9726 regs->version = 0;
9727
Matt Carlson97bd8e42011-04-13 11:05:04 +00009728 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009729
Matt Carlson80096062010-08-02 11:26:06 +00009730 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009731 return;
9732
David S. Millerf47c11e2005-06-24 20:18:35 -07009733 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009734
Matt Carlson97bd8e42011-04-13 11:05:04 +00009735 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009736
David S. Millerf47c11e2005-06-24 20:18:35 -07009737 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009738}
9739
9740static int tg3_get_eeprom_len(struct net_device *dev)
9741{
9742 struct tg3 *tp = netdev_priv(dev);
9743
9744 return tp->nvram_size;
9745}
9746
Linus Torvalds1da177e2005-04-16 15:20:36 -07009747static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9748{
9749 struct tg3 *tp = netdev_priv(dev);
9750 int ret;
9751 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -08009752 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009753 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009754
Joe Perches63c3a662011-04-26 08:12:10 +00009755 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +00009756 return -EINVAL;
9757
Matt Carlson80096062010-08-02 11:26:06 +00009758 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009759 return -EAGAIN;
9760
Linus Torvalds1da177e2005-04-16 15:20:36 -07009761 offset = eeprom->offset;
9762 len = eeprom->len;
9763 eeprom->len = 0;
9764
9765 eeprom->magic = TG3_EEPROM_MAGIC;
9766
9767 if (offset & 3) {
9768 /* adjustments to start on required 4 byte boundary */
9769 b_offset = offset & 3;
9770 b_count = 4 - b_offset;
9771 if (b_count > len) {
9772 /* i.e. offset=1 len=2 */
9773 b_count = len;
9774 }
Matt Carlsona9dc5292009-02-25 14:25:30 +00009775 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009776 if (ret)
9777 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +00009778 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009779 len -= b_count;
9780 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009781 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009782 }
9783
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009784 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009785 pd = &data[eeprom->len];
9786 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00009787 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009788 if (ret) {
9789 eeprom->len += i;
9790 return ret;
9791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009792 memcpy(pd + i, &val, 4);
9793 }
9794 eeprom->len += i;
9795
9796 if (len & 3) {
9797 /* read last bytes not ending on 4 byte boundary */
9798 pd = &data[eeprom->len];
9799 b_count = len & 3;
9800 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009801 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009802 if (ret)
9803 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009804 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009805 eeprom->len += b_count;
9806 }
9807 return 0;
9808}
9809
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009810static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009811
9812static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9813{
9814 struct tg3 *tp = netdev_priv(dev);
9815 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009816 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009817 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009818 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009819
Matt Carlson80096062010-08-02 11:26:06 +00009820 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009821 return -EAGAIN;
9822
Joe Perches63c3a662011-04-26 08:12:10 +00009823 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +00009824 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009825 return -EINVAL;
9826
9827 offset = eeprom->offset;
9828 len = eeprom->len;
9829
9830 if ((b_offset = (offset & 3))) {
9831 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +00009832 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009833 if (ret)
9834 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009835 len += b_offset;
9836 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -07009837 if (len < 4)
9838 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009839 }
9840
9841 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -07009842 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009843 /* adjustments to end on required 4 byte boundary */
9844 odd_len = 1;
9845 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009846 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009847 if (ret)
9848 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009849 }
9850
9851 buf = data;
9852 if (b_offset || odd_len) {
9853 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009854 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009855 return -ENOMEM;
9856 if (b_offset)
9857 memcpy(buf, &start, 4);
9858 if (odd_len)
9859 memcpy(buf+len-4, &end, 4);
9860 memcpy(buf + b_offset, data, eeprom->len);
9861 }
9862
9863 ret = tg3_nvram_write_block(tp, offset, len, buf);
9864
9865 if (buf != data)
9866 kfree(buf);
9867
9868 return ret;
9869}
9870
9871static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9872{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009873 struct tg3 *tp = netdev_priv(dev);
9874
Joe Perches63c3a662011-04-26 08:12:10 +00009875 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009876 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009877 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009878 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009879 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9880 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009881 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009882
Linus Torvalds1da177e2005-04-16 15:20:36 -07009883 cmd->supported = (SUPPORTED_Autoneg);
9884
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009885 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009886 cmd->supported |= (SUPPORTED_1000baseT_Half |
9887 SUPPORTED_1000baseT_Full);
9888
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009889 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009890 cmd->supported |= (SUPPORTED_100baseT_Half |
9891 SUPPORTED_100baseT_Full |
9892 SUPPORTED_10baseT_Half |
9893 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -08009894 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -07009895 cmd->port = PORT_TP;
9896 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009897 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -07009898 cmd->port = PORT_FIBRE;
9899 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009900
Linus Torvalds1da177e2005-04-16 15:20:36 -07009901 cmd->advertising = tp->link_config.advertising;
9902 if (netif_running(dev)) {
David Decotigny70739492011-04-27 18:32:40 +00009903 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009904 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson64c22182010-10-14 10:37:44 +00009905 } else {
David Decotigny70739492011-04-27 18:32:40 +00009906 ethtool_cmd_speed_set(cmd, SPEED_INVALID);
Matt Carlson64c22182010-10-14 10:37:44 +00009907 cmd->duplex = DUPLEX_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009908 }
Matt Carlson882e9792009-09-01 13:21:36 +00009909 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009910 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009911 cmd->autoneg = tp->link_config.autoneg;
9912 cmd->maxtxpkt = 0;
9913 cmd->maxrxpkt = 0;
9914 return 0;
9915}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009916
Linus Torvalds1da177e2005-04-16 15:20:36 -07009917static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9918{
9919 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +00009920 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009921
Joe Perches63c3a662011-04-26 08:12:10 +00009922 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009923 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009924 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009925 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009926 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9927 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009928 }
9929
Matt Carlson7e5856b2009-02-25 14:23:01 +00009930 if (cmd->autoneg != AUTONEG_ENABLE &&
9931 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -07009932 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009933
9934 if (cmd->autoneg == AUTONEG_DISABLE &&
9935 cmd->duplex != DUPLEX_FULL &&
9936 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -07009937 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009938
Matt Carlson7e5856b2009-02-25 14:23:01 +00009939 if (cmd->autoneg == AUTONEG_ENABLE) {
9940 u32 mask = ADVERTISED_Autoneg |
9941 ADVERTISED_Pause |
9942 ADVERTISED_Asym_Pause;
9943
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009944 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +00009945 mask |= ADVERTISED_1000baseT_Half |
9946 ADVERTISED_1000baseT_Full;
9947
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009948 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +00009949 mask |= ADVERTISED_100baseT_Half |
9950 ADVERTISED_100baseT_Full |
9951 ADVERTISED_10baseT_Half |
9952 ADVERTISED_10baseT_Full |
9953 ADVERTISED_TP;
9954 else
9955 mask |= ADVERTISED_FIBRE;
9956
9957 if (cmd->advertising & ~mask)
9958 return -EINVAL;
9959
9960 mask &= (ADVERTISED_1000baseT_Half |
9961 ADVERTISED_1000baseT_Full |
9962 ADVERTISED_100baseT_Half |
9963 ADVERTISED_100baseT_Full |
9964 ADVERTISED_10baseT_Half |
9965 ADVERTISED_10baseT_Full);
9966
9967 cmd->advertising &= mask;
9968 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009969 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +00009970 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +00009971 return -EINVAL;
9972
9973 if (cmd->duplex != DUPLEX_FULL)
9974 return -EINVAL;
9975 } else {
David Decotigny25db0332011-04-27 18:32:39 +00009976 if (speed != SPEED_100 &&
9977 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +00009978 return -EINVAL;
9979 }
9980 }
9981
David S. Millerf47c11e2005-06-24 20:18:35 -07009982 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009983
9984 tp->link_config.autoneg = cmd->autoneg;
9985 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -07009986 tp->link_config.advertising = (cmd->advertising |
9987 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009988 tp->link_config.speed = SPEED_INVALID;
9989 tp->link_config.duplex = DUPLEX_INVALID;
9990 } else {
9991 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +00009992 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009993 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009994 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009995
Michael Chan24fcad62006-12-17 17:06:46 -08009996 tp->link_config.orig_speed = tp->link_config.speed;
9997 tp->link_config.orig_duplex = tp->link_config.duplex;
9998 tp->link_config.orig_autoneg = tp->link_config.autoneg;
9999
Linus Torvalds1da177e2005-04-16 15:20:36 -070010000 if (netif_running(dev))
10001 tg3_setup_phy(tp, 1);
10002
David S. Millerf47c11e2005-06-24 20:18:35 -070010003 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010004
Linus Torvalds1da177e2005-04-16 15:20:36 -070010005 return 0;
10006}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010007
Linus Torvalds1da177e2005-04-16 15:20:36 -070010008static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10009{
10010 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010011
Linus Torvalds1da177e2005-04-16 15:20:36 -070010012 strcpy(info->driver, DRV_MODULE_NAME);
10013 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -080010014 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010015 strcpy(info->bus_info, pci_name(tp->pdev));
10016}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010017
Linus Torvalds1da177e2005-04-16 15:20:36 -070010018static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10019{
10020 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010021
Joe Perches63c3a662011-04-26 08:12:10 +000010022 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070010023 wol->supported = WAKE_MAGIC;
10024 else
10025 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010026 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010027 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010028 wol->wolopts = WAKE_MAGIC;
10029 memset(&wol->sopass, 0, sizeof(wol->sopass));
10030}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010031
Linus Torvalds1da177e2005-04-16 15:20:36 -070010032static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10033{
10034 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010035 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010036
Linus Torvalds1da177e2005-04-16 15:20:36 -070010037 if (wol->wolopts & ~WAKE_MAGIC)
10038 return -EINVAL;
10039 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010040 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010041 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010042
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010043 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
10044
David S. Millerf47c11e2005-06-24 20:18:35 -070010045 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010046 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000010047 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010048 else
Joe Perches63c3a662011-04-26 08:12:10 +000010049 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070010050 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010051
Linus Torvalds1da177e2005-04-16 15:20:36 -070010052 return 0;
10053}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010054
Linus Torvalds1da177e2005-04-16 15:20:36 -070010055static u32 tg3_get_msglevel(struct net_device *dev)
10056{
10057 struct tg3 *tp = netdev_priv(dev);
10058 return tp->msg_enable;
10059}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010060
Linus Torvalds1da177e2005-04-16 15:20:36 -070010061static void tg3_set_msglevel(struct net_device *dev, u32 value)
10062{
10063 struct tg3 *tp = netdev_priv(dev);
10064 tp->msg_enable = value;
10065}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010066
Linus Torvalds1da177e2005-04-16 15:20:36 -070010067static int tg3_nway_reset(struct net_device *dev)
10068{
10069 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010070 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010071
Linus Torvalds1da177e2005-04-16 15:20:36 -070010072 if (!netif_running(dev))
10073 return -EAGAIN;
10074
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010075 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010076 return -EINVAL;
10077
Joe Perches63c3a662011-04-26 08:12:10 +000010078 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010079 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010080 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010081 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010082 } else {
10083 u32 bmcr;
10084
10085 spin_lock_bh(&tp->lock);
10086 r = -EINVAL;
10087 tg3_readphy(tp, MII_BMCR, &bmcr);
10088 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10089 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010090 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010091 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10092 BMCR_ANENABLE);
10093 r = 0;
10094 }
10095 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010096 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010097
Linus Torvalds1da177e2005-04-16 15:20:36 -070010098 return r;
10099}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010100
Linus Torvalds1da177e2005-04-16 15:20:36 -070010101static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10102{
10103 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010104
Matt Carlson2c49a442010-09-30 10:34:35 +000010105 ering->rx_max_pending = tp->rx_std_ring_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010106 ering->rx_mini_max_pending = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010107 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000010108 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010109 else
10110 ering->rx_jumbo_max_pending = 0;
10111
10112 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010113
10114 ering->rx_pending = tp->rx_pending;
10115 ering->rx_mini_pending = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010116 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080010117 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10118 else
10119 ering->rx_jumbo_pending = 0;
10120
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010121 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010122}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010123
Linus Torvalds1da177e2005-04-16 15:20:36 -070010124static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10125{
10126 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010127 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010128
Matt Carlson2c49a442010-09-30 10:34:35 +000010129 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10130 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010131 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10132 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000010133 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010134 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010135 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010136
Michael Chanbbe832c2005-06-24 20:20:04 -070010137 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010138 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010139 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010140 irq_sync = 1;
10141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010142
Michael Chanbbe832c2005-06-24 20:20:04 -070010143 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010144
Linus Torvalds1da177e2005-04-16 15:20:36 -070010145 tp->rx_pending = ering->rx_pending;
10146
Joe Perches63c3a662011-04-26 08:12:10 +000010147 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010148 tp->rx_pending > 63)
10149 tp->rx_pending = 63;
10150 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010151
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010152 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010153 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010154
10155 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010156 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010157 err = tg3_restart_hw(tp, 1);
10158 if (!err)
10159 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010160 }
10161
David S. Millerf47c11e2005-06-24 20:18:35 -070010162 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010163
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010164 if (irq_sync && !err)
10165 tg3_phy_start(tp);
10166
Michael Chanb9ec6c12006-07-25 16:37:27 -070010167 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010168}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010169
Linus Torvalds1da177e2005-04-16 15:20:36 -070010170static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10171{
10172 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010173
Joe Perches63c3a662011-04-26 08:12:10 +000010174 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080010175
Steve Glendinninge18ce342008-12-16 02:00:00 -080010176 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010177 epause->rx_pause = 1;
10178 else
10179 epause->rx_pause = 0;
10180
Steve Glendinninge18ce342008-12-16 02:00:00 -080010181 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010182 epause->tx_pause = 1;
10183 else
10184 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010185}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010186
Linus Torvalds1da177e2005-04-16 15:20:36 -070010187static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10188{
10189 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010190 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010191
Joe Perches63c3a662011-04-26 08:12:10 +000010192 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000010193 u32 newadv;
10194 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010195
Matt Carlson27121682010-02-17 15:16:57 +000010196 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010197
Matt Carlson27121682010-02-17 15:16:57 +000010198 if (!(phydev->supported & SUPPORTED_Pause) ||
10199 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010200 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010201 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010202
Matt Carlson27121682010-02-17 15:16:57 +000010203 tp->link_config.flowctrl = 0;
10204 if (epause->rx_pause) {
10205 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010206
Matt Carlson27121682010-02-17 15:16:57 +000010207 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010208 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010209 newadv = ADVERTISED_Pause;
10210 } else
10211 newadv = ADVERTISED_Pause |
10212 ADVERTISED_Asym_Pause;
10213 } else if (epause->tx_pause) {
10214 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10215 newadv = ADVERTISED_Asym_Pause;
10216 } else
10217 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010218
Matt Carlson27121682010-02-17 15:16:57 +000010219 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010220 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010221 else
Joe Perches63c3a662011-04-26 08:12:10 +000010222 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010223
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010224 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010225 u32 oldadv = phydev->advertising &
10226 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10227 if (oldadv != newadv) {
10228 phydev->advertising &=
10229 ~(ADVERTISED_Pause |
10230 ADVERTISED_Asym_Pause);
10231 phydev->advertising |= newadv;
10232 if (phydev->autoneg) {
10233 /*
10234 * Always renegotiate the link to
10235 * inform our link partner of our
10236 * flow control settings, even if the
10237 * flow control is forced. Let
10238 * tg3_adjust_link() do the final
10239 * flow control setup.
10240 */
10241 return phy_start_aneg(phydev);
10242 }
10243 }
10244
10245 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010246 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010247 } else {
10248 tp->link_config.orig_advertising &=
10249 ~(ADVERTISED_Pause |
10250 ADVERTISED_Asym_Pause);
10251 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010252 }
10253 } else {
10254 int irq_sync = 0;
10255
10256 if (netif_running(dev)) {
10257 tg3_netif_stop(tp);
10258 irq_sync = 1;
10259 }
10260
10261 tg3_full_lock(tp, irq_sync);
10262
10263 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010264 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010265 else
Joe Perches63c3a662011-04-26 08:12:10 +000010266 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010267 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010268 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010269 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010270 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010271 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010272 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010273 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010274 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010275
10276 if (netif_running(dev)) {
10277 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10278 err = tg3_restart_hw(tp, 1);
10279 if (!err)
10280 tg3_netif_start(tp);
10281 }
10282
10283 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010285
Michael Chanb9ec6c12006-07-25 16:37:27 -070010286 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010287}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010288
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010289static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010290{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010291 switch (sset) {
10292 case ETH_SS_TEST:
10293 return TG3_NUM_TEST;
10294 case ETH_SS_STATS:
10295 return TG3_NUM_STATS;
10296 default:
10297 return -EOPNOTSUPP;
10298 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010299}
10300
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010301static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010302{
10303 switch (stringset) {
10304 case ETH_SS_STATS:
10305 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10306 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010307 case ETH_SS_TEST:
10308 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10309 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010310 default:
10311 WARN_ON(1); /* we need a WARN() */
10312 break;
10313 }
10314}
10315
stephen hemminger81b87092011-04-04 08:43:50 +000010316static int tg3_set_phys_id(struct net_device *dev,
10317 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070010318{
10319 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070010320
10321 if (!netif_running(tp->dev))
10322 return -EAGAIN;
10323
stephen hemminger81b87092011-04-04 08:43:50 +000010324 switch (state) {
10325 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000010326 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070010327
stephen hemminger81b87092011-04-04 08:43:50 +000010328 case ETHTOOL_ID_ON:
10329 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10330 LED_CTRL_1000MBPS_ON |
10331 LED_CTRL_100MBPS_ON |
10332 LED_CTRL_10MBPS_ON |
10333 LED_CTRL_TRAFFIC_OVERRIDE |
10334 LED_CTRL_TRAFFIC_BLINK |
10335 LED_CTRL_TRAFFIC_LED);
10336 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010337
stephen hemminger81b87092011-04-04 08:43:50 +000010338 case ETHTOOL_ID_OFF:
10339 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10340 LED_CTRL_TRAFFIC_OVERRIDE);
10341 break;
Michael Chan4009a932005-09-05 17:52:54 -070010342
stephen hemminger81b87092011-04-04 08:43:50 +000010343 case ETHTOOL_ID_INACTIVE:
10344 tw32(MAC_LED_CTRL, tp->led_ctrl);
10345 break;
Michael Chan4009a932005-09-05 17:52:54 -070010346 }
stephen hemminger81b87092011-04-04 08:43:50 +000010347
Michael Chan4009a932005-09-05 17:52:54 -070010348 return 0;
10349}
10350
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010351static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010352 struct ethtool_stats *estats, u64 *tmp_stats)
10353{
10354 struct tg3 *tp = netdev_priv(dev);
10355 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10356}
10357
Matt Carlsonc3e94502011-04-13 11:05:08 +000010358static __be32 * tg3_vpd_readblock(struct tg3 *tp)
10359{
10360 int i;
10361 __be32 *buf;
10362 u32 offset = 0, len = 0;
10363 u32 magic, val;
10364
Joe Perches63c3a662011-04-26 08:12:10 +000010365 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000010366 return NULL;
10367
10368 if (magic == TG3_EEPROM_MAGIC) {
10369 for (offset = TG3_NVM_DIR_START;
10370 offset < TG3_NVM_DIR_END;
10371 offset += TG3_NVM_DIRENT_SIZE) {
10372 if (tg3_nvram_read(tp, offset, &val))
10373 return NULL;
10374
10375 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
10376 TG3_NVM_DIRTYPE_EXTVPD)
10377 break;
10378 }
10379
10380 if (offset != TG3_NVM_DIR_END) {
10381 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
10382 if (tg3_nvram_read(tp, offset + 4, &offset))
10383 return NULL;
10384
10385 offset = tg3_nvram_logical_addr(tp, offset);
10386 }
10387 }
10388
10389 if (!offset || !len) {
10390 offset = TG3_NVM_VPD_OFF;
10391 len = TG3_NVM_VPD_LEN;
10392 }
10393
10394 buf = kmalloc(len, GFP_KERNEL);
10395 if (buf == NULL)
10396 return NULL;
10397
10398 if (magic == TG3_EEPROM_MAGIC) {
10399 for (i = 0; i < len; i += 4) {
10400 /* The data is in little-endian format in NVRAM.
10401 * Use the big-endian read routines to preserve
10402 * the byte order as it exists in NVRAM.
10403 */
10404 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
10405 goto error;
10406 }
10407 } else {
10408 u8 *ptr;
10409 ssize_t cnt;
10410 unsigned int pos = 0;
10411
10412 ptr = (u8 *)&buf[0];
10413 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
10414 cnt = pci_read_vpd(tp->pdev, pos,
10415 len - pos, ptr);
10416 if (cnt == -ETIMEDOUT || cnt == -EINTR)
10417 cnt = 0;
10418 else if (cnt < 0)
10419 goto error;
10420 }
10421 if (pos != len)
10422 goto error;
10423 }
10424
10425 return buf;
10426
10427error:
10428 kfree(buf);
10429 return NULL;
10430}
10431
Michael Chan566f86a2005-05-29 14:56:58 -070010432#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010433#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10434#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10435#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Michael Chanb16250e2006-09-27 16:10:14 -070010436#define NVRAM_SELFBOOT_HW_SIZE 0x20
10437#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010438
10439static int tg3_test_nvram(struct tg3 *tp)
10440{
Al Virob9fc7dc2007-12-17 22:59:57 -080010441 u32 csum, magic;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010442 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010443 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010444
Joe Perches63c3a662011-04-26 08:12:10 +000010445 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010446 return 0;
10447
Matt Carlsone4f34112009-02-25 14:25:00 +000010448 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010449 return -EIO;
10450
Michael Chan1b277772006-03-20 22:27:48 -080010451 if (magic == TG3_EEPROM_MAGIC)
10452 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010453 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010454 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10455 TG3_EEPROM_SB_FORMAT_1) {
10456 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10457 case TG3_EEPROM_SB_REVISION_0:
10458 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10459 break;
10460 case TG3_EEPROM_SB_REVISION_2:
10461 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10462 break;
10463 case TG3_EEPROM_SB_REVISION_3:
10464 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10465 break;
10466 default:
10467 return 0;
10468 }
10469 } else
Michael Chan1b277772006-03-20 22:27:48 -080010470 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010471 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10472 size = NVRAM_SELFBOOT_HW_SIZE;
10473 else
Michael Chan1b277772006-03-20 22:27:48 -080010474 return -EIO;
10475
10476 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010477 if (buf == NULL)
10478 return -ENOMEM;
10479
Michael Chan1b277772006-03-20 22:27:48 -080010480 err = -EIO;
10481 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010482 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10483 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010484 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010485 }
Michael Chan1b277772006-03-20 22:27:48 -080010486 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010487 goto out;
10488
Michael Chan1b277772006-03-20 22:27:48 -080010489 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010490 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010491 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010492 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010493 u8 *buf8 = (u8 *) buf, csum8 = 0;
10494
Al Virob9fc7dc2007-12-17 22:59:57 -080010495 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010496 TG3_EEPROM_SB_REVISION_2) {
10497 /* For rev 2, the csum doesn't include the MBA. */
10498 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10499 csum8 += buf8[i];
10500 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10501 csum8 += buf8[i];
10502 } else {
10503 for (i = 0; i < size; i++)
10504 csum8 += buf8[i];
10505 }
Michael Chan1b277772006-03-20 22:27:48 -080010506
Adrian Bunkad96b482006-04-05 22:21:04 -070010507 if (csum8 == 0) {
10508 err = 0;
10509 goto out;
10510 }
10511
10512 err = -EIO;
10513 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010514 }
Michael Chan566f86a2005-05-29 14:56:58 -070010515
Al Virob9fc7dc2007-12-17 22:59:57 -080010516 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010517 TG3_EEPROM_MAGIC_HW) {
10518 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010519 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010520 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010521
10522 /* Separate the parity bits and the data bytes. */
10523 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10524 if ((i == 0) || (i == 8)) {
10525 int l;
10526 u8 msk;
10527
10528 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10529 parity[k++] = buf8[i] & msk;
10530 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000010531 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010532 int l;
10533 u8 msk;
10534
10535 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10536 parity[k++] = buf8[i] & msk;
10537 i++;
10538
10539 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10540 parity[k++] = buf8[i] & msk;
10541 i++;
10542 }
10543 data[j++] = buf8[i];
10544 }
10545
10546 err = -EIO;
10547 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10548 u8 hw8 = hweight8(data[i]);
10549
10550 if ((hw8 & 0x1) && parity[i])
10551 goto out;
10552 else if (!(hw8 & 0x1) && !parity[i])
10553 goto out;
10554 }
10555 err = 0;
10556 goto out;
10557 }
10558
Matt Carlson01c3a392011-03-09 16:58:20 +000010559 err = -EIO;
10560
Michael Chan566f86a2005-05-29 14:56:58 -070010561 /* Bootstrap checksum at offset 0x10 */
10562 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000010563 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010564 goto out;
10565
10566 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10567 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000010568 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000010569 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010570
Matt Carlsonc3e94502011-04-13 11:05:08 +000010571 kfree(buf);
10572
10573 buf = tg3_vpd_readblock(tp);
10574 if (!buf)
10575 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000010576
10577 i = pci_vpd_find_tag((u8 *)buf, 0, TG3_NVM_VPD_LEN,
10578 PCI_VPD_LRDT_RO_DATA);
10579 if (i > 0) {
10580 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
10581 if (j < 0)
10582 goto out;
10583
10584 if (i + PCI_VPD_LRDT_TAG_SIZE + j > TG3_NVM_VPD_LEN)
10585 goto out;
10586
10587 i += PCI_VPD_LRDT_TAG_SIZE;
10588 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
10589 PCI_VPD_RO_KEYWORD_CHKSUM);
10590 if (j > 0) {
10591 u8 csum8 = 0;
10592
10593 j += PCI_VPD_INFO_FLD_HDR_SIZE;
10594
10595 for (i = 0; i <= j; i++)
10596 csum8 += ((u8 *)buf)[i];
10597
10598 if (csum8)
10599 goto out;
10600 }
10601 }
10602
Michael Chan566f86a2005-05-29 14:56:58 -070010603 err = 0;
10604
10605out:
10606 kfree(buf);
10607 return err;
10608}
10609
Michael Chanca430072005-05-29 14:57:23 -070010610#define TG3_SERDES_TIMEOUT_SEC 2
10611#define TG3_COPPER_TIMEOUT_SEC 6
10612
10613static int tg3_test_link(struct tg3 *tp)
10614{
10615 int i, max;
10616
10617 if (!netif_running(tp->dev))
10618 return -ENODEV;
10619
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010620 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070010621 max = TG3_SERDES_TIMEOUT_SEC;
10622 else
10623 max = TG3_COPPER_TIMEOUT_SEC;
10624
10625 for (i = 0; i < max; i++) {
10626 if (netif_carrier_ok(tp->dev))
10627 return 0;
10628
10629 if (msleep_interruptible(1000))
10630 break;
10631 }
10632
10633 return -EIO;
10634}
10635
Michael Chana71116d2005-05-29 14:58:11 -070010636/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080010637static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070010638{
Michael Chanb16250e2006-09-27 16:10:14 -070010639 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070010640 u32 offset, read_mask, write_mask, val, save_val, read_val;
10641 static struct {
10642 u16 offset;
10643 u16 flags;
10644#define TG3_FL_5705 0x1
10645#define TG3_FL_NOT_5705 0x2
10646#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070010647#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070010648 u32 read_mask;
10649 u32 write_mask;
10650 } reg_tbl[] = {
10651 /* MAC Control Registers */
10652 { MAC_MODE, TG3_FL_NOT_5705,
10653 0x00000000, 0x00ef6f8c },
10654 { MAC_MODE, TG3_FL_5705,
10655 0x00000000, 0x01ef6b8c },
10656 { MAC_STATUS, TG3_FL_NOT_5705,
10657 0x03800107, 0x00000000 },
10658 { MAC_STATUS, TG3_FL_5705,
10659 0x03800100, 0x00000000 },
10660 { MAC_ADDR_0_HIGH, 0x0000,
10661 0x00000000, 0x0000ffff },
10662 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010663 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070010664 { MAC_RX_MTU_SIZE, 0x0000,
10665 0x00000000, 0x0000ffff },
10666 { MAC_TX_MODE, 0x0000,
10667 0x00000000, 0x00000070 },
10668 { MAC_TX_LENGTHS, 0x0000,
10669 0x00000000, 0x00003fff },
10670 { MAC_RX_MODE, TG3_FL_NOT_5705,
10671 0x00000000, 0x000007fc },
10672 { MAC_RX_MODE, TG3_FL_5705,
10673 0x00000000, 0x000007dc },
10674 { MAC_HASH_REG_0, 0x0000,
10675 0x00000000, 0xffffffff },
10676 { MAC_HASH_REG_1, 0x0000,
10677 0x00000000, 0xffffffff },
10678 { MAC_HASH_REG_2, 0x0000,
10679 0x00000000, 0xffffffff },
10680 { MAC_HASH_REG_3, 0x0000,
10681 0x00000000, 0xffffffff },
10682
10683 /* Receive Data and Receive BD Initiator Control Registers. */
10684 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
10685 0x00000000, 0xffffffff },
10686 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
10687 0x00000000, 0xffffffff },
10688 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
10689 0x00000000, 0x00000003 },
10690 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
10691 0x00000000, 0xffffffff },
10692 { RCVDBDI_STD_BD+0, 0x0000,
10693 0x00000000, 0xffffffff },
10694 { RCVDBDI_STD_BD+4, 0x0000,
10695 0x00000000, 0xffffffff },
10696 { RCVDBDI_STD_BD+8, 0x0000,
10697 0x00000000, 0xffff0002 },
10698 { RCVDBDI_STD_BD+0xc, 0x0000,
10699 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010700
Michael Chana71116d2005-05-29 14:58:11 -070010701 /* Receive BD Initiator Control Registers. */
10702 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
10703 0x00000000, 0xffffffff },
10704 { RCVBDI_STD_THRESH, TG3_FL_5705,
10705 0x00000000, 0x000003ff },
10706 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
10707 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010708
Michael Chana71116d2005-05-29 14:58:11 -070010709 /* Host Coalescing Control Registers. */
10710 { HOSTCC_MODE, TG3_FL_NOT_5705,
10711 0x00000000, 0x00000004 },
10712 { HOSTCC_MODE, TG3_FL_5705,
10713 0x00000000, 0x000000f6 },
10714 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
10715 0x00000000, 0xffffffff },
10716 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
10717 0x00000000, 0x000003ff },
10718 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
10719 0x00000000, 0xffffffff },
10720 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
10721 0x00000000, 0x000003ff },
10722 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
10723 0x00000000, 0xffffffff },
10724 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10725 0x00000000, 0x000000ff },
10726 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
10727 0x00000000, 0xffffffff },
10728 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10729 0x00000000, 0x000000ff },
10730 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
10731 0x00000000, 0xffffffff },
10732 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
10733 0x00000000, 0xffffffff },
10734 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10735 0x00000000, 0xffffffff },
10736 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10737 0x00000000, 0x000000ff },
10738 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10739 0x00000000, 0xffffffff },
10740 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10741 0x00000000, 0x000000ff },
10742 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
10743 0x00000000, 0xffffffff },
10744 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
10745 0x00000000, 0xffffffff },
10746 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
10747 0x00000000, 0xffffffff },
10748 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
10749 0x00000000, 0xffffffff },
10750 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
10751 0x00000000, 0xffffffff },
10752 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
10753 0xffffffff, 0x00000000 },
10754 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
10755 0xffffffff, 0x00000000 },
10756
10757 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070010758 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010759 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070010760 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010761 0x00000000, 0x007fffff },
10762 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
10763 0x00000000, 0x0000003f },
10764 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
10765 0x00000000, 0x000001ff },
10766 { BUFMGR_MB_HIGH_WATER, 0x0000,
10767 0x00000000, 0x000001ff },
10768 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
10769 0xffffffff, 0x00000000 },
10770 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
10771 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010772
Michael Chana71116d2005-05-29 14:58:11 -070010773 /* Mailbox Registers */
10774 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
10775 0x00000000, 0x000001ff },
10776 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
10777 0x00000000, 0x000001ff },
10778 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
10779 0x00000000, 0x000007ff },
10780 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
10781 0x00000000, 0x000001ff },
10782
10783 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
10784 };
10785
Michael Chanb16250e2006-09-27 16:10:14 -070010786 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010787 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070010788 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000010789 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070010790 is_5750 = 1;
10791 }
Michael Chana71116d2005-05-29 14:58:11 -070010792
10793 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
10794 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
10795 continue;
10796
10797 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
10798 continue;
10799
Joe Perches63c3a662011-04-26 08:12:10 +000010800 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070010801 (reg_tbl[i].flags & TG3_FL_NOT_5788))
10802 continue;
10803
Michael Chanb16250e2006-09-27 16:10:14 -070010804 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
10805 continue;
10806
Michael Chana71116d2005-05-29 14:58:11 -070010807 offset = (u32) reg_tbl[i].offset;
10808 read_mask = reg_tbl[i].read_mask;
10809 write_mask = reg_tbl[i].write_mask;
10810
10811 /* Save the original register content */
10812 save_val = tr32(offset);
10813
10814 /* Determine the read-only value. */
10815 read_val = save_val & read_mask;
10816
10817 /* Write zero to the register, then make sure the read-only bits
10818 * are not changed and the read/write bits are all zeros.
10819 */
10820 tw32(offset, 0);
10821
10822 val = tr32(offset);
10823
10824 /* Test the read-only and read/write bits. */
10825 if (((val & read_mask) != read_val) || (val & write_mask))
10826 goto out;
10827
10828 /* Write ones to all the bits defined by RdMask and WrMask, then
10829 * make sure the read-only bits are not changed and the
10830 * read/write bits are all ones.
10831 */
10832 tw32(offset, read_mask | write_mask);
10833
10834 val = tr32(offset);
10835
10836 /* Test the read-only bits. */
10837 if ((val & read_mask) != read_val)
10838 goto out;
10839
10840 /* Test the read/write bits. */
10841 if ((val & write_mask) != write_mask)
10842 goto out;
10843
10844 tw32(offset, save_val);
10845 }
10846
10847 return 0;
10848
10849out:
Michael Chan9f88f292006-12-07 00:22:54 -080010850 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000010851 netdev_err(tp->dev,
10852 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070010853 tw32(offset, save_val);
10854 return -EIO;
10855}
10856
Michael Chan7942e1d2005-05-29 14:58:36 -070010857static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
10858{
Arjan van de Venf71e1302006-03-03 21:33:57 -050010859 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070010860 int i;
10861 u32 j;
10862
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020010863 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070010864 for (j = 0; j < len; j += 4) {
10865 u32 val;
10866
10867 tg3_write_mem(tp, offset + j, test_pattern[i]);
10868 tg3_read_mem(tp, offset + j, &val);
10869 if (val != test_pattern[i])
10870 return -EIO;
10871 }
10872 }
10873 return 0;
10874}
10875
10876static int tg3_test_memory(struct tg3 *tp)
10877{
10878 static struct mem_entry {
10879 u32 offset;
10880 u32 len;
10881 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080010882 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070010883 { 0x00002000, 0x1c000},
10884 { 0xffffffff, 0x00000}
10885 }, mem_tbl_5705[] = {
10886 { 0x00000100, 0x0000c},
10887 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070010888 { 0x00004000, 0x00800},
10889 { 0x00006000, 0x01000},
10890 { 0x00008000, 0x02000},
10891 { 0x00010000, 0x0e000},
10892 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080010893 }, mem_tbl_5755[] = {
10894 { 0x00000200, 0x00008},
10895 { 0x00004000, 0x00800},
10896 { 0x00006000, 0x00800},
10897 { 0x00008000, 0x02000},
10898 { 0x00010000, 0x0c000},
10899 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070010900 }, mem_tbl_5906[] = {
10901 { 0x00000200, 0x00008},
10902 { 0x00004000, 0x00400},
10903 { 0x00006000, 0x00400},
10904 { 0x00008000, 0x01000},
10905 { 0x00010000, 0x01000},
10906 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010907 }, mem_tbl_5717[] = {
10908 { 0x00000200, 0x00008},
10909 { 0x00010000, 0x0a000},
10910 { 0x00020000, 0x13c00},
10911 { 0xffffffff, 0x00000}
10912 }, mem_tbl_57765[] = {
10913 { 0x00000200, 0x00008},
10914 { 0x00004000, 0x00800},
10915 { 0x00006000, 0x09800},
10916 { 0x00010000, 0x0a000},
10917 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070010918 };
10919 struct mem_entry *mem_tbl;
10920 int err = 0;
10921 int i;
10922
Joe Perches63c3a662011-04-26 08:12:10 +000010923 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010924 mem_tbl = mem_tbl_5717;
10925 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
10926 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000010927 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080010928 mem_tbl = mem_tbl_5755;
10929 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
10930 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000010931 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080010932 mem_tbl = mem_tbl_5705;
10933 else
Michael Chan7942e1d2005-05-29 14:58:36 -070010934 mem_tbl = mem_tbl_570x;
10935
10936 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000010937 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
10938 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070010939 break;
10940 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010941
Michael Chan7942e1d2005-05-29 14:58:36 -070010942 return err;
10943}
10944
Michael Chan9f40dea2005-09-05 17:53:06 -070010945#define TG3_MAC_LOOPBACK 0
10946#define TG3_PHY_LOOPBACK 1
Matt Carlsonbb158d62011-04-25 12:42:47 +000010947#define TG3_TSO_LOOPBACK 2
10948
10949#define TG3_TSO_MSS 500
10950
10951#define TG3_TSO_IP_HDR_LEN 20
10952#define TG3_TSO_TCP_HDR_LEN 20
10953#define TG3_TSO_TCP_OPT_LEN 12
10954
10955static const u8 tg3_tso_header[] = {
109560x08, 0x00,
109570x45, 0x00, 0x00, 0x00,
109580x00, 0x00, 0x40, 0x00,
109590x40, 0x06, 0x00, 0x00,
109600x0a, 0x00, 0x00, 0x01,
109610x0a, 0x00, 0x00, 0x02,
109620x0d, 0x00, 0xe0, 0x00,
109630x00, 0x00, 0x01, 0x00,
109640x00, 0x00, 0x02, 0x00,
109650x80, 0x10, 0x10, 0x00,
109660x14, 0x09, 0x00, 0x00,
109670x01, 0x01, 0x08, 0x0a,
109680x11, 0x11, 0x11, 0x11,
109690x11, 0x11, 0x11, 0x11,
10970};
Michael Chan9f40dea2005-09-05 17:53:06 -070010971
Matt Carlson4852a862011-04-13 11:05:07 +000010972static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, int loopback_mode)
Michael Chanc76949a2005-05-29 14:58:59 -070010973{
Michael Chan9f40dea2005-09-05 17:53:06 -070010974 u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000010975 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Michael Chanc76949a2005-05-29 14:58:59 -070010976 struct sk_buff *skb, *rx_skb;
10977 u8 *tx_data;
10978 dma_addr_t map;
10979 int num_pkts, tx_len, rx_len, i, err;
10980 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000010981 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000010982 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070010983
Matt Carlsonc8873402010-02-12 14:47:11 +000010984 tnapi = &tp->napi[0];
10985 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010986 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000010987 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000010988 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000010989 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000010990 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010991 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010992 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000010993
Michael Chan9f40dea2005-09-05 17:53:06 -070010994 if (loopback_mode == TG3_MAC_LOOPBACK) {
Michael Chanc94e3942005-09-27 12:12:42 -070010995 /* HW errata - mac loopback fails in some cases on 5780.
10996 * Normal traffic and PHY loopback are not affected by
Matt Carlsonaba49f22011-01-25 15:58:53 +000010997 * errata. Also, the MAC loopback test is deprecated for
10998 * all newer ASIC revisions.
Michael Chanc94e3942005-09-27 12:12:42 -070010999 */
Matt Carlsonaba49f22011-01-25 15:58:53 +000011000 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000011001 tg3_flag(tp, CPMU_PRESENT))
Michael Chanc94e3942005-09-27 12:12:42 -070011002 return 0;
11003
Matt Carlson49692ca2011-01-25 15:58:52 +000011004 mac_mode = tp->mac_mode &
11005 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
11006 mac_mode |= MAC_MODE_PORT_INT_LPBACK;
Joe Perches63c3a662011-04-26 08:12:10 +000011007 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011008 mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011009 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
Michael Chan3f7045c2006-09-27 16:02:29 -070011010 mac_mode |= MAC_MODE_PORT_MODE_MII;
11011 else
11012 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chan9f40dea2005-09-05 17:53:06 -070011013 tw32(MAC_MODE, mac_mode);
Matt Carlsonbb158d62011-04-25 12:42:47 +000011014 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011015 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +000011016 tg3_phy_fet_toggle_apd(tp, false);
Michael Chan5d64ad32006-12-07 00:19:40 -080011017 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
11018 } else
11019 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
Michael Chan3f7045c2006-09-27 16:02:29 -070011020
Matt Carlson9ef8ca92007-07-11 19:48:29 -070011021 tg3_phy_toggle_automdix(tp, 0);
11022
Michael Chan3f7045c2006-09-27 16:02:29 -070011023 tg3_writephy(tp, MII_BMCR, val);
Michael Chanc94e3942005-09-27 12:12:42 -070011024 udelay(40);
Michael Chan5d64ad32006-12-07 00:19:40 -080011025
Matt Carlson49692ca2011-01-25 15:58:52 +000011026 mac_mode = tp->mac_mode &
11027 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011028 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson1061b7c2010-02-12 14:47:12 +000011029 tg3_writephy(tp, MII_TG3_FET_PTEST,
11030 MII_TG3_FET_PTEST_FRC_TX_LINK |
11031 MII_TG3_FET_PTEST_FRC_TX_LOCK);
11032 /* The write needs to be flushed for the AC131 */
11033 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
11034 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
Michael Chan5d64ad32006-12-07 00:19:40 -080011035 mac_mode |= MAC_MODE_PORT_MODE_MII;
11036 } else
11037 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chanb16250e2006-09-27 16:10:14 -070011038
Michael Chanc94e3942005-09-27 12:12:42 -070011039 /* reset to prevent losing 1st rx packet intermittently */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011040 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Michael Chanc94e3942005-09-27 12:12:42 -070011041 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
11042 udelay(10);
11043 tw32_f(MAC_RX_MODE, tp->rx_mode);
11044 }
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011045 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlson79eb6902010-02-17 15:17:03 +000011046 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
11047 if (masked_phy_id == TG3_PHY_ID_BCM5401)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011048 mac_mode &= ~MAC_MODE_LINK_POLARITY;
Matt Carlson79eb6902010-02-17 15:17:03 +000011049 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011050 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chanff18ff02006-03-27 23:17:27 -080011051 tg3_writephy(tp, MII_TG3_EXT_CTRL,
11052 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
11053 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011054 tw32(MAC_MODE, mac_mode);
Matt Carlson49692ca2011-01-25 15:58:52 +000011055
11056 /* Wait for link */
11057 for (i = 0; i < 100; i++) {
11058 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
11059 break;
11060 mdelay(1);
11061 }
Matt Carlson859a588792010-04-05 10:19:28 +000011062 }
Michael Chanc76949a2005-05-29 14:58:59 -070011063
11064 err = -EIO;
11065
Matt Carlson4852a862011-04-13 11:05:07 +000011066 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070011067 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070011068 if (!skb)
11069 return -ENOMEM;
11070
Michael Chanc76949a2005-05-29 14:58:59 -070011071 tx_data = skb_put(skb, tx_len);
11072 memcpy(tx_data, tp->dev->dev_addr, 6);
11073 memset(tx_data + 6, 0x0, 8);
11074
Matt Carlson4852a862011-04-13 11:05:07 +000011075 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070011076
Matt Carlsonbb158d62011-04-25 12:42:47 +000011077 if (loopback_mode == TG3_TSO_LOOPBACK) {
11078 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
11079
11080 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
11081 TG3_TSO_TCP_OPT_LEN;
11082
11083 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
11084 sizeof(tg3_tso_header));
11085 mss = TG3_TSO_MSS;
11086
11087 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
11088 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
11089
11090 /* Set the total length field in the IP header */
11091 iph->tot_len = htons((u16)(mss + hdr_len));
11092
11093 base_flags = (TXD_FLAG_CPU_PRE_DMA |
11094 TXD_FLAG_CPU_POST_DMA);
11095
Joe Perches63c3a662011-04-26 08:12:10 +000011096 if (tg3_flag(tp, HW_TSO_1) ||
11097 tg3_flag(tp, HW_TSO_2) ||
11098 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011099 struct tcphdr *th;
11100 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
11101 th = (struct tcphdr *)&tx_data[val];
11102 th->check = 0;
11103 } else
11104 base_flags |= TXD_FLAG_TCPUDP_CSUM;
11105
Joe Perches63c3a662011-04-26 08:12:10 +000011106 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011107 mss |= (hdr_len & 0xc) << 12;
11108 if (hdr_len & 0x10)
11109 base_flags |= 0x00000010;
11110 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000011111 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011112 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000011113 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlsonbb158d62011-04-25 12:42:47 +000011114 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
11115 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
11116 } else {
11117 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
11118 }
11119
11120 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
11121 } else {
11122 num_pkts = 1;
11123 data_off = ETH_HLEN;
11124 }
11125
11126 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070011127 tx_data[i] = (u8) (i & 0xff);
11128
Alexander Duyckf4188d82009-12-02 16:48:38 +000011129 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
11130 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000011131 dev_kfree_skb(skb);
11132 return -EIO;
11133 }
Michael Chanc76949a2005-05-29 14:58:59 -070011134
11135 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011136 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011137
11138 udelay(10);
11139
Matt Carlson898a56f2009-08-28 14:02:40 +000011140 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011141
Matt Carlsonbb158d62011-04-25 12:42:47 +000011142 tg3_set_txd(tnapi, tnapi->tx_prod, map, tx_len,
11143 base_flags, (mss << 1) | 1);
Michael Chanc76949a2005-05-29 14:58:59 -070011144
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011145 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011146
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011147 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11148 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011149
11150 udelay(10);
11151
Matt Carlson303fc922009-11-02 14:27:34 +000011152 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11153 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011154 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011155 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011156
11157 udelay(10);
11158
Matt Carlson898a56f2009-08-28 14:02:40 +000011159 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11160 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011161 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011162 (rx_idx == (rx_start_idx + num_pkts)))
11163 break;
11164 }
11165
Alexander Duyckf4188d82009-12-02 16:48:38 +000011166 pci_unmap_single(tp->pdev, map, tx_len, PCI_DMA_TODEVICE);
Michael Chanc76949a2005-05-29 14:58:59 -070011167 dev_kfree_skb(skb);
11168
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011169 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011170 goto out;
11171
11172 if (rx_idx != rx_start_idx + num_pkts)
11173 goto out;
11174
Matt Carlsonbb158d62011-04-25 12:42:47 +000011175 val = data_off;
11176 while (rx_idx != rx_start_idx) {
11177 desc = &rnapi->rx_rcb[rx_start_idx++];
11178 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11179 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070011180
Matt Carlsonbb158d62011-04-25 12:42:47 +000011181 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11182 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000011183 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070011184
Matt Carlsonbb158d62011-04-25 12:42:47 +000011185 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
11186 - ETH_FCS_LEN;
11187
11188 if (loopback_mode != TG3_TSO_LOOPBACK) {
11189 if (rx_len != tx_len)
11190 goto out;
11191
11192 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
11193 if (opaque_key != RXD_OPAQUE_RING_STD)
11194 goto out;
11195 } else {
11196 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
11197 goto out;
11198 }
11199 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
11200 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000011201 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011202 goto out;
11203 }
11204
11205 if (opaque_key == RXD_OPAQUE_RING_STD) {
11206 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
11207 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
11208 mapping);
11209 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
11210 rx_skb = tpr->rx_jmb_buffers[desc_idx].skb;
11211 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
11212 mapping);
11213 } else
Matt Carlson4852a862011-04-13 11:05:07 +000011214 goto out;
11215
Matt Carlsonbb158d62011-04-25 12:42:47 +000011216 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
11217 PCI_DMA_FROMDEVICE);
11218
11219 for (i = data_off; i < rx_len; i++, val++) {
11220 if (*(rx_skb->data + i) != (u8) (val & 0xff))
11221 goto out;
11222 }
Matt Carlson4852a862011-04-13 11:05:07 +000011223 }
11224
Michael Chanc76949a2005-05-29 14:58:59 -070011225 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011226
Michael Chanc76949a2005-05-29 14:58:59 -070011227 /* tg3_free_rings will unmap and free the rx_skb */
11228out:
11229 return err;
11230}
11231
Matt Carlson00c266b2011-04-25 12:42:46 +000011232#define TG3_STD_LOOPBACK_FAILED 1
11233#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000011234#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson00c266b2011-04-25 12:42:46 +000011235
11236#define TG3_MAC_LOOPBACK_SHIFT 0
11237#define TG3_PHY_LOOPBACK_SHIFT 4
Matt Carlsonbb158d62011-04-25 12:42:47 +000011238#define TG3_LOOPBACK_FAILED 0x00000077
Michael Chan9f40dea2005-09-05 17:53:06 -070011239
11240static int tg3_test_loopback(struct tg3 *tp)
11241{
11242 int err = 0;
Matt Carlsonab789042011-01-25 15:58:54 +000011243 u32 eee_cap, cpmuctrl = 0;
Michael Chan9f40dea2005-09-05 17:53:06 -070011244
11245 if (!netif_running(tp->dev))
11246 return TG3_LOOPBACK_FAILED;
11247
Matt Carlsonab789042011-01-25 15:58:54 +000011248 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11249 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11250
Michael Chanb9ec6c12006-07-25 16:37:27 -070011251 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011252 if (err) {
11253 err = TG3_LOOPBACK_FAILED;
11254 goto done;
11255 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011256
Joe Perches63c3a662011-04-26 08:12:10 +000011257 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000011258 int i;
11259
11260 /* Reroute all rx packets to the 1st queue */
11261 for (i = MAC_RSS_INDIR_TBL_0;
11262 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
11263 tw32(i, 0x0);
11264 }
11265
Matt Carlson6833c042008-11-21 17:18:59 -080011266 /* Turn off gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011267 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080011268 tg3_phy_toggle_apd(tp, false);
11269
Joe Perches63c3a662011-04-26 08:12:10 +000011270 if (tg3_flag(tp, CPMU_PRESENT)) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070011271 int i;
11272 u32 status;
11273
11274 tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
11275
11276 /* Wait for up to 40 microseconds to acquire lock. */
11277 for (i = 0; i < 4; i++) {
11278 status = tr32(TG3_CPMU_MUTEX_GNT);
11279 if (status == CPMU_MUTEX_GNT_DRIVER)
11280 break;
11281 udelay(10);
11282 }
11283
Matt Carlsonab789042011-01-25 15:58:54 +000011284 if (status != CPMU_MUTEX_GNT_DRIVER) {
11285 err = TG3_LOOPBACK_FAILED;
11286 goto done;
11287 }
Matt Carlson9936bcf2007-10-10 18:03:07 -070011288
Matt Carlsonb2a5c192008-04-03 21:44:44 -070011289 /* Turn off link-based power management. */
Matt Carlsone8750932007-11-12 21:11:51 -080011290 cpmuctrl = tr32(TG3_CPMU_CTRL);
Matt Carlson109115e2008-05-02 16:48:59 -070011291 tw32(TG3_CPMU_CTRL,
11292 cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
11293 CPMU_CTRL_LINK_AWARE_MODE));
Matt Carlson9936bcf2007-10-10 18:03:07 -070011294 }
11295
Matt Carlson4852a862011-04-13 11:05:07 +000011296 if (tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_MAC_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011297 err |= TG3_STD_LOOPBACK_FAILED << TG3_MAC_LOOPBACK_SHIFT;
Matt Carlson9936bcf2007-10-10 18:03:07 -070011298
Joe Perches63c3a662011-04-26 08:12:10 +000011299 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson4852a862011-04-13 11:05:07 +000011300 tg3_run_loopback(tp, 9000 + ETH_HLEN, TG3_MAC_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011301 err |= TG3_JMB_LOOPBACK_FAILED << TG3_MAC_LOOPBACK_SHIFT;
Matt Carlson4852a862011-04-13 11:05:07 +000011302
Joe Perches63c3a662011-04-26 08:12:10 +000011303 if (tg3_flag(tp, CPMU_PRESENT)) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070011304 tw32(TG3_CPMU_CTRL, cpmuctrl);
11305
11306 /* Release the mutex */
11307 tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
11308 }
11309
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011310 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011311 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson4852a862011-04-13 11:05:07 +000011312 if (tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_PHY_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011313 err |= TG3_STD_LOOPBACK_FAILED <<
11314 TG3_PHY_LOOPBACK_SHIFT;
Joe Perches63c3a662011-04-26 08:12:10 +000011315 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonbb158d62011-04-25 12:42:47 +000011316 tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_TSO_LOOPBACK))
11317 err |= TG3_TSO_LOOPBACK_FAILED <<
11318 TG3_PHY_LOOPBACK_SHIFT;
Joe Perches63c3a662011-04-26 08:12:10 +000011319 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson4852a862011-04-13 11:05:07 +000011320 tg3_run_loopback(tp, 9000 + ETH_HLEN, TG3_PHY_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011321 err |= TG3_JMB_LOOPBACK_FAILED <<
11322 TG3_PHY_LOOPBACK_SHIFT;
Michael Chan9f40dea2005-09-05 17:53:06 -070011323 }
11324
Matt Carlson6833c042008-11-21 17:18:59 -080011325 /* Re-enable gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011326 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080011327 tg3_phy_toggle_apd(tp, true);
11328
Matt Carlsonab789042011-01-25 15:58:54 +000011329done:
11330 tp->phy_flags |= eee_cap;
11331
Michael Chan9f40dea2005-09-05 17:53:06 -070011332 return err;
11333}
11334
Michael Chan4cafd3f2005-05-29 14:56:34 -070011335static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11336 u64 *data)
11337{
Michael Chan566f86a2005-05-29 14:56:58 -070011338 struct tg3 *tp = netdev_priv(dev);
11339
Matt Carlson80096062010-08-02 11:26:06 +000011340 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011341 tg3_power_up(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011342
Michael Chan566f86a2005-05-29 14:56:58 -070011343 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
11344
11345 if (tg3_test_nvram(tp) != 0) {
11346 etest->flags |= ETH_TEST_FL_FAILED;
11347 data[0] = 1;
11348 }
Michael Chanca430072005-05-29 14:57:23 -070011349 if (tg3_test_link(tp) != 0) {
11350 etest->flags |= ETH_TEST_FL_FAILED;
11351 data[1] = 1;
11352 }
Michael Chana71116d2005-05-29 14:58:11 -070011353 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011354 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070011355
Michael Chanbbe832c2005-06-24 20:20:04 -070011356 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011357 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011358 tg3_netif_stop(tp);
11359 irq_sync = 1;
11360 }
11361
11362 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070011363
11364 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080011365 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011366 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000011367 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070011368 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080011369 if (!err)
11370 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011371
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011372 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad12006-03-20 22:27:35 -080011373 tg3_phy_reset(tp);
11374
Michael Chana71116d2005-05-29 14:58:11 -070011375 if (tg3_test_registers(tp) != 0) {
11376 etest->flags |= ETH_TEST_FL_FAILED;
11377 data[2] = 1;
11378 }
Michael Chan7942e1d2005-05-29 14:58:36 -070011379 if (tg3_test_memory(tp) != 0) {
11380 etest->flags |= ETH_TEST_FL_FAILED;
11381 data[3] = 1;
11382 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011383 if ((data[4] = tg3_test_loopback(tp)) != 0)
Michael Chanc76949a2005-05-29 14:58:59 -070011384 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070011385
David S. Millerf47c11e2005-06-24 20:18:35 -070011386 tg3_full_unlock(tp);
11387
Michael Chand4bc3922005-05-29 14:59:20 -070011388 if (tg3_test_interrupt(tp) != 0) {
11389 etest->flags |= ETH_TEST_FL_FAILED;
11390 data[5] = 1;
11391 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011392
11393 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070011394
Michael Chana71116d2005-05-29 14:58:11 -070011395 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11396 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011397 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011398 err2 = tg3_restart_hw(tp, 1);
11399 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070011400 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011401 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011402
11403 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011404
11405 if (irq_sync && !err2)
11406 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011407 }
Matt Carlson80096062010-08-02 11:26:06 +000011408 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011409 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011410
Michael Chan4cafd3f2005-05-29 14:56:34 -070011411}
11412
Linus Torvalds1da177e2005-04-16 15:20:36 -070011413static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
11414{
11415 struct mii_ioctl_data *data = if_mii(ifr);
11416 struct tg3 *tp = netdev_priv(dev);
11417 int err;
11418
Joe Perches63c3a662011-04-26 08:12:10 +000011419 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011420 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011421 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011422 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011423 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000011424 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011425 }
11426
Matt Carlson33f401a2010-04-05 10:19:27 +000011427 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011428 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000011429 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011430
11431 /* fallthru */
11432 case SIOCGMIIREG: {
11433 u32 mii_regval;
11434
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011435 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011436 break; /* We have no PHY */
11437
Matt Carlson34eea5a2011-04-20 07:57:38 +000011438 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011439 return -EAGAIN;
11440
David S. Millerf47c11e2005-06-24 20:18:35 -070011441 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011442 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070011443 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011444
11445 data->val_out = mii_regval;
11446
11447 return err;
11448 }
11449
11450 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011451 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011452 break; /* We have no PHY */
11453
Matt Carlson34eea5a2011-04-20 07:57:38 +000011454 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011455 return -EAGAIN;
11456
David S. Millerf47c11e2005-06-24 20:18:35 -070011457 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011458 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070011459 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011460
11461 return err;
11462
11463 default:
11464 /* do nothing */
11465 break;
11466 }
11467 return -EOPNOTSUPP;
11468}
11469
David S. Miller15f98502005-05-18 22:49:26 -070011470static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11471{
11472 struct tg3 *tp = netdev_priv(dev);
11473
11474 memcpy(ec, &tp->coal, sizeof(*ec));
11475 return 0;
11476}
11477
Michael Chand244c892005-07-05 14:42:33 -070011478static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11479{
11480 struct tg3 *tp = netdev_priv(dev);
11481 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11482 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11483
Joe Perches63c3a662011-04-26 08:12:10 +000011484 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070011485 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11486 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11487 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11488 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11489 }
11490
11491 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11492 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11493 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11494 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11495 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11496 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11497 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11498 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11499 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11500 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11501 return -EINVAL;
11502
11503 /* No rx interrupts will be generated if both are zero */
11504 if ((ec->rx_coalesce_usecs == 0) &&
11505 (ec->rx_max_coalesced_frames == 0))
11506 return -EINVAL;
11507
11508 /* No tx interrupts will be generated if both are zero */
11509 if ((ec->tx_coalesce_usecs == 0) &&
11510 (ec->tx_max_coalesced_frames == 0))
11511 return -EINVAL;
11512
11513 /* Only copy relevant parameters, ignore all others. */
11514 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11515 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11516 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11517 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11518 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11519 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11520 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11521 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11522 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11523
11524 if (netif_running(dev)) {
11525 tg3_full_lock(tp, 0);
11526 __tg3_set_coalesce(tp, &tp->coal);
11527 tg3_full_unlock(tp);
11528 }
11529 return 0;
11530}
11531
Jeff Garzik7282d492006-09-13 14:30:00 -040011532static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011533 .get_settings = tg3_get_settings,
11534 .set_settings = tg3_set_settings,
11535 .get_drvinfo = tg3_get_drvinfo,
11536 .get_regs_len = tg3_get_regs_len,
11537 .get_regs = tg3_get_regs,
11538 .get_wol = tg3_get_wol,
11539 .set_wol = tg3_set_wol,
11540 .get_msglevel = tg3_get_msglevel,
11541 .set_msglevel = tg3_set_msglevel,
11542 .nway_reset = tg3_nway_reset,
11543 .get_link = ethtool_op_get_link,
11544 .get_eeprom_len = tg3_get_eeprom_len,
11545 .get_eeprom = tg3_get_eeprom,
11546 .set_eeprom = tg3_set_eeprom,
11547 .get_ringparam = tg3_get_ringparam,
11548 .set_ringparam = tg3_set_ringparam,
11549 .get_pauseparam = tg3_get_pauseparam,
11550 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011551 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011552 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000011553 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011554 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011555 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011556 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011557 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011558};
11559
11560static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11561{
Michael Chan1b277772006-03-20 22:27:48 -080011562 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011563
11564 tp->nvram_size = EEPROM_CHIP_SIZE;
11565
Matt Carlsone4f34112009-02-25 14:25:00 +000011566 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011567 return;
11568
Michael Chanb16250e2006-09-27 16:10:14 -070011569 if ((magic != TG3_EEPROM_MAGIC) &&
11570 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11571 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011572 return;
11573
11574 /*
11575 * Size the chip by reading offsets at increasing powers of two.
11576 * When we encounter our validation signature, we know the addressing
11577 * has wrapped around, and thus have our chip size.
11578 */
Michael Chan1b277772006-03-20 22:27:48 -080011579 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011580
11581 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011582 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011583 return;
11584
Michael Chan18201802006-03-20 22:29:15 -080011585 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011586 break;
11587
11588 cursize <<= 1;
11589 }
11590
11591 tp->nvram_size = cursize;
11592}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011593
Linus Torvalds1da177e2005-04-16 15:20:36 -070011594static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11595{
11596 u32 val;
11597
Joe Perches63c3a662011-04-26 08:12:10 +000011598 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011599 return;
11600
11601 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011602 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011603 tg3_get_eeprom_size(tp);
11604 return;
11605 }
11606
Matt Carlson6d348f22009-02-25 14:25:52 +000011607 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011608 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011609 /* This is confusing. We want to operate on the
11610 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11611 * call will read from NVRAM and byteswap the data
11612 * according to the byteswapping settings for all
11613 * other register accesses. This ensures the data we
11614 * want will always reside in the lower 16-bits.
11615 * However, the data in NVRAM is in LE format, which
11616 * means the data from the NVRAM read will always be
11617 * opposite the endianness of the CPU. The 16-bit
11618 * byteswap then brings the data to CPU endianness.
11619 */
11620 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011621 return;
11622 }
11623 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070011624 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011625}
11626
11627static void __devinit tg3_get_nvram_info(struct tg3 *tp)
11628{
11629 u32 nvcfg1;
11630
11631 nvcfg1 = tr32(NVRAM_CFG1);
11632 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000011633 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011634 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011635 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11636 tw32(NVRAM_CFG1, nvcfg1);
11637 }
11638
Michael Chan4c987482005-09-05 17:52:38 -070011639 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) ||
Joe Perches63c3a662011-04-26 08:12:10 +000011640 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011641 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011642 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
11643 tp->nvram_jedecnum = JEDEC_ATMEL;
11644 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011645 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011646 break;
11647 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
11648 tp->nvram_jedecnum = JEDEC_ATMEL;
11649 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
11650 break;
11651 case FLASH_VENDOR_ATMEL_EEPROM:
11652 tp->nvram_jedecnum = JEDEC_ATMEL;
11653 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011654 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011655 break;
11656 case FLASH_VENDOR_ST:
11657 tp->nvram_jedecnum = JEDEC_ST;
11658 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011659 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011660 break;
11661 case FLASH_VENDOR_SAIFUN:
11662 tp->nvram_jedecnum = JEDEC_SAIFUN;
11663 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
11664 break;
11665 case FLASH_VENDOR_SST_SMALL:
11666 case FLASH_VENDOR_SST_LARGE:
11667 tp->nvram_jedecnum = JEDEC_SST;
11668 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
11669 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011670 }
Matt Carlson8590a602009-08-28 12:29:16 +000011671 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011672 tp->nvram_jedecnum = JEDEC_ATMEL;
11673 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011674 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011675 }
11676}
11677
Matt Carlsona1b950d2009-09-01 13:20:17 +000011678static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
11679{
11680 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
11681 case FLASH_5752PAGE_SIZE_256:
11682 tp->nvram_pagesize = 256;
11683 break;
11684 case FLASH_5752PAGE_SIZE_512:
11685 tp->nvram_pagesize = 512;
11686 break;
11687 case FLASH_5752PAGE_SIZE_1K:
11688 tp->nvram_pagesize = 1024;
11689 break;
11690 case FLASH_5752PAGE_SIZE_2K:
11691 tp->nvram_pagesize = 2048;
11692 break;
11693 case FLASH_5752PAGE_SIZE_4K:
11694 tp->nvram_pagesize = 4096;
11695 break;
11696 case FLASH_5752PAGE_SIZE_264:
11697 tp->nvram_pagesize = 264;
11698 break;
11699 case FLASH_5752PAGE_SIZE_528:
11700 tp->nvram_pagesize = 528;
11701 break;
11702 }
11703}
11704
Michael Chan361b4ac2005-04-21 17:11:21 -070011705static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
11706{
11707 u32 nvcfg1;
11708
11709 nvcfg1 = tr32(NVRAM_CFG1);
11710
Michael Chane6af3012005-04-21 17:12:05 -070011711 /* NVRAM protection for TPM */
11712 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000011713 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070011714
Michael Chan361b4ac2005-04-21 17:11:21 -070011715 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011716 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
11717 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
11718 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011719 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011720 break;
11721 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11722 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011723 tg3_flag_set(tp, NVRAM_BUFFERED);
11724 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011725 break;
11726 case FLASH_5752VENDOR_ST_M45PE10:
11727 case FLASH_5752VENDOR_ST_M45PE20:
11728 case FLASH_5752VENDOR_ST_M45PE40:
11729 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011730 tg3_flag_set(tp, NVRAM_BUFFERED);
11731 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011732 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070011733 }
11734
Joe Perches63c3a662011-04-26 08:12:10 +000011735 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000011736 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000011737 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070011738 /* For eeprom, set pagesize to maximum eeprom size */
11739 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11740
11741 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11742 tw32(NVRAM_CFG1, nvcfg1);
11743 }
11744}
11745
Michael Chand3c7b882006-03-23 01:28:25 -080011746static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
11747{
Matt Carlson989a9d22007-05-05 11:51:05 -070011748 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080011749
11750 nvcfg1 = tr32(NVRAM_CFG1);
11751
11752 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070011753 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011754 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070011755 protect = 1;
11756 }
Michael Chand3c7b882006-03-23 01:28:25 -080011757
Matt Carlson989a9d22007-05-05 11:51:05 -070011758 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11759 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011760 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11761 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11762 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11763 case FLASH_5755VENDOR_ATMEL_FLASH_5:
11764 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011765 tg3_flag_set(tp, NVRAM_BUFFERED);
11766 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011767 tp->nvram_pagesize = 264;
11768 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
11769 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
11770 tp->nvram_size = (protect ? 0x3e200 :
11771 TG3_NVRAM_SIZE_512KB);
11772 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
11773 tp->nvram_size = (protect ? 0x1f200 :
11774 TG3_NVRAM_SIZE_256KB);
11775 else
11776 tp->nvram_size = (protect ? 0x1f200 :
11777 TG3_NVRAM_SIZE_128KB);
11778 break;
11779 case FLASH_5752VENDOR_ST_M45PE10:
11780 case FLASH_5752VENDOR_ST_M45PE20:
11781 case FLASH_5752VENDOR_ST_M45PE40:
11782 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011783 tg3_flag_set(tp, NVRAM_BUFFERED);
11784 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011785 tp->nvram_pagesize = 256;
11786 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
11787 tp->nvram_size = (protect ?
11788 TG3_NVRAM_SIZE_64KB :
11789 TG3_NVRAM_SIZE_128KB);
11790 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
11791 tp->nvram_size = (protect ?
11792 TG3_NVRAM_SIZE_64KB :
11793 TG3_NVRAM_SIZE_256KB);
11794 else
11795 tp->nvram_size = (protect ?
11796 TG3_NVRAM_SIZE_128KB :
11797 TG3_NVRAM_SIZE_512KB);
11798 break;
Michael Chand3c7b882006-03-23 01:28:25 -080011799 }
11800}
11801
Michael Chan1b277772006-03-20 22:27:48 -080011802static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
11803{
11804 u32 nvcfg1;
11805
11806 nvcfg1 = tr32(NVRAM_CFG1);
11807
11808 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011809 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
11810 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11811 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
11812 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11813 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011814 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011815 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080011816
Matt Carlson8590a602009-08-28 12:29:16 +000011817 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11818 tw32(NVRAM_CFG1, nvcfg1);
11819 break;
11820 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11821 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11822 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11823 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11824 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011825 tg3_flag_set(tp, NVRAM_BUFFERED);
11826 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011827 tp->nvram_pagesize = 264;
11828 break;
11829 case FLASH_5752VENDOR_ST_M45PE10:
11830 case FLASH_5752VENDOR_ST_M45PE20:
11831 case FLASH_5752VENDOR_ST_M45PE40:
11832 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011833 tg3_flag_set(tp, NVRAM_BUFFERED);
11834 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011835 tp->nvram_pagesize = 256;
11836 break;
Michael Chan1b277772006-03-20 22:27:48 -080011837 }
11838}
11839
Matt Carlson6b91fa02007-10-10 18:01:09 -070011840static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
11841{
11842 u32 nvcfg1, protect = 0;
11843
11844 nvcfg1 = tr32(NVRAM_CFG1);
11845
11846 /* NVRAM protection for TPM */
11847 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011848 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070011849 protect = 1;
11850 }
11851
11852 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11853 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011854 case FLASH_5761VENDOR_ATMEL_ADB021D:
11855 case FLASH_5761VENDOR_ATMEL_ADB041D:
11856 case FLASH_5761VENDOR_ATMEL_ADB081D:
11857 case FLASH_5761VENDOR_ATMEL_ADB161D:
11858 case FLASH_5761VENDOR_ATMEL_MDB021D:
11859 case FLASH_5761VENDOR_ATMEL_MDB041D:
11860 case FLASH_5761VENDOR_ATMEL_MDB081D:
11861 case FLASH_5761VENDOR_ATMEL_MDB161D:
11862 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011863 tg3_flag_set(tp, NVRAM_BUFFERED);
11864 tg3_flag_set(tp, FLASH);
11865 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000011866 tp->nvram_pagesize = 256;
11867 break;
11868 case FLASH_5761VENDOR_ST_A_M45PE20:
11869 case FLASH_5761VENDOR_ST_A_M45PE40:
11870 case FLASH_5761VENDOR_ST_A_M45PE80:
11871 case FLASH_5761VENDOR_ST_A_M45PE16:
11872 case FLASH_5761VENDOR_ST_M_M45PE20:
11873 case FLASH_5761VENDOR_ST_M_M45PE40:
11874 case FLASH_5761VENDOR_ST_M_M45PE80:
11875 case FLASH_5761VENDOR_ST_M_M45PE16:
11876 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011877 tg3_flag_set(tp, NVRAM_BUFFERED);
11878 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011879 tp->nvram_pagesize = 256;
11880 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011881 }
11882
11883 if (protect) {
11884 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
11885 } else {
11886 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011887 case FLASH_5761VENDOR_ATMEL_ADB161D:
11888 case FLASH_5761VENDOR_ATMEL_MDB161D:
11889 case FLASH_5761VENDOR_ST_A_M45PE16:
11890 case FLASH_5761VENDOR_ST_M_M45PE16:
11891 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
11892 break;
11893 case FLASH_5761VENDOR_ATMEL_ADB081D:
11894 case FLASH_5761VENDOR_ATMEL_MDB081D:
11895 case FLASH_5761VENDOR_ST_A_M45PE80:
11896 case FLASH_5761VENDOR_ST_M_M45PE80:
11897 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
11898 break;
11899 case FLASH_5761VENDOR_ATMEL_ADB041D:
11900 case FLASH_5761VENDOR_ATMEL_MDB041D:
11901 case FLASH_5761VENDOR_ST_A_M45PE40:
11902 case FLASH_5761VENDOR_ST_M_M45PE40:
11903 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11904 break;
11905 case FLASH_5761VENDOR_ATMEL_ADB021D:
11906 case FLASH_5761VENDOR_ATMEL_MDB021D:
11907 case FLASH_5761VENDOR_ST_A_M45PE20:
11908 case FLASH_5761VENDOR_ST_M_M45PE20:
11909 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11910 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011911 }
11912 }
11913}
11914
Michael Chanb5d37722006-09-27 16:06:21 -070011915static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
11916{
11917 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011918 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070011919 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11920}
11921
Matt Carlson321d32a2008-11-21 17:22:19 -080011922static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
11923{
11924 u32 nvcfg1;
11925
11926 nvcfg1 = tr32(NVRAM_CFG1);
11927
11928 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11929 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11930 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11931 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011932 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080011933 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11934
11935 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11936 tw32(NVRAM_CFG1, nvcfg1);
11937 return;
11938 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11939 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11940 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11941 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11942 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11943 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11944 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11945 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011946 tg3_flag_set(tp, NVRAM_BUFFERED);
11947 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080011948
11949 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11950 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11951 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11952 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11953 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11954 break;
11955 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11956 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11957 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11958 break;
11959 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11960 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11961 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11962 break;
11963 }
11964 break;
11965 case FLASH_5752VENDOR_ST_M45PE10:
11966 case FLASH_5752VENDOR_ST_M45PE20:
11967 case FLASH_5752VENDOR_ST_M45PE40:
11968 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011969 tg3_flag_set(tp, NVRAM_BUFFERED);
11970 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080011971
11972 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11973 case FLASH_5752VENDOR_ST_M45PE10:
11974 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11975 break;
11976 case FLASH_5752VENDOR_ST_M45PE20:
11977 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11978 break;
11979 case FLASH_5752VENDOR_ST_M45PE40:
11980 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11981 break;
11982 }
11983 break;
11984 default:
Joe Perches63c3a662011-04-26 08:12:10 +000011985 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080011986 return;
11987 }
11988
Matt Carlsona1b950d2009-09-01 13:20:17 +000011989 tg3_nvram_get_pagesize(tp, nvcfg1);
11990 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000011991 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000011992}
11993
11994
11995static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
11996{
11997 u32 nvcfg1;
11998
11999 nvcfg1 = tr32(NVRAM_CFG1);
12000
12001 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12002 case FLASH_5717VENDOR_ATMEL_EEPROM:
12003 case FLASH_5717VENDOR_MICRO_EEPROM:
12004 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012005 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012006 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12007
12008 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12009 tw32(NVRAM_CFG1, nvcfg1);
12010 return;
12011 case FLASH_5717VENDOR_ATMEL_MDB011D:
12012 case FLASH_5717VENDOR_ATMEL_ADB011B:
12013 case FLASH_5717VENDOR_ATMEL_ADB011D:
12014 case FLASH_5717VENDOR_ATMEL_MDB021D:
12015 case FLASH_5717VENDOR_ATMEL_ADB021B:
12016 case FLASH_5717VENDOR_ATMEL_ADB021D:
12017 case FLASH_5717VENDOR_ATMEL_45USPT:
12018 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012019 tg3_flag_set(tp, NVRAM_BUFFERED);
12020 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012021
12022 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12023 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012024 /* Detect size with tg3_nvram_get_size() */
12025 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012026 case FLASH_5717VENDOR_ATMEL_ADB021B:
12027 case FLASH_5717VENDOR_ATMEL_ADB021D:
12028 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12029 break;
12030 default:
12031 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12032 break;
12033 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012034 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012035 case FLASH_5717VENDOR_ST_M_M25PE10:
12036 case FLASH_5717VENDOR_ST_A_M25PE10:
12037 case FLASH_5717VENDOR_ST_M_M45PE10:
12038 case FLASH_5717VENDOR_ST_A_M45PE10:
12039 case FLASH_5717VENDOR_ST_M_M25PE20:
12040 case FLASH_5717VENDOR_ST_A_M25PE20:
12041 case FLASH_5717VENDOR_ST_M_M45PE20:
12042 case FLASH_5717VENDOR_ST_A_M45PE20:
12043 case FLASH_5717VENDOR_ST_25USPT:
12044 case FLASH_5717VENDOR_ST_45USPT:
12045 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012046 tg3_flag_set(tp, NVRAM_BUFFERED);
12047 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012048
12049 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12050 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012051 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012052 /* Detect size with tg3_nvram_get_size() */
12053 break;
12054 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012055 case FLASH_5717VENDOR_ST_A_M45PE20:
12056 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12057 break;
12058 default:
12059 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12060 break;
12061 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012062 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012063 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012064 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012065 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080012066 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000012067
12068 tg3_nvram_get_pagesize(tp, nvcfg1);
12069 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012070 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080012071}
12072
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012073static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
12074{
12075 u32 nvcfg1, nvmpinstrp;
12076
12077 nvcfg1 = tr32(NVRAM_CFG1);
12078 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
12079
12080 switch (nvmpinstrp) {
12081 case FLASH_5720_EEPROM_HD:
12082 case FLASH_5720_EEPROM_LD:
12083 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012084 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012085
12086 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12087 tw32(NVRAM_CFG1, nvcfg1);
12088 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
12089 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12090 else
12091 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
12092 return;
12093 case FLASH_5720VENDOR_M_ATMEL_DB011D:
12094 case FLASH_5720VENDOR_A_ATMEL_DB011B:
12095 case FLASH_5720VENDOR_A_ATMEL_DB011D:
12096 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12097 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12098 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12099 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12100 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12101 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12102 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12103 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12104 case FLASH_5720VENDOR_ATMEL_45USPT:
12105 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012106 tg3_flag_set(tp, NVRAM_BUFFERED);
12107 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012108
12109 switch (nvmpinstrp) {
12110 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12111 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12112 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12113 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12114 break;
12115 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12116 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12117 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12118 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12119 break;
12120 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12121 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12122 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12123 break;
12124 default:
12125 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12126 break;
12127 }
12128 break;
12129 case FLASH_5720VENDOR_M_ST_M25PE10:
12130 case FLASH_5720VENDOR_M_ST_M45PE10:
12131 case FLASH_5720VENDOR_A_ST_M25PE10:
12132 case FLASH_5720VENDOR_A_ST_M45PE10:
12133 case FLASH_5720VENDOR_M_ST_M25PE20:
12134 case FLASH_5720VENDOR_M_ST_M45PE20:
12135 case FLASH_5720VENDOR_A_ST_M25PE20:
12136 case FLASH_5720VENDOR_A_ST_M45PE20:
12137 case FLASH_5720VENDOR_M_ST_M25PE40:
12138 case FLASH_5720VENDOR_M_ST_M45PE40:
12139 case FLASH_5720VENDOR_A_ST_M25PE40:
12140 case FLASH_5720VENDOR_A_ST_M45PE40:
12141 case FLASH_5720VENDOR_M_ST_M25PE80:
12142 case FLASH_5720VENDOR_M_ST_M45PE80:
12143 case FLASH_5720VENDOR_A_ST_M25PE80:
12144 case FLASH_5720VENDOR_A_ST_M45PE80:
12145 case FLASH_5720VENDOR_ST_25USPT:
12146 case FLASH_5720VENDOR_ST_45USPT:
12147 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012148 tg3_flag_set(tp, NVRAM_BUFFERED);
12149 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012150
12151 switch (nvmpinstrp) {
12152 case FLASH_5720VENDOR_M_ST_M25PE20:
12153 case FLASH_5720VENDOR_M_ST_M45PE20:
12154 case FLASH_5720VENDOR_A_ST_M25PE20:
12155 case FLASH_5720VENDOR_A_ST_M45PE20:
12156 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12157 break;
12158 case FLASH_5720VENDOR_M_ST_M25PE40:
12159 case FLASH_5720VENDOR_M_ST_M45PE40:
12160 case FLASH_5720VENDOR_A_ST_M25PE40:
12161 case FLASH_5720VENDOR_A_ST_M45PE40:
12162 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12163 break;
12164 case FLASH_5720VENDOR_M_ST_M25PE80:
12165 case FLASH_5720VENDOR_M_ST_M45PE80:
12166 case FLASH_5720VENDOR_A_ST_M25PE80:
12167 case FLASH_5720VENDOR_A_ST_M45PE80:
12168 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12169 break;
12170 default:
12171 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12172 break;
12173 }
12174 break;
12175 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012176 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012177 return;
12178 }
12179
12180 tg3_nvram_get_pagesize(tp, nvcfg1);
12181 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012182 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012183}
12184
Linus Torvalds1da177e2005-04-16 15:20:36 -070012185/* Chips other than 5700/5701 use the NVRAM for fetching info. */
12186static void __devinit tg3_nvram_init(struct tg3 *tp)
12187{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012188 tw32_f(GRC_EEPROM_ADDR,
12189 (EEPROM_ADDR_FSM_RESET |
12190 (EEPROM_DEFAULT_CLOCK_PERIOD <<
12191 EEPROM_ADDR_CLKPERD_SHIFT)));
12192
Michael Chan9d57f012006-12-07 00:23:25 -080012193 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012194
12195 /* Enable seeprom accesses. */
12196 tw32_f(GRC_LOCAL_CTRL,
12197 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
12198 udelay(100);
12199
12200 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12201 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000012202 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012203
Michael Chanec41c7d2006-01-17 02:40:55 -080012204 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000012205 netdev_warn(tp->dev,
12206 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000012207 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080012208 return;
12209 }
Michael Chane6af3012005-04-21 17:12:05 -070012210 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012211
Matt Carlson989a9d22007-05-05 11:51:05 -070012212 tp->nvram_size = 0;
12213
Michael Chan361b4ac2005-04-21 17:11:21 -070012214 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12215 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080012216 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
12217 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070012218 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012219 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12220 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080012221 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012222 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
12223 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070012224 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12225 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000012226 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
12227 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080012228 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012229 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12230 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000012231 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012232 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
12233 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070012234 else
12235 tg3_get_nvram_info(tp);
12236
Matt Carlson989a9d22007-05-05 11:51:05 -070012237 if (tp->nvram_size == 0)
12238 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012239
Michael Chane6af3012005-04-21 17:12:05 -070012240 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080012241 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012242
12243 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000012244 tg3_flag_clear(tp, NVRAM);
12245 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012246
12247 tg3_get_eeprom_size(tp);
12248 }
12249}
12250
Linus Torvalds1da177e2005-04-16 15:20:36 -070012251static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
12252 u32 offset, u32 len, u8 *buf)
12253{
12254 int i, j, rc = 0;
12255 u32 val;
12256
12257 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012258 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012259 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012260
12261 addr = offset + i;
12262
12263 memcpy(&data, buf + i, 4);
12264
Matt Carlson62cedd12009-04-20 14:52:29 -070012265 /*
12266 * The SEEPROM interface expects the data to always be opposite
12267 * the native endian format. We accomplish this by reversing
12268 * all the operations that would have been performed on the
12269 * data from a call to tg3_nvram_read_be32().
12270 */
12271 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012272
12273 val = tr32(GRC_EEPROM_ADDR);
12274 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
12275
12276 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
12277 EEPROM_ADDR_READ);
12278 tw32(GRC_EEPROM_ADDR, val |
12279 (0 << EEPROM_ADDR_DEVID_SHIFT) |
12280 (addr & EEPROM_ADDR_ADDR_MASK) |
12281 EEPROM_ADDR_START |
12282 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012283
Michael Chan9d57f012006-12-07 00:23:25 -080012284 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012285 val = tr32(GRC_EEPROM_ADDR);
12286
12287 if (val & EEPROM_ADDR_COMPLETE)
12288 break;
Michael Chan9d57f012006-12-07 00:23:25 -080012289 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012290 }
12291 if (!(val & EEPROM_ADDR_COMPLETE)) {
12292 rc = -EBUSY;
12293 break;
12294 }
12295 }
12296
12297 return rc;
12298}
12299
12300/* offset and length are dword aligned */
12301static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
12302 u8 *buf)
12303{
12304 int ret = 0;
12305 u32 pagesize = tp->nvram_pagesize;
12306 u32 pagemask = pagesize - 1;
12307 u32 nvram_cmd;
12308 u8 *tmp;
12309
12310 tmp = kmalloc(pagesize, GFP_KERNEL);
12311 if (tmp == NULL)
12312 return -ENOMEM;
12313
12314 while (len) {
12315 int j;
Michael Chane6af3012005-04-21 17:12:05 -070012316 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012317
12318 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012319
Linus Torvalds1da177e2005-04-16 15:20:36 -070012320 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012321 ret = tg3_nvram_read_be32(tp, phy_addr + j,
12322 (__be32 *) (tmp + j));
12323 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012324 break;
12325 }
12326 if (ret)
12327 break;
12328
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012329 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012330 size = pagesize;
12331 if (len < size)
12332 size = len;
12333
12334 len -= size;
12335
12336 memcpy(tmp + page_off, buf, size);
12337
12338 offset = offset + (pagesize - page_off);
12339
Michael Chane6af3012005-04-21 17:12:05 -070012340 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012341
12342 /*
12343 * Before we can erase the flash page, we need
12344 * to issue a special "write enable" command.
12345 */
12346 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12347
12348 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12349 break;
12350
12351 /* Erase the target page */
12352 tw32(NVRAM_ADDR, phy_addr);
12353
12354 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
12355 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
12356
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012357 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012358 break;
12359
12360 /* Issue another write enable to start the write. */
12361 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12362
12363 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12364 break;
12365
12366 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012367 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012368
Al Virob9fc7dc2007-12-17 22:59:57 -080012369 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000012370
Al Virob9fc7dc2007-12-17 22:59:57 -080012371 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012372
12373 tw32(NVRAM_ADDR, phy_addr + j);
12374
12375 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
12376 NVRAM_CMD_WR;
12377
12378 if (j == 0)
12379 nvram_cmd |= NVRAM_CMD_FIRST;
12380 else if (j == (pagesize - 4))
12381 nvram_cmd |= NVRAM_CMD_LAST;
12382
12383 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12384 break;
12385 }
12386 if (ret)
12387 break;
12388 }
12389
12390 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12391 tg3_nvram_exec_cmd(tp, nvram_cmd);
12392
12393 kfree(tmp);
12394
12395 return ret;
12396}
12397
12398/* offset and length are dword aligned */
12399static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
12400 u8 *buf)
12401{
12402 int i, ret = 0;
12403
12404 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012405 u32 page_off, phy_addr, nvram_cmd;
12406 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012407
12408 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080012409 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012410
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012411 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012412
Michael Chan18201802006-03-20 22:29:15 -080012413 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012414
12415 tw32(NVRAM_ADDR, phy_addr);
12416
12417 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
12418
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012419 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012420 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070012421 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012422 nvram_cmd |= NVRAM_CMD_LAST;
12423
12424 if (i == (len - 4))
12425 nvram_cmd |= NVRAM_CMD_LAST;
12426
Matt Carlson321d32a2008-11-21 17:22:19 -080012427 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
Joe Perches63c3a662011-04-26 08:12:10 +000012428 !tg3_flag(tp, 5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070012429 (tp->nvram_jedecnum == JEDEC_ST) &&
12430 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012431
12432 if ((ret = tg3_nvram_exec_cmd(tp,
12433 NVRAM_CMD_WREN | NVRAM_CMD_GO |
12434 NVRAM_CMD_DONE)))
12435
12436 break;
12437 }
Joe Perches63c3a662011-04-26 08:12:10 +000012438 if (!tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012439 /* We always do complete word writes to eeprom. */
12440 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
12441 }
12442
12443 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12444 break;
12445 }
12446 return ret;
12447}
12448
12449/* offset and length are dword aligned */
12450static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
12451{
12452 int ret;
12453
Joe Perches63c3a662011-04-26 08:12:10 +000012454 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012455 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
12456 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012457 udelay(40);
12458 }
12459
Joe Perches63c3a662011-04-26 08:12:10 +000012460 if (!tg3_flag(tp, NVRAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012461 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a588792010-04-05 10:19:28 +000012462 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012463 u32 grc_mode;
12464
Michael Chanec41c7d2006-01-17 02:40:55 -080012465 ret = tg3_nvram_lock(tp);
12466 if (ret)
12467 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012468
Michael Chane6af3012005-04-21 17:12:05 -070012469 tg3_enable_nvram_access(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000012470 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012471 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012472
12473 grc_mode = tr32(GRC_MODE);
12474 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
12475
Joe Perches63c3a662011-04-26 08:12:10 +000012476 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012477 ret = tg3_nvram_write_block_buffered(tp, offset, len,
12478 buf);
Matt Carlson859a588792010-04-05 10:19:28 +000012479 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012480 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
12481 buf);
12482 }
12483
12484 grc_mode = tr32(GRC_MODE);
12485 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
12486
Michael Chane6af3012005-04-21 17:12:05 -070012487 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012488 tg3_nvram_unlock(tp);
12489 }
12490
Joe Perches63c3a662011-04-26 08:12:10 +000012491 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012492 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012493 udelay(40);
12494 }
12495
12496 return ret;
12497}
12498
12499struct subsys_tbl_ent {
12500 u16 subsys_vendor, subsys_devid;
12501 u32 phy_id;
12502};
12503
Matt Carlson24daf2b2010-02-17 15:17:02 +000012504static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012505 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012506 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012507 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012508 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012509 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012510 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012511 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012512 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12513 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
12514 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012515 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012516 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012517 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012518 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12519 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
12520 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012521 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012522 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012523 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012524 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012525 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012526 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012527 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012528
12529 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012530 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012531 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012532 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012533 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012534 { TG3PCI_SUBVENDOR_ID_3COM,
12535 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
12536 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012537 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012538 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012539 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012540
12541 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012542 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012543 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012544 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012545 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012546 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012547 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012548 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012549 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012550
12551 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012552 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012553 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012554 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012555 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012556 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12557 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
12558 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012559 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012560 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012561 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012562
12563 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012564 { TG3PCI_SUBVENDOR_ID_IBM,
12565 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012566};
12567
Matt Carlson24daf2b2010-02-17 15:17:02 +000012568static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012569{
12570 int i;
12571
12572 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
12573 if ((subsys_id_to_phy_id[i].subsys_vendor ==
12574 tp->pdev->subsystem_vendor) &&
12575 (subsys_id_to_phy_id[i].subsys_devid ==
12576 tp->pdev->subsystem_device))
12577 return &subsys_id_to_phy_id[i];
12578 }
12579 return NULL;
12580}
12581
Michael Chan7d0c41e2005-04-21 17:06:20 -070012582static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012583{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012584 u32 val;
Michael Chancaf636c72006-03-22 01:05:31 -080012585 u16 pmcsr;
12586
12587 /* On some early chips the SRAM cannot be accessed in D3hot state,
12588 * so need make sure we're in D0.
12589 */
12590 pci_read_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, &pmcsr);
12591 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
12592 pci_write_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, pmcsr);
12593 msleep(1);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012594
12595 /* Make sure register accesses (indirect or otherwise)
12596 * will function correctly.
12597 */
12598 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12599 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012600
David S. Millerf49639e2006-06-09 11:58:36 -070012601 /* The memory arbiter has to be enabled in order for SRAM accesses
12602 * to succeed. Normally on powerup the tg3 chip firmware will make
12603 * sure it is enabled, but other entities such as system netboot
12604 * code might disable it.
12605 */
12606 val = tr32(MEMARB_MODE);
12607 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
12608
Matt Carlson79eb6902010-02-17 15:17:03 +000012609 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012610 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12611
Gary Zambranoa85feb82007-05-05 11:52:19 -070012612 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000012613 tg3_flag_set(tp, EEPROM_WRITE_PROT);
12614 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080012615
Michael Chanb5d37722006-09-27 16:06:21 -070012616 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012617 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012618 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
12619 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080012620 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012621 val = tr32(VCPU_CFGSHDW);
12622 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000012623 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070012624 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012625 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012626 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012627 device_set_wakeup_enable(&tp->pdev->dev, true);
12628 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012629 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070012630 }
12631
Linus Torvalds1da177e2005-04-16 15:20:36 -070012632 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12633 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12634 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012635 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012636 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012637
12638 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12639 tp->nic_sram_data_cfg = nic_cfg;
12640
12641 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
12642 ver >>= NIC_SRAM_DATA_VER_SHIFT;
12643 if ((GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700) &&
12644 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) &&
12645 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703) &&
12646 (ver > 0) && (ver < 0x100))
12647 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
12648
Matt Carlsona9daf362008-05-25 23:49:44 -070012649 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
12650 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
12651
Linus Torvalds1da177e2005-04-16 15:20:36 -070012652 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
12653 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
12654 eeprom_phy_serdes = 1;
12655
12656 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
12657 if (nic_phy_id != 0) {
12658 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
12659 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
12660
12661 eeprom_phy_id = (id1 >> 16) << 10;
12662 eeprom_phy_id |= (id2 & 0xfc00) << 16;
12663 eeprom_phy_id |= (id2 & 0x03ff) << 0;
12664 } else
12665 eeprom_phy_id = 0;
12666
Michael Chan7d0c41e2005-04-21 17:06:20 -070012667 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070012668 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000012669 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012670 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000012671 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012672 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070012673 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070012674
Joe Perches63c3a662011-04-26 08:12:10 +000012675 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012676 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
12677 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070012678 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070012679 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
12680
12681 switch (led_cfg) {
12682 default:
12683 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
12684 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12685 break;
12686
12687 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
12688 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12689 break;
12690
12691 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
12692 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070012693
12694 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
12695 * read on some older 5700/5701 bootcode.
12696 */
12697 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
12698 ASIC_REV_5700 ||
12699 GET_ASIC_REV(tp->pci_chip_rev_id) ==
12700 ASIC_REV_5701)
12701 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12702
Linus Torvalds1da177e2005-04-16 15:20:36 -070012703 break;
12704
12705 case SHASTA_EXT_LED_SHARED:
12706 tp->led_ctrl = LED_CTRL_MODE_SHARED;
12707 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
12708 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
12709 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12710 LED_CTRL_MODE_PHY_2);
12711 break;
12712
12713 case SHASTA_EXT_LED_MAC:
12714 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
12715 break;
12716
12717 case SHASTA_EXT_LED_COMBO:
12718 tp->led_ctrl = LED_CTRL_MODE_COMBO;
12719 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
12720 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12721 LED_CTRL_MODE_PHY_2);
12722 break;
12723
Stephen Hemminger855e1112008-04-16 16:37:28 -070012724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012725
12726 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12727 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
12728 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
12729 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12730
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012731 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
12732 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080012733
Michael Chan9d26e212006-12-07 00:21:14 -080012734 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000012735 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080012736 if ((tp->pdev->subsystem_vendor ==
12737 PCI_VENDOR_ID_ARIMA) &&
12738 (tp->pdev->subsystem_device == 0x205a ||
12739 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000012740 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080012741 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000012742 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
12743 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080012744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012745
12746 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000012747 tg3_flag_set(tp, ENABLE_ASF);
12748 if (tg3_flag(tp, 5750_PLUS))
12749 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012750 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012751
12752 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000012753 tg3_flag(tp, 5750_PLUS))
12754 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012755
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012756 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070012757 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000012758 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012759
Joe Perches63c3a662011-04-26 08:12:10 +000012760 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012761 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012762 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012763 device_set_wakeup_enable(&tp->pdev->dev, true);
12764 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012765
Linus Torvalds1da177e2005-04-16 15:20:36 -070012766 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012767 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012768
12769 /* serdes signal pre-emphasis in register 0x590 set by */
12770 /* bootcode if bit 18 is set */
12771 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012772 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070012773
Joe Perches63c3a662011-04-26 08:12:10 +000012774 if ((tg3_flag(tp, 57765_PLUS) ||
12775 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
12776 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080012777 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012778 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080012779
Joe Perches63c3a662011-04-26 08:12:10 +000012780 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson8c69b1e2010-08-02 11:26:00 +000012781 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000012782 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070012783 u32 cfg3;
12784
12785 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
12786 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000012787 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070012788 }
Matt Carlsona9daf362008-05-25 23:49:44 -070012789
Matt Carlson14417062010-02-17 15:16:59 +000012790 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000012791 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070012792 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000012793 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070012794 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000012795 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012796 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012797done:
Joe Perches63c3a662011-04-26 08:12:10 +000012798 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000012799 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000012800 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000012801 else
12802 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012803}
12804
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012805static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
12806{
12807 int i;
12808 u32 val;
12809
12810 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
12811 tw32(OTP_CTRL, cmd);
12812
12813 /* Wait for up to 1 ms for command to execute. */
12814 for (i = 0; i < 100; i++) {
12815 val = tr32(OTP_STATUS);
12816 if (val & OTP_STATUS_CMD_DONE)
12817 break;
12818 udelay(10);
12819 }
12820
12821 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
12822}
12823
12824/* Read the gphy configuration from the OTP region of the chip. The gphy
12825 * configuration is a 32-bit value that straddles the alignment boundary.
12826 * We do two 32-bit reads and then shift and merge the results.
12827 */
12828static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
12829{
12830 u32 bhalf_otp, thalf_otp;
12831
12832 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
12833
12834 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
12835 return 0;
12836
12837 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
12838
12839 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12840 return 0;
12841
12842 thalf_otp = tr32(OTP_READ_DATA);
12843
12844 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
12845
12846 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12847 return 0;
12848
12849 bhalf_otp = tr32(OTP_READ_DATA);
12850
12851 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
12852}
12853
Matt Carlsone256f8a2011-03-09 16:58:24 +000012854static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
12855{
12856 u32 adv = ADVERTISED_Autoneg |
12857 ADVERTISED_Pause;
12858
12859 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
12860 adv |= ADVERTISED_1000baseT_Half |
12861 ADVERTISED_1000baseT_Full;
12862
12863 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
12864 adv |= ADVERTISED_100baseT_Half |
12865 ADVERTISED_100baseT_Full |
12866 ADVERTISED_10baseT_Half |
12867 ADVERTISED_10baseT_Full |
12868 ADVERTISED_TP;
12869 else
12870 adv |= ADVERTISED_FIBRE;
12871
12872 tp->link_config.advertising = adv;
12873 tp->link_config.speed = SPEED_INVALID;
12874 tp->link_config.duplex = DUPLEX_INVALID;
12875 tp->link_config.autoneg = AUTONEG_ENABLE;
12876 tp->link_config.active_speed = SPEED_INVALID;
12877 tp->link_config.active_duplex = DUPLEX_INVALID;
12878 tp->link_config.orig_speed = SPEED_INVALID;
12879 tp->link_config.orig_duplex = DUPLEX_INVALID;
12880 tp->link_config.orig_autoneg = AUTONEG_INVALID;
12881}
12882
Michael Chan7d0c41e2005-04-21 17:06:20 -070012883static int __devinit tg3_phy_probe(struct tg3 *tp)
12884{
12885 u32 hw_phy_id_1, hw_phy_id_2;
12886 u32 hw_phy_id, hw_phy_id_masked;
12887 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012888
Matt Carlsone256f8a2011-03-09 16:58:24 +000012889 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000012890 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000012891 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
12892
Joe Perches63c3a662011-04-26 08:12:10 +000012893 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012894 return tg3_phy_init(tp);
12895
Linus Torvalds1da177e2005-04-16 15:20:36 -070012896 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010012897 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012898 */
12899 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000012900 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000012901 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012902 } else {
12903 /* Now read the physical PHY_ID from the chip and verify
12904 * that it is sane. If it doesn't look good, we fall back
12905 * to either the hard-coded table based PHY_ID and failing
12906 * that the value found in the eeprom area.
12907 */
12908 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
12909 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
12910
12911 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
12912 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
12913 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
12914
Matt Carlson79eb6902010-02-17 15:17:03 +000012915 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012916 }
12917
Matt Carlson79eb6902010-02-17 15:17:03 +000012918 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012919 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000012920 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012921 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070012922 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012923 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012924 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000012925 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070012926 /* Do nothing, phy ID already set up in
12927 * tg3_get_eeprom_hw_cfg().
12928 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012929 } else {
12930 struct subsys_tbl_ent *p;
12931
12932 /* No eeprom signature? Try the hardcoded
12933 * subsys device table.
12934 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012935 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012936 if (!p)
12937 return -ENODEV;
12938
12939 tp->phy_id = p->phy_id;
12940 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000012941 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012942 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012943 }
12944 }
12945
Matt Carlsona6b68da2010-12-06 08:28:52 +000012946 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
12947 ((tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
12948 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
12949 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12950 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000012951 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
12952
Matt Carlsone256f8a2011-03-09 16:58:24 +000012953 tg3_phy_init_link_config(tp);
12954
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012955 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000012956 !tg3_flag(tp, ENABLE_APE) &&
12957 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000012958 u32 bmsr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012959
12960 tg3_readphy(tp, MII_BMSR, &bmsr);
12961 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
12962 (bmsr & BMSR_LSTATUS))
12963 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012964
Linus Torvalds1da177e2005-04-16 15:20:36 -070012965 err = tg3_phy_reset(tp);
12966 if (err)
12967 return err;
12968
Matt Carlson42b64a42011-05-19 12:12:49 +000012969 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012970
Michael Chan3600d912006-12-07 00:21:48 -080012971 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
12972 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
12973 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
12974 if (!tg3_copper_is_advertising_all(tp, mask)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000012975 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
12976 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012977
12978 tg3_writephy(tp, MII_BMCR,
12979 BMCR_ANENABLE | BMCR_ANRESTART);
12980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012981 }
12982
12983skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000012984 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012985 err = tg3_init_5401phy_dsp(tp);
12986 if (err)
12987 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012988
Linus Torvalds1da177e2005-04-16 15:20:36 -070012989 err = tg3_init_5401phy_dsp(tp);
12990 }
12991
Linus Torvalds1da177e2005-04-16 15:20:36 -070012992 return err;
12993}
12994
Matt Carlson184b8902010-04-05 10:19:25 +000012995static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012996{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000012997 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012998 unsigned int block_end, rosize, len;
Matt Carlson184b8902010-04-05 10:19:25 +000012999 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013000
Matt Carlsonc3e94502011-04-13 11:05:08 +000013001 vpd_data = (u8 *)tg3_vpd_readblock(tp);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013002 if (!vpd_data)
13003 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013004
Matt Carlson4181b2c2010-02-26 14:04:45 +000013005 i = pci_vpd_find_tag(vpd_data, 0, TG3_NVM_VPD_LEN,
13006 PCI_VPD_LRDT_RO_DATA);
13007 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013008 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013009
13010 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
13011 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
13012 i += PCI_VPD_LRDT_TAG_SIZE;
13013
13014 if (block_end > TG3_NVM_VPD_LEN)
13015 goto out_not_found;
13016
Matt Carlson184b8902010-04-05 10:19:25 +000013017 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13018 PCI_VPD_RO_KEYWORD_MFR_ID);
13019 if (j > 0) {
13020 len = pci_vpd_info_field_size(&vpd_data[j]);
13021
13022 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13023 if (j + len > block_end || len != 4 ||
13024 memcmp(&vpd_data[j], "1028", 4))
13025 goto partno;
13026
13027 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13028 PCI_VPD_RO_KEYWORD_VENDOR0);
13029 if (j < 0)
13030 goto partno;
13031
13032 len = pci_vpd_info_field_size(&vpd_data[j]);
13033
13034 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13035 if (j + len > block_end)
13036 goto partno;
13037
13038 memcpy(tp->fw_ver, &vpd_data[j], len);
13039 strncat(tp->fw_ver, " bc ", TG3_NVM_VPD_LEN - len - 1);
13040 }
13041
13042partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000013043 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13044 PCI_VPD_RO_KEYWORD_PARTNO);
13045 if (i < 0)
13046 goto out_not_found;
13047
13048 len = pci_vpd_info_field_size(&vpd_data[i]);
13049
13050 i += PCI_VPD_INFO_FLD_HDR_SIZE;
13051 if (len > TG3_BPN_SIZE ||
13052 (len + i) > TG3_NVM_VPD_LEN)
13053 goto out_not_found;
13054
13055 memcpy(tp->board_part_number, &vpd_data[i], len);
13056
Linus Torvalds1da177e2005-04-16 15:20:36 -070013057out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013058 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000013059 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013060 return;
13061
13062out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000013063 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13064 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
13065 strcpy(tp->board_part_number, "BCM5717");
13066 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
13067 strcpy(tp->board_part_number, "BCM5718");
13068 else
13069 goto nomatch;
13070 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
13071 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
13072 strcpy(tp->board_part_number, "BCM57780");
13073 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
13074 strcpy(tp->board_part_number, "BCM57760");
13075 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
13076 strcpy(tp->board_part_number, "BCM57790");
13077 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
13078 strcpy(tp->board_part_number, "BCM57788");
13079 else
13080 goto nomatch;
13081 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
13082 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
13083 strcpy(tp->board_part_number, "BCM57761");
13084 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
13085 strcpy(tp->board_part_number, "BCM57765");
13086 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
13087 strcpy(tp->board_part_number, "BCM57781");
13088 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
13089 strcpy(tp->board_part_number, "BCM57785");
13090 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
13091 strcpy(tp->board_part_number, "BCM57791");
13092 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13093 strcpy(tp->board_part_number, "BCM57795");
13094 else
13095 goto nomatch;
13096 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070013097 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000013098 } else {
13099nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070013100 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000013101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013102}
13103
Matt Carlson9c8a6202007-10-21 16:16:08 -070013104static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
13105{
13106 u32 val;
13107
Matt Carlsone4f34112009-02-25 14:25:00 +000013108 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013109 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013110 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013111 val != 0)
13112 return 0;
13113
13114 return 1;
13115}
13116
Matt Carlsonacd9c112009-02-25 14:26:33 +000013117static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13118{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013119 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000013120 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013121 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013122
13123 if (tg3_nvram_read(tp, 0xc, &offset) ||
13124 tg3_nvram_read(tp, 0x4, &start))
13125 return;
13126
13127 offset = tg3_nvram_logical_addr(tp, offset);
13128
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013129 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013130 return;
13131
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013132 if ((val & 0xfc000000) == 0x0c000000) {
13133 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013134 return;
13135
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013136 if (val == 0)
13137 newver = true;
13138 }
13139
Matt Carlson75f99362010-04-05 10:19:24 +000013140 dst_off = strlen(tp->fw_ver);
13141
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013142 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013143 if (TG3_VER_SIZE - dst_off < 16 ||
13144 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013145 return;
13146
13147 offset = offset + ver_offset - start;
13148 for (i = 0; i < 16; i += 4) {
13149 __be32 v;
13150 if (tg3_nvram_read_be32(tp, offset + i, &v))
13151 return;
13152
Matt Carlson75f99362010-04-05 10:19:24 +000013153 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013154 }
13155 } else {
13156 u32 major, minor;
13157
13158 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13159 return;
13160
13161 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13162 TG3_NVM_BCVER_MAJSFT;
13163 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013164 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13165 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013166 }
13167}
13168
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013169static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13170{
13171 u32 val, major, minor;
13172
13173 /* Use native endian representation */
13174 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13175 return;
13176
13177 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13178 TG3_NVM_HWSB_CFG1_MAJSFT;
13179 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13180 TG3_NVM_HWSB_CFG1_MINSFT;
13181
13182 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13183}
13184
Matt Carlsondfe00d72008-11-21 17:19:41 -080013185static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13186{
13187 u32 offset, major, minor, build;
13188
Matt Carlson75f99362010-04-05 10:19:24 +000013189 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013190
13191 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13192 return;
13193
13194 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13195 case TG3_EEPROM_SB_REVISION_0:
13196 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13197 break;
13198 case TG3_EEPROM_SB_REVISION_2:
13199 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13200 break;
13201 case TG3_EEPROM_SB_REVISION_3:
13202 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13203 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013204 case TG3_EEPROM_SB_REVISION_4:
13205 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13206 break;
13207 case TG3_EEPROM_SB_REVISION_5:
13208 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13209 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013210 case TG3_EEPROM_SB_REVISION_6:
13211 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13212 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013213 default:
13214 return;
13215 }
13216
Matt Carlsone4f34112009-02-25 14:25:00 +000013217 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013218 return;
13219
13220 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13221 TG3_EEPROM_SB_EDH_BLD_SHFT;
13222 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13223 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13224 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13225
13226 if (minor > 99 || build > 26)
13227 return;
13228
Matt Carlson75f99362010-04-05 10:19:24 +000013229 offset = strlen(tp->fw_ver);
13230 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13231 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013232
13233 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013234 offset = strlen(tp->fw_ver);
13235 if (offset < TG3_VER_SIZE - 1)
13236 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013237 }
13238}
13239
Matt Carlsonacd9c112009-02-25 14:26:33 +000013240static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013241{
13242 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013243 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013244
13245 for (offset = TG3_NVM_DIR_START;
13246 offset < TG3_NVM_DIR_END;
13247 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013248 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013249 return;
13250
13251 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13252 break;
13253 }
13254
13255 if (offset == TG3_NVM_DIR_END)
13256 return;
13257
Joe Perches63c3a662011-04-26 08:12:10 +000013258 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013259 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013260 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013261 return;
13262
Matt Carlsone4f34112009-02-25 14:25:00 +000013263 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013264 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013265 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013266 return;
13267
13268 offset += val - start;
13269
Matt Carlsonacd9c112009-02-25 14:26:33 +000013270 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013271
Matt Carlsonacd9c112009-02-25 14:26:33 +000013272 tp->fw_ver[vlen++] = ',';
13273 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013274
13275 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013276 __be32 v;
13277 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013278 return;
13279
Al Virob9fc7dc2007-12-17 22:59:57 -080013280 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013281
Matt Carlsonacd9c112009-02-25 14:26:33 +000013282 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13283 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013284 break;
13285 }
13286
Matt Carlsonacd9c112009-02-25 14:26:33 +000013287 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13288 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013289 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013290}
13291
Matt Carlson7fd76442009-02-25 14:27:20 +000013292static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13293{
13294 int vlen;
13295 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013296 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013297
Joe Perches63c3a662011-04-26 08:12:10 +000013298 if (!tg3_flag(tp, ENABLE_APE) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000013299 return;
13300
13301 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13302 if (apedata != APE_SEG_SIG_MAGIC)
13303 return;
13304
13305 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13306 if (!(apedata & APE_FW_STATUS_READY))
13307 return;
13308
13309 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13310
Matt Carlsondc6d0742010-09-15 08:59:55 +000013311 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
Joe Perches63c3a662011-04-26 08:12:10 +000013312 tg3_flag_set(tp, APE_HAS_NCSI);
Matt Carlsonecc79642010-08-02 11:26:01 +000013313 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013314 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013315 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013316 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013317
Matt Carlson7fd76442009-02-25 14:27:20 +000013318 vlen = strlen(tp->fw_ver);
13319
Matt Carlsonecc79642010-08-02 11:26:01 +000013320 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13321 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013322 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13323 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13324 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13325 (apedata & APE_FW_VERSION_BLDMSK));
13326}
13327
Matt Carlsonacd9c112009-02-25 14:26:33 +000013328static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13329{
13330 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013331 bool vpd_vers = false;
13332
13333 if (tp->fw_ver[0] != 0)
13334 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013335
Joe Perches63c3a662011-04-26 08:12:10 +000013336 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000013337 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013338 return;
13339 }
13340
Matt Carlsonacd9c112009-02-25 14:26:33 +000013341 if (tg3_nvram_read(tp, 0, &val))
13342 return;
13343
13344 if (val == TG3_EEPROM_MAGIC)
13345 tg3_read_bc_ver(tp);
13346 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13347 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013348 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13349 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013350 else
13351 return;
13352
Joe Perches63c3a662011-04-26 08:12:10 +000013353 if (!tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || vpd_vers)
Matt Carlson75f99362010-04-05 10:19:24 +000013354 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013355
13356 tg3_read_mgmtfw_ver(tp);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013357
Matt Carlson75f99362010-04-05 10:19:24 +000013358done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013359 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013360}
13361
Michael Chan7544b092007-05-05 13:08:32 -070013362static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
13363
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013364static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13365{
Joe Perches63c3a662011-04-26 08:12:10 +000013366 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013367 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000013368 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013369 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013370 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013371 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013372}
13373
Matt Carlson41434702011-03-09 16:58:22 +000013374static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013375 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13376 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13377 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13378 { },
13379};
13380
Linus Torvalds1da177e2005-04-16 15:20:36 -070013381static int __devinit tg3_get_invariants(struct tg3 *tp)
13382{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013383 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013384 u32 pci_state_reg, grc_misc_cfg;
13385 u32 val;
13386 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013387 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013388
Linus Torvalds1da177e2005-04-16 15:20:36 -070013389 /* Force memory write invalidate off. If we leave it on,
13390 * then on 5700_BX chips we have to enable a workaround.
13391 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13392 * to match the cacheline size. The Broadcom driver have this
13393 * workaround but turns MWI off all the times so never uses
13394 * it. This seems to suggest that the workaround is insufficient.
13395 */
13396 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13397 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13398 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13399
13400 /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
13401 * has the register indirect write enable bit set before
13402 * we try to access any of the MMIO registers. It is also
13403 * critical that the PCI-X hw workaround situation is decided
13404 * before that as well.
13405 */
13406 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13407 &misc_ctrl_reg);
13408
13409 tp->pci_chip_rev_id = (misc_ctrl_reg >>
13410 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070013411 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13412 u32 prod_id_asic_rev;
13413
Matt Carlson5001e2f2009-11-13 13:03:51 +000013414 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13415 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013416 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
13417 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013418 pci_read_config_dword(tp->pdev,
13419 TG3PCI_GEN2_PRODID_ASICREV,
13420 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000013421 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13422 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13423 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13424 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13425 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13426 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13427 pci_read_config_dword(tp->pdev,
13428 TG3PCI_GEN15_PRODID_ASICREV,
13429 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013430 else
13431 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
13432 &prod_id_asic_rev);
13433
Matt Carlson321d32a2008-11-21 17:22:19 -080013434 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070013435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013436
Michael Chanff645be2005-04-21 17:09:53 -070013437 /* Wrong chip ID in 5752 A0. This code can be removed later
13438 * as A0 is not in production.
13439 */
13440 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13441 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13442
Michael Chan68929142005-08-09 20:17:14 -070013443 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
13444 * we need to disable memory and use config. cycles
13445 * only to access all registers. The 5702/03 chips
13446 * can mistakenly decode the special cycles from the
13447 * ICH chipsets as memory write cycles, causing corruption
13448 * of register and memory space. Only certain ICH bridges
13449 * will drive special cycles with non-zero data during the
13450 * address phase which can fall within the 5703's address
13451 * range. This is not an ICH bug as the PCI spec allows
13452 * non-zero address during special cycles. However, only
13453 * these ICH bridges are known to drive non-zero addresses
13454 * during special cycles.
13455 *
13456 * Since special cycles do not cross PCI bridges, we only
13457 * enable this workaround if the 5703 is on the secondary
13458 * bus of these ICH bridges.
13459 */
13460 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
13461 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
13462 static struct tg3_dev_id {
13463 u32 vendor;
13464 u32 device;
13465 u32 rev;
13466 } ich_chipsets[] = {
13467 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
13468 PCI_ANY_ID },
13469 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
13470 PCI_ANY_ID },
13471 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
13472 0xa },
13473 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
13474 PCI_ANY_ID },
13475 { },
13476 };
13477 struct tg3_dev_id *pci_id = &ich_chipsets[0];
13478 struct pci_dev *bridge = NULL;
13479
13480 while (pci_id->vendor != 0) {
13481 bridge = pci_get_device(pci_id->vendor, pci_id->device,
13482 bridge);
13483 if (!bridge) {
13484 pci_id++;
13485 continue;
13486 }
13487 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070013488 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070013489 continue;
13490 }
13491 if (bridge->subordinate &&
13492 (bridge->subordinate->number ==
13493 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013494 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070013495 pci_dev_put(bridge);
13496 break;
13497 }
13498 }
13499 }
13500
Matt Carlson41588ba2008-04-19 18:12:33 -070013501 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
13502 static struct tg3_dev_id {
13503 u32 vendor;
13504 u32 device;
13505 } bridge_chipsets[] = {
13506 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
13507 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
13508 { },
13509 };
13510 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
13511 struct pci_dev *bridge = NULL;
13512
13513 while (pci_id->vendor != 0) {
13514 bridge = pci_get_device(pci_id->vendor,
13515 pci_id->device,
13516 bridge);
13517 if (!bridge) {
13518 pci_id++;
13519 continue;
13520 }
13521 if (bridge->subordinate &&
13522 (bridge->subordinate->number <=
13523 tp->pdev->bus->number) &&
13524 (bridge->subordinate->subordinate >=
13525 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013526 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070013527 pci_dev_put(bridge);
13528 break;
13529 }
13530 }
13531 }
13532
Michael Chan4a29cc22006-03-19 13:21:12 -080013533 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
13534 * DMA addresses > 40-bit. This bridge may have other additional
13535 * 57xx devices behind it in some 4-port NIC designs for example.
13536 * Any tg3 device found behind the bridge will also need the 40-bit
13537 * DMA workaround.
13538 */
Michael Chana4e2b342005-10-26 15:46:52 -070013539 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
13540 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Joe Perches63c3a662011-04-26 08:12:10 +000013541 tg3_flag_set(tp, 5780_CLASS);
13542 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070013543 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000013544 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080013545 struct pci_dev *bridge = NULL;
13546
13547 do {
13548 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
13549 PCI_DEVICE_ID_SERVERWORKS_EPB,
13550 bridge);
13551 if (bridge && bridge->subordinate &&
13552 (bridge->subordinate->number <=
13553 tp->pdev->bus->number) &&
13554 (bridge->subordinate->subordinate >=
13555 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013556 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080013557 pci_dev_put(bridge);
13558 break;
13559 }
13560 } while (bridge);
13561 }
Michael Chan4cf78e42005-07-25 12:29:19 -070013562
Linus Torvalds1da177e2005-04-16 15:20:36 -070013563 /* Initialize misc host control in PCI block. */
13564 tp->misc_host_ctrl |= (misc_ctrl_reg &
13565 MISC_HOST_CTRL_CHIPREV);
13566 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13567 tp->misc_host_ctrl);
13568
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013569 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
13570 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013571 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13572 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Michael Chan7544b092007-05-05 13:08:32 -070013573 tp->pdev_peer = tg3_find_peer(tp);
13574
Matt Carlsonc885e822010-08-02 11:25:57 +000013575 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013576 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13577 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000013578 tg3_flag_set(tp, 5717_PLUS);
Matt Carlson0a58d662011-04-05 14:22:45 +000013579
13580 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013581 tg3_flag(tp, 5717_PLUS))
13582 tg3_flag_set(tp, 57765_PLUS);
Matt Carlsonc885e822010-08-02 11:25:57 +000013583
Matt Carlson321d32a2008-11-21 17:22:19 -080013584 /* Intentionally exclude ASIC_REV_5906 */
13585 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad12006-03-20 22:27:35 -080013586 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013587 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013588 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013589 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013590 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013591 tg3_flag(tp, 57765_PLUS))
13592 tg3_flag_set(tp, 5755_PLUS);
Matt Carlson321d32a2008-11-21 17:22:19 -080013593
13594 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13595 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070013596 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013597 tg3_flag(tp, 5755_PLUS) ||
13598 tg3_flag(tp, 5780_CLASS))
13599 tg3_flag_set(tp, 5750_PLUS);
John W. Linville6708e5c2005-04-21 17:00:52 -070013600
John W. Linville1b440c562005-04-21 17:03:18 -070013601 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) ||
Joe Perches63c3a662011-04-26 08:12:10 +000013602 tg3_flag(tp, 5750_PLUS))
13603 tg3_flag_set(tp, 5705_PLUS);
John W. Linville1b440c562005-04-21 17:03:18 -070013604
Matt Carlson027455a2008-12-21 20:19:30 -080013605 /* 5700 B0 chips do not support checksumming correctly due
13606 * to hardware bugs.
13607 */
Michał Mirosławdc668912011-04-07 03:35:07 +000013608 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
13609 u32 features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013610
Joe Perches63c3a662011-04-26 08:12:10 +000013611 if (tg3_flag(tp, 5755_PLUS))
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013612 features |= NETIF_F_IPV6_CSUM;
13613 tp->dev->features |= features;
Michał Mirosławdc668912011-04-07 03:35:07 +000013614 tp->dev->hw_features |= features;
13615 tp->dev->vlan_features |= features;
Matt Carlson027455a2008-12-21 20:19:30 -080013616 }
13617
Matt Carlson507399f2009-11-13 13:03:37 +000013618 /* Determine TSO capabilities */
Matt Carlson2866d952011-02-10 20:06:46 -080013619 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlson4d163b72011-01-25 15:58:48 +000013620 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000013621 else if (tg3_flag(tp, 57765_PLUS))
13622 tg3_flag_set(tp, HW_TSO_3);
13623 else if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000013624 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000013625 tg3_flag_set(tp, HW_TSO_2);
13626 else if (tg3_flag(tp, 5750_PLUS)) {
13627 tg3_flag_set(tp, HW_TSO_1);
13628 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013629 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13630 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000013631 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013632 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13633 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13634 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000013635 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013636 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13637 tp->fw_needed = FIRMWARE_TG3TSO5;
13638 else
13639 tp->fw_needed = FIRMWARE_TG3TSO;
13640 }
13641
Matt Carlsondabc5c62011-05-19 12:12:52 +000013642 /* Selectively allow TSO based on operating conditions */
13643 if ((tg3_flag(tp, HW_TSO_1) ||
13644 tg3_flag(tp, HW_TSO_2) ||
13645 tg3_flag(tp, HW_TSO_3)) ||
13646 (tp->fw_needed && !tg3_flag(tp, ENABLE_ASF)))
13647 tg3_flag_set(tp, TSO_CAPABLE);
13648 else {
13649 tg3_flag_clear(tp, TSO_CAPABLE);
13650 tg3_flag_clear(tp, TSO_BUG);
13651 tp->fw_needed = NULL;
13652 }
13653
13654 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
13655 tp->fw_needed = FIRMWARE_TG3;
13656
Matt Carlson507399f2009-11-13 13:03:37 +000013657 tp->irq_max = 1;
13658
Joe Perches63c3a662011-04-26 08:12:10 +000013659 if (tg3_flag(tp, 5750_PLUS)) {
13660 tg3_flag_set(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070013661 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
13662 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
13663 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
13664 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
13665 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000013666 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070013667
Joe Perches63c3a662011-04-26 08:12:10 +000013668 if (tg3_flag(tp, 5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070013669 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000013670 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070013671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013672
Joe Perches63c3a662011-04-26 08:12:10 +000013673 if (tg3_flag(tp, 57765_PLUS)) {
13674 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000013675 tp->irq_max = TG3_IRQ_MAX_VECS;
13676 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013677 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000013678
Matt Carlson2ffcc982011-05-19 12:12:44 +000013679 /* All chips can get confused if TX buffers
13680 * straddle the 4GB address boundary.
13681 */
13682 tg3_flag_set(tp, 4G_DMA_BNDRY_BUG);
13683
13684 if (tg3_flag(tp, 5755_PLUS))
Joe Perches63c3a662011-04-26 08:12:10 +000013685 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlson2ffcc982011-05-19 12:12:44 +000013686 else
Joe Perches63c3a662011-04-26 08:12:10 +000013687 tg3_flag_set(tp, 40BIT_DMA_LIMIT_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013688
Joe Perches63c3a662011-04-26 08:12:10 +000013689 if (tg3_flag(tp, 5717_PLUS))
13690 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000013691
Joe Perches63c3a662011-04-26 08:12:10 +000013692 if (tg3_flag(tp, 57765_PLUS) &&
Matt Carlson2866d952011-02-10 20:06:46 -080013693 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
Joe Perches63c3a662011-04-26 08:12:10 +000013694 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000013695
Joe Perches63c3a662011-04-26 08:12:10 +000013696 if (!tg3_flag(tp, 5705_PLUS) ||
13697 tg3_flag(tp, 5780_CLASS) ||
13698 tg3_flag(tp, USE_JUMBO_BDFLAG))
13699 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070013700
Matt Carlson52f44902008-11-21 17:17:04 -080013701 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13702 &pci_state_reg);
13703
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013704 tp->pcie_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_EXP);
13705 if (tp->pcie_cap != 0) {
13706 u16 lnkctl;
13707
Joe Perches63c3a662011-04-26 08:12:10 +000013708 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013709
Matt Carlsoncf790032010-11-24 08:31:48 +000013710 tp->pcie_readrq = 4096;
Matt Carlsond78b59f2011-04-05 14:22:46 +000013711 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13712 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Matt Carlsonb4495ed2011-01-25 15:58:47 +000013713 tp->pcie_readrq = 2048;
Matt Carlsoncf790032010-11-24 08:31:48 +000013714
13715 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013716
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013717 pci_read_config_word(tp->pdev,
13718 tp->pcie_cap + PCI_EXP_LNKCTL,
13719 &lnkctl);
13720 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
13721 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000013722 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000013723 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013724 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013725 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000013726 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
13727 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000013728 tg3_flag_set(tp, CLKREQ_BUG);
Matt Carlson614b0592010-01-20 16:58:02 +000013729 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000013730 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080013731 }
Matt Carlson52f44902008-11-21 17:17:04 -080013732 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Joe Perches63c3a662011-04-26 08:12:10 +000013733 tg3_flag_set(tp, PCI_EXPRESS);
13734 } else if (!tg3_flag(tp, 5705_PLUS) ||
13735 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080013736 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
13737 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000013738 dev_err(&tp->pdev->dev,
13739 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080013740 return -EIO;
13741 }
13742
13743 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000013744 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080013745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013746
Michael Chan399de502005-10-03 14:02:39 -070013747 /* If we have an AMD 762 or VIA K8T800 chipset, write
13748 * reordering to the mailbox registers done by the host
13749 * controller can cause major troubles. We read back from
13750 * every mailbox register write to force the writes to be
13751 * posted to the chip in order.
13752 */
Matt Carlson41434702011-03-09 16:58:22 +000013753 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013754 !tg3_flag(tp, PCI_EXPRESS))
13755 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070013756
Matt Carlson69fc4052008-12-21 20:19:57 -080013757 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
13758 &tp->pci_cacheline_sz);
13759 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13760 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013761 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13762 tp->pci_lat_timer < 64) {
13763 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080013764 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13765 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013766 }
13767
Matt Carlson52f44902008-11-21 17:17:04 -080013768 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
13769 /* 5700 BX chips need to have their TX producer index
13770 * mailboxes written twice to workaround a bug.
13771 */
Joe Perches63c3a662011-04-26 08:12:10 +000013772 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070013773
Matt Carlson52f44902008-11-21 17:17:04 -080013774 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013775 *
13776 * The workaround is to use indirect register accesses
13777 * for all chip writes not to mailbox registers.
13778 */
Joe Perches63c3a662011-04-26 08:12:10 +000013779 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013780 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013781
Joe Perches63c3a662011-04-26 08:12:10 +000013782 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013783
13784 /* The chip can have it's power management PCI config
13785 * space registers clobbered due to this bug.
13786 * So explicitly force the chip into D0 here.
13787 */
Matt Carlson9974a352007-10-07 23:27:28 -070013788 pci_read_config_dword(tp->pdev,
13789 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013790 &pm_reg);
13791 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
13792 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070013793 pci_write_config_dword(tp->pdev,
13794 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013795 pm_reg);
13796
13797 /* Also, force SERR#/PERR# in PCI command. */
13798 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13799 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
13800 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13801 }
13802 }
13803
Linus Torvalds1da177e2005-04-16 15:20:36 -070013804 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000013805 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013806 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000013807 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013808
13809 /* Chip-specific fixup from Broadcom driver */
13810 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
13811 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
13812 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
13813 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
13814 }
13815
Michael Chan1ee582d2005-08-09 20:16:46 -070013816 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070013817 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013818 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070013819 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070013820 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013821 tp->write32_tx_mbox = tg3_write32;
13822 tp->write32_rx_mbox = tg3_write32;
13823
13824 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000013825 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070013826 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013827 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013828 (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson98efd8a2007-05-05 12:47:25 -070013829 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
13830 /*
13831 * Back to back register writes can cause problems on these
13832 * chips, the workaround is to read back all reg writes
13833 * except those to mailbox regs.
13834 *
13835 * See tg3_write_indirect_reg32().
13836 */
Michael Chan1ee582d2005-08-09 20:16:46 -070013837 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013838 }
13839
Joe Perches63c3a662011-04-26 08:12:10 +000013840 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070013841 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000013842 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070013843 tp->write32_rx_mbox = tg3_write_flush_reg32;
13844 }
Michael Chan20094932005-08-09 20:16:32 -070013845
Joe Perches63c3a662011-04-26 08:12:10 +000013846 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070013847 tp->read32 = tg3_read_indirect_reg32;
13848 tp->write32 = tg3_write_indirect_reg32;
13849 tp->read32_mbox = tg3_read_indirect_mbox;
13850 tp->write32_mbox = tg3_write_indirect_mbox;
13851 tp->write32_tx_mbox = tg3_write_indirect_mbox;
13852 tp->write32_rx_mbox = tg3_write_indirect_mbox;
13853
13854 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070013855 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070013856
13857 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13858 pci_cmd &= ~PCI_COMMAND_MEMORY;
13859 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13860 }
Michael Chanb5d37722006-09-27 16:06:21 -070013861 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13862 tp->read32_mbox = tg3_read32_mbox_5906;
13863 tp->write32_mbox = tg3_write32_mbox_5906;
13864 tp->write32_tx_mbox = tg3_write32_mbox_5906;
13865 tp->write32_rx_mbox = tg3_write32_mbox_5906;
13866 }
Michael Chan68929142005-08-09 20:17:14 -070013867
Michael Chanbbadf502006-04-06 21:46:34 -070013868 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013869 (tg3_flag(tp, PCIX_MODE) &&
Michael Chanbbadf502006-04-06 21:46:34 -070013870 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070013871 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000013872 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070013873
Michael Chan7d0c41e2005-04-21 17:06:20 -070013874 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000013875 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070013876 * determined before calling tg3_set_power_state() so that
13877 * we know whether or not to switch out of Vaux power.
13878 * When the flag is set, it means that GPIO1 is used for eeprom
13879 * write protect and also implies that it is a LOM where GPIOs
13880 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013881 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070013882 tg3_get_eeprom_hw_cfg(tp);
13883
Joe Perches63c3a662011-04-26 08:12:10 +000013884 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070013885 /* Allow reads and writes to the
13886 * APE register and memory space.
13887 */
13888 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000013889 PCISTATE_ALLOW_APE_SHMEM_WR |
13890 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070013891 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
13892 pci_state_reg);
13893 }
13894
Matt Carlson9936bcf2007-10-10 18:03:07 -070013895 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013896 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013897 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013898 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013899 tg3_flag(tp, 57765_PLUS))
13900 tg3_flag_set(tp, CPMU_PRESENT);
Matt Carlsond30cdd22007-10-07 23:28:35 -070013901
Matt Carlsonbea8a632011-04-25 12:42:49 +000013902 /* Set up tp->grc_local_ctrl before calling tg3_power_up().
Michael Chan314fba32005-04-21 17:07:04 -070013903 * GPIO1 driven high will bring 5700's external PHY out of reset.
13904 * It is also used as eeprom write protect on LOMs.
13905 */
13906 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
13907 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
Joe Perches63c3a662011-04-26 08:12:10 +000013908 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070013909 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
13910 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070013911 /* Unused GPIO3 must be driven as output on 5752 because there
13912 * are no pull-up resistors on unused GPIO pins.
13913 */
13914 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
13915 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070013916
Matt Carlson321d32a2008-11-21 17:22:19 -080013917 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000013918 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
13919 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Michael Chanaf36e6b2006-03-23 01:28:06 -080013920 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13921
Matt Carlson8d519ab2009-04-20 06:58:01 +000013922 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
13923 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070013924 /* Turn off the debug UART. */
13925 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013926 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070013927 /* Keep VMain power. */
13928 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
13929 GRC_LCLCTRL_GPIO_OUTPUT0;
13930 }
13931
Linus Torvalds1da177e2005-04-16 15:20:36 -070013932 /* Force the chip into D0. */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000013933 err = tg3_power_up(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013934 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000013935 dev_err(&tp->pdev->dev, "Transition to D0 failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070013936 return err;
13937 }
13938
Linus Torvalds1da177e2005-04-16 15:20:36 -070013939 /* Derive initial jumbo mode from MTU assigned in
13940 * ether_setup() via the alloc_etherdev() call
13941 */
Joe Perches63c3a662011-04-26 08:12:10 +000013942 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
13943 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013944
13945 /* Determine WakeOnLan speed to use. */
13946 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13947 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
13948 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
13949 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000013950 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013951 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013952 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013953 }
13954
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013955 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013956 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013957
Linus Torvalds1da177e2005-04-16 15:20:36 -070013958 /* A few boards don't want Ethernet@WireSpeed phy feature */
13959 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13960 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
13961 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070013962 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013963 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
13964 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13965 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013966
13967 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
13968 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013969 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013970 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013971 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013972
Joe Perches63c3a662011-04-26 08:12:10 +000013973 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013974 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080013975 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013976 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000013977 !tg3_flag(tp, 57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070013978 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013979 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013980 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13981 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080013982 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
13983 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013984 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080013985 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013986 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080013987 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013988 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070013989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013990
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013991 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13992 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
13993 tp->phy_otp = tg3_read_otp_phycfg(tp);
13994 if (tp->phy_otp == 0)
13995 tp->phy_otp = TG3_OTP_DEFAULT;
13996 }
13997
Joe Perches63c3a662011-04-26 08:12:10 +000013998 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070013999 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
14000 else
14001 tp->mi_mode = MAC_MI_MODE_BASE;
14002
Linus Torvalds1da177e2005-04-16 15:20:36 -070014003 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014004 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
14005 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
14006 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
14007
Matt Carlson4d958472011-04-20 07:57:35 +000014008 /* Set these bits to enable statistics workaround. */
14009 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14010 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
14011 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
14012 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
14013 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
14014 }
14015
Matt Carlson321d32a2008-11-21 17:22:19 -080014016 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14017 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000014018 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070014019
Matt Carlson158d7ab2008-05-29 01:37:54 -070014020 err = tg3_mdio_init(tp);
14021 if (err)
14022 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014023
14024 /* Initialize data/descriptor byte/word swapping. */
14025 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000014026 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14027 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
14028 GRC_MODE_WORD_SWAP_B2HRX_DATA |
14029 GRC_MODE_B2HRX_ENABLE |
14030 GRC_MODE_HTX2B_ENABLE |
14031 GRC_MODE_HOST_STACKUP);
14032 else
14033 val &= GRC_MODE_HOST_STACKUP;
14034
Linus Torvalds1da177e2005-04-16 15:20:36 -070014035 tw32(GRC_MODE, val | tp->grc_mode);
14036
14037 tg3_switch_clocks(tp);
14038
14039 /* Clear this out for sanity. */
14040 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
14041
14042 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14043 &pci_state_reg);
14044 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014045 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014046 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
14047
14048 if (chiprevid == CHIPREV_ID_5701_A0 ||
14049 chiprevid == CHIPREV_ID_5701_B0 ||
14050 chiprevid == CHIPREV_ID_5701_B2 ||
14051 chiprevid == CHIPREV_ID_5701_B5) {
14052 void __iomem *sram_base;
14053
14054 /* Write some dummy words into the SRAM status block
14055 * area, see if it reads back correctly. If the return
14056 * value is bad, force enable the PCIX workaround.
14057 */
14058 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
14059
14060 writel(0x00000000, sram_base);
14061 writel(0x00000000, sram_base + 4);
14062 writel(0xffffffff, sram_base + 4);
14063 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000014064 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014065 }
14066 }
14067
14068 udelay(50);
14069 tg3_nvram_init(tp);
14070
14071 grc_misc_cfg = tr32(GRC_MISC_CFG);
14072 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
14073
Linus Torvalds1da177e2005-04-16 15:20:36 -070014074 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14075 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
14076 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000014077 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014078
Joe Perches63c3a662011-04-26 08:12:10 +000014079 if (!tg3_flag(tp, IS_5788) &&
David S. Millerfac9b832005-05-18 22:46:34 -070014080 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700))
Joe Perches63c3a662011-04-26 08:12:10 +000014081 tg3_flag_set(tp, TAGGED_STATUS);
14082 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070014083 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
14084 HOSTCC_MODE_CLRTICK_TXBD);
14085
14086 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
14087 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14088 tp->misc_host_ctrl);
14089 }
14090
Matt Carlson3bda1252008-08-15 14:08:22 -070014091 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000014092 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000014093 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070014094 else
14095 tp->mac_mode = TG3_DEF_MAC_MODE;
14096
Linus Torvalds1da177e2005-04-16 15:20:36 -070014097 /* these are limited to 10/100 only */
14098 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14099 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14100 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14101 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14102 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
14103 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
14104 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
14105 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14106 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080014107 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
14108 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014109 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000014110 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14111 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014112 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14113 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014114
14115 err = tg3_phy_probe(tp);
14116 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014117 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014118 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014119 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014120 }
14121
Matt Carlson184b8902010-04-05 10:19:25 +000014122 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080014123 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014124
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014125 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
14126 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014127 } else {
14128 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014129 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014130 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014131 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014132 }
14133
14134 /* 5700 {AX,BX} chips have a broken status block link
14135 * change bit implementation, so we must use the
14136 * status register in those cases.
14137 */
14138 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014139 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014140 else
Joe Perches63c3a662011-04-26 08:12:10 +000014141 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014142
14143 /* The led_ctrl is set during tg3_phy_probe, here we might
14144 * have to force the link status polling mechanism based
14145 * upon subsystem IDs.
14146 */
14147 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070014148 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014149 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
14150 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000014151 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014152 }
14153
14154 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014155 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000014156 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014157 else
Joe Perches63c3a662011-04-26 08:12:10 +000014158 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014159
Matt Carlsonbf933c82011-01-25 15:58:49 +000014160 tp->rx_offset = NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014161 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014162 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014163 tg3_flag(tp, PCIX_MODE)) {
Matt Carlsonbf933c82011-01-25 15:58:49 +000014164 tp->rx_offset = 0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014165#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000014166 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014167#endif
14168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014169
Matt Carlson2c49a442010-09-30 10:34:35 +000014170 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14171 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014172 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14173
Matt Carlson2c49a442010-09-30 10:34:35 +000014174 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070014175
14176 /* Increment the rx prod index on the rx std ring by at most
14177 * 8 for these chips to workaround hw errata.
14178 */
14179 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14180 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14181 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14182 tp->rx_std_max_post = 8;
14183
Joe Perches63c3a662011-04-26 08:12:10 +000014184 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070014185 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14186 PCIE_PWR_MGMT_L1_THRESH_MSK;
14187
Linus Torvalds1da177e2005-04-16 15:20:36 -070014188 return err;
14189}
14190
David S. Miller49b6e95f2007-03-29 01:38:42 -070014191#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014192static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14193{
14194 struct net_device *dev = tp->dev;
14195 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014196 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070014197 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014198 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014199
David S. Miller49b6e95f2007-03-29 01:38:42 -070014200 addr = of_get_property(dp, "local-mac-address", &len);
14201 if (addr && len == 6) {
14202 memcpy(dev->dev_addr, addr, 6);
14203 memcpy(dev->perm_addr, dev->dev_addr, 6);
14204 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014205 }
14206 return -ENODEV;
14207}
14208
14209static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14210{
14211 struct net_device *dev = tp->dev;
14212
14213 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070014214 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014215 return 0;
14216}
14217#endif
14218
14219static int __devinit tg3_get_device_address(struct tg3 *tp)
14220{
14221 struct net_device *dev = tp->dev;
14222 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080014223 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014224
David S. Miller49b6e95f2007-03-29 01:38:42 -070014225#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014226 if (!tg3_get_macaddr_sparc(tp))
14227 return 0;
14228#endif
14229
14230 mac_offset = 0x7c;
David S. Millerf49639e2006-06-09 11:58:36 -070014231 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
Joe Perches63c3a662011-04-26 08:12:10 +000014232 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014233 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14234 mac_offset = 0xcc;
14235 if (tg3_nvram_lock(tp))
14236 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14237 else
14238 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000014239 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlsona50d0792010-06-05 17:24:37 +000014240 if (PCI_FUNC(tp->pdev->devfn) & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014241 mac_offset = 0xcc;
Matt Carlsona50d0792010-06-05 17:24:37 +000014242 if (PCI_FUNC(tp->pdev->devfn) > 1)
14243 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014244 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014245 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014246
14247 /* First try to get it from MAC address mailbox. */
14248 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14249 if ((hi >> 16) == 0x484b) {
14250 dev->dev_addr[0] = (hi >> 8) & 0xff;
14251 dev->dev_addr[1] = (hi >> 0) & 0xff;
14252
14253 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14254 dev->dev_addr[2] = (lo >> 24) & 0xff;
14255 dev->dev_addr[3] = (lo >> 16) & 0xff;
14256 dev->dev_addr[4] = (lo >> 8) & 0xff;
14257 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014258
Michael Chan008652b2006-03-27 23:14:53 -080014259 /* Some old bootcode may report a 0 MAC address in SRAM */
14260 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14261 }
14262 if (!addr_ok) {
14263 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000014264 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000014265 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000014266 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070014267 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14268 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080014269 }
14270 /* Finally just fetch it out of the MAC control regs. */
14271 else {
14272 hi = tr32(MAC_ADDR_0_HIGH);
14273 lo = tr32(MAC_ADDR_0_LOW);
14274
14275 dev->dev_addr[5] = lo & 0xff;
14276 dev->dev_addr[4] = (lo >> 8) & 0xff;
14277 dev->dev_addr[3] = (lo >> 16) & 0xff;
14278 dev->dev_addr[2] = (lo >> 24) & 0xff;
14279 dev->dev_addr[1] = hi & 0xff;
14280 dev->dev_addr[0] = (hi >> 8) & 0xff;
14281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014282 }
14283
14284 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014285#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014286 if (!tg3_get_default_macaddr_sparc(tp))
14287 return 0;
14288#endif
14289 return -EINVAL;
14290 }
John W. Linville2ff43692005-09-12 14:44:20 -070014291 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014292 return 0;
14293}
14294
David S. Miller59e6b432005-05-18 22:50:10 -070014295#define BOUNDARY_SINGLE_CACHELINE 1
14296#define BOUNDARY_MULTI_CACHELINE 2
14297
14298static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14299{
14300 int cacheline_size;
14301 u8 byte;
14302 int goal;
14303
14304 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14305 if (byte == 0)
14306 cacheline_size = 1024;
14307 else
14308 cacheline_size = (int) byte * 4;
14309
14310 /* On 5703 and later chips, the boundary bits have no
14311 * effect.
14312 */
14313 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14314 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014315 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070014316 goto out;
14317
14318#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14319 goal = BOUNDARY_MULTI_CACHELINE;
14320#else
14321#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14322 goal = BOUNDARY_SINGLE_CACHELINE;
14323#else
14324 goal = 0;
14325#endif
14326#endif
14327
Joe Perches63c3a662011-04-26 08:12:10 +000014328 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014329 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14330 goto out;
14331 }
14332
David S. Miller59e6b432005-05-18 22:50:10 -070014333 if (!goal)
14334 goto out;
14335
14336 /* PCI controllers on most RISC systems tend to disconnect
14337 * when a device tries to burst across a cache-line boundary.
14338 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14339 *
14340 * Unfortunately, for PCI-E there are only limited
14341 * write-side controls for this, and thus for reads
14342 * we will still get the disconnects. We'll also waste
14343 * these PCI cycles for both read and write for chips
14344 * other than 5700 and 5701 which do not implement the
14345 * boundary bits.
14346 */
Joe Perches63c3a662011-04-26 08:12:10 +000014347 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014348 switch (cacheline_size) {
14349 case 16:
14350 case 32:
14351 case 64:
14352 case 128:
14353 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14354 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14355 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14356 } else {
14357 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14358 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14359 }
14360 break;
14361
14362 case 256:
14363 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14364 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14365 break;
14366
14367 default:
14368 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14369 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14370 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014371 }
Joe Perches63c3a662011-04-26 08:12:10 +000014372 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014373 switch (cacheline_size) {
14374 case 16:
14375 case 32:
14376 case 64:
14377 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14378 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14379 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14380 break;
14381 }
14382 /* fallthrough */
14383 case 128:
14384 default:
14385 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14386 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14387 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014388 }
David S. Miller59e6b432005-05-18 22:50:10 -070014389 } else {
14390 switch (cacheline_size) {
14391 case 16:
14392 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14393 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14394 DMA_RWCTRL_WRITE_BNDRY_16);
14395 break;
14396 }
14397 /* fallthrough */
14398 case 32:
14399 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14400 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14401 DMA_RWCTRL_WRITE_BNDRY_32);
14402 break;
14403 }
14404 /* fallthrough */
14405 case 64:
14406 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14407 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14408 DMA_RWCTRL_WRITE_BNDRY_64);
14409 break;
14410 }
14411 /* fallthrough */
14412 case 128:
14413 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14414 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14415 DMA_RWCTRL_WRITE_BNDRY_128);
14416 break;
14417 }
14418 /* fallthrough */
14419 case 256:
14420 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14421 DMA_RWCTRL_WRITE_BNDRY_256);
14422 break;
14423 case 512:
14424 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14425 DMA_RWCTRL_WRITE_BNDRY_512);
14426 break;
14427 case 1024:
14428 default:
14429 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14430 DMA_RWCTRL_WRITE_BNDRY_1024);
14431 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014432 }
David S. Miller59e6b432005-05-18 22:50:10 -070014433 }
14434
14435out:
14436 return val;
14437}
14438
Linus Torvalds1da177e2005-04-16 15:20:36 -070014439static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
14440{
14441 struct tg3_internal_buffer_desc test_desc;
14442 u32 sram_dma_descs;
14443 int i, ret;
14444
14445 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
14446
14447 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
14448 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
14449 tw32(RDMAC_STATUS, 0);
14450 tw32(WDMAC_STATUS, 0);
14451
14452 tw32(BUFMGR_MODE, 0);
14453 tw32(FTQ_RESET, 0);
14454
14455 test_desc.addr_hi = ((u64) buf_dma) >> 32;
14456 test_desc.addr_lo = buf_dma & 0xffffffff;
14457 test_desc.nic_mbuf = 0x00002100;
14458 test_desc.len = size;
14459
14460 /*
14461 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
14462 * the *second* time the tg3 driver was getting loaded after an
14463 * initial scan.
14464 *
14465 * Broadcom tells me:
14466 * ...the DMA engine is connected to the GRC block and a DMA
14467 * reset may affect the GRC block in some unpredictable way...
14468 * The behavior of resets to individual blocks has not been tested.
14469 *
14470 * Broadcom noted the GRC reset will also reset all sub-components.
14471 */
14472 if (to_device) {
14473 test_desc.cqid_sqid = (13 << 8) | 2;
14474
14475 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
14476 udelay(40);
14477 } else {
14478 test_desc.cqid_sqid = (16 << 8) | 7;
14479
14480 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
14481 udelay(40);
14482 }
14483 test_desc.flags = 0x00000005;
14484
14485 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
14486 u32 val;
14487
14488 val = *(((u32 *)&test_desc) + i);
14489 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
14490 sram_dma_descs + (i * sizeof(u32)));
14491 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
14492 }
14493 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
14494
Matt Carlson859a588792010-04-05 10:19:28 +000014495 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014496 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000014497 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014498 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014499
14500 ret = -ENODEV;
14501 for (i = 0; i < 40; i++) {
14502 u32 val;
14503
14504 if (to_device)
14505 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
14506 else
14507 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
14508 if ((val & 0xffff) == sram_dma_descs) {
14509 ret = 0;
14510 break;
14511 }
14512
14513 udelay(100);
14514 }
14515
14516 return ret;
14517}
14518
David S. Millerded73402005-05-23 13:59:47 -070014519#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070014520
Matt Carlson41434702011-03-09 16:58:22 +000014521static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080014522 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14523 { },
14524};
14525
Linus Torvalds1da177e2005-04-16 15:20:36 -070014526static int __devinit tg3_test_dma(struct tg3 *tp)
14527{
14528 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070014529 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014530 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014531
Matt Carlson4bae65c2010-11-24 08:31:52 +000014532 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
14533 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014534 if (!buf) {
14535 ret = -ENOMEM;
14536 goto out_nofree;
14537 }
14538
14539 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
14540 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
14541
David S. Miller59e6b432005-05-18 22:50:10 -070014542 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014543
Joe Perches63c3a662011-04-26 08:12:10 +000014544 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014545 goto out;
14546
Joe Perches63c3a662011-04-26 08:12:10 +000014547 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014548 /* DMA read watermark not used on PCIE */
14549 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000014550 } else if (!tg3_flag(tp, PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070014551 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14552 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014553 tp->dma_rwctrl |= 0x003f0000;
14554 else
14555 tp->dma_rwctrl |= 0x003f000f;
14556 } else {
14557 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14558 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
14559 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080014560 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014561
Michael Chan4a29cc22006-03-19 13:21:12 -080014562 /* If the 5704 is behind the EPB bridge, we can
14563 * do the less restrictive ONE_DMA workaround for
14564 * better performance.
14565 */
Joe Perches63c3a662011-04-26 08:12:10 +000014566 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Michael Chan4a29cc22006-03-19 13:21:12 -080014567 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14568 tp->dma_rwctrl |= 0x8000;
14569 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014570 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
14571
Michael Chan49afdeb2007-02-13 12:17:03 -080014572 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
14573 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070014574 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080014575 tp->dma_rwctrl |=
14576 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
14577 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
14578 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070014579 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
14580 /* 5780 always in PCIX mode */
14581 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070014582 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
14583 /* 5714 always in PCIX mode */
14584 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014585 } else {
14586 tp->dma_rwctrl |= 0x001b000f;
14587 }
14588 }
14589
14590 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14591 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14592 tp->dma_rwctrl &= 0xfffffff0;
14593
14594 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14595 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
14596 /* Remove this if it causes problems for some boards. */
14597 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
14598
14599 /* On 5700/5701 chips, we need to set this bit.
14600 * Otherwise the chip will issue cacheline transactions
14601 * to streamable DMA memory with not all the byte
14602 * enables turned on. This is an error on several
14603 * RISC PCI controllers, in particular sparc64.
14604 *
14605 * On 5703/5704 chips, this bit has been reassigned
14606 * a different meaning. In particular, it is used
14607 * on those chips to enable a PCI-X workaround.
14608 */
14609 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
14610 }
14611
14612 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14613
14614#if 0
14615 /* Unneeded, already done by tg3_get_invariants. */
14616 tg3_switch_clocks(tp);
14617#endif
14618
Linus Torvalds1da177e2005-04-16 15:20:36 -070014619 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14620 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
14621 goto out;
14622
David S. Miller59e6b432005-05-18 22:50:10 -070014623 /* It is best to perform DMA test with maximum write burst size
14624 * to expose the 5700/5701 write DMA bug.
14625 */
14626 saved_dma_rwctrl = tp->dma_rwctrl;
14627 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14628 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14629
Linus Torvalds1da177e2005-04-16 15:20:36 -070014630 while (1) {
14631 u32 *p = buf, i;
14632
14633 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
14634 p[i] = i;
14635
14636 /* Send the buffer to the chip. */
14637 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
14638 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000014639 dev_err(&tp->pdev->dev,
14640 "%s: Buffer write failed. err = %d\n",
14641 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014642 break;
14643 }
14644
14645#if 0
14646 /* validate data reached card RAM correctly. */
14647 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14648 u32 val;
14649 tg3_read_mem(tp, 0x2100 + (i*4), &val);
14650 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000014651 dev_err(&tp->pdev->dev,
14652 "%s: Buffer corrupted on device! "
14653 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014654 /* ret = -ENODEV here? */
14655 }
14656 p[i] = 0;
14657 }
14658#endif
14659 /* Now read it back. */
14660 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
14661 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014662 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
14663 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014664 break;
14665 }
14666
14667 /* Verify it. */
14668 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14669 if (p[i] == i)
14670 continue;
14671
David S. Miller59e6b432005-05-18 22:50:10 -070014672 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14673 DMA_RWCTRL_WRITE_BNDRY_16) {
14674 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014675 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
14676 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14677 break;
14678 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000014679 dev_err(&tp->pdev->dev,
14680 "%s: Buffer corrupted on read back! "
14681 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014682 ret = -ENODEV;
14683 goto out;
14684 }
14685 }
14686
14687 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
14688 /* Success. */
14689 ret = 0;
14690 break;
14691 }
14692 }
David S. Miller59e6b432005-05-18 22:50:10 -070014693 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14694 DMA_RWCTRL_WRITE_BNDRY_16) {
14695 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070014696 * now look for chipsets that are known to expose the
14697 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070014698 */
Matt Carlson41434702011-03-09 16:58:22 +000014699 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014700 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14701 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000014702 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014703 /* Safe to use the calculated DMA boundary. */
14704 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000014705 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070014706
David S. Miller59e6b432005-05-18 22:50:10 -070014707 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014709
14710out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000014711 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014712out_nofree:
14713 return ret;
14714}
14715
Linus Torvalds1da177e2005-04-16 15:20:36 -070014716static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
14717{
Joe Perches63c3a662011-04-26 08:12:10 +000014718 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000014719 tp->bufmgr_config.mbuf_read_dma_low_water =
14720 DEFAULT_MB_RDMA_LOW_WATER_5705;
14721 tp->bufmgr_config.mbuf_mac_rx_low_water =
14722 DEFAULT_MB_MACRX_LOW_WATER_57765;
14723 tp->bufmgr_config.mbuf_high_water =
14724 DEFAULT_MB_HIGH_WATER_57765;
14725
14726 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14727 DEFAULT_MB_RDMA_LOW_WATER_5705;
14728 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14729 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
14730 tp->bufmgr_config.mbuf_high_water_jumbo =
14731 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000014732 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070014733 tp->bufmgr_config.mbuf_read_dma_low_water =
14734 DEFAULT_MB_RDMA_LOW_WATER_5705;
14735 tp->bufmgr_config.mbuf_mac_rx_low_water =
14736 DEFAULT_MB_MACRX_LOW_WATER_5705;
14737 tp->bufmgr_config.mbuf_high_water =
14738 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070014739 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14740 tp->bufmgr_config.mbuf_mac_rx_low_water =
14741 DEFAULT_MB_MACRX_LOW_WATER_5906;
14742 tp->bufmgr_config.mbuf_high_water =
14743 DEFAULT_MB_HIGH_WATER_5906;
14744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014745
Michael Chanfdfec1722005-07-25 12:31:48 -070014746 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14747 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
14748 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14749 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
14750 tp->bufmgr_config.mbuf_high_water_jumbo =
14751 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
14752 } else {
14753 tp->bufmgr_config.mbuf_read_dma_low_water =
14754 DEFAULT_MB_RDMA_LOW_WATER;
14755 tp->bufmgr_config.mbuf_mac_rx_low_water =
14756 DEFAULT_MB_MACRX_LOW_WATER;
14757 tp->bufmgr_config.mbuf_high_water =
14758 DEFAULT_MB_HIGH_WATER;
14759
14760 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14761 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
14762 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14763 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
14764 tp->bufmgr_config.mbuf_high_water_jumbo =
14765 DEFAULT_MB_HIGH_WATER_JUMBO;
14766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014767
14768 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
14769 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
14770}
14771
14772static char * __devinit tg3_phy_string(struct tg3 *tp)
14773{
Matt Carlson79eb6902010-02-17 15:17:03 +000014774 switch (tp->phy_id & TG3_PHY_ID_MASK) {
14775 case TG3_PHY_ID_BCM5400: return "5400";
14776 case TG3_PHY_ID_BCM5401: return "5401";
14777 case TG3_PHY_ID_BCM5411: return "5411";
14778 case TG3_PHY_ID_BCM5701: return "5701";
14779 case TG3_PHY_ID_BCM5703: return "5703";
14780 case TG3_PHY_ID_BCM5704: return "5704";
14781 case TG3_PHY_ID_BCM5705: return "5705";
14782 case TG3_PHY_ID_BCM5750: return "5750";
14783 case TG3_PHY_ID_BCM5752: return "5752";
14784 case TG3_PHY_ID_BCM5714: return "5714";
14785 case TG3_PHY_ID_BCM5780: return "5780";
14786 case TG3_PHY_ID_BCM5755: return "5755";
14787 case TG3_PHY_ID_BCM5787: return "5787";
14788 case TG3_PHY_ID_BCM5784: return "5784";
14789 case TG3_PHY_ID_BCM5756: return "5722/5756";
14790 case TG3_PHY_ID_BCM5906: return "5906";
14791 case TG3_PHY_ID_BCM5761: return "5761";
14792 case TG3_PHY_ID_BCM5718C: return "5718C";
14793 case TG3_PHY_ID_BCM5718S: return "5718S";
14794 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000014795 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000014796 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000014797 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070014798 case 0: return "serdes";
14799 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070014800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014801}
14802
Michael Chanf9804dd2005-09-27 12:13:10 -070014803static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
14804{
Joe Perches63c3a662011-04-26 08:12:10 +000014805 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070014806 strcpy(str, "PCI Express");
14807 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000014808 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070014809 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
14810
14811 strcpy(str, "PCIX:");
14812
14813 if ((clock_ctrl == 7) ||
14814 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
14815 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
14816 strcat(str, "133MHz");
14817 else if (clock_ctrl == 0)
14818 strcat(str, "33MHz");
14819 else if (clock_ctrl == 2)
14820 strcat(str, "50MHz");
14821 else if (clock_ctrl == 4)
14822 strcat(str, "66MHz");
14823 else if (clock_ctrl == 6)
14824 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070014825 } else {
14826 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000014827 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070014828 strcat(str, "66MHz");
14829 else
14830 strcat(str, "33MHz");
14831 }
Joe Perches63c3a662011-04-26 08:12:10 +000014832 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070014833 strcat(str, ":32-bit");
14834 else
14835 strcat(str, ":64-bit");
14836 return str;
14837}
14838
Michael Chan8c2dc7e2005-12-19 16:26:02 -080014839static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014840{
14841 struct pci_dev *peer;
14842 unsigned int func, devnr = tp->pdev->devfn & ~7;
14843
14844 for (func = 0; func < 8; func++) {
14845 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14846 if (peer && peer != tp->pdev)
14847 break;
14848 pci_dev_put(peer);
14849 }
Michael Chan16fe9d72005-12-13 21:09:54 -080014850 /* 5704 can be configured in single-port mode, set peer to
14851 * tp->pdev in that case.
14852 */
14853 if (!peer) {
14854 peer = tp->pdev;
14855 return peer;
14856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014857
14858 /*
14859 * We don't need to keep the refcount elevated; there's no way
14860 * to remove one half of this device without removing the other
14861 */
14862 pci_dev_put(peer);
14863
14864 return peer;
14865}
14866
David S. Miller15f98502005-05-18 22:49:26 -070014867static void __devinit tg3_init_coal(struct tg3 *tp)
14868{
14869 struct ethtool_coalesce *ec = &tp->coal;
14870
14871 memset(ec, 0, sizeof(*ec));
14872 ec->cmd = ETHTOOL_GCOALESCE;
14873 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
14874 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
14875 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
14876 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
14877 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
14878 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
14879 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
14880 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
14881 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
14882
14883 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
14884 HOSTCC_MODE_CLRTICK_TXBD)) {
14885 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
14886 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
14887 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
14888 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
14889 }
Michael Chand244c892005-07-05 14:42:33 -070014890
Joe Perches63c3a662011-04-26 08:12:10 +000014891 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070014892 ec->rx_coalesce_usecs_irq = 0;
14893 ec->tx_coalesce_usecs_irq = 0;
14894 ec->stats_block_coalesce_usecs = 0;
14895 }
David S. Miller15f98502005-05-18 22:49:26 -070014896}
14897
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014898static const struct net_device_ops tg3_netdev_ops = {
14899 .ndo_open = tg3_open,
14900 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080014901 .ndo_start_xmit = tg3_start_xmit,
Eric Dumazet511d2222010-07-07 20:44:24 +000014902 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -080014903 .ndo_validate_addr = eth_validate_addr,
14904 .ndo_set_multicast_list = tg3_set_rx_mode,
14905 .ndo_set_mac_address = tg3_set_mac_addr,
14906 .ndo_do_ioctl = tg3_ioctl,
14907 .ndo_tx_timeout = tg3_tx_timeout,
14908 .ndo_change_mtu = tg3_change_mtu,
Michał Mirosławdc668912011-04-07 03:35:07 +000014909 .ndo_fix_features = tg3_fix_features,
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000014910 .ndo_set_features = tg3_set_features,
Stephen Hemminger00829822008-11-20 20:14:53 -080014911#ifdef CONFIG_NET_POLL_CONTROLLER
14912 .ndo_poll_controller = tg3_poll_controller,
14913#endif
14914};
14915
Linus Torvalds1da177e2005-04-16 15:20:36 -070014916static int __devinit tg3_init_one(struct pci_dev *pdev,
14917 const struct pci_device_id *ent)
14918{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014919 struct net_device *dev;
14920 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000014921 int i, err, pm_cap;
14922 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070014923 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080014924 u64 dma_mask, persist_dma_mask;
Michał Mirosławdc668912011-04-07 03:35:07 +000014925 u32 hw_features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014926
Joe Perches05dbe002010-02-17 19:44:19 +000014927 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014928
14929 err = pci_enable_device(pdev);
14930 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014931 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014932 return err;
14933 }
14934
Linus Torvalds1da177e2005-04-16 15:20:36 -070014935 err = pci_request_regions(pdev, DRV_MODULE_NAME);
14936 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014937 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014938 goto err_out_disable_pdev;
14939 }
14940
14941 pci_set_master(pdev);
14942
14943 /* Find power-management capability. */
14944 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
14945 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000014946 dev_err(&pdev->dev,
14947 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014948 err = -EIO;
14949 goto err_out_free_res;
14950 }
14951
Matt Carlsonfe5f5782009-09-01 13:09:39 +000014952 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014953 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000014954 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014955 err = -ENOMEM;
14956 goto err_out_free_res;
14957 }
14958
Linus Torvalds1da177e2005-04-16 15:20:36 -070014959 SET_NETDEV_DEV(dev, &pdev->dev);
14960
Linus Torvalds1da177e2005-04-16 15:20:36 -070014961 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014962
14963 tp = netdev_priv(dev);
14964 tp->pdev = pdev;
14965 tp->dev = dev;
14966 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014967 tp->rx_mode = TG3_DEF_RX_MODE;
14968 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070014969
Linus Torvalds1da177e2005-04-16 15:20:36 -070014970 if (tg3_debug > 0)
14971 tp->msg_enable = tg3_debug;
14972 else
14973 tp->msg_enable = TG3_DEF_MSG_ENABLE;
14974
14975 /* The word/byte swap controls here control register access byte
14976 * swapping. DMA data byte swapping is controlled in the GRC_MODE
14977 * setting below.
14978 */
14979 tp->misc_host_ctrl =
14980 MISC_HOST_CTRL_MASK_PCI_INT |
14981 MISC_HOST_CTRL_WORD_SWAP |
14982 MISC_HOST_CTRL_INDIR_ACCESS |
14983 MISC_HOST_CTRL_PCISTATE_RW;
14984
14985 /* The NONFRM (non-frame) byte/word swap controls take effect
14986 * on descriptor entries, anything which isn't packet data.
14987 *
14988 * The StrongARM chips on the board (one for tx, one for rx)
14989 * are running in big-endian mode.
14990 */
14991 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
14992 GRC_MODE_WSWAP_NONFRM_DATA);
14993#ifdef __BIG_ENDIAN
14994 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
14995#endif
14996 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014997 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000014998 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014999
Matt Carlsond5fe4882008-11-21 17:20:32 -080015000 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010015001 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015002 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015003 err = -ENOMEM;
15004 goto err_out_free_dev;
15005 }
15006
Linus Torvalds1da177e2005-04-16 15:20:36 -070015007 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
15008 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015009
Linus Torvalds1da177e2005-04-16 15:20:36 -070015010 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015011 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000015012 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015013 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015014
15015 err = tg3_get_invariants(tp);
15016 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015017 dev_err(&pdev->dev,
15018 "Problem fetching invariants of chip, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015019 goto err_out_iounmap;
15020 }
15021
Michael Chan4a29cc22006-03-19 13:21:12 -080015022 /* The EPB bridge inside 5714, 5715, and 5780 and any
15023 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080015024 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
15025 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
15026 * do DMA address check in tg3_start_xmit().
15027 */
Joe Perches63c3a662011-04-26 08:12:10 +000015028 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070015029 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000015030 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070015031 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080015032#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070015033 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015034#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080015035 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070015036 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015037
15038 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070015039 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080015040 err = pci_set_dma_mask(pdev, dma_mask);
15041 if (!err) {
15042 dev->features |= NETIF_F_HIGHDMA;
15043 err = pci_set_consistent_dma_mask(pdev,
15044 persist_dma_mask);
15045 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015046 dev_err(&pdev->dev, "Unable to obtain 64 bit "
15047 "DMA for consistent allocations\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080015048 goto err_out_iounmap;
15049 }
15050 }
15051 }
Yang Hongyang284901a2009-04-06 19:01:15 -070015052 if (err || dma_mask == DMA_BIT_MASK(32)) {
15053 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080015054 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015055 dev_err(&pdev->dev,
15056 "No usable DMA configuration, aborting\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080015057 goto err_out_iounmap;
15058 }
15059 }
15060
Michael Chanfdfec1722005-07-25 12:31:48 -070015061 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015062
Michael Chan4e3a7aa2006-03-20 17:47:44 -080015063 /* TSO is on by default on chips that support hardware TSO.
15064 * Firmware TSO on older chips gives lower performance, so it
15065 * is off by default, but can be enabled using ethtool.
15066 */
Joe Perches63c3a662011-04-26 08:12:10 +000015067 if ((tg3_flag(tp, HW_TSO_1) ||
15068 tg3_flag(tp, HW_TSO_2) ||
15069 tg3_flag(tp, HW_TSO_3)) &&
Michał Mirosławdc668912011-04-07 03:35:07 +000015070 (dev->features & NETIF_F_IP_CSUM))
15071 hw_features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000015072 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Michał Mirosławdc668912011-04-07 03:35:07 +000015073 if (dev->features & NETIF_F_IPV6_CSUM)
15074 hw_features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000015075 if (tg3_flag(tp, HW_TSO_3) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000015076 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070015077 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15078 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Joe Perches63c3a662011-04-26 08:12:10 +000015079 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000015080 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
15081 hw_features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070015082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015083
Michał Mirosławdc668912011-04-07 03:35:07 +000015084 dev->hw_features |= hw_features;
15085 dev->features |= hw_features;
15086 dev->vlan_features |= hw_features;
15087
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015088 /*
15089 * Add loopback capability only for a subset of devices that support
15090 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
15091 * loopback for the remaining devices.
15092 */
15093 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
15094 !tg3_flag(tp, CPMU_PRESENT))
15095 /* Add the loopback capability */
15096 dev->hw_features |= NETIF_F_LOOPBACK;
15097
Linus Torvalds1da177e2005-04-16 15:20:36 -070015098 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015099 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015100 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015101 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015102 tp->rx_pending = 63;
15103 }
15104
Linus Torvalds1da177e2005-04-16 15:20:36 -070015105 err = tg3_get_device_address(tp);
15106 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015107 dev_err(&pdev->dev,
15108 "Could not obtain valid ethernet address, aborting\n");
Matt Carlson026a6c22009-12-03 08:36:24 +000015109 goto err_out_iounmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015110 }
15111
Joe Perches63c3a662011-04-26 08:12:10 +000015112 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson63532392008-11-03 16:49:57 -080015113 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
Al Viro79ea13c2008-01-24 02:06:46 -080015114 if (!tp->aperegs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015115 dev_err(&pdev->dev,
15116 "Cannot map APE registers, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015117 err = -ENOMEM;
Matt Carlson026a6c22009-12-03 08:36:24 +000015118 goto err_out_iounmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015119 }
15120
15121 tg3_ape_lock_init(tp);
Matt Carlson7fd76442009-02-25 14:27:20 +000015122
Joe Perches63c3a662011-04-26 08:12:10 +000015123 if (tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000015124 tg3_read_dash_ver(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015125 }
15126
Matt Carlsonc88864d2007-11-12 21:07:01 -080015127 /*
15128 * Reset chip in case UNDI or EFI driver did not shutdown
15129 * DMA self test will enable WDMAC and we'll see (spurious)
15130 * pending DMA on the PCI bus at that point.
15131 */
15132 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15133 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15134 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15135 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15136 }
15137
15138 err = tg3_test_dma(tp);
15139 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015140 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080015141 goto err_out_apeunmap;
15142 }
15143
Matt Carlson78f90dc2009-11-13 13:03:42 +000015144 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15145 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15146 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000015147 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000015148 struct tg3_napi *tnapi = &tp->napi[i];
15149
15150 tnapi->tp = tp;
15151 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15152
15153 tnapi->int_mbox = intmbx;
15154 if (i < 4)
15155 intmbx += 0x8;
15156 else
15157 intmbx += 0x4;
15158
15159 tnapi->consmbox = rcvmbx;
15160 tnapi->prodmbox = sndmbx;
15161
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015162 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015163 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015164 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000015165 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000015166
Joe Perches63c3a662011-04-26 08:12:10 +000015167 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000015168 break;
15169
15170 /*
15171 * If we support MSIX, we'll be using RSS. If we're using
15172 * RSS, the first vector only handles link interrupts and the
15173 * remaining vectors handle rx and tx interrupts. Reuse the
15174 * mailbox values for the next iteration. The values we setup
15175 * above are still useful for the single vectored mode.
15176 */
15177 if (!i)
15178 continue;
15179
15180 rcvmbx += 0x8;
15181
15182 if (sndmbx & 0x4)
15183 sndmbx -= 0x4;
15184 else
15185 sndmbx += 0xc;
15186 }
15187
Matt Carlsonc88864d2007-11-12 21:07:01 -080015188 tg3_init_coal(tp);
15189
Michael Chanc49a1562006-12-17 17:07:29 -080015190 pci_set_drvdata(pdev, dev);
15191
Linus Torvalds1da177e2005-04-16 15:20:36 -070015192 err = register_netdev(dev);
15193 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015194 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015195 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015196 }
15197
Joe Perches05dbe002010-02-17 19:44:19 +000015198 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15199 tp->board_part_number,
15200 tp->pci_chip_rev_id,
15201 tg3_bus_string(tp, str),
15202 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015203
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015204 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000015205 struct phy_device *phydev;
15206 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000015207 netdev_info(dev,
15208 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015209 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015210 } else {
15211 char *ethtype;
15212
15213 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15214 ethtype = "10/100Base-TX";
15215 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15216 ethtype = "1000Base-SX";
15217 else
15218 ethtype = "10/100/1000Base-T";
15219
Matt Carlson5129c3a2010-04-05 10:19:23 +000015220 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000015221 "(WireSpeed[%d], EEE[%d])\n",
15222 tg3_phy_string(tp), ethtype,
15223 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
15224 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015225 }
Matt Carlsondf59c942008-11-03 16:52:56 -080015226
Joe Perches05dbe002010-02-17 19:44:19 +000015227 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000015228 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015229 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015230 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015231 tg3_flag(tp, ENABLE_ASF) != 0,
15232 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000015233 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15234 tp->dma_rwctrl,
15235 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15236 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015237
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015238 pci_save_state(pdev);
15239
Linus Torvalds1da177e2005-04-16 15:20:36 -070015240 return 0;
15241
Matt Carlson0d3031d2007-10-10 18:02:43 -070015242err_out_apeunmap:
15243 if (tp->aperegs) {
15244 iounmap(tp->aperegs);
15245 tp->aperegs = NULL;
15246 }
15247
Linus Torvalds1da177e2005-04-16 15:20:36 -070015248err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070015249 if (tp->regs) {
15250 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015251 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015253
15254err_out_free_dev:
15255 free_netdev(dev);
15256
15257err_out_free_res:
15258 pci_release_regions(pdev);
15259
15260err_out_disable_pdev:
15261 pci_disable_device(pdev);
15262 pci_set_drvdata(pdev, NULL);
15263 return err;
15264}
15265
15266static void __devexit tg3_remove_one(struct pci_dev *pdev)
15267{
15268 struct net_device *dev = pci_get_drvdata(pdev);
15269
15270 if (dev) {
15271 struct tg3 *tp = netdev_priv(dev);
15272
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015273 if (tp->fw)
15274 release_firmware(tp->fw);
15275
Tejun Heo23f333a2010-12-12 16:45:14 +010015276 cancel_work_sync(&tp->reset_task);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015277
Joe Perches63c3a662011-04-26 08:12:10 +000015278 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015279 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015280 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015281 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015282
Linus Torvalds1da177e2005-04-16 15:20:36 -070015283 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015284 if (tp->aperegs) {
15285 iounmap(tp->aperegs);
15286 tp->aperegs = NULL;
15287 }
Michael Chan68929142005-08-09 20:17:14 -070015288 if (tp->regs) {
15289 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015290 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015292 free_netdev(dev);
15293 pci_release_regions(pdev);
15294 pci_disable_device(pdev);
15295 pci_set_drvdata(pdev, NULL);
15296 }
15297}
15298
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015299#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015300static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015301{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015302 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015303 struct net_device *dev = pci_get_drvdata(pdev);
15304 struct tg3 *tp = netdev_priv(dev);
15305 int err;
15306
15307 if (!netif_running(dev))
15308 return 0;
15309
Tejun Heo23f333a2010-12-12 16:45:14 +010015310 flush_work_sync(&tp->reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015311 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015312 tg3_netif_stop(tp);
15313
15314 del_timer_sync(&tp->timer);
15315
David S. Millerf47c11e2005-06-24 20:18:35 -070015316 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015317 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015318 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015319
15320 netif_device_detach(dev);
15321
David S. Millerf47c11e2005-06-24 20:18:35 -070015322 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015323 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000015324 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070015325 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015326
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015327 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015328 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015329 int err2;
15330
David S. Millerf47c11e2005-06-24 20:18:35 -070015331 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015332
Joe Perches63c3a662011-04-26 08:12:10 +000015333 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015334 err2 = tg3_restart_hw(tp, 1);
15335 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015336 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015337
15338 tp->timer.expires = jiffies + tp->timer_offset;
15339 add_timer(&tp->timer);
15340
15341 netif_device_attach(dev);
15342 tg3_netif_start(tp);
15343
Michael Chanb9ec6c12006-07-25 16:37:27 -070015344out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015345 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015346
15347 if (!err2)
15348 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015349 }
15350
15351 return err;
15352}
15353
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015354static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015355{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015356 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015357 struct net_device *dev = pci_get_drvdata(pdev);
15358 struct tg3 *tp = netdev_priv(dev);
15359 int err;
15360
15361 if (!netif_running(dev))
15362 return 0;
15363
Linus Torvalds1da177e2005-04-16 15:20:36 -070015364 netif_device_attach(dev);
15365
David S. Millerf47c11e2005-06-24 20:18:35 -070015366 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015367
Joe Perches63c3a662011-04-26 08:12:10 +000015368 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070015369 err = tg3_restart_hw(tp, 1);
15370 if (err)
15371 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015372
15373 tp->timer.expires = jiffies + tp->timer_offset;
15374 add_timer(&tp->timer);
15375
Linus Torvalds1da177e2005-04-16 15:20:36 -070015376 tg3_netif_start(tp);
15377
Michael Chanb9ec6c12006-07-25 16:37:27 -070015378out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015379 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015380
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015381 if (!err)
15382 tg3_phy_start(tp);
15383
Michael Chanb9ec6c12006-07-25 16:37:27 -070015384 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015385}
15386
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015387static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015388#define TG3_PM_OPS (&tg3_pm_ops)
15389
15390#else
15391
15392#define TG3_PM_OPS NULL
15393
15394#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015395
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015396/**
15397 * tg3_io_error_detected - called when PCI error is detected
15398 * @pdev: Pointer to PCI device
15399 * @state: The current pci connection state
15400 *
15401 * This function is called after a PCI bus error affecting
15402 * this device has been detected.
15403 */
15404static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
15405 pci_channel_state_t state)
15406{
15407 struct net_device *netdev = pci_get_drvdata(pdev);
15408 struct tg3 *tp = netdev_priv(netdev);
15409 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
15410
15411 netdev_info(netdev, "PCI I/O error detected\n");
15412
15413 rtnl_lock();
15414
15415 if (!netif_running(netdev))
15416 goto done;
15417
15418 tg3_phy_stop(tp);
15419
15420 tg3_netif_stop(tp);
15421
15422 del_timer_sync(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +000015423 tg3_flag_clear(tp, RESTART_TIMER);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015424
15425 /* Want to make sure that the reset task doesn't run */
15426 cancel_work_sync(&tp->reset_task);
Joe Perches63c3a662011-04-26 08:12:10 +000015427 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
15428 tg3_flag_clear(tp, RESTART_TIMER);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015429
15430 netif_device_detach(netdev);
15431
15432 /* Clean up software state, even if MMIO is blocked */
15433 tg3_full_lock(tp, 0);
15434 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
15435 tg3_full_unlock(tp);
15436
15437done:
15438 if (state == pci_channel_io_perm_failure)
15439 err = PCI_ERS_RESULT_DISCONNECT;
15440 else
15441 pci_disable_device(pdev);
15442
15443 rtnl_unlock();
15444
15445 return err;
15446}
15447
15448/**
15449 * tg3_io_slot_reset - called after the pci bus has been reset.
15450 * @pdev: Pointer to PCI device
15451 *
15452 * Restart the card from scratch, as if from a cold-boot.
15453 * At this point, the card has exprienced a hard reset,
15454 * followed by fixups by BIOS, and has its config space
15455 * set up identically to what it was at cold boot.
15456 */
15457static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
15458{
15459 struct net_device *netdev = pci_get_drvdata(pdev);
15460 struct tg3 *tp = netdev_priv(netdev);
15461 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
15462 int err;
15463
15464 rtnl_lock();
15465
15466 if (pci_enable_device(pdev)) {
15467 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
15468 goto done;
15469 }
15470
15471 pci_set_master(pdev);
15472 pci_restore_state(pdev);
15473 pci_save_state(pdev);
15474
15475 if (!netif_running(netdev)) {
15476 rc = PCI_ERS_RESULT_RECOVERED;
15477 goto done;
15478 }
15479
15480 err = tg3_power_up(tp);
15481 if (err) {
15482 netdev_err(netdev, "Failed to restore register access.\n");
15483 goto done;
15484 }
15485
15486 rc = PCI_ERS_RESULT_RECOVERED;
15487
15488done:
15489 rtnl_unlock();
15490
15491 return rc;
15492}
15493
15494/**
15495 * tg3_io_resume - called when traffic can start flowing again.
15496 * @pdev: Pointer to PCI device
15497 *
15498 * This callback is called when the error recovery driver tells
15499 * us that its OK to resume normal operation.
15500 */
15501static void tg3_io_resume(struct pci_dev *pdev)
15502{
15503 struct net_device *netdev = pci_get_drvdata(pdev);
15504 struct tg3 *tp = netdev_priv(netdev);
15505 int err;
15506
15507 rtnl_lock();
15508
15509 if (!netif_running(netdev))
15510 goto done;
15511
15512 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000015513 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015514 err = tg3_restart_hw(tp, 1);
15515 tg3_full_unlock(tp);
15516 if (err) {
15517 netdev_err(netdev, "Cannot restart hardware after reset.\n");
15518 goto done;
15519 }
15520
15521 netif_device_attach(netdev);
15522
15523 tp->timer.expires = jiffies + tp->timer_offset;
15524 add_timer(&tp->timer);
15525
15526 tg3_netif_start(tp);
15527
15528 tg3_phy_start(tp);
15529
15530done:
15531 rtnl_unlock();
15532}
15533
15534static struct pci_error_handlers tg3_err_handler = {
15535 .error_detected = tg3_io_error_detected,
15536 .slot_reset = tg3_io_slot_reset,
15537 .resume = tg3_io_resume
15538};
15539
Linus Torvalds1da177e2005-04-16 15:20:36 -070015540static struct pci_driver tg3_driver = {
15541 .name = DRV_MODULE_NAME,
15542 .id_table = tg3_pci_tbl,
15543 .probe = tg3_init_one,
15544 .remove = __devexit_p(tg3_remove_one),
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015545 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015546 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015547};
15548
15549static int __init tg3_init(void)
15550{
Jeff Garzik29917622006-08-19 17:48:59 -040015551 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015552}
15553
15554static void __exit tg3_cleanup(void)
15555{
15556 pci_unregister_driver(&tg3_driver);
15557}
15558
15559module_init(tg3_init);
15560module_exit(tg3_cleanup);