blob: 38f68594f76b0261212ff646cc4f816bc9d6905a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * tg3.c: Broadcom Tigon3 ethernet driver.
3 *
4 * Copyright (C) 2001, 2002, 2003, 2004 David S. Miller (davem@redhat.com)
5 * Copyright (C) 2001, 2002, 2003 Jeff Garzik (jgarzik@pobox.com)
6 * Copyright (C) 2004 Sun Microsystems Inc.
Matt Carlsonb86fb2c2011-01-25 15:58:57 +00007 * Copyright (C) 2005-2011 Broadcom Corporation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Firmware is:
Michael Chan49cabf42005-06-06 15:15:17 -070010 * Derived from proprietary unpublished source code,
11 * Copyright (C) 2000-2003 Broadcom Corporation.
12 *
13 * Permission is hereby granted for the distribution of this firmware
14 * data in hexadecimal or equivalent format, provided this copyright
15 * notice is accompanying it.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <linux/module.h>
20#include <linux/moduleparam.h>
Matt Carlson6867c842010-07-11 09:31:44 +000021#include <linux/stringify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/kernel.h>
23#include <linux/types.h>
24#include <linux/compiler.h>
25#include <linux/slab.h>
26#include <linux/delay.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020027#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/init.h>
29#include <linux/ioport.h>
30#include <linux/pci.h>
31#include <linux/netdevice.h>
32#include <linux/etherdevice.h>
33#include <linux/skbuff.h>
34#include <linux/ethtool.h>
Matt Carlson3110f5f52010-12-06 08:28:50 +000035#include <linux/mdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/mii.h>
Matt Carlson158d7ab2008-05-29 01:37:54 -070037#include <linux/phy.h>
Matt Carlsona9daf362008-05-25 23:49:44 -070038#include <linux/brcmphy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/if_vlan.h>
40#include <linux/ip.h>
41#include <linux/tcp.h>
42#include <linux/workqueue.h>
Michael Chan61487482005-09-05 17:53:19 -070043#include <linux/prefetch.h>
Tobias Klauserf9a5f7d2005-10-29 15:09:26 +020044#include <linux/dma-mapping.h>
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080045#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#include <net/checksum.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030048#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#include <asm/system.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000051#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <asm/byteorder.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000053#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
David S. Miller49b6e95f2007-03-29 01:38:42 -070055#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <asm/idprom.h>
David S. Miller49b6e95f2007-03-29 01:38:42 -070057#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#endif
59
Matt Carlson63532392008-11-03 16:49:57 -080060#define BAR_0 0
61#define BAR_2 2
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include "tg3.h"
64
Joe Perches63c3a662011-04-26 08:12:10 +000065/* Functions & macros to verify TG3_FLAGS types */
66
67static inline int _tg3_flag(enum TG3_FLAGS flag, unsigned long *bits)
68{
69 return test_bit(flag, bits);
70}
71
72static inline void _tg3_flag_set(enum TG3_FLAGS flag, unsigned long *bits)
73{
74 set_bit(flag, bits);
75}
76
77static inline void _tg3_flag_clear(enum TG3_FLAGS flag, unsigned long *bits)
78{
79 clear_bit(flag, bits);
80}
81
82#define tg3_flag(tp, flag) \
83 _tg3_flag(TG3_FLAG_##flag, (tp)->tg3_flags)
84#define tg3_flag_set(tp, flag) \
85 _tg3_flag_set(TG3_FLAG_##flag, (tp)->tg3_flags)
86#define tg3_flag_clear(tp, flag) \
87 _tg3_flag_clear(TG3_FLAG_##flag, (tp)->tg3_flags)
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#define DRV_MODULE_NAME "tg3"
Matt Carlson6867c842010-07-11 09:31:44 +000090#define TG3_MAJ_NUM 3
Matt Carlson43a5f002011-05-19 12:12:56 +000091#define TG3_MIN_NUM 119
Matt Carlson6867c842010-07-11 09:31:44 +000092#define DRV_MODULE_VERSION \
93 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Matt Carlson43a5f002011-05-19 12:12:56 +000094#define DRV_MODULE_RELDATE "May 18, 2011"
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96#define TG3_DEF_MAC_MODE 0
97#define TG3_DEF_RX_MODE 0
98#define TG3_DEF_TX_MODE 0
99#define TG3_DEF_MSG_ENABLE \
100 (NETIF_MSG_DRV | \
101 NETIF_MSG_PROBE | \
102 NETIF_MSG_LINK | \
103 NETIF_MSG_TIMER | \
104 NETIF_MSG_IFDOWN | \
105 NETIF_MSG_IFUP | \
106 NETIF_MSG_RX_ERR | \
107 NETIF_MSG_TX_ERR)
108
109/* length of time before we decide the hardware is borked,
110 * and dev->tx_timeout() should be called to fix the problem
111 */
Joe Perches63c3a662011-04-26 08:12:10 +0000112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#define TG3_TX_TIMEOUT (5 * HZ)
114
115/* hardware minimum and maximum for a single frame's data payload */
116#define TG3_MIN_MTU 60
117#define TG3_MAX_MTU(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000118 (tg3_flag(tp, JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120/* These numbers seem to be hard coded in the NIC firmware somehow.
121 * You can't change the ring sizes, but you can change where you place
122 * them in the NIC onboard memory.
123 */
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000124#define TG3_RX_STD_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000125 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000126 TG3_RX_STD_MAX_SIZE_5717 : TG3_RX_STD_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#define TG3_DEF_RX_RING_PENDING 200
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000128#define TG3_RX_JMB_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000129 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000130 TG3_RX_JMB_MAX_SIZE_5717 : TG3_RX_JMB_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131#define TG3_DEF_RX_JUMBO_RING_PENDING 100
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000132#define TG3_RSS_INDIR_TBL_SIZE 128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134/* Do not place this n-ring entries value into the tp struct itself,
135 * we really want to expose these constants to GCC so that modulo et
136 * al. operations are done with shifts and masks instead of with
137 * hw multiply/modulo instructions. Another solution would be to
138 * replace things like '% foo' with '& (foo - 1)'.
139 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141#define TG3_TX_RING_SIZE 512
142#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
143
Matt Carlson2c49a442010-09-30 10:34:35 +0000144#define TG3_RX_STD_RING_BYTES(tp) \
145 (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
146#define TG3_RX_JMB_RING_BYTES(tp) \
147 (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
148#define TG3_RX_RCB_RING_BYTES(tp) \
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000149 (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
151 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
153
Matt Carlson287be122009-08-28 13:58:46 +0000154#define TG3_DMA_BYTE_ENAB 64
155
156#define TG3_RX_STD_DMA_SZ 1536
157#define TG3_RX_JMB_DMA_SZ 9046
158
159#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
160
161#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
162#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Matt Carlson2c49a442010-09-30 10:34:35 +0000164#define TG3_RX_STD_BUFF_RING_SIZE(tp) \
165 (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000166
Matt Carlson2c49a442010-09-30 10:34:35 +0000167#define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
168 (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000169
Matt Carlsond2757fc2010-04-12 06:58:27 +0000170/* Due to a hardware bug, the 5701 can only DMA to memory addresses
171 * that are at least dword aligned when used in PCIX mode. The driver
172 * works around this bug by double copying the packet. This workaround
173 * is built into the normal double copy length check for efficiency.
174 *
175 * However, the double copy is only necessary on those architectures
176 * where unaligned memory accesses are inefficient. For those architectures
177 * where unaligned memory accesses incur little penalty, we can reintegrate
178 * the 5701 in the normal rx path. Doing so saves a device structure
179 * dereference by hardcoding the double copy threshold in place.
180 */
181#define TG3_RX_COPY_THRESHOLD 256
182#if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
183 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
184#else
185 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
186#endif
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000189#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Matt Carlsonad829262008-11-21 17:16:16 -0800191#define TG3_RAW_IP_ALIGN 2
192
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000193#define TG3_FW_UPDATE_TIMEOUT_SEC 5
194
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800195#define FIRMWARE_TG3 "tigon/tg3.bin"
196#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
197#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199static char version[] __devinitdata =
Joe Perches05dbe002010-02-17 19:44:19 +0000200 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
203MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
204MODULE_LICENSE("GPL");
205MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800206MODULE_FIRMWARE(FIRMWARE_TG3);
207MODULE_FIRMWARE(FIRMWARE_TG3TSO);
208MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
211module_param(tg3_debug, int, 0);
212MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
213
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000214static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700215 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
216 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
217 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
218 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
219 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
220 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
221 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
222 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
223 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
224 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
225 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
226 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
227 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
228 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
229 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
230 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
231 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
232 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
233 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901)},
234 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2)},
235 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
236 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700237 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700238 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700239 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700240 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F)},
242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F)},
247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Michael Chan676917d2006-12-07 00:20:22 -0800255 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700256 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
257 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
258 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
259 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
260 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
261 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
262 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700263 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
264 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700265 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
266 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700267 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700268 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
269 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800270 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
271 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000272 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
273 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Matt Carlson321d32a2008-11-21 17:22:19 -0800274 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
275 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
276 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790)},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000277 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000278 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
279 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000280 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
281 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
282 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
283 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
284 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791)},
285 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795)},
Matt Carlson302b5002010-06-05 17:24:38 +0000286 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Matt Carlsonba1f3c72011-04-05 14:22:50 +0000287 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700288 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
289 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
290 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
291 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
292 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
293 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
294 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
Meelis Roos1dcb14d2011-05-25 05:43:47 +0000295 {PCI_DEVICE(0x10cf, 0x11a2)}, /* Fujitsu 1000base-SX with BCM5703SKHB */
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700296 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297};
298
299MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
300
Andreas Mohr50da8592006-08-14 23:54:30 -0700301static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000303} ethtool_stats_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 { "rx_octets" },
305 { "rx_fragments" },
306 { "rx_ucast_packets" },
307 { "rx_mcast_packets" },
308 { "rx_bcast_packets" },
309 { "rx_fcs_errors" },
310 { "rx_align_errors" },
311 { "rx_xon_pause_rcvd" },
312 { "rx_xoff_pause_rcvd" },
313 { "rx_mac_ctrl_rcvd" },
314 { "rx_xoff_entered" },
315 { "rx_frame_too_long_errors" },
316 { "rx_jabbers" },
317 { "rx_undersize_packets" },
318 { "rx_in_length_errors" },
319 { "rx_out_length_errors" },
320 { "rx_64_or_less_octet_packets" },
321 { "rx_65_to_127_octet_packets" },
322 { "rx_128_to_255_octet_packets" },
323 { "rx_256_to_511_octet_packets" },
324 { "rx_512_to_1023_octet_packets" },
325 { "rx_1024_to_1522_octet_packets" },
326 { "rx_1523_to_2047_octet_packets" },
327 { "rx_2048_to_4095_octet_packets" },
328 { "rx_4096_to_8191_octet_packets" },
329 { "rx_8192_to_9022_octet_packets" },
330
331 { "tx_octets" },
332 { "tx_collisions" },
333
334 { "tx_xon_sent" },
335 { "tx_xoff_sent" },
336 { "tx_flow_control" },
337 { "tx_mac_errors" },
338 { "tx_single_collisions" },
339 { "tx_mult_collisions" },
340 { "tx_deferred" },
341 { "tx_excessive_collisions" },
342 { "tx_late_collisions" },
343 { "tx_collide_2times" },
344 { "tx_collide_3times" },
345 { "tx_collide_4times" },
346 { "tx_collide_5times" },
347 { "tx_collide_6times" },
348 { "tx_collide_7times" },
349 { "tx_collide_8times" },
350 { "tx_collide_9times" },
351 { "tx_collide_10times" },
352 { "tx_collide_11times" },
353 { "tx_collide_12times" },
354 { "tx_collide_13times" },
355 { "tx_collide_14times" },
356 { "tx_collide_15times" },
357 { "tx_ucast_packets" },
358 { "tx_mcast_packets" },
359 { "tx_bcast_packets" },
360 { "tx_carrier_sense_errors" },
361 { "tx_discards" },
362 { "tx_errors" },
363
364 { "dma_writeq_full" },
365 { "dma_write_prioq_full" },
366 { "rxbds_empty" },
367 { "rx_discards" },
368 { "rx_errors" },
369 { "rx_threshold_hit" },
370
371 { "dma_readq_full" },
372 { "dma_read_prioq_full" },
373 { "tx_comp_queue_full" },
374
375 { "ring_set_send_prod_index" },
376 { "ring_status_update" },
377 { "nic_irqs" },
378 { "nic_avoided_irqs" },
Matt Carlson4452d092011-05-19 12:12:51 +0000379 { "nic_tx_threshold_hit" },
380
381 { "mbuf_lwm_thresh_hit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382};
383
Matt Carlson48fa55a2011-04-13 11:05:06 +0000384#define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
385
386
Andreas Mohr50da8592006-08-14 23:54:30 -0700387static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700388 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000389} ethtool_test_keys[] = {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700390 { "nvram test (online) " },
391 { "link test (online) " },
392 { "register test (offline)" },
393 { "memory test (offline)" },
394 { "loopback test (offline)" },
395 { "interrupt test (offline)" },
396};
397
Matt Carlson48fa55a2011-04-13 11:05:06 +0000398#define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
399
400
Michael Chanb401e9e2005-12-19 16:27:04 -0800401static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
402{
403 writel(val, tp->regs + off);
404}
405
406static u32 tg3_read32(struct tg3 *tp, u32 off)
407{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000408 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800409}
410
Matt Carlson0d3031d2007-10-10 18:02:43 -0700411static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
412{
413 writel(val, tp->aperegs + off);
414}
415
416static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
417{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000418 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700419}
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
422{
Michael Chan68929142005-08-09 20:17:14 -0700423 unsigned long flags;
424
425 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700426 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
427 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700428 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700429}
430
431static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
432{
433 writel(val, tp->regs + off);
434 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
Michael Chan68929142005-08-09 20:17:14 -0700437static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
438{
439 unsigned long flags;
440 u32 val;
441
442 spin_lock_irqsave(&tp->indirect_lock, flags);
443 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
444 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
445 spin_unlock_irqrestore(&tp->indirect_lock, flags);
446 return val;
447}
448
449static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
450{
451 unsigned long flags;
452
453 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
454 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
455 TG3_64BIT_REG_LOW, val);
456 return;
457 }
Matt Carlson66711e62009-11-13 13:03:49 +0000458 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700459 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
460 TG3_64BIT_REG_LOW, val);
461 return;
462 }
463
464 spin_lock_irqsave(&tp->indirect_lock, flags);
465 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
466 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
467 spin_unlock_irqrestore(&tp->indirect_lock, flags);
468
469 /* In indirect mode when disabling interrupts, we also need
470 * to clear the interrupt bit in the GRC local ctrl register.
471 */
472 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
473 (val == 0x1)) {
474 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
475 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
476 }
477}
478
479static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
480{
481 unsigned long flags;
482 u32 val;
483
484 spin_lock_irqsave(&tp->indirect_lock, flags);
485 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
486 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
487 spin_unlock_irqrestore(&tp->indirect_lock, flags);
488 return val;
489}
490
Michael Chanb401e9e2005-12-19 16:27:04 -0800491/* usec_wait specifies the wait time in usec when writing to certain registers
492 * where it is unsafe to read back the register without some delay.
493 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
494 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
495 */
496static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Joe Perches63c3a662011-04-26 08:12:10 +0000498 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800499 /* Non-posted methods */
500 tp->write32(tp, off, val);
501 else {
502 /* Posted method */
503 tg3_write32(tp, off, val);
504 if (usec_wait)
505 udelay(usec_wait);
506 tp->read32(tp, off);
507 }
508 /* Wait again after the read for the posted method to guarantee that
509 * the wait time is met.
510 */
511 if (usec_wait)
512 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
Michael Chan09ee9292005-08-09 20:17:00 -0700515static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
516{
517 tp->write32_mbox(tp, off, val);
Joe Perches63c3a662011-04-26 08:12:10 +0000518 if (!tg3_flag(tp, MBOX_WRITE_REORDER) && !tg3_flag(tp, ICH_WORKAROUND))
Michael Chan68929142005-08-09 20:17:14 -0700519 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700520}
521
Michael Chan20094932005-08-09 20:16:32 -0700522static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
524 void __iomem *mbox = tp->regs + off;
525 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000526 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000528 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 readl(mbox);
530}
531
Michael Chanb5d37722006-09-27 16:06:21 -0700532static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
533{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000534 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700535}
536
537static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
538{
539 writel(val, tp->regs + off + GRCMBOX_BASE);
540}
541
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000542#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700543#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000544#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
545#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
546#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700547
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000548#define tw32(reg, val) tp->write32(tp, reg, val)
549#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
550#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
551#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
554{
Michael Chan68929142005-08-09 20:17:14 -0700555 unsigned long flags;
556
Matt Carlson6ff6f812011-05-19 12:12:54 +0000557 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700558 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
559 return;
560
Michael Chan68929142005-08-09 20:17:14 -0700561 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000562 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700563 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
564 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Michael Chanbbadf502006-04-06 21:46:34 -0700566 /* Always leave this as zero. */
567 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
568 } else {
569 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
570 tw32_f(TG3PCI_MEM_WIN_DATA, val);
571
572 /* Always leave this as zero. */
573 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
574 }
Michael Chan68929142005-08-09 20:17:14 -0700575 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
578static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
579{
Michael Chan68929142005-08-09 20:17:14 -0700580 unsigned long flags;
581
Matt Carlson6ff6f812011-05-19 12:12:54 +0000582 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700583 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
584 *val = 0;
585 return;
586 }
587
Michael Chan68929142005-08-09 20:17:14 -0700588 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000589 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700590 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
591 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Michael Chanbbadf502006-04-06 21:46:34 -0700593 /* Always leave this as zero. */
594 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
595 } else {
596 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
597 *val = tr32(TG3PCI_MEM_WIN_DATA);
598
599 /* Always leave this as zero. */
600 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
601 }
Michael Chan68929142005-08-09 20:17:14 -0700602 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
Matt Carlson0d3031d2007-10-10 18:02:43 -0700605static void tg3_ape_lock_init(struct tg3 *tp)
606{
607 int i;
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000608 u32 regbase;
609
610 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
611 regbase = TG3_APE_LOCK_GRANT;
612 else
613 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700614
615 /* Make sure the driver hasn't any stale locks. */
616 for (i = 0; i < 8; i++)
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000617 tg3_ape_write32(tp, regbase + 4 * i, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700618}
619
620static int tg3_ape_lock(struct tg3 *tp, int locknum)
621{
622 int i, off;
623 int ret = 0;
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000624 u32 status, req, gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700625
Joe Perches63c3a662011-04-26 08:12:10 +0000626 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700627 return 0;
628
629 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000630 case TG3_APE_LOCK_GRC:
631 case TG3_APE_LOCK_MEM:
632 break;
633 default:
634 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700635 }
636
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000637 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
638 req = TG3_APE_LOCK_REQ;
639 gnt = TG3_APE_LOCK_GRANT;
640 } else {
641 req = TG3_APE_PER_LOCK_REQ;
642 gnt = TG3_APE_PER_LOCK_GRANT;
643 }
644
Matt Carlson0d3031d2007-10-10 18:02:43 -0700645 off = 4 * locknum;
646
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000647 tg3_ape_write32(tp, req + off, APE_LOCK_REQ_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700648
649 /* Wait for up to 1 millisecond to acquire lock. */
650 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000651 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700652 if (status == APE_LOCK_GRANT_DRIVER)
653 break;
654 udelay(10);
655 }
656
657 if (status != APE_LOCK_GRANT_DRIVER) {
658 /* Revoke the lock request. */
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000659 tg3_ape_write32(tp, gnt + off,
Matt Carlson0d3031d2007-10-10 18:02:43 -0700660 APE_LOCK_GRANT_DRIVER);
661
662 ret = -EBUSY;
663 }
664
665 return ret;
666}
667
668static void tg3_ape_unlock(struct tg3 *tp, int locknum)
669{
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000670 u32 gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700671
Joe Perches63c3a662011-04-26 08:12:10 +0000672 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700673 return;
674
675 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000676 case TG3_APE_LOCK_GRC:
677 case TG3_APE_LOCK_MEM:
678 break;
679 default:
680 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700681 }
682
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000683 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
684 gnt = TG3_APE_LOCK_GRANT;
685 else
686 gnt = TG3_APE_PER_LOCK_GRANT;
687
688 tg3_ape_write32(tp, gnt + 4 * locknum, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700689}
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691static void tg3_disable_ints(struct tg3 *tp)
692{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000693 int i;
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 tw32(TG3PCI_MISC_HOST_CTRL,
696 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000697 for (i = 0; i < tp->irq_max; i++)
698 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701static void tg3_enable_ints(struct tg3 *tp)
702{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000703 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000704
Michael Chanbbe832c2005-06-24 20:20:04 -0700705 tp->irq_sync = 0;
706 wmb();
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 tw32(TG3PCI_MISC_HOST_CTRL,
709 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000710
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000711 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000712 for (i = 0; i < tp->irq_cnt; i++) {
713 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000714
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000715 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +0000716 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000717 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
718
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000719 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000720 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000721
722 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +0000723 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000724 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
725 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
726 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000727 tw32(HOSTCC_MODE, tp->coal_now);
728
729 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
Matt Carlson17375d22009-08-28 14:02:18 +0000732static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700733{
Matt Carlson17375d22009-08-28 14:02:18 +0000734 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000735 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700736 unsigned int work_exists = 0;
737
738 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +0000739 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -0700740 if (sblk->status & SD_STATUS_LINK_CHG)
741 work_exists = 1;
742 }
743 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000744 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000745 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700746 work_exists = 1;
747
748 return work_exists;
749}
750
Matt Carlson17375d22009-08-28 14:02:18 +0000751/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700752 * similar to tg3_enable_ints, but it accurately determines whether there
753 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400754 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 */
Matt Carlson17375d22009-08-28 14:02:18 +0000756static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Matt Carlson17375d22009-08-28 14:02:18 +0000758 struct tg3 *tp = tnapi->tp;
759
Matt Carlson898a56f2009-08-28 14:02:40 +0000760 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 mmiowb();
762
David S. Millerfac9b832005-05-18 22:46:34 -0700763 /* When doing tagged status, this work check is unnecessary.
764 * The last_tag we write above tells the chip which piece of
765 * work we've completed.
766 */
Joe Perches63c3a662011-04-26 08:12:10 +0000767 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700768 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000769 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772static void tg3_switch_clocks(struct tg3 *tp)
773{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000774 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 u32 orig_clock_ctrl;
776
Joe Perches63c3a662011-04-26 08:12:10 +0000777 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700778 return;
779
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000780 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 orig_clock_ctrl = clock_ctrl;
783 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
784 CLOCK_CTRL_CLKRUN_OENABLE |
785 0x1f);
786 tp->pci_clock_ctrl = clock_ctrl;
787
Joe Perches63c3a662011-04-26 08:12:10 +0000788 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800790 tw32_wait_f(TG3PCI_CLOCK_CTRL,
791 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800794 tw32_wait_f(TG3PCI_CLOCK_CTRL,
795 clock_ctrl |
796 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
797 40);
798 tw32_wait_f(TG3PCI_CLOCK_CTRL,
799 clock_ctrl | (CLOCK_CTRL_ALTCLK),
800 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800802 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
805#define PHY_BUSY_LOOPS 5000
806
807static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
808{
809 u32 frame_val;
810 unsigned int loops;
811 int ret;
812
813 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
814 tw32_f(MAC_MI_MODE,
815 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
816 udelay(80);
817 }
818
819 *val = 0x0;
820
Matt Carlson882e9792009-09-01 13:21:36 +0000821 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 MI_COM_PHY_ADDR_MASK);
823 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
824 MI_COM_REG_ADDR_MASK);
825 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 tw32_f(MAC_MI_COM, frame_val);
828
829 loops = PHY_BUSY_LOOPS;
830 while (loops != 0) {
831 udelay(10);
832 frame_val = tr32(MAC_MI_COM);
833
834 if ((frame_val & MI_COM_BUSY) == 0) {
835 udelay(5);
836 frame_val = tr32(MAC_MI_COM);
837 break;
838 }
839 loops -= 1;
840 }
841
842 ret = -EBUSY;
843 if (loops != 0) {
844 *val = frame_val & MI_COM_DATA_MASK;
845 ret = 0;
846 }
847
848 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
849 tw32_f(MAC_MI_MODE, tp->mi_mode);
850 udelay(80);
851 }
852
853 return ret;
854}
855
856static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
857{
858 u32 frame_val;
859 unsigned int loops;
860 int ret;
861
Matt Carlsonf07e9af2010-08-02 11:26:07 +0000862 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Michael Chanb5d37722006-09-27 16:06:21 -0700863 (reg == MII_TG3_CTRL || reg == MII_TG3_AUX_CTRL))
864 return 0;
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
867 tw32_f(MAC_MI_MODE,
868 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
869 udelay(80);
870 }
871
Matt Carlson882e9792009-09-01 13:21:36 +0000872 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 MI_COM_PHY_ADDR_MASK);
874 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
875 MI_COM_REG_ADDR_MASK);
876 frame_val |= (val & MI_COM_DATA_MASK);
877 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 tw32_f(MAC_MI_COM, frame_val);
880
881 loops = PHY_BUSY_LOOPS;
882 while (loops != 0) {
883 udelay(10);
884 frame_val = tr32(MAC_MI_COM);
885 if ((frame_val & MI_COM_BUSY) == 0) {
886 udelay(5);
887 frame_val = tr32(MAC_MI_COM);
888 break;
889 }
890 loops -= 1;
891 }
892
893 ret = -EBUSY;
894 if (loops != 0)
895 ret = 0;
896
897 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
898 tw32_f(MAC_MI_MODE, tp->mi_mode);
899 udelay(80);
900 }
901
902 return ret;
903}
904
Matt Carlsonb0988c12011-04-20 07:57:39 +0000905static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
906{
907 int err;
908
909 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
910 if (err)
911 goto done;
912
913 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
914 if (err)
915 goto done;
916
917 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
918 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
919 if (err)
920 goto done;
921
922 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
923
924done:
925 return err;
926}
927
928static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
929{
930 int err;
931
932 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
933 if (err)
934 goto done;
935
936 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
937 if (err)
938 goto done;
939
940 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
941 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
942 if (err)
943 goto done;
944
945 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
946
947done:
948 return err;
949}
950
951static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
952{
953 int err;
954
955 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
956 if (!err)
957 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
958
959 return err;
960}
961
962static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
963{
964 int err;
965
966 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
967 if (!err)
968 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
969
970 return err;
971}
972
Matt Carlson15ee95c2011-04-20 07:57:40 +0000973static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
974{
975 int err;
976
977 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
978 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
979 MII_TG3_AUXCTL_SHDWSEL_MISC);
980 if (!err)
981 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
982
983 return err;
984}
985
Matt Carlsonb4bd2922011-04-20 07:57:41 +0000986static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
987{
988 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
989 set |= MII_TG3_AUXCTL_MISC_WREN;
990
991 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
992}
993
Matt Carlson1d36ba42011-04-20 07:57:42 +0000994#define TG3_PHY_AUXCTL_SMDSP_ENABLE(tp) \
995 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
996 MII_TG3_AUXCTL_ACTL_SMDSP_ENA | \
997 MII_TG3_AUXCTL_ACTL_TX_6DB)
998
999#define TG3_PHY_AUXCTL_SMDSP_DISABLE(tp) \
1000 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1001 MII_TG3_AUXCTL_ACTL_TX_6DB);
1002
Matt Carlson95e28692008-05-25 23:44:14 -07001003static int tg3_bmcr_reset(struct tg3 *tp)
1004{
1005 u32 phy_control;
1006 int limit, err;
1007
1008 /* OK, reset it, and poll the BMCR_RESET bit until it
1009 * clears or we time out.
1010 */
1011 phy_control = BMCR_RESET;
1012 err = tg3_writephy(tp, MII_BMCR, phy_control);
1013 if (err != 0)
1014 return -EBUSY;
1015
1016 limit = 5000;
1017 while (limit--) {
1018 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1019 if (err != 0)
1020 return -EBUSY;
1021
1022 if ((phy_control & BMCR_RESET) == 0) {
1023 udelay(40);
1024 break;
1025 }
1026 udelay(10);
1027 }
Roel Kluind4675b52009-02-12 16:33:27 -08001028 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001029 return -EBUSY;
1030
1031 return 0;
1032}
1033
Matt Carlson158d7ab2008-05-29 01:37:54 -07001034static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1035{
Francois Romieu3d165432009-01-19 16:56:50 -08001036 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001037 u32 val;
1038
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001039 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001040
1041 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001042 val = -EIO;
1043
1044 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001045
1046 return val;
1047}
1048
1049static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1050{
Francois Romieu3d165432009-01-19 16:56:50 -08001051 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001052 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001053
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001054 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001055
1056 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001057 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001058
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001059 spin_unlock_bh(&tp->lock);
1060
1061 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001062}
1063
1064static int tg3_mdio_reset(struct mii_bus *bp)
1065{
1066 return 0;
1067}
1068
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001069static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001070{
1071 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001072 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001073
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001074 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001075 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001076 case PHY_ID_BCM50610:
1077 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001078 val = MAC_PHYCFG2_50610_LED_MODES;
1079 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001080 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001081 val = MAC_PHYCFG2_AC131_LED_MODES;
1082 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001083 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001084 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1085 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001086 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001087 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1088 break;
1089 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001090 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001091 }
1092
1093 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1094 tw32(MAC_PHYCFG2, val);
1095
1096 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001097 val &= ~(MAC_PHYCFG1_RGMII_INT |
1098 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1099 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001100 tw32(MAC_PHYCFG1, val);
1101
1102 return;
1103 }
1104
Joe Perches63c3a662011-04-26 08:12:10 +00001105 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001106 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1107 MAC_PHYCFG2_FMODE_MASK_MASK |
1108 MAC_PHYCFG2_GMODE_MASK_MASK |
1109 MAC_PHYCFG2_ACT_MASK_MASK |
1110 MAC_PHYCFG2_QUAL_MASK_MASK |
1111 MAC_PHYCFG2_INBAND_ENABLE;
1112
1113 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001114
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001115 val = tr32(MAC_PHYCFG1);
1116 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1117 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001118 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1119 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001120 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001121 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001122 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1123 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001124 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1125 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1126 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001127
Matt Carlsona9daf362008-05-25 23:49:44 -07001128 val = tr32(MAC_EXT_RGMII_MODE);
1129 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1130 MAC_RGMII_MODE_RX_QUALITY |
1131 MAC_RGMII_MODE_RX_ACTIVITY |
1132 MAC_RGMII_MODE_RX_ENG_DET |
1133 MAC_RGMII_MODE_TX_ENABLE |
1134 MAC_RGMII_MODE_TX_LOWPWR |
1135 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001136 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1137 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001138 val |= MAC_RGMII_MODE_RX_INT_B |
1139 MAC_RGMII_MODE_RX_QUALITY |
1140 MAC_RGMII_MODE_RX_ACTIVITY |
1141 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001142 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001143 val |= MAC_RGMII_MODE_TX_ENABLE |
1144 MAC_RGMII_MODE_TX_LOWPWR |
1145 MAC_RGMII_MODE_TX_RESET;
1146 }
1147 tw32(MAC_EXT_RGMII_MODE, val);
1148}
1149
Matt Carlson158d7ab2008-05-29 01:37:54 -07001150static void tg3_mdio_start(struct tg3 *tp)
1151{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001152 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1153 tw32_f(MAC_MI_MODE, tp->mi_mode);
1154 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001155
Joe Perches63c3a662011-04-26 08:12:10 +00001156 if (tg3_flag(tp, MDIOBUS_INITED) &&
Matt Carlson9ea48182010-02-17 15:17:01 +00001157 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1158 tg3_mdio_config_5785(tp);
1159}
1160
1161static int tg3_mdio_init(struct tg3 *tp)
1162{
1163 int i;
1164 u32 reg;
1165 struct phy_device *phydev;
1166
Joe Perches63c3a662011-04-26 08:12:10 +00001167 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001168 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001169
Matt Carlson9c7df912010-06-05 17:24:36 +00001170 tp->phy_addr = PCI_FUNC(tp->pdev->devfn) + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001171
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001172 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1173 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1174 else
1175 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1176 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001177 if (is_serdes)
1178 tp->phy_addr += 7;
1179 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001180 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001181
Matt Carlson158d7ab2008-05-29 01:37:54 -07001182 tg3_mdio_start(tp);
1183
Joe Perches63c3a662011-04-26 08:12:10 +00001184 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001185 return 0;
1186
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001187 tp->mdio_bus = mdiobus_alloc();
1188 if (tp->mdio_bus == NULL)
1189 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001190
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001191 tp->mdio_bus->name = "tg3 mdio bus";
1192 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001193 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001194 tp->mdio_bus->priv = tp;
1195 tp->mdio_bus->parent = &tp->pdev->dev;
1196 tp->mdio_bus->read = &tg3_mdio_read;
1197 tp->mdio_bus->write = &tg3_mdio_write;
1198 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001199 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001200 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001201
1202 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001203 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001204
1205 /* The bus registration will look for all the PHYs on the mdio bus.
1206 * Unfortunately, it does not ensure the PHY is powered up before
1207 * accessing the PHY ID registers. A chip reset is the
1208 * quickest way to bring the device back to an operational state..
1209 */
1210 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1211 tg3_bmcr_reset(tp);
1212
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001213 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001214 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001215 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001216 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001217 return i;
1218 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001219
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001220 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001221
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001222 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001223 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001224 mdiobus_unregister(tp->mdio_bus);
1225 mdiobus_free(tp->mdio_bus);
1226 return -ENODEV;
1227 }
1228
1229 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001230 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001231 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001232 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001233 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001234 case PHY_ID_BCM50610:
1235 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001236 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001237 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001238 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001239 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001240 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001241 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001242 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001243 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001244 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001245 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001246 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001247 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001248 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001249 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001250 case PHY_ID_RTL8201E:
1251 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001252 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e092009-11-02 14:31:11 +00001253 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001254 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001255 break;
1256 }
1257
Joe Perches63c3a662011-04-26 08:12:10 +00001258 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001259
1260 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1261 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001262
1263 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001264}
1265
1266static void tg3_mdio_fini(struct tg3 *tp)
1267{
Joe Perches63c3a662011-04-26 08:12:10 +00001268 if (tg3_flag(tp, MDIOBUS_INITED)) {
1269 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001270 mdiobus_unregister(tp->mdio_bus);
1271 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001272 }
1273}
1274
Matt Carlson95e28692008-05-25 23:44:14 -07001275/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001276static inline void tg3_generate_fw_event(struct tg3 *tp)
1277{
1278 u32 val;
1279
1280 val = tr32(GRC_RX_CPU_EVENT);
1281 val |= GRC_RX_CPU_DRIVER_EVENT;
1282 tw32_f(GRC_RX_CPU_EVENT, val);
1283
1284 tp->last_event_jiffies = jiffies;
1285}
1286
1287#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1288
1289/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001290static void tg3_wait_for_event_ack(struct tg3 *tp)
1291{
1292 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001293 unsigned int delay_cnt;
1294 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001295
Matt Carlson4ba526c2008-08-15 14:10:04 -07001296 /* If enough time has passed, no wait is necessary. */
1297 time_remain = (long)(tp->last_event_jiffies + 1 +
1298 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1299 (long)jiffies;
1300 if (time_remain < 0)
1301 return;
1302
1303 /* Check if we can shorten the wait time. */
1304 delay_cnt = jiffies_to_usecs(time_remain);
1305 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1306 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1307 delay_cnt = (delay_cnt >> 3) + 1;
1308
1309 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001310 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1311 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001312 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001313 }
1314}
1315
1316/* tp->lock is held. */
1317static void tg3_ump_link_report(struct tg3 *tp)
1318{
1319 u32 reg;
1320 u32 val;
1321
Joe Perches63c3a662011-04-26 08:12:10 +00001322 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson95e28692008-05-25 23:44:14 -07001323 return;
1324
1325 tg3_wait_for_event_ack(tp);
1326
1327 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1328
1329 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1330
1331 val = 0;
1332 if (!tg3_readphy(tp, MII_BMCR, &reg))
1333 val = reg << 16;
1334 if (!tg3_readphy(tp, MII_BMSR, &reg))
1335 val |= (reg & 0xffff);
1336 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1337
1338 val = 0;
1339 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1340 val = reg << 16;
1341 if (!tg3_readphy(tp, MII_LPA, &reg))
1342 val |= (reg & 0xffff);
1343 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1344
1345 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001346 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001347 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1348 val = reg << 16;
1349 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1350 val |= (reg & 0xffff);
1351 }
1352 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1353
1354 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1355 val = reg << 16;
1356 else
1357 val = 0;
1358 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1359
Matt Carlson4ba526c2008-08-15 14:10:04 -07001360 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001361}
1362
1363static void tg3_link_report(struct tg3 *tp)
1364{
1365 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001366 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001367 tg3_ump_link_report(tp);
1368 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001369 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1370 (tp->link_config.active_speed == SPEED_1000 ?
1371 1000 :
1372 (tp->link_config.active_speed == SPEED_100 ?
1373 100 : 10)),
1374 (tp->link_config.active_duplex == DUPLEX_FULL ?
1375 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001376
Joe Perches05dbe002010-02-17 19:44:19 +00001377 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1378 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1379 "on" : "off",
1380 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1381 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001382
1383 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1384 netdev_info(tp->dev, "EEE is %s\n",
1385 tp->setlpicnt ? "enabled" : "disabled");
1386
Matt Carlson95e28692008-05-25 23:44:14 -07001387 tg3_ump_link_report(tp);
1388 }
1389}
1390
1391static u16 tg3_advert_flowctrl_1000T(u8 flow_ctrl)
1392{
1393 u16 miireg;
1394
Steve Glendinninge18ce342008-12-16 02:00:00 -08001395 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001396 miireg = ADVERTISE_PAUSE_CAP;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001397 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001398 miireg = ADVERTISE_PAUSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001399 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001400 miireg = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1401 else
1402 miireg = 0;
1403
1404 return miireg;
1405}
1406
1407static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1408{
1409 u16 miireg;
1410
Steve Glendinninge18ce342008-12-16 02:00:00 -08001411 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001412 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001413 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001414 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001415 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001416 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1417 else
1418 miireg = 0;
1419
1420 return miireg;
1421}
1422
Matt Carlson95e28692008-05-25 23:44:14 -07001423static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1424{
1425 u8 cap = 0;
1426
1427 if (lcladv & ADVERTISE_1000XPAUSE) {
1428 if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1429 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001430 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001431 else if (rmtadv & LPA_1000XPAUSE_ASYM)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001432 cap = FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001433 } else {
1434 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001435 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001436 }
1437 } else if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1438 if ((rmtadv & LPA_1000XPAUSE) && (rmtadv & LPA_1000XPAUSE_ASYM))
Steve Glendinninge18ce342008-12-16 02:00:00 -08001439 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001440 }
1441
1442 return cap;
1443}
1444
Matt Carlsonf51f3562008-05-25 23:45:08 -07001445static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001446{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001447 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001448 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001449 u32 old_rx_mode = tp->rx_mode;
1450 u32 old_tx_mode = tp->tx_mode;
1451
Joe Perches63c3a662011-04-26 08:12:10 +00001452 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001453 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001454 else
1455 autoneg = tp->link_config.autoneg;
1456
Joe Perches63c3a662011-04-26 08:12:10 +00001457 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001458 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001459 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001460 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001461 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001462 } else
1463 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001464
Matt Carlsonf51f3562008-05-25 23:45:08 -07001465 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001466
Steve Glendinninge18ce342008-12-16 02:00:00 -08001467 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001468 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1469 else
1470 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1471
Matt Carlsonf51f3562008-05-25 23:45:08 -07001472 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001473 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001474
Steve Glendinninge18ce342008-12-16 02:00:00 -08001475 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001476 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1477 else
1478 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1479
Matt Carlsonf51f3562008-05-25 23:45:08 -07001480 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001481 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001482}
1483
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001484static void tg3_adjust_link(struct net_device *dev)
1485{
1486 u8 oldflowctrl, linkmesg = 0;
1487 u32 mac_mode, lcl_adv, rmt_adv;
1488 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001489 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001490
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001491 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001492
1493 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1494 MAC_MODE_HALF_DUPLEX);
1495
1496 oldflowctrl = tp->link_config.active_flowctrl;
1497
1498 if (phydev->link) {
1499 lcl_adv = 0;
1500 rmt_adv = 0;
1501
1502 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1503 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001504 else if (phydev->speed == SPEED_1000 ||
1505 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001506 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001507 else
1508 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001509
1510 if (phydev->duplex == DUPLEX_HALF)
1511 mac_mode |= MAC_MODE_HALF_DUPLEX;
1512 else {
1513 lcl_adv = tg3_advert_flowctrl_1000T(
1514 tp->link_config.flowctrl);
1515
1516 if (phydev->pause)
1517 rmt_adv = LPA_PAUSE_CAP;
1518 if (phydev->asym_pause)
1519 rmt_adv |= LPA_PAUSE_ASYM;
1520 }
1521
1522 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1523 } else
1524 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1525
1526 if (mac_mode != tp->mac_mode) {
1527 tp->mac_mode = mac_mode;
1528 tw32_f(MAC_MODE, tp->mac_mode);
1529 udelay(40);
1530 }
1531
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001532 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1533 if (phydev->speed == SPEED_10)
1534 tw32(MAC_MI_STAT,
1535 MAC_MI_STAT_10MBPS_MODE |
1536 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1537 else
1538 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1539 }
1540
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001541 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1542 tw32(MAC_TX_LENGTHS,
1543 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1544 (6 << TX_LENGTHS_IPG_SHIFT) |
1545 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1546 else
1547 tw32(MAC_TX_LENGTHS,
1548 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1549 (6 << TX_LENGTHS_IPG_SHIFT) |
1550 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1551
1552 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1553 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1554 phydev->speed != tp->link_config.active_speed ||
1555 phydev->duplex != tp->link_config.active_duplex ||
1556 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001557 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001558
1559 tp->link_config.active_speed = phydev->speed;
1560 tp->link_config.active_duplex = phydev->duplex;
1561
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001562 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001563
1564 if (linkmesg)
1565 tg3_link_report(tp);
1566}
1567
1568static int tg3_phy_init(struct tg3 *tp)
1569{
1570 struct phy_device *phydev;
1571
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001572 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001573 return 0;
1574
1575 /* Bring the PHY back to a known state. */
1576 tg3_bmcr_reset(tp);
1577
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001578 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001579
1580 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08001581 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001582 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001583 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001584 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001585 return PTR_ERR(phydev);
1586 }
1587
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001588 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001589 switch (phydev->interface) {
1590 case PHY_INTERFACE_MODE_GMII:
1591 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001592 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08001593 phydev->supported &= (PHY_GBIT_FEATURES |
1594 SUPPORTED_Pause |
1595 SUPPORTED_Asym_Pause);
1596 break;
1597 }
1598 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001599 case PHY_INTERFACE_MODE_MII:
1600 phydev->supported &= (PHY_BASIC_FEATURES |
1601 SUPPORTED_Pause |
1602 SUPPORTED_Asym_Pause);
1603 break;
1604 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001605 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001606 return -EINVAL;
1607 }
1608
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001609 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001610
1611 phydev->advertising = phydev->supported;
1612
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001613 return 0;
1614}
1615
1616static void tg3_phy_start(struct tg3 *tp)
1617{
1618 struct phy_device *phydev;
1619
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001620 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001621 return;
1622
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001623 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001624
Matt Carlson80096062010-08-02 11:26:06 +00001625 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
1626 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001627 phydev->speed = tp->link_config.orig_speed;
1628 phydev->duplex = tp->link_config.orig_duplex;
1629 phydev->autoneg = tp->link_config.orig_autoneg;
1630 phydev->advertising = tp->link_config.orig_advertising;
1631 }
1632
1633 phy_start(phydev);
1634
1635 phy_start_aneg(phydev);
1636}
1637
1638static void tg3_phy_stop(struct tg3 *tp)
1639{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001640 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001641 return;
1642
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001643 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001644}
1645
1646static void tg3_phy_fini(struct tg3 *tp)
1647{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001648 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001649 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001650 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001651 }
1652}
1653
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001654static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1655{
1656 u32 phytest;
1657
1658 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1659 u32 phy;
1660
1661 tg3_writephy(tp, MII_TG3_FET_TEST,
1662 phytest | MII_TG3_FET_SHADOW_EN);
1663 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1664 if (enable)
1665 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1666 else
1667 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1668 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1669 }
1670 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1671 }
1672}
1673
Matt Carlson6833c042008-11-21 17:18:59 -08001674static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1675{
1676 u32 reg;
1677
Joe Perches63c3a662011-04-26 08:12:10 +00001678 if (!tg3_flag(tp, 5705_PLUS) ||
1679 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001680 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001681 return;
1682
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001683 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001684 tg3_phy_fet_toggle_apd(tp, enable);
1685 return;
1686 }
1687
Matt Carlson6833c042008-11-21 17:18:59 -08001688 reg = MII_TG3_MISC_SHDW_WREN |
1689 MII_TG3_MISC_SHDW_SCR5_SEL |
1690 MII_TG3_MISC_SHDW_SCR5_LPED |
1691 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1692 MII_TG3_MISC_SHDW_SCR5_SDTL |
1693 MII_TG3_MISC_SHDW_SCR5_C125OE;
1694 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1695 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1696
1697 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1698
1699
1700 reg = MII_TG3_MISC_SHDW_WREN |
1701 MII_TG3_MISC_SHDW_APD_SEL |
1702 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1703 if (enable)
1704 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1705
1706 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1707}
1708
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001709static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
1710{
1711 u32 phy;
1712
Joe Perches63c3a662011-04-26 08:12:10 +00001713 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001714 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001715 return;
1716
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001717 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001718 u32 ephy;
1719
Matt Carlson535ef6e2009-08-25 10:09:36 +00001720 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
1721 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
1722
1723 tg3_writephy(tp, MII_TG3_FET_TEST,
1724 ephy | MII_TG3_FET_SHADOW_EN);
1725 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001726 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00001727 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001728 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00001729 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1730 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001731 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00001732 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001733 }
1734 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00001735 int ret;
1736
1737 ret = tg3_phy_auxctl_read(tp,
1738 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
1739 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001740 if (enable)
1741 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1742 else
1743 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001744 tg3_phy_auxctl_write(tp,
1745 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001746 }
1747 }
1748}
1749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750static void tg3_phy_set_wirespeed(struct tg3 *tp)
1751{
Matt Carlson15ee95c2011-04-20 07:57:40 +00001752 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 u32 val;
1754
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001755 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 return;
1757
Matt Carlson15ee95c2011-04-20 07:57:40 +00001758 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
1759 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001760 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
1761 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762}
1763
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001764static void tg3_phy_apply_otp(struct tg3 *tp)
1765{
1766 u32 otp, phy;
1767
1768 if (!tp->phy_otp)
1769 return;
1770
1771 otp = tp->phy_otp;
1772
Matt Carlson1d36ba42011-04-20 07:57:42 +00001773 if (TG3_PHY_AUXCTL_SMDSP_ENABLE(tp))
1774 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001775
1776 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
1777 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
1778 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
1779
1780 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
1781 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
1782 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
1783
1784 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
1785 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
1786 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
1787
1788 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
1789 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
1790
1791 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
1792 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
1793
1794 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
1795 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
1796 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
1797
Matt Carlson1d36ba42011-04-20 07:57:42 +00001798 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001799}
1800
Matt Carlson52b02d02010-10-14 10:37:41 +00001801static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
1802{
1803 u32 val;
1804
1805 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
1806 return;
1807
1808 tp->setlpicnt = 0;
1809
1810 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
1811 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00001812 tp->link_config.active_duplex == DUPLEX_FULL &&
1813 (tp->link_config.active_speed == SPEED_100 ||
1814 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00001815 u32 eeectl;
1816
1817 if (tp->link_config.active_speed == SPEED_1000)
1818 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
1819 else
1820 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
1821
1822 tw32(TG3_CPMU_EEE_CTRL, eeectl);
1823
Matt Carlson3110f5f52010-12-06 08:28:50 +00001824 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
1825 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00001826
Matt Carlsonb0c59432011-05-19 12:12:48 +00001827 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
1828 val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
Matt Carlson52b02d02010-10-14 10:37:41 +00001829 tp->setlpicnt = 2;
1830 }
1831
1832 if (!tp->setlpicnt) {
1833 val = tr32(TG3_CPMU_EEE_MODE);
1834 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
1835 }
1836}
1837
Matt Carlsonb0c59432011-05-19 12:12:48 +00001838static void tg3_phy_eee_enable(struct tg3 *tp)
1839{
1840 u32 val;
1841
1842 if (tp->link_config.active_speed == SPEED_1000 &&
1843 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
1844 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
1845 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) &&
1846 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
1847 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0003);
1848 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
1849 }
1850
1851 val = tr32(TG3_CPMU_EEE_MODE);
1852 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
1853}
1854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855static int tg3_wait_macro_done(struct tg3 *tp)
1856{
1857 int limit = 100;
1858
1859 while (limit--) {
1860 u32 tmp32;
1861
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001862 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 if ((tmp32 & 0x1000) == 0)
1864 break;
1865 }
1866 }
Roel Kluind4675b52009-02-12 16:33:27 -08001867 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 return -EBUSY;
1869
1870 return 0;
1871}
1872
1873static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
1874{
1875 static const u32 test_pat[4][6] = {
1876 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
1877 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
1878 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
1879 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
1880 };
1881 int chan;
1882
1883 for (chan = 0; chan < 4; chan++) {
1884 int i;
1885
1886 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1887 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001888 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
1890 for (i = 0; i < 6; i++)
1891 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
1892 test_pat[chan][i]);
1893
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001894 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 if (tg3_wait_macro_done(tp)) {
1896 *resetp = 1;
1897 return -EBUSY;
1898 }
1899
1900 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1901 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001902 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 if (tg3_wait_macro_done(tp)) {
1904 *resetp = 1;
1905 return -EBUSY;
1906 }
1907
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001908 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 if (tg3_wait_macro_done(tp)) {
1910 *resetp = 1;
1911 return -EBUSY;
1912 }
1913
1914 for (i = 0; i < 6; i += 2) {
1915 u32 low, high;
1916
1917 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
1918 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
1919 tg3_wait_macro_done(tp)) {
1920 *resetp = 1;
1921 return -EBUSY;
1922 }
1923 low &= 0x7fff;
1924 high &= 0x000f;
1925 if (low != test_pat[chan][i] ||
1926 high != test_pat[chan][i+1]) {
1927 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
1928 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
1929 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
1930
1931 return -EBUSY;
1932 }
1933 }
1934 }
1935
1936 return 0;
1937}
1938
1939static int tg3_phy_reset_chanpat(struct tg3 *tp)
1940{
1941 int chan;
1942
1943 for (chan = 0; chan < 4; chan++) {
1944 int i;
1945
1946 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1947 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001948 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 for (i = 0; i < 6; i++)
1950 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001951 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 if (tg3_wait_macro_done(tp))
1953 return -EBUSY;
1954 }
1955
1956 return 0;
1957}
1958
1959static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
1960{
1961 u32 reg32, phy9_orig;
1962 int retries, do_phy_reset, err;
1963
1964 retries = 10;
1965 do_phy_reset = 1;
1966 do {
1967 if (do_phy_reset) {
1968 err = tg3_bmcr_reset(tp);
1969 if (err)
1970 return err;
1971 do_phy_reset = 0;
1972 }
1973
1974 /* Disable transmitter and interrupt. */
1975 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
1976 continue;
1977
1978 reg32 |= 0x3000;
1979 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1980
1981 /* Set full-duplex, 1000 mbps. */
1982 tg3_writephy(tp, MII_BMCR,
1983 BMCR_FULLDPLX | TG3_BMCR_SPEED1000);
1984
1985 /* Set to master mode. */
1986 if (tg3_readphy(tp, MII_TG3_CTRL, &phy9_orig))
1987 continue;
1988
1989 tg3_writephy(tp, MII_TG3_CTRL,
1990 (MII_TG3_CTRL_AS_MASTER |
1991 MII_TG3_CTRL_ENABLE_AS_MASTER));
1992
Matt Carlson1d36ba42011-04-20 07:57:42 +00001993 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
1994 if (err)
1995 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
1997 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00001998 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
2000 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2001 if (!err)
2002 break;
2003 } while (--retries);
2004
2005 err = tg3_phy_reset_chanpat(tp);
2006 if (err)
2007 return err;
2008
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002009 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
2011 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002012 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Matt Carlson1d36ba42011-04-20 07:57:42 +00002014 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016 tg3_writephy(tp, MII_TG3_CTRL, phy9_orig);
2017
2018 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2019 reg32 &= ~0x3000;
2020 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2021 } else if (!err)
2022 err = -EBUSY;
2023
2024 return err;
2025}
2026
2027/* This will reset the tigon3 PHY if there is no valid
2028 * link unless the FORCE argument is non-zero.
2029 */
2030static int tg3_phy_reset(struct tg3 *tp)
2031{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002032 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 int err;
2034
Michael Chan60189dd2006-12-17 17:08:07 -08002035 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002036 val = tr32(GRC_MISC_CFG);
2037 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2038 udelay(40);
2039 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002040 err = tg3_readphy(tp, MII_BMSR, &val);
2041 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 if (err != 0)
2043 return -EBUSY;
2044
Michael Chanc8e1e822006-04-29 18:55:17 -07002045 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
2046 netif_carrier_off(tp->dev);
2047 tg3_link_report(tp);
2048 }
2049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2051 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2052 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2053 err = tg3_phy_reset_5703_4_5(tp);
2054 if (err)
2055 return err;
2056 goto out;
2057 }
2058
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002059 cpmuctrl = 0;
2060 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2061 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2062 cpmuctrl = tr32(TG3_CPMU_CTRL);
2063 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2064 tw32(TG3_CPMU_CTRL,
2065 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2066 }
2067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 err = tg3_bmcr_reset(tp);
2069 if (err)
2070 return err;
2071
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002072 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002073 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2074 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002075
2076 tw32(TG3_CPMU_CTRL, cpmuctrl);
2077 }
2078
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002079 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2080 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002081 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2082 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2083 CPMU_LSPD_1000MB_MACCLK_12_5) {
2084 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2085 udelay(40);
2086 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2087 }
2088 }
2089
Joe Perches63c3a662011-04-26 08:12:10 +00002090 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002091 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002092 return 0;
2093
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002094 tg3_phy_apply_otp(tp);
2095
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002096 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002097 tg3_phy_toggle_apd(tp, true);
2098 else
2099 tg3_phy_toggle_apd(tp, false);
2100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002102 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
2103 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002104 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2105 tg3_phydsp_write(tp, 0x000a, 0x0323);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002106 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002108
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002109 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002110 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2111 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002113
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002114 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002115 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2116 tg3_phydsp_write(tp, 0x000a, 0x310b);
2117 tg3_phydsp_write(tp, 0x201f, 0x9506);
2118 tg3_phydsp_write(tp, 0x401f, 0x14e2);
2119 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2120 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002121 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002122 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2123 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2124 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2125 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2126 tg3_writephy(tp, MII_TG3_TEST1,
2127 MII_TG3_TEST1_TRIM_EN | 0x4);
2128 } else
2129 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2130
2131 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2132 }
Michael Chanc424cb22006-04-29 18:56:34 -07002133 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002134
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 /* Set Extended packet length bit (bit 14) on all chips that */
2136 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002137 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002139 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002140 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002142 err = tg3_phy_auxctl_read(tp,
2143 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2144 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002145 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2146 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 }
2148
2149 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2150 * jumbo frames transmission.
2151 */
Joe Perches63c3a662011-04-26 08:12:10 +00002152 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002153 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002154 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002155 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 }
2157
Michael Chan715116a2006-09-27 16:09:25 -07002158 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002159 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002160 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002161 }
2162
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002163 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 tg3_phy_set_wirespeed(tp);
2165 return 0;
2166}
2167
2168static void tg3_frob_aux_power(struct tg3 *tp)
2169{
Matt Carlson683644b2011-03-09 16:58:23 +00002170 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
Matt Carlson334355a2010-01-20 16:58:10 +00002172 /* The GPIOs do something completely different on 57765. */
Joe Perches63c3a662011-04-26 08:12:10 +00002173 if (!tg3_flag(tp, IS_NIC) ||
Matt Carlsona50d0792010-06-05 17:24:37 +00002174 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson334355a2010-01-20 16:58:10 +00002175 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 return;
2177
Matt Carlson683644b2011-03-09 16:58:23 +00002178 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2179 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +00002180 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2181 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) &&
Matt Carlson683644b2011-03-09 16:58:23 +00002182 tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002183 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002185 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002186
Michael Chanbc1c7562006-03-20 17:48:03 -08002187 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002188 if (dev_peer) {
2189 struct tg3 *tp_peer = netdev_priv(dev_peer);
2190
Joe Perches63c3a662011-04-26 08:12:10 +00002191 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002192 return;
2193
Joe Perches63c3a662011-04-26 08:12:10 +00002194 if (tg3_flag(tp_peer, WOL_ENABLE) ||
2195 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002196 need_vaux = true;
2197 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Joe Perches63c3a662011-04-26 08:12:10 +00002200 if (tg3_flag(tp, WOL_ENABLE) || tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002201 need_vaux = true;
2202
2203 if (need_vaux) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2205 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002206 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2207 (GRC_LCLCTRL_GPIO_OE0 |
2208 GRC_LCLCTRL_GPIO_OE1 |
2209 GRC_LCLCTRL_GPIO_OE2 |
2210 GRC_LCLCTRL_GPIO_OUTPUT0 |
2211 GRC_LCLCTRL_GPIO_OUTPUT1),
2212 100);
Matt Carlson8d519ab2009-04-20 06:58:01 +00002213 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2214 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -07002215 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2216 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2217 GRC_LCLCTRL_GPIO_OE1 |
2218 GRC_LCLCTRL_GPIO_OE2 |
2219 GRC_LCLCTRL_GPIO_OUTPUT0 |
2220 GRC_LCLCTRL_GPIO_OUTPUT1 |
2221 tp->grc_local_ctrl;
2222 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2223
2224 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2225 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2226
2227 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2228 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 } else {
2230 u32 no_gpio2;
Michael Chandc56b7d2005-12-19 16:26:28 -08002231 u32 grc_local_ctrl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
Michael Chandc56b7d2005-12-19 16:26:28 -08002233 /* Workaround to prevent overdrawing Amps. */
2234 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2235 ASIC_REV_5714) {
2236 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chanb401e9e2005-12-19 16:27:04 -08002237 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2238 grc_local_ctrl, 100);
Michael Chandc56b7d2005-12-19 16:26:28 -08002239 }
2240
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 /* On 5753 and variants, GPIO2 cannot be used. */
2242 no_gpio2 = tp->nic_sram_data_cfg &
2243 NIC_SRAM_DATA_CFG_NO_GPIO2;
2244
Michael Chandc56b7d2005-12-19 16:26:28 -08002245 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 GRC_LCLCTRL_GPIO_OE1 |
2247 GRC_LCLCTRL_GPIO_OE2 |
2248 GRC_LCLCTRL_GPIO_OUTPUT1 |
2249 GRC_LCLCTRL_GPIO_OUTPUT2;
2250 if (no_gpio2) {
2251 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2252 GRC_LCLCTRL_GPIO_OUTPUT2);
2253 }
Michael Chanb401e9e2005-12-19 16:27:04 -08002254 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2255 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
2257 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2258
Michael Chanb401e9e2005-12-19 16:27:04 -08002259 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2260 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
2262 if (!no_gpio2) {
2263 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chanb401e9e2005-12-19 16:27:04 -08002264 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2265 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 }
2267 }
2268 } else {
2269 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
2270 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002271 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2272 (GRC_LCLCTRL_GPIO_OE1 |
2273 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Michael Chanb401e9e2005-12-19 16:27:04 -08002275 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2276 GRC_LCLCTRL_GPIO_OE1, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Michael Chanb401e9e2005-12-19 16:27:04 -08002278 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2279 (GRC_LCLCTRL_GPIO_OE1 |
2280 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 }
2282 }
2283}
2284
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002285static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2286{
2287 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2288 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002289 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002290 if (speed != SPEED_10)
2291 return 1;
2292 } else if (speed == SPEED_10)
2293 return 1;
2294
2295 return 0;
2296}
2297
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298static int tg3_setup_phy(struct tg3 *, int);
2299
2300#define RESET_KIND_SHUTDOWN 0
2301#define RESET_KIND_INIT 1
2302#define RESET_KIND_SUSPEND 2
2303
2304static void tg3_write_sig_post_reset(struct tg3 *, int);
2305static int tg3_halt_cpu(struct tg3 *, u32);
2306
Matt Carlson0a459aa2008-11-03 16:54:15 -08002307static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002308{
Matt Carlsonce057f02007-11-12 21:08:03 -08002309 u32 val;
2310
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002311 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002312 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2313 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2314 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2315
2316 sg_dig_ctrl |=
2317 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2318 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2319 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2320 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002321 return;
Michael Chan51297242007-02-13 12:17:57 -08002322 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002323
Michael Chan60189dd2006-12-17 17:08:07 -08002324 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002325 tg3_bmcr_reset(tp);
2326 val = tr32(GRC_MISC_CFG);
2327 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2328 udelay(40);
2329 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002330 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002331 u32 phytest;
2332 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2333 u32 phy;
2334
2335 tg3_writephy(tp, MII_ADVERTISE, 0);
2336 tg3_writephy(tp, MII_BMCR,
2337 BMCR_ANENABLE | BMCR_ANRESTART);
2338
2339 tg3_writephy(tp, MII_TG3_FET_TEST,
2340 phytest | MII_TG3_FET_SHADOW_EN);
2341 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2342 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2343 tg3_writephy(tp,
2344 MII_TG3_FET_SHDW_AUXMODE4,
2345 phy);
2346 }
2347 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2348 }
2349 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002350 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002351 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2352 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002353
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002354 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2355 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2356 MII_TG3_AUXCTL_PCTL_VREG_11V;
2357 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07002358 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002359
Michael Chan15c3b692006-03-22 01:06:52 -08002360 /* The PHY should not be powered down on some chips because
2361 * of bugs.
2362 */
2363 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2364 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2365 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002366 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Michael Chan15c3b692006-03-22 01:06:52 -08002367 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002368
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002369 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2370 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002371 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2372 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2373 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2374 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2375 }
2376
Michael Chan15c3b692006-03-22 01:06:52 -08002377 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2378}
2379
Matt Carlson3f007892008-11-03 16:51:36 -08002380/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002381static int tg3_nvram_lock(struct tg3 *tp)
2382{
Joe Perches63c3a662011-04-26 08:12:10 +00002383 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002384 int i;
2385
2386 if (tp->nvram_lock_cnt == 0) {
2387 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2388 for (i = 0; i < 8000; i++) {
2389 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2390 break;
2391 udelay(20);
2392 }
2393 if (i == 8000) {
2394 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2395 return -ENODEV;
2396 }
2397 }
2398 tp->nvram_lock_cnt++;
2399 }
2400 return 0;
2401}
2402
2403/* tp->lock is held. */
2404static void tg3_nvram_unlock(struct tg3 *tp)
2405{
Joe Perches63c3a662011-04-26 08:12:10 +00002406 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002407 if (tp->nvram_lock_cnt > 0)
2408 tp->nvram_lock_cnt--;
2409 if (tp->nvram_lock_cnt == 0)
2410 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2411 }
2412}
2413
2414/* tp->lock is held. */
2415static void tg3_enable_nvram_access(struct tg3 *tp)
2416{
Joe Perches63c3a662011-04-26 08:12:10 +00002417 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002418 u32 nvaccess = tr32(NVRAM_ACCESS);
2419
2420 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2421 }
2422}
2423
2424/* tp->lock is held. */
2425static void tg3_disable_nvram_access(struct tg3 *tp)
2426{
Joe Perches63c3a662011-04-26 08:12:10 +00002427 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002428 u32 nvaccess = tr32(NVRAM_ACCESS);
2429
2430 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2431 }
2432}
2433
2434static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2435 u32 offset, u32 *val)
2436{
2437 u32 tmp;
2438 int i;
2439
2440 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2441 return -EINVAL;
2442
2443 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2444 EEPROM_ADDR_DEVID_MASK |
2445 EEPROM_ADDR_READ);
2446 tw32(GRC_EEPROM_ADDR,
2447 tmp |
2448 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2449 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2450 EEPROM_ADDR_ADDR_MASK) |
2451 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2452
2453 for (i = 0; i < 1000; i++) {
2454 tmp = tr32(GRC_EEPROM_ADDR);
2455
2456 if (tmp & EEPROM_ADDR_COMPLETE)
2457 break;
2458 msleep(1);
2459 }
2460 if (!(tmp & EEPROM_ADDR_COMPLETE))
2461 return -EBUSY;
2462
Matt Carlson62cedd12009-04-20 14:52:29 -07002463 tmp = tr32(GRC_EEPROM_DATA);
2464
2465 /*
2466 * The data will always be opposite the native endian
2467 * format. Perform a blind byteswap to compensate.
2468 */
2469 *val = swab32(tmp);
2470
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002471 return 0;
2472}
2473
2474#define NVRAM_CMD_TIMEOUT 10000
2475
2476static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2477{
2478 int i;
2479
2480 tw32(NVRAM_CMD, nvram_cmd);
2481 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2482 udelay(10);
2483 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2484 udelay(10);
2485 break;
2486 }
2487 }
2488
2489 if (i == NVRAM_CMD_TIMEOUT)
2490 return -EBUSY;
2491
2492 return 0;
2493}
2494
2495static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2496{
Joe Perches63c3a662011-04-26 08:12:10 +00002497 if (tg3_flag(tp, NVRAM) &&
2498 tg3_flag(tp, NVRAM_BUFFERED) &&
2499 tg3_flag(tp, FLASH) &&
2500 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002501 (tp->nvram_jedecnum == JEDEC_ATMEL))
2502
2503 addr = ((addr / tp->nvram_pagesize) <<
2504 ATMEL_AT45DB0X1B_PAGE_POS) +
2505 (addr % tp->nvram_pagesize);
2506
2507 return addr;
2508}
2509
2510static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2511{
Joe Perches63c3a662011-04-26 08:12:10 +00002512 if (tg3_flag(tp, NVRAM) &&
2513 tg3_flag(tp, NVRAM_BUFFERED) &&
2514 tg3_flag(tp, FLASH) &&
2515 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002516 (tp->nvram_jedecnum == JEDEC_ATMEL))
2517
2518 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2519 tp->nvram_pagesize) +
2520 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2521
2522 return addr;
2523}
2524
Matt Carlsone4f34112009-02-25 14:25:00 +00002525/* NOTE: Data read in from NVRAM is byteswapped according to
2526 * the byteswapping settings for all other register accesses.
2527 * tg3 devices are BE devices, so on a BE machine, the data
2528 * returned will be exactly as it is seen in NVRAM. On a LE
2529 * machine, the 32-bit value will be byteswapped.
2530 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002531static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2532{
2533 int ret;
2534
Joe Perches63c3a662011-04-26 08:12:10 +00002535 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002536 return tg3_nvram_read_using_eeprom(tp, offset, val);
2537
2538 offset = tg3_nvram_phys_addr(tp, offset);
2539
2540 if (offset > NVRAM_ADDR_MSK)
2541 return -EINVAL;
2542
2543 ret = tg3_nvram_lock(tp);
2544 if (ret)
2545 return ret;
2546
2547 tg3_enable_nvram_access(tp);
2548
2549 tw32(NVRAM_ADDR, offset);
2550 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2551 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2552
2553 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002554 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002555
2556 tg3_disable_nvram_access(tp);
2557
2558 tg3_nvram_unlock(tp);
2559
2560 return ret;
2561}
2562
Matt Carlsona9dc5292009-02-25 14:25:30 +00002563/* Ensures NVRAM data is in bytestream format. */
2564static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002565{
2566 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002567 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002568 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002569 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002570 return res;
2571}
2572
2573/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08002574static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2575{
2576 u32 addr_high, addr_low;
2577 int i;
2578
2579 addr_high = ((tp->dev->dev_addr[0] << 8) |
2580 tp->dev->dev_addr[1]);
2581 addr_low = ((tp->dev->dev_addr[2] << 24) |
2582 (tp->dev->dev_addr[3] << 16) |
2583 (tp->dev->dev_addr[4] << 8) |
2584 (tp->dev->dev_addr[5] << 0));
2585 for (i = 0; i < 4; i++) {
2586 if (i == 1 && skip_mac_1)
2587 continue;
2588 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2589 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2590 }
2591
2592 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2593 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2594 for (i = 0; i < 12; i++) {
2595 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2596 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2597 }
2598 }
2599
2600 addr_high = (tp->dev->dev_addr[0] +
2601 tp->dev->dev_addr[1] +
2602 tp->dev->dev_addr[2] +
2603 tp->dev->dev_addr[3] +
2604 tp->dev->dev_addr[4] +
2605 tp->dev->dev_addr[5]) &
2606 TX_BACKOFF_SEED_MASK;
2607 tw32(MAC_TX_BACKOFF_SEED, addr_high);
2608}
2609
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002610static void tg3_enable_register_access(struct tg3 *tp)
2611{
2612 /*
2613 * Make sure register accesses (indirect or otherwise) will function
2614 * correctly.
2615 */
2616 pci_write_config_dword(tp->pdev,
2617 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
2618}
2619
2620static int tg3_power_up(struct tg3 *tp)
2621{
2622 tg3_enable_register_access(tp);
2623
2624 pci_set_power_state(tp->pdev, PCI_D0);
2625
2626 /* Switch out of Vaux if it is a NIC */
Joe Perches63c3a662011-04-26 08:12:10 +00002627 if (tg3_flag(tp, IS_NIC))
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002628 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl, 100);
2629
2630 return 0;
2631}
2632
2633static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634{
2635 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002636 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002638 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002639
2640 /* Restore the CLKREQ setting. */
Joe Perches63c3a662011-04-26 08:12:10 +00002641 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002642 u16 lnkctl;
2643
2644 pci_read_config_word(tp->pdev,
2645 tp->pcie_cap + PCI_EXP_LNKCTL,
2646 &lnkctl);
2647 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2648 pci_write_config_word(tp->pdev,
2649 tp->pcie_cap + PCI_EXP_LNKCTL,
2650 lnkctl);
2651 }
2652
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2654 tw32(TG3PCI_MISC_HOST_CTRL,
2655 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2656
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002657 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00002658 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002659
Joe Perches63c3a662011-04-26 08:12:10 +00002660 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002661 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002662 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00002663 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002664 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002665 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002666
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002667 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002668
Matt Carlson80096062010-08-02 11:26:06 +00002669 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002670
2671 tp->link_config.orig_speed = phydev->speed;
2672 tp->link_config.orig_duplex = phydev->duplex;
2673 tp->link_config.orig_autoneg = phydev->autoneg;
2674 tp->link_config.orig_advertising = phydev->advertising;
2675
2676 advertising = ADVERTISED_TP |
2677 ADVERTISED_Pause |
2678 ADVERTISED_Autoneg |
2679 ADVERTISED_10baseT_Half;
2680
Joe Perches63c3a662011-04-26 08:12:10 +00002681 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
2682 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002683 advertising |=
2684 ADVERTISED_100baseT_Half |
2685 ADVERTISED_100baseT_Full |
2686 ADVERTISED_10baseT_Full;
2687 else
2688 advertising |= ADVERTISED_10baseT_Full;
2689 }
2690
2691 phydev->advertising = advertising;
2692
2693 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002694
2695 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00002696 if (phyid != PHY_ID_BCMAC131) {
2697 phyid &= PHY_BCM_OUI_MASK;
2698 if (phyid == PHY_BCM_OUI_1 ||
2699 phyid == PHY_BCM_OUI_2 ||
2700 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08002701 do_low_power = true;
2702 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002703 }
Matt Carlsondd477002008-05-25 23:45:58 -07002704 } else {
Matt Carlson20232762008-12-21 20:18:56 -08002705 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002706
Matt Carlson80096062010-08-02 11:26:06 +00002707 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
2708 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07002709 tp->link_config.orig_speed = tp->link_config.speed;
2710 tp->link_config.orig_duplex = tp->link_config.duplex;
2711 tp->link_config.orig_autoneg = tp->link_config.autoneg;
2712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002714 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Matt Carlsondd477002008-05-25 23:45:58 -07002715 tp->link_config.speed = SPEED_10;
2716 tp->link_config.duplex = DUPLEX_HALF;
2717 tp->link_config.autoneg = AUTONEG_ENABLE;
2718 tg3_setup_phy(tp, 0);
2719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 }
2721
Michael Chanb5d37722006-09-27 16:06:21 -07002722 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2723 u32 val;
2724
2725 val = tr32(GRC_VCPU_EXT_CTRL);
2726 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00002727 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08002728 int i;
2729 u32 val;
2730
2731 for (i = 0; i < 200; i++) {
2732 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2733 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2734 break;
2735 msleep(1);
2736 }
2737 }
Joe Perches63c3a662011-04-26 08:12:10 +00002738 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07002739 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2740 WOL_DRV_STATE_SHUTDOWN |
2741 WOL_DRV_WOL |
2742 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08002743
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002744 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 u32 mac_mode;
2746
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002747 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002748 if (do_low_power &&
2749 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
2750 tg3_phy_auxctl_write(tp,
2751 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
2752 MII_TG3_AUXCTL_PCTL_WOL_EN |
2753 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2754 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07002755 udelay(40);
2756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002758 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07002759 mac_mode = MAC_MODE_PORT_MODE_GMII;
2760 else
2761 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002763 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2764 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2765 ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00002766 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002767 SPEED_100 : SPEED_10;
2768 if (tg3_5700_link_polarity(tp, speed))
2769 mac_mode |= MAC_MODE_LINK_POLARITY;
2770 else
2771 mac_mode &= ~MAC_MODE_LINK_POLARITY;
2772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 } else {
2774 mac_mode = MAC_MODE_PORT_MODE_TBI;
2775 }
2776
Joe Perches63c3a662011-04-26 08:12:10 +00002777 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 tw32(MAC_LED_CTRL, tp->led_ctrl);
2779
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002780 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00002781 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
2782 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002783 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
Joe Perches63c3a662011-04-26 08:12:10 +00002785 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00002786 mac_mode |= MAC_MODE_APE_TX_EN |
2787 MAC_MODE_APE_RX_EN |
2788 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07002789
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 tw32_f(MAC_MODE, mac_mode);
2791 udelay(100);
2792
2793 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2794 udelay(10);
2795 }
2796
Joe Perches63c3a662011-04-26 08:12:10 +00002797 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2799 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2800 u32 base_val;
2801
2802 base_val = tp->pci_clock_ctrl;
2803 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2804 CLOCK_CTRL_TXCLK_DISABLE);
2805
Michael Chanb401e9e2005-12-19 16:27:04 -08002806 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
2807 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00002808 } else if (tg3_flag(tp, 5780_CLASS) ||
2809 tg3_flag(tp, CPMU_PRESENT) ||
Matt Carlson6ff6f812011-05-19 12:12:54 +00002810 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07002811 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00002812 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 u32 newbits1, newbits2;
2814
2815 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2816 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2817 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
2818 CLOCK_CTRL_TXCLK_DISABLE |
2819 CLOCK_CTRL_ALTCLK);
2820 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00002821 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 newbits1 = CLOCK_CTRL_625_CORE;
2823 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
2824 } else {
2825 newbits1 = CLOCK_CTRL_ALTCLK;
2826 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2827 }
2828
Michael Chanb401e9e2005-12-19 16:27:04 -08002829 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
2830 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
Michael Chanb401e9e2005-12-19 16:27:04 -08002832 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
2833 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
Joe Perches63c3a662011-04-26 08:12:10 +00002835 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 u32 newbits3;
2837
2838 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2839 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2840 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
2841 CLOCK_CTRL_TXCLK_DISABLE |
2842 CLOCK_CTRL_44MHZ_CORE);
2843 } else {
2844 newbits3 = CLOCK_CTRL_44MHZ_CORE;
2845 }
2846
Michael Chanb401e9e2005-12-19 16:27:04 -08002847 tw32_wait_f(TG3PCI_CLOCK_CTRL,
2848 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 }
2850 }
2851
Joe Perches63c3a662011-04-26 08:12:10 +00002852 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08002853 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08002854
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 tg3_frob_aux_power(tp);
2856
2857 /* Workaround for unstable PLL clock */
2858 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
2859 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
2860 u32 val = tr32(0x7d00);
2861
2862 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
2863 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00002864 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08002865 int err;
2866
2867 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08002869 if (!err)
2870 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08002871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 }
2873
Michael Chanbbadf502006-04-06 21:46:34 -07002874 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
2875
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 return 0;
2877}
2878
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002879static void tg3_power_down(struct tg3 *tp)
2880{
2881 tg3_power_down_prepare(tp);
2882
Joe Perches63c3a662011-04-26 08:12:10 +00002883 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002884 pci_set_power_state(tp->pdev, PCI_D3hot);
2885}
2886
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
2888{
2889 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
2890 case MII_TG3_AUX_STAT_10HALF:
2891 *speed = SPEED_10;
2892 *duplex = DUPLEX_HALF;
2893 break;
2894
2895 case MII_TG3_AUX_STAT_10FULL:
2896 *speed = SPEED_10;
2897 *duplex = DUPLEX_FULL;
2898 break;
2899
2900 case MII_TG3_AUX_STAT_100HALF:
2901 *speed = SPEED_100;
2902 *duplex = DUPLEX_HALF;
2903 break;
2904
2905 case MII_TG3_AUX_STAT_100FULL:
2906 *speed = SPEED_100;
2907 *duplex = DUPLEX_FULL;
2908 break;
2909
2910 case MII_TG3_AUX_STAT_1000HALF:
2911 *speed = SPEED_1000;
2912 *duplex = DUPLEX_HALF;
2913 break;
2914
2915 case MII_TG3_AUX_STAT_1000FULL:
2916 *speed = SPEED_1000;
2917 *duplex = DUPLEX_FULL;
2918 break;
2919
2920 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002921 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07002922 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
2923 SPEED_10;
2924 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
2925 DUPLEX_HALF;
2926 break;
2927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 *speed = SPEED_INVALID;
2929 *duplex = DUPLEX_INVALID;
2930 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932}
2933
Matt Carlson42b64a42011-05-19 12:12:49 +00002934static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935{
Matt Carlson42b64a42011-05-19 12:12:49 +00002936 int err = 0;
2937 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
Matt Carlson42b64a42011-05-19 12:12:49 +00002939 new_adv = ADVERTISE_CSMA;
2940 if (advertise & ADVERTISED_10baseT_Half)
2941 new_adv |= ADVERTISE_10HALF;
2942 if (advertise & ADVERTISED_10baseT_Full)
2943 new_adv |= ADVERTISE_10FULL;
2944 if (advertise & ADVERTISED_100baseT_Half)
2945 new_adv |= ADVERTISE_100HALF;
2946 if (advertise & ADVERTISED_100baseT_Full)
2947 new_adv |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
Matt Carlson42b64a42011-05-19 12:12:49 +00002949 new_adv |= tg3_advert_flowctrl_1000T(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
Matt Carlson42b64a42011-05-19 12:12:49 +00002951 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
2952 if (err)
2953 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954
Matt Carlson42b64a42011-05-19 12:12:49 +00002955 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
2956 goto done;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002957
Matt Carlson42b64a42011-05-19 12:12:49 +00002958 new_adv = 0;
2959 if (advertise & ADVERTISED_1000baseT_Half)
2960 new_adv |= MII_TG3_CTRL_ADV_1000_HALF;
2961 if (advertise & ADVERTISED_1000baseT_Full)
2962 new_adv |= MII_TG3_CTRL_ADV_1000_FULL;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002963
Matt Carlson42b64a42011-05-19 12:12:49 +00002964 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2965 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
2966 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2967 MII_TG3_CTRL_ENABLE_AS_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968
Matt Carlson42b64a42011-05-19 12:12:49 +00002969 err = tg3_writephy(tp, MII_TG3_CTRL, new_adv);
2970 if (err)
2971 goto done;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002972
Matt Carlson42b64a42011-05-19 12:12:49 +00002973 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2974 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
Matt Carlson42b64a42011-05-19 12:12:49 +00002976 tw32(TG3_CPMU_EEE_MODE,
2977 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002978
Matt Carlson42b64a42011-05-19 12:12:49 +00002979 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
2980 if (!err) {
2981 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00002982
Matt Carlson21a00ab2011-01-25 15:58:55 +00002983 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
2984 case ASIC_REV_5717:
2985 case ASIC_REV_57765:
2986 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
2987 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
2988 MII_TG3_DSP_CH34TP2_HIBW01);
2989 /* Fall through */
2990 case ASIC_REV_5719:
2991 val = MII_TG3_DSP_TAP26_ALNOKO |
2992 MII_TG3_DSP_TAP26_RMRXSTO |
2993 MII_TG3_DSP_TAP26_OPCSINPT;
2994 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
2995 }
Matt Carlson52b02d02010-10-14 10:37:41 +00002996
Matt Carlsona6b68da2010-12-06 08:28:52 +00002997 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00002998 /* Advertise 100-BaseTX EEE ability */
2999 if (advertise & ADVERTISED_100baseT_Full)
3000 val |= MDIO_AN_EEE_ADV_100TX;
3001 /* Advertise 1000-BaseT EEE ability */
3002 if (advertise & ADVERTISED_1000baseT_Full)
3003 val |= MDIO_AN_EEE_ADV_1000T;
3004 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlson52b02d02010-10-14 10:37:41 +00003005
Matt Carlson42b64a42011-05-19 12:12:49 +00003006 err2 = TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
3007 if (!err)
3008 err = err2;
3009 }
3010
3011done:
3012 return err;
3013}
3014
3015static void tg3_phy_copper_begin(struct tg3 *tp)
3016{
3017 u32 new_adv;
3018 int i;
3019
3020 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
3021 new_adv = ADVERTISED_10baseT_Half |
3022 ADVERTISED_10baseT_Full;
3023 if (tg3_flag(tp, WOL_SPEED_100MB))
3024 new_adv |= ADVERTISED_100baseT_Half |
3025 ADVERTISED_100baseT_Full;
3026
3027 tg3_phy_autoneg_cfg(tp, new_adv,
3028 FLOW_CTRL_TX | FLOW_CTRL_RX);
3029 } else if (tp->link_config.speed == SPEED_INVALID) {
3030 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
3031 tp->link_config.advertising &=
3032 ~(ADVERTISED_1000baseT_Half |
3033 ADVERTISED_1000baseT_Full);
3034
3035 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
3036 tp->link_config.flowctrl);
3037 } else {
3038 /* Asking for a specific link mode. */
3039 if (tp->link_config.speed == SPEED_1000) {
3040 if (tp->link_config.duplex == DUPLEX_FULL)
3041 new_adv = ADVERTISED_1000baseT_Full;
3042 else
3043 new_adv = ADVERTISED_1000baseT_Half;
3044 } else if (tp->link_config.speed == SPEED_100) {
3045 if (tp->link_config.duplex == DUPLEX_FULL)
3046 new_adv = ADVERTISED_100baseT_Full;
3047 else
3048 new_adv = ADVERTISED_100baseT_Half;
3049 } else {
3050 if (tp->link_config.duplex == DUPLEX_FULL)
3051 new_adv = ADVERTISED_10baseT_Full;
3052 else
3053 new_adv = ADVERTISED_10baseT_Half;
3054 }
3055
3056 tg3_phy_autoneg_cfg(tp, new_adv,
3057 tp->link_config.flowctrl);
Matt Carlson52b02d02010-10-14 10:37:41 +00003058 }
3059
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
3061 tp->link_config.speed != SPEED_INVALID) {
3062 u32 bmcr, orig_bmcr;
3063
3064 tp->link_config.active_speed = tp->link_config.speed;
3065 tp->link_config.active_duplex = tp->link_config.duplex;
3066
3067 bmcr = 0;
3068 switch (tp->link_config.speed) {
3069 default:
3070 case SPEED_10:
3071 break;
3072
3073 case SPEED_100:
3074 bmcr |= BMCR_SPEED100;
3075 break;
3076
3077 case SPEED_1000:
3078 bmcr |= TG3_BMCR_SPEED1000;
3079 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081
3082 if (tp->link_config.duplex == DUPLEX_FULL)
3083 bmcr |= BMCR_FULLDPLX;
3084
3085 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3086 (bmcr != orig_bmcr)) {
3087 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3088 for (i = 0; i < 1500; i++) {
3089 u32 tmp;
3090
3091 udelay(10);
3092 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3093 tg3_readphy(tp, MII_BMSR, &tmp))
3094 continue;
3095 if (!(tmp & BMSR_LSTATUS)) {
3096 udelay(40);
3097 break;
3098 }
3099 }
3100 tg3_writephy(tp, MII_BMCR, bmcr);
3101 udelay(40);
3102 }
3103 } else {
3104 tg3_writephy(tp, MII_BMCR,
3105 BMCR_ANENABLE | BMCR_ANRESTART);
3106 }
3107}
3108
3109static int tg3_init_5401phy_dsp(struct tg3 *tp)
3110{
3111 int err;
3112
3113 /* Turn off tap power management. */
3114 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003115 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003117 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3118 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3119 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3120 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3121 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
3123 udelay(40);
3124
3125 return err;
3126}
3127
Michael Chan3600d912006-12-07 00:21:48 -08003128static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129{
Michael Chan3600d912006-12-07 00:21:48 -08003130 u32 adv_reg, all_mask = 0;
3131
3132 if (mask & ADVERTISED_10baseT_Half)
3133 all_mask |= ADVERTISE_10HALF;
3134 if (mask & ADVERTISED_10baseT_Full)
3135 all_mask |= ADVERTISE_10FULL;
3136 if (mask & ADVERTISED_100baseT_Half)
3137 all_mask |= ADVERTISE_100HALF;
3138 if (mask & ADVERTISED_100baseT_Full)
3139 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
3141 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
3142 return 0;
3143
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 if ((adv_reg & all_mask) != all_mask)
3145 return 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003146 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 u32 tg3_ctrl;
3148
Michael Chan3600d912006-12-07 00:21:48 -08003149 all_mask = 0;
3150 if (mask & ADVERTISED_1000baseT_Half)
3151 all_mask |= ADVERTISE_1000HALF;
3152 if (mask & ADVERTISED_1000baseT_Full)
3153 all_mask |= ADVERTISE_1000FULL;
3154
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 if (tg3_readphy(tp, MII_TG3_CTRL, &tg3_ctrl))
3156 return 0;
3157
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 if ((tg3_ctrl & all_mask) != all_mask)
3159 return 0;
3160 }
3161 return 1;
3162}
3163
Matt Carlsonef167e22007-12-20 20:10:01 -08003164static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
3165{
3166 u32 curadv, reqadv;
3167
3168 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3169 return 1;
3170
3171 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3172 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3173
3174 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3175 if (curadv != reqadv)
3176 return 0;
3177
Joe Perches63c3a662011-04-26 08:12:10 +00003178 if (tg3_flag(tp, PAUSE_AUTONEG))
Matt Carlsonef167e22007-12-20 20:10:01 -08003179 tg3_readphy(tp, MII_LPA, rmtadv);
3180 } else {
3181 /* Reprogram the advertisement register, even if it
3182 * does not affect the current link. If the link
3183 * gets renegotiated in the future, we can save an
3184 * additional renegotiation cycle by advertising
3185 * it correctly in the first place.
3186 */
3187 if (curadv != reqadv) {
3188 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3189 ADVERTISE_PAUSE_ASYM);
3190 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3191 }
3192 }
3193
3194 return 1;
3195}
3196
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3198{
3199 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003200 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08003201 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 u16 current_speed;
3203 u8 current_duplex;
3204 int i, err;
3205
3206 tw32(MAC_EVENT, 0);
3207
3208 tw32_f(MAC_STATUS,
3209 (MAC_STATUS_SYNC_CHANGED |
3210 MAC_STATUS_CFG_CHANGED |
3211 MAC_STATUS_MI_COMPLETION |
3212 MAC_STATUS_LNKSTATE_CHANGED));
3213 udelay(40);
3214
Matt Carlson8ef21422008-05-02 16:47:53 -07003215 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3216 tw32_f(MAC_MI_MODE,
3217 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3218 udelay(80);
3219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003221 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222
3223 /* Some third-party PHYs need to be reset on link going
3224 * down.
3225 */
3226 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3227 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3228 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3229 netif_carrier_ok(tp->dev)) {
3230 tg3_readphy(tp, MII_BMSR, &bmsr);
3231 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3232 !(bmsr & BMSR_LSTATUS))
3233 force_reset = 1;
3234 }
3235 if (force_reset)
3236 tg3_phy_reset(tp);
3237
Matt Carlson79eb6902010-02-17 15:17:03 +00003238 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 tg3_readphy(tp, MII_BMSR, &bmsr);
3240 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00003241 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 bmsr = 0;
3243
3244 if (!(bmsr & BMSR_LSTATUS)) {
3245 err = tg3_init_5401phy_dsp(tp);
3246 if (err)
3247 return err;
3248
3249 tg3_readphy(tp, MII_BMSR, &bmsr);
3250 for (i = 0; i < 1000; i++) {
3251 udelay(10);
3252 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3253 (bmsr & BMSR_LSTATUS)) {
3254 udelay(40);
3255 break;
3256 }
3257 }
3258
Matt Carlson79eb6902010-02-17 15:17:03 +00003259 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3260 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 !(bmsr & BMSR_LSTATUS) &&
3262 tp->link_config.active_speed == SPEED_1000) {
3263 err = tg3_phy_reset(tp);
3264 if (!err)
3265 err = tg3_init_5401phy_dsp(tp);
3266 if (err)
3267 return err;
3268 }
3269 }
3270 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3271 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3272 /* 5701 {A0,B0} CRC bug workaround */
3273 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00003274 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3275 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
3276 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 }
3278
3279 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003280 tg3_readphy(tp, MII_TG3_ISTAT, &val);
3281 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003283 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003285 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3287
3288 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3289 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3290 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3291 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3292 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3293 else
3294 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3295 }
3296
3297 current_link_up = 0;
3298 current_speed = SPEED_INVALID;
3299 current_duplex = DUPLEX_INVALID;
3300
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003301 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00003302 err = tg3_phy_auxctl_read(tp,
3303 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3304 &val);
3305 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003306 tg3_phy_auxctl_write(tp,
3307 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3308 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 goto relink;
3310 }
3311 }
3312
3313 bmsr = 0;
3314 for (i = 0; i < 100; i++) {
3315 tg3_readphy(tp, MII_BMSR, &bmsr);
3316 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3317 (bmsr & BMSR_LSTATUS))
3318 break;
3319 udelay(40);
3320 }
3321
3322 if (bmsr & BMSR_LSTATUS) {
3323 u32 aux_stat, bmcr;
3324
3325 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3326 for (i = 0; i < 2000; i++) {
3327 udelay(10);
3328 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3329 aux_stat)
3330 break;
3331 }
3332
3333 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3334 &current_speed,
3335 &current_duplex);
3336
3337 bmcr = 0;
3338 for (i = 0; i < 200; i++) {
3339 tg3_readphy(tp, MII_BMCR, &bmcr);
3340 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3341 continue;
3342 if (bmcr && bmcr != 0x7fff)
3343 break;
3344 udelay(10);
3345 }
3346
Matt Carlsonef167e22007-12-20 20:10:01 -08003347 lcl_adv = 0;
3348 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349
Matt Carlsonef167e22007-12-20 20:10:01 -08003350 tp->link_config.active_speed = current_speed;
3351 tp->link_config.active_duplex = current_duplex;
3352
3353 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3354 if ((bmcr & BMCR_ANENABLE) &&
3355 tg3_copper_is_advertising_all(tp,
3356 tp->link_config.advertising)) {
3357 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3358 &rmt_adv))
3359 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 }
3361 } else {
3362 if (!(bmcr & BMCR_ANENABLE) &&
3363 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003364 tp->link_config.duplex == current_duplex &&
3365 tp->link_config.flowctrl ==
3366 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 }
3369 }
3370
Matt Carlsonef167e22007-12-20 20:10:01 -08003371 if (current_link_up == 1 &&
3372 tp->link_config.active_duplex == DUPLEX_FULL)
3373 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 }
3375
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376relink:
Matt Carlson80096062010-08-02 11:26:06 +00003377 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 tg3_phy_copper_begin(tp);
3379
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003380 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00003381 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
3382 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 current_link_up = 1;
3384 }
3385
3386 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3387 if (current_link_up == 1) {
3388 if (tp->link_config.active_speed == SPEED_100 ||
3389 tp->link_config.active_speed == SPEED_10)
3390 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3391 else
3392 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003393 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003394 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3395 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3397
3398 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3399 if (tp->link_config.active_duplex == DUPLEX_HALF)
3400 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3401
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003403 if (current_link_up == 1 &&
3404 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003406 else
3407 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 }
3409
3410 /* ??? Without this setting Netgear GA302T PHY does not
3411 * ??? send/receive packets...
3412 */
Matt Carlson79eb6902010-02-17 15:17:03 +00003413 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3415 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3416 tw32_f(MAC_MI_MODE, tp->mi_mode);
3417 udelay(80);
3418 }
3419
3420 tw32_f(MAC_MODE, tp->mac_mode);
3421 udelay(40);
3422
Matt Carlson52b02d02010-10-14 10:37:41 +00003423 tg3_phy_eee_adjust(tp, current_link_up);
3424
Joe Perches63c3a662011-04-26 08:12:10 +00003425 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 /* Polled via timer. */
3427 tw32_f(MAC_EVENT, 0);
3428 } else {
3429 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3430 }
3431 udelay(40);
3432
3433 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3434 current_link_up == 1 &&
3435 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00003436 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 udelay(120);
3438 tw32_f(MAC_STATUS,
3439 (MAC_STATUS_SYNC_CHANGED |
3440 MAC_STATUS_CFG_CHANGED));
3441 udelay(40);
3442 tg3_write_mem(tp,
3443 NIC_SRAM_FIRMWARE_MBOX,
3444 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3445 }
3446
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003447 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00003448 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003449 u16 oldlnkctl, newlnkctl;
3450
3451 pci_read_config_word(tp->pdev,
3452 tp->pcie_cap + PCI_EXP_LNKCTL,
3453 &oldlnkctl);
3454 if (tp->link_config.active_speed == SPEED_100 ||
3455 tp->link_config.active_speed == SPEED_10)
3456 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3457 else
3458 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3459 if (newlnkctl != oldlnkctl)
3460 pci_write_config_word(tp->pdev,
3461 tp->pcie_cap + PCI_EXP_LNKCTL,
3462 newlnkctl);
3463 }
3464
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 if (current_link_up != netif_carrier_ok(tp->dev)) {
3466 if (current_link_up)
3467 netif_carrier_on(tp->dev);
3468 else
3469 netif_carrier_off(tp->dev);
3470 tg3_link_report(tp);
3471 }
3472
3473 return 0;
3474}
3475
3476struct tg3_fiber_aneginfo {
3477 int state;
3478#define ANEG_STATE_UNKNOWN 0
3479#define ANEG_STATE_AN_ENABLE 1
3480#define ANEG_STATE_RESTART_INIT 2
3481#define ANEG_STATE_RESTART 3
3482#define ANEG_STATE_DISABLE_LINK_OK 4
3483#define ANEG_STATE_ABILITY_DETECT_INIT 5
3484#define ANEG_STATE_ABILITY_DETECT 6
3485#define ANEG_STATE_ACK_DETECT_INIT 7
3486#define ANEG_STATE_ACK_DETECT 8
3487#define ANEG_STATE_COMPLETE_ACK_INIT 9
3488#define ANEG_STATE_COMPLETE_ACK 10
3489#define ANEG_STATE_IDLE_DETECT_INIT 11
3490#define ANEG_STATE_IDLE_DETECT 12
3491#define ANEG_STATE_LINK_OK 13
3492#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3493#define ANEG_STATE_NEXT_PAGE_WAIT 15
3494
3495 u32 flags;
3496#define MR_AN_ENABLE 0x00000001
3497#define MR_RESTART_AN 0x00000002
3498#define MR_AN_COMPLETE 0x00000004
3499#define MR_PAGE_RX 0x00000008
3500#define MR_NP_LOADED 0x00000010
3501#define MR_TOGGLE_TX 0x00000020
3502#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3503#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3504#define MR_LP_ADV_SYM_PAUSE 0x00000100
3505#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3506#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3507#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3508#define MR_LP_ADV_NEXT_PAGE 0x00001000
3509#define MR_TOGGLE_RX 0x00002000
3510#define MR_NP_RX 0x00004000
3511
3512#define MR_LINK_OK 0x80000000
3513
3514 unsigned long link_time, cur_time;
3515
3516 u32 ability_match_cfg;
3517 int ability_match_count;
3518
3519 char ability_match, idle_match, ack_match;
3520
3521 u32 txconfig, rxconfig;
3522#define ANEG_CFG_NP 0x00000080
3523#define ANEG_CFG_ACK 0x00000040
3524#define ANEG_CFG_RF2 0x00000020
3525#define ANEG_CFG_RF1 0x00000010
3526#define ANEG_CFG_PS2 0x00000001
3527#define ANEG_CFG_PS1 0x00008000
3528#define ANEG_CFG_HD 0x00004000
3529#define ANEG_CFG_FD 0x00002000
3530#define ANEG_CFG_INVAL 0x00001f06
3531
3532};
3533#define ANEG_OK 0
3534#define ANEG_DONE 1
3535#define ANEG_TIMER_ENAB 2
3536#define ANEG_FAILED -1
3537
3538#define ANEG_STATE_SETTLE_TIME 10000
3539
3540static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3541 struct tg3_fiber_aneginfo *ap)
3542{
Matt Carlson5be73b42007-12-20 20:09:29 -08003543 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 unsigned long delta;
3545 u32 rx_cfg_reg;
3546 int ret;
3547
3548 if (ap->state == ANEG_STATE_UNKNOWN) {
3549 ap->rxconfig = 0;
3550 ap->link_time = 0;
3551 ap->cur_time = 0;
3552 ap->ability_match_cfg = 0;
3553 ap->ability_match_count = 0;
3554 ap->ability_match = 0;
3555 ap->idle_match = 0;
3556 ap->ack_match = 0;
3557 }
3558 ap->cur_time++;
3559
3560 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3561 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3562
3563 if (rx_cfg_reg != ap->ability_match_cfg) {
3564 ap->ability_match_cfg = rx_cfg_reg;
3565 ap->ability_match = 0;
3566 ap->ability_match_count = 0;
3567 } else {
3568 if (++ap->ability_match_count > 1) {
3569 ap->ability_match = 1;
3570 ap->ability_match_cfg = rx_cfg_reg;
3571 }
3572 }
3573 if (rx_cfg_reg & ANEG_CFG_ACK)
3574 ap->ack_match = 1;
3575 else
3576 ap->ack_match = 0;
3577
3578 ap->idle_match = 0;
3579 } else {
3580 ap->idle_match = 1;
3581 ap->ability_match_cfg = 0;
3582 ap->ability_match_count = 0;
3583 ap->ability_match = 0;
3584 ap->ack_match = 0;
3585
3586 rx_cfg_reg = 0;
3587 }
3588
3589 ap->rxconfig = rx_cfg_reg;
3590 ret = ANEG_OK;
3591
Matt Carlson33f401a2010-04-05 10:19:27 +00003592 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 case ANEG_STATE_UNKNOWN:
3594 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3595 ap->state = ANEG_STATE_AN_ENABLE;
3596
3597 /* fallthru */
3598 case ANEG_STATE_AN_ENABLE:
3599 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3600 if (ap->flags & MR_AN_ENABLE) {
3601 ap->link_time = 0;
3602 ap->cur_time = 0;
3603 ap->ability_match_cfg = 0;
3604 ap->ability_match_count = 0;
3605 ap->ability_match = 0;
3606 ap->idle_match = 0;
3607 ap->ack_match = 0;
3608
3609 ap->state = ANEG_STATE_RESTART_INIT;
3610 } else {
3611 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3612 }
3613 break;
3614
3615 case ANEG_STATE_RESTART_INIT:
3616 ap->link_time = ap->cur_time;
3617 ap->flags &= ~(MR_NP_LOADED);
3618 ap->txconfig = 0;
3619 tw32(MAC_TX_AUTO_NEG, 0);
3620 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3621 tw32_f(MAC_MODE, tp->mac_mode);
3622 udelay(40);
3623
3624 ret = ANEG_TIMER_ENAB;
3625 ap->state = ANEG_STATE_RESTART;
3626
3627 /* fallthru */
3628 case ANEG_STATE_RESTART:
3629 delta = ap->cur_time - ap->link_time;
Matt Carlson859a5882010-04-05 10:19:28 +00003630 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a5882010-04-05 10:19:28 +00003632 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 break;
3635
3636 case ANEG_STATE_DISABLE_LINK_OK:
3637 ret = ANEG_DONE;
3638 break;
3639
3640 case ANEG_STATE_ABILITY_DETECT_INIT:
3641 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003642 ap->txconfig = ANEG_CFG_FD;
3643 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3644 if (flowctrl & ADVERTISE_1000XPAUSE)
3645 ap->txconfig |= ANEG_CFG_PS1;
3646 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3647 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3649 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3650 tw32_f(MAC_MODE, tp->mac_mode);
3651 udelay(40);
3652
3653 ap->state = ANEG_STATE_ABILITY_DETECT;
3654 break;
3655
3656 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a5882010-04-05 10:19:28 +00003657 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 break;
3660
3661 case ANEG_STATE_ACK_DETECT_INIT:
3662 ap->txconfig |= ANEG_CFG_ACK;
3663 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3664 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3665 tw32_f(MAC_MODE, tp->mac_mode);
3666 udelay(40);
3667
3668 ap->state = ANEG_STATE_ACK_DETECT;
3669
3670 /* fallthru */
3671 case ANEG_STATE_ACK_DETECT:
3672 if (ap->ack_match != 0) {
3673 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3674 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3675 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3676 } else {
3677 ap->state = ANEG_STATE_AN_ENABLE;
3678 }
3679 } else if (ap->ability_match != 0 &&
3680 ap->rxconfig == 0) {
3681 ap->state = ANEG_STATE_AN_ENABLE;
3682 }
3683 break;
3684
3685 case ANEG_STATE_COMPLETE_ACK_INIT:
3686 if (ap->rxconfig & ANEG_CFG_INVAL) {
3687 ret = ANEG_FAILED;
3688 break;
3689 }
3690 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3691 MR_LP_ADV_HALF_DUPLEX |
3692 MR_LP_ADV_SYM_PAUSE |
3693 MR_LP_ADV_ASYM_PAUSE |
3694 MR_LP_ADV_REMOTE_FAULT1 |
3695 MR_LP_ADV_REMOTE_FAULT2 |
3696 MR_LP_ADV_NEXT_PAGE |
3697 MR_TOGGLE_RX |
3698 MR_NP_RX);
3699 if (ap->rxconfig & ANEG_CFG_FD)
3700 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3701 if (ap->rxconfig & ANEG_CFG_HD)
3702 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3703 if (ap->rxconfig & ANEG_CFG_PS1)
3704 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3705 if (ap->rxconfig & ANEG_CFG_PS2)
3706 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3707 if (ap->rxconfig & ANEG_CFG_RF1)
3708 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3709 if (ap->rxconfig & ANEG_CFG_RF2)
3710 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3711 if (ap->rxconfig & ANEG_CFG_NP)
3712 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3713
3714 ap->link_time = ap->cur_time;
3715
3716 ap->flags ^= (MR_TOGGLE_TX);
3717 if (ap->rxconfig & 0x0008)
3718 ap->flags |= MR_TOGGLE_RX;
3719 if (ap->rxconfig & ANEG_CFG_NP)
3720 ap->flags |= MR_NP_RX;
3721 ap->flags |= MR_PAGE_RX;
3722
3723 ap->state = ANEG_STATE_COMPLETE_ACK;
3724 ret = ANEG_TIMER_ENAB;
3725 break;
3726
3727 case ANEG_STATE_COMPLETE_ACK:
3728 if (ap->ability_match != 0 &&
3729 ap->rxconfig == 0) {
3730 ap->state = ANEG_STATE_AN_ENABLE;
3731 break;
3732 }
3733 delta = ap->cur_time - ap->link_time;
3734 if (delta > ANEG_STATE_SETTLE_TIME) {
3735 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3736 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3737 } else {
3738 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3739 !(ap->flags & MR_NP_RX)) {
3740 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3741 } else {
3742 ret = ANEG_FAILED;
3743 }
3744 }
3745 }
3746 break;
3747
3748 case ANEG_STATE_IDLE_DETECT_INIT:
3749 ap->link_time = ap->cur_time;
3750 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3751 tw32_f(MAC_MODE, tp->mac_mode);
3752 udelay(40);
3753
3754 ap->state = ANEG_STATE_IDLE_DETECT;
3755 ret = ANEG_TIMER_ENAB;
3756 break;
3757
3758 case ANEG_STATE_IDLE_DETECT:
3759 if (ap->ability_match != 0 &&
3760 ap->rxconfig == 0) {
3761 ap->state = ANEG_STATE_AN_ENABLE;
3762 break;
3763 }
3764 delta = ap->cur_time - ap->link_time;
3765 if (delta > ANEG_STATE_SETTLE_TIME) {
3766 /* XXX another gem from the Broadcom driver :( */
3767 ap->state = ANEG_STATE_LINK_OK;
3768 }
3769 break;
3770
3771 case ANEG_STATE_LINK_OK:
3772 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3773 ret = ANEG_DONE;
3774 break;
3775
3776 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3777 /* ??? unimplemented */
3778 break;
3779
3780 case ANEG_STATE_NEXT_PAGE_WAIT:
3781 /* ??? unimplemented */
3782 break;
3783
3784 default:
3785 ret = ANEG_FAILED;
3786 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788
3789 return ret;
3790}
3791
Matt Carlson5be73b42007-12-20 20:09:29 -08003792static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793{
3794 int res = 0;
3795 struct tg3_fiber_aneginfo aninfo;
3796 int status = ANEG_FAILED;
3797 unsigned int tick;
3798 u32 tmp;
3799
3800 tw32_f(MAC_TX_AUTO_NEG, 0);
3801
3802 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3803 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3804 udelay(40);
3805
3806 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3807 udelay(40);
3808
3809 memset(&aninfo, 0, sizeof(aninfo));
3810 aninfo.flags |= MR_AN_ENABLE;
3811 aninfo.state = ANEG_STATE_UNKNOWN;
3812 aninfo.cur_time = 0;
3813 tick = 0;
3814 while (++tick < 195000) {
3815 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3816 if (status == ANEG_DONE || status == ANEG_FAILED)
3817 break;
3818
3819 udelay(1);
3820 }
3821
3822 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3823 tw32_f(MAC_MODE, tp->mac_mode);
3824 udelay(40);
3825
Matt Carlson5be73b42007-12-20 20:09:29 -08003826 *txflags = aninfo.txconfig;
3827 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828
3829 if (status == ANEG_DONE &&
3830 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3831 MR_LP_ADV_FULL_DUPLEX)))
3832 res = 1;
3833
3834 return res;
3835}
3836
3837static void tg3_init_bcm8002(struct tg3 *tp)
3838{
3839 u32 mac_status = tr32(MAC_STATUS);
3840 int i;
3841
3842 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00003843 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 !(mac_status & MAC_STATUS_PCS_SYNCED))
3845 return;
3846
3847 /* Set PLL lock range. */
3848 tg3_writephy(tp, 0x16, 0x8007);
3849
3850 /* SW reset */
3851 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
3852
3853 /* Wait for reset to complete. */
3854 /* XXX schedule_timeout() ... */
3855 for (i = 0; i < 500; i++)
3856 udelay(10);
3857
3858 /* Config mode; select PMA/Ch 1 regs. */
3859 tg3_writephy(tp, 0x10, 0x8411);
3860
3861 /* Enable auto-lock and comdet, select txclk for tx. */
3862 tg3_writephy(tp, 0x11, 0x0a10);
3863
3864 tg3_writephy(tp, 0x18, 0x00a0);
3865 tg3_writephy(tp, 0x16, 0x41ff);
3866
3867 /* Assert and deassert POR. */
3868 tg3_writephy(tp, 0x13, 0x0400);
3869 udelay(40);
3870 tg3_writephy(tp, 0x13, 0x0000);
3871
3872 tg3_writephy(tp, 0x11, 0x0a50);
3873 udelay(40);
3874 tg3_writephy(tp, 0x11, 0x0a10);
3875
3876 /* Wait for signal to stabilize */
3877 /* XXX schedule_timeout() ... */
3878 for (i = 0; i < 15000; i++)
3879 udelay(10);
3880
3881 /* Deselect the channel register so we can read the PHYID
3882 * later.
3883 */
3884 tg3_writephy(tp, 0x10, 0x8011);
3885}
3886
3887static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
3888{
Matt Carlson82cd3d12007-12-20 20:09:00 -08003889 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 u32 sg_dig_ctrl, sg_dig_status;
3891 u32 serdes_cfg, expected_sg_dig_ctrl;
3892 int workaround, port_a;
3893 int current_link_up;
3894
3895 serdes_cfg = 0;
3896 expected_sg_dig_ctrl = 0;
3897 workaround = 0;
3898 port_a = 1;
3899 current_link_up = 0;
3900
3901 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
3902 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
3903 workaround = 1;
3904 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
3905 port_a = 0;
3906
3907 /* preserve bits 0-11,13,14 for signal pre-emphasis */
3908 /* preserve bits 20-23 for voltage regulator */
3909 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
3910 }
3911
3912 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3913
3914 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003915 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 if (workaround) {
3917 u32 val = serdes_cfg;
3918
3919 if (port_a)
3920 val |= 0xc010000;
3921 else
3922 val |= 0x4010000;
3923 tw32_f(MAC_SERDES_CFG, val);
3924 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003925
3926 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 }
3928 if (mac_status & MAC_STATUS_PCS_SYNCED) {
3929 tg3_setup_flow_control(tp, 0, 0);
3930 current_link_up = 1;
3931 }
3932 goto out;
3933 }
3934
3935 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003936 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937
Matt Carlson82cd3d12007-12-20 20:09:00 -08003938 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3939 if (flowctrl & ADVERTISE_1000XPAUSE)
3940 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
3941 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3942 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943
3944 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003945 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07003946 tp->serdes_counter &&
3947 ((mac_status & (MAC_STATUS_PCS_SYNCED |
3948 MAC_STATUS_RCVD_CFG)) ==
3949 MAC_STATUS_PCS_SYNCED)) {
3950 tp->serdes_counter--;
3951 current_link_up = 1;
3952 goto out;
3953 }
3954restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 if (workaround)
3956 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003957 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 udelay(5);
3959 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
3960
Michael Chan3d3ebe72006-09-27 15:59:15 -07003961 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003962 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
3964 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003965 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 mac_status = tr32(MAC_STATUS);
3967
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003968 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08003970 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971
Matt Carlson82cd3d12007-12-20 20:09:00 -08003972 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
3973 local_adv |= ADVERTISE_1000XPAUSE;
3974 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
3975 local_adv |= ADVERTISE_1000XPSE_ASYM;
3976
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003977 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003978 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003979 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003980 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981
3982 tg3_setup_flow_control(tp, local_adv, remote_adv);
3983 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003984 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003985 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003986 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003987 if (tp->serdes_counter)
3988 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 else {
3990 if (workaround) {
3991 u32 val = serdes_cfg;
3992
3993 if (port_a)
3994 val |= 0xc010000;
3995 else
3996 val |= 0x4010000;
3997
3998 tw32_f(MAC_SERDES_CFG, val);
3999 }
4000
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004001 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 udelay(40);
4003
4004 /* Link parallel detection - link is up */
4005 /* only if we have PCS_SYNC and not */
4006 /* receiving config code words */
4007 mac_status = tr32(MAC_STATUS);
4008 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
4009 !(mac_status & MAC_STATUS_RCVD_CFG)) {
4010 tg3_setup_flow_control(tp, 0, 0);
4011 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004012 tp->phy_flags |=
4013 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004014 tp->serdes_counter =
4015 SERDES_PARALLEL_DET_TIMEOUT;
4016 } else
4017 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 }
4019 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07004020 } else {
4021 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004022 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 }
4024
4025out:
4026 return current_link_up;
4027}
4028
4029static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
4030{
4031 int current_link_up = 0;
4032
Michael Chan5cf64b82007-05-05 12:11:21 -07004033 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035
4036 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08004037 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004039
Matt Carlson5be73b42007-12-20 20:09:29 -08004040 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4041 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042
Matt Carlson5be73b42007-12-20 20:09:29 -08004043 if (txflags & ANEG_CFG_PS1)
4044 local_adv |= ADVERTISE_1000XPAUSE;
4045 if (txflags & ANEG_CFG_PS2)
4046 local_adv |= ADVERTISE_1000XPSE_ASYM;
4047
4048 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4049 remote_adv |= LPA_1000XPAUSE;
4050 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4051 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052
4053 tg3_setup_flow_control(tp, local_adv, remote_adv);
4054
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 current_link_up = 1;
4056 }
4057 for (i = 0; i < 30; i++) {
4058 udelay(20);
4059 tw32_f(MAC_STATUS,
4060 (MAC_STATUS_SYNC_CHANGED |
4061 MAC_STATUS_CFG_CHANGED));
4062 udelay(40);
4063 if ((tr32(MAC_STATUS) &
4064 (MAC_STATUS_SYNC_CHANGED |
4065 MAC_STATUS_CFG_CHANGED)) == 0)
4066 break;
4067 }
4068
4069 mac_status = tr32(MAC_STATUS);
4070 if (current_link_up == 0 &&
4071 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4072 !(mac_status & MAC_STATUS_RCVD_CFG))
4073 current_link_up = 1;
4074 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004075 tg3_setup_flow_control(tp, 0, 0);
4076
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 /* Forcing 1000FD link up. */
4078 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079
4080 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4081 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004082
4083 tw32_f(MAC_MODE, tp->mac_mode);
4084 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 }
4086
4087out:
4088 return current_link_up;
4089}
4090
4091static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4092{
4093 u32 orig_pause_cfg;
4094 u16 orig_active_speed;
4095 u8 orig_active_duplex;
4096 u32 mac_status;
4097 int current_link_up;
4098 int i;
4099
Matt Carlson8d018622007-12-20 20:05:44 -08004100 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 orig_active_speed = tp->link_config.active_speed;
4102 orig_active_duplex = tp->link_config.active_duplex;
4103
Joe Perches63c3a662011-04-26 08:12:10 +00004104 if (!tg3_flag(tp, HW_AUTONEG) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 netif_carrier_ok(tp->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004106 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 mac_status = tr32(MAC_STATUS);
4108 mac_status &= (MAC_STATUS_PCS_SYNCED |
4109 MAC_STATUS_SIGNAL_DET |
4110 MAC_STATUS_CFG_CHANGED |
4111 MAC_STATUS_RCVD_CFG);
4112 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4113 MAC_STATUS_SIGNAL_DET)) {
4114 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4115 MAC_STATUS_CFG_CHANGED));
4116 return 0;
4117 }
4118 }
4119
4120 tw32_f(MAC_TX_AUTO_NEG, 0);
4121
4122 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4123 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
4124 tw32_f(MAC_MODE, tp->mac_mode);
4125 udelay(40);
4126
Matt Carlson79eb6902010-02-17 15:17:03 +00004127 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 tg3_init_bcm8002(tp);
4129
4130 /* Enable link change event even when serdes polling. */
4131 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4132 udelay(40);
4133
4134 current_link_up = 0;
4135 mac_status = tr32(MAC_STATUS);
4136
Joe Perches63c3a662011-04-26 08:12:10 +00004137 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4139 else
4140 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4141
Matt Carlson898a56f2009-08-28 14:02:40 +00004142 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00004144 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145
4146 for (i = 0; i < 100; i++) {
4147 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4148 MAC_STATUS_CFG_CHANGED));
4149 udelay(5);
4150 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07004151 MAC_STATUS_CFG_CHANGED |
4152 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 break;
4154 }
4155
4156 mac_status = tr32(MAC_STATUS);
4157 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4158 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004159 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4160 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 tw32_f(MAC_MODE, (tp->mac_mode |
4162 MAC_MODE_SEND_CONFIGS));
4163 udelay(1);
4164 tw32_f(MAC_MODE, tp->mac_mode);
4165 }
4166 }
4167
4168 if (current_link_up == 1) {
4169 tp->link_config.active_speed = SPEED_1000;
4170 tp->link_config.active_duplex = DUPLEX_FULL;
4171 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4172 LED_CTRL_LNKLED_OVERRIDE |
4173 LED_CTRL_1000MBPS_ON));
4174 } else {
4175 tp->link_config.active_speed = SPEED_INVALID;
4176 tp->link_config.active_duplex = DUPLEX_INVALID;
4177 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4178 LED_CTRL_LNKLED_OVERRIDE |
4179 LED_CTRL_TRAFFIC_OVERRIDE));
4180 }
4181
4182 if (current_link_up != netif_carrier_ok(tp->dev)) {
4183 if (current_link_up)
4184 netif_carrier_on(tp->dev);
4185 else
4186 netif_carrier_off(tp->dev);
4187 tg3_link_report(tp);
4188 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004189 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 if (orig_pause_cfg != now_pause_cfg ||
4191 orig_active_speed != tp->link_config.active_speed ||
4192 orig_active_duplex != tp->link_config.active_duplex)
4193 tg3_link_report(tp);
4194 }
4195
4196 return 0;
4197}
4198
Michael Chan747e8f82005-07-25 12:33:22 -07004199static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4200{
4201 int current_link_up, err = 0;
4202 u32 bmsr, bmcr;
4203 u16 current_speed;
4204 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004205 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004206
4207 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4208 tw32_f(MAC_MODE, tp->mac_mode);
4209 udelay(40);
4210
4211 tw32(MAC_EVENT, 0);
4212
4213 tw32_f(MAC_STATUS,
4214 (MAC_STATUS_SYNC_CHANGED |
4215 MAC_STATUS_CFG_CHANGED |
4216 MAC_STATUS_MI_COMPLETION |
4217 MAC_STATUS_LNKSTATE_CHANGED));
4218 udelay(40);
4219
4220 if (force_reset)
4221 tg3_phy_reset(tp);
4222
4223 current_link_up = 0;
4224 current_speed = SPEED_INVALID;
4225 current_duplex = DUPLEX_INVALID;
4226
4227 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4228 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004229 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4230 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4231 bmsr |= BMSR_LSTATUS;
4232 else
4233 bmsr &= ~BMSR_LSTATUS;
4234 }
Michael Chan747e8f82005-07-25 12:33:22 -07004235
4236 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4237
4238 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004239 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004240 /* do nothing, just check for link up at the end */
4241 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4242 u32 adv, new_adv;
4243
4244 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4245 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4246 ADVERTISE_1000XPAUSE |
4247 ADVERTISE_1000XPSE_ASYM |
4248 ADVERTISE_SLCT);
4249
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004250 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004251
4252 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4253 new_adv |= ADVERTISE_1000XHALF;
4254 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4255 new_adv |= ADVERTISE_1000XFULL;
4256
4257 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4258 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4259 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4260 tg3_writephy(tp, MII_BMCR, bmcr);
4261
4262 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004263 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004264 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004265
4266 return err;
4267 }
4268 } else {
4269 u32 new_bmcr;
4270
4271 bmcr &= ~BMCR_SPEED1000;
4272 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4273
4274 if (tp->link_config.duplex == DUPLEX_FULL)
4275 new_bmcr |= BMCR_FULLDPLX;
4276
4277 if (new_bmcr != bmcr) {
4278 /* BMCR_SPEED1000 is a reserved bit that needs
4279 * to be set on write.
4280 */
4281 new_bmcr |= BMCR_SPEED1000;
4282
4283 /* Force a linkdown */
4284 if (netif_carrier_ok(tp->dev)) {
4285 u32 adv;
4286
4287 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4288 adv &= ~(ADVERTISE_1000XFULL |
4289 ADVERTISE_1000XHALF |
4290 ADVERTISE_SLCT);
4291 tg3_writephy(tp, MII_ADVERTISE, adv);
4292 tg3_writephy(tp, MII_BMCR, bmcr |
4293 BMCR_ANRESTART |
4294 BMCR_ANENABLE);
4295 udelay(10);
4296 netif_carrier_off(tp->dev);
4297 }
4298 tg3_writephy(tp, MII_BMCR, new_bmcr);
4299 bmcr = new_bmcr;
4300 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4301 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004302 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4303 ASIC_REV_5714) {
4304 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4305 bmsr |= BMSR_LSTATUS;
4306 else
4307 bmsr &= ~BMSR_LSTATUS;
4308 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004309 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004310 }
4311 }
4312
4313 if (bmsr & BMSR_LSTATUS) {
4314 current_speed = SPEED_1000;
4315 current_link_up = 1;
4316 if (bmcr & BMCR_FULLDPLX)
4317 current_duplex = DUPLEX_FULL;
4318 else
4319 current_duplex = DUPLEX_HALF;
4320
Matt Carlsonef167e22007-12-20 20:10:01 -08004321 local_adv = 0;
4322 remote_adv = 0;
4323
Michael Chan747e8f82005-07-25 12:33:22 -07004324 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004325 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004326
4327 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4328 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4329 common = local_adv & remote_adv;
4330 if (common & (ADVERTISE_1000XHALF |
4331 ADVERTISE_1000XFULL)) {
4332 if (common & ADVERTISE_1000XFULL)
4333 current_duplex = DUPLEX_FULL;
4334 else
4335 current_duplex = DUPLEX_HALF;
Joe Perches63c3a662011-04-26 08:12:10 +00004336 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00004337 /* Link is up via parallel detect */
Matt Carlson859a5882010-04-05 10:19:28 +00004338 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07004339 current_link_up = 0;
Matt Carlson859a5882010-04-05 10:19:28 +00004340 }
Michael Chan747e8f82005-07-25 12:33:22 -07004341 }
4342 }
4343
Matt Carlsonef167e22007-12-20 20:10:01 -08004344 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4345 tg3_setup_flow_control(tp, local_adv, remote_adv);
4346
Michael Chan747e8f82005-07-25 12:33:22 -07004347 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4348 if (tp->link_config.active_duplex == DUPLEX_HALF)
4349 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4350
4351 tw32_f(MAC_MODE, tp->mac_mode);
4352 udelay(40);
4353
4354 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4355
4356 tp->link_config.active_speed = current_speed;
4357 tp->link_config.active_duplex = current_duplex;
4358
4359 if (current_link_up != netif_carrier_ok(tp->dev)) {
4360 if (current_link_up)
4361 netif_carrier_on(tp->dev);
4362 else {
4363 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004364 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004365 }
4366 tg3_link_report(tp);
4367 }
4368 return err;
4369}
4370
4371static void tg3_serdes_parallel_detect(struct tg3 *tp)
4372{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004373 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004374 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004375 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004376 return;
4377 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004378
Michael Chan747e8f82005-07-25 12:33:22 -07004379 if (!netif_carrier_ok(tp->dev) &&
4380 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4381 u32 bmcr;
4382
4383 tg3_readphy(tp, MII_BMCR, &bmcr);
4384 if (bmcr & BMCR_ANENABLE) {
4385 u32 phy1, phy2;
4386
4387 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004388 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
4389 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07004390
4391 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004392 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4393 MII_TG3_DSP_EXP1_INT_STAT);
4394 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
4395 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004396
4397 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4398 /* We have signal detect and not receiving
4399 * config code words, link is up by parallel
4400 * detection.
4401 */
4402
4403 bmcr &= ~BMCR_ANENABLE;
4404 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4405 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004406 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004407 }
4408 }
Matt Carlson859a5882010-04-05 10:19:28 +00004409 } else if (netif_carrier_ok(tp->dev) &&
4410 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004411 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004412 u32 phy2;
4413
4414 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004415 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4416 MII_TG3_DSP_EXP1_INT_STAT);
4417 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004418 if (phy2 & 0x20) {
4419 u32 bmcr;
4420
4421 /* Config code words received, turn on autoneg. */
4422 tg3_readphy(tp, MII_BMCR, &bmcr);
4423 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4424
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004425 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004426
4427 }
4428 }
4429}
4430
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4432{
Matt Carlsonf2096f92011-04-05 14:22:48 +00004433 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 int err;
4435
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004436 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004438 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07004439 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a5882010-04-05 10:19:28 +00004440 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442
Matt Carlsonbcb37f62008-11-03 16:52:09 -08004443 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00004444 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08004445
4446 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4447 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4448 scale = 65;
4449 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4450 scale = 6;
4451 else
4452 scale = 12;
4453
4454 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4455 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4456 tw32(GRC_MISC_CFG, val);
4457 }
4458
Matt Carlsonf2096f92011-04-05 14:22:48 +00004459 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4460 (6 << TX_LENGTHS_IPG_SHIFT);
4461 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
4462 val |= tr32(MAC_TX_LENGTHS) &
4463 (TX_LENGTHS_JMB_FRM_LEN_MSK |
4464 TX_LENGTHS_CNT_DWN_VAL_MSK);
4465
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 if (tp->link_config.active_speed == SPEED_1000 &&
4467 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00004468 tw32(MAC_TX_LENGTHS, val |
4469 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00004471 tw32(MAC_TX_LENGTHS, val |
4472 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473
Joe Perches63c3a662011-04-26 08:12:10 +00004474 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 if (netif_carrier_ok(tp->dev)) {
4476 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07004477 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 } else {
4479 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4480 }
4481 }
4482
Joe Perches63c3a662011-04-26 08:12:10 +00004483 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00004484 val = tr32(PCIE_PWR_MGMT_THRESH);
Matt Carlson8ed5d972007-05-07 00:25:49 -07004485 if (!netif_carrier_ok(tp->dev))
4486 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4487 tp->pwrmgmt_thresh;
4488 else
4489 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4490 tw32(PCIE_PWR_MGMT_THRESH, val);
4491 }
4492
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 return err;
4494}
4495
Matt Carlson66cfd1b2010-09-30 10:34:30 +00004496static inline int tg3_irq_sync(struct tg3 *tp)
4497{
4498 return tp->irq_sync;
4499}
4500
Matt Carlson97bd8e42011-04-13 11:05:04 +00004501static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
4502{
4503 int i;
4504
4505 dst = (u32 *)((u8 *)dst + off);
4506 for (i = 0; i < len; i += sizeof(u32))
4507 *dst++ = tr32(off + i);
4508}
4509
4510static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
4511{
4512 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
4513 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
4514 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
4515 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
4516 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
4517 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
4518 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
4519 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
4520 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
4521 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
4522 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
4523 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
4524 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
4525 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
4526 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
4527 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
4528 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
4529 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
4530 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
4531
Joe Perches63c3a662011-04-26 08:12:10 +00004532 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00004533 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
4534
4535 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
4536 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
4537 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
4538 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
4539 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
4540 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
4541 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
4542 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
4543
Joe Perches63c3a662011-04-26 08:12:10 +00004544 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00004545 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
4546 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
4547 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
4548 }
4549
4550 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
4551 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
4552 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
4553 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
4554 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
4555
Joe Perches63c3a662011-04-26 08:12:10 +00004556 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00004557 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
4558}
4559
4560static void tg3_dump_state(struct tg3 *tp)
4561{
4562 int i;
4563 u32 *regs;
4564
4565 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
4566 if (!regs) {
4567 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
4568 return;
4569 }
4570
Joe Perches63c3a662011-04-26 08:12:10 +00004571 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00004572 /* Read up to but not including private PCI registers */
4573 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
4574 regs[i / sizeof(u32)] = tr32(i);
4575 } else
4576 tg3_dump_legacy_regs(tp, regs);
4577
4578 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
4579 if (!regs[i + 0] && !regs[i + 1] &&
4580 !regs[i + 2] && !regs[i + 3])
4581 continue;
4582
4583 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
4584 i * 4,
4585 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
4586 }
4587
4588 kfree(regs);
4589
4590 for (i = 0; i < tp->irq_cnt; i++) {
4591 struct tg3_napi *tnapi = &tp->napi[i];
4592
4593 /* SW status block */
4594 netdev_err(tp->dev,
4595 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
4596 i,
4597 tnapi->hw_status->status,
4598 tnapi->hw_status->status_tag,
4599 tnapi->hw_status->rx_jumbo_consumer,
4600 tnapi->hw_status->rx_consumer,
4601 tnapi->hw_status->rx_mini_consumer,
4602 tnapi->hw_status->idx[0].rx_producer,
4603 tnapi->hw_status->idx[0].tx_consumer);
4604
4605 netdev_err(tp->dev,
4606 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
4607 i,
4608 tnapi->last_tag, tnapi->last_irq_tag,
4609 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
4610 tnapi->rx_rcb_ptr,
4611 tnapi->prodring.rx_std_prod_idx,
4612 tnapi->prodring.rx_std_cons_idx,
4613 tnapi->prodring.rx_jmb_prod_idx,
4614 tnapi->prodring.rx_jmb_cons_idx);
4615 }
4616}
4617
Michael Chandf3e6542006-05-26 17:48:07 -07004618/* This is called whenever we suspect that the system chipset is re-
4619 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4620 * is bogus tx completions. We try to recover by setting the
4621 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4622 * in the workqueue.
4623 */
4624static void tg3_tx_recover(struct tg3 *tp)
4625{
Joe Perches63c3a662011-04-26 08:12:10 +00004626 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07004627 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4628
Matt Carlson5129c3a2010-04-05 10:19:23 +00004629 netdev_warn(tp->dev,
4630 "The system may be re-ordering memory-mapped I/O "
4631 "cycles to the network device, attempting to recover. "
4632 "Please report the problem to the driver maintainer "
4633 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07004634
4635 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00004636 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07004637 spin_unlock(&tp->lock);
4638}
4639
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004640static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07004641{
Matt Carlsonf65aac12010-08-02 11:26:03 +00004642 /* Tell compiler to fetch tx indices from memory. */
4643 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004644 return tnapi->tx_pending -
4645 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07004646}
4647
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648/* Tigon3 never reports partial packet sends. So we do not
4649 * need special logic to handle SKBs that have not had all
4650 * of their frags sent yet, like SunGEM does.
4651 */
Matt Carlson17375d22009-08-28 14:02:18 +00004652static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653{
Matt Carlson17375d22009-08-28 14:02:18 +00004654 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004655 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004656 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004657 struct netdev_queue *txq;
4658 int index = tnapi - tp->napi;
4659
Joe Perches63c3a662011-04-26 08:12:10 +00004660 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004661 index--;
4662
4663 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664
4665 while (sw_idx != hw_idx) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00004666 struct ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004668 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669
Michael Chandf3e6542006-05-26 17:48:07 -07004670 if (unlikely(skb == NULL)) {
4671 tg3_tx_recover(tp);
4672 return;
4673 }
4674
Alexander Duyckf4188d82009-12-02 16:48:38 +00004675 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004676 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004677 skb_headlen(skb),
4678 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679
4680 ri->skb = NULL;
4681
4682 sw_idx = NEXT_TX(sw_idx);
4683
4684 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004685 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004686 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4687 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00004688
4689 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004690 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004691 skb_shinfo(skb)->frags[i].size,
4692 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 sw_idx = NEXT_TX(sw_idx);
4694 }
4695
David S. Millerf47c11e2005-06-24 20:18:35 -07004696 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004697
4698 if (unlikely(tx_bug)) {
4699 tg3_tx_recover(tp);
4700 return;
4701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 }
4703
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004704 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705
Michael Chan1b2a7202006-08-07 21:46:02 -07004706 /* Need to make the tx_cons update visible to tg3_start_xmit()
4707 * before checking for netif_queue_stopped(). Without the
4708 * memory barrier, there is a small possibility that tg3_start_xmit()
4709 * will miss it and cause the queue to be stopped forever.
4710 */
4711 smp_mb();
4712
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004713 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004714 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004715 __netif_tx_lock(txq, smp_processor_id());
4716 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004717 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004718 netif_tx_wake_queue(txq);
4719 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07004720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721}
4722
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004723static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
4724{
4725 if (!ri->skb)
4726 return;
4727
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004728 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004729 map_sz, PCI_DMA_FROMDEVICE);
4730 dev_kfree_skb_any(ri->skb);
4731 ri->skb = NULL;
4732}
4733
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734/* Returns size of skb allocated or < 0 on error.
4735 *
4736 * We only need to fill in the address because the other members
4737 * of the RX descriptor are invariant, see tg3_init_rings.
4738 *
4739 * Note the purposeful assymetry of cpu vs. chip accesses. For
4740 * posting buffers we only dirty the first cache line of the RX
4741 * descriptor (containing the address). Whereas for the RX status
4742 * buffers the cpu only reads the last cacheline of the RX descriptor
4743 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4744 */
Matt Carlson86b21e52009-11-13 13:03:45 +00004745static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00004746 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747{
4748 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00004749 struct ring_info *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 struct sk_buff *skb;
4751 dma_addr_t mapping;
4752 int skb_size, dest_idx;
4753
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 switch (opaque_key) {
4755 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004756 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00004757 desc = &tpr->rx_std[dest_idx];
4758 map = &tpr->rx_std_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004759 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 break;
4761
4762 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004763 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004764 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004765 map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004766 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 break;
4768
4769 default:
4770 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772
4773 /* Do not overwrite any of the map or rp information
4774 * until we are sure we can commit to a new buffer.
4775 *
4776 * Callers depend upon this behavior and assume that
4777 * we leave everything unchanged if we fail.
4778 */
Matt Carlson287be122009-08-28 13:58:46 +00004779 skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 if (skb == NULL)
4781 return -ENOMEM;
4782
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 skb_reserve(skb, tp->rx_offset);
4784
Matt Carlson287be122009-08-28 13:58:46 +00004785 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00004787 if (pci_dma_mapping_error(tp->pdev, mapping)) {
4788 dev_kfree_skb(skb);
4789 return -EIO;
4790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791
4792 map->skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004793 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 desc->addr_hi = ((u64)mapping >> 32);
4796 desc->addr_lo = ((u64)mapping & 0xffffffff);
4797
4798 return skb_size;
4799}
4800
4801/* We only need to move over in the address because the other
4802 * members of the RX descriptor are invariant. See notes above
4803 * tg3_alloc_rx_skb for full details.
4804 */
Matt Carlsona3896162009-11-13 13:03:44 +00004805static void tg3_recycle_rx(struct tg3_napi *tnapi,
4806 struct tg3_rx_prodring_set *dpr,
4807 u32 opaque_key, int src_idx,
4808 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809{
Matt Carlson17375d22009-08-28 14:02:18 +00004810 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4812 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004813 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004814 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815
4816 switch (opaque_key) {
4817 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004818 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004819 dest_desc = &dpr->rx_std[dest_idx];
4820 dest_map = &dpr->rx_std_buffers[dest_idx];
4821 src_desc = &spr->rx_std[src_idx];
4822 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 break;
4824
4825 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004826 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004827 dest_desc = &dpr->rx_jmb[dest_idx].std;
4828 dest_map = &dpr->rx_jmb_buffers[dest_idx];
4829 src_desc = &spr->rx_jmb[src_idx].std;
4830 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 break;
4832
4833 default:
4834 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836
4837 dest_map->skb = src_map->skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004838 dma_unmap_addr_set(dest_map, mapping,
4839 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 dest_desc->addr_hi = src_desc->addr_hi;
4841 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00004842
4843 /* Ensure that the update to the skb happens after the physical
4844 * addresses have been transferred to the new BD location.
4845 */
4846 smp_wmb();
4847
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 src_map->skb = NULL;
4849}
4850
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851/* The RX ring scheme is composed of multiple rings which post fresh
4852 * buffers to the chip, and one special ring the chip uses to report
4853 * status back to the host.
4854 *
4855 * The special ring reports the status of received packets to the
4856 * host. The chip does not write into the original descriptor the
4857 * RX buffer was obtained from. The chip simply takes the original
4858 * descriptor as provided by the host, updates the status and length
4859 * field, then writes this into the next status ring entry.
4860 *
4861 * Each ring the host uses to post buffers to the chip is described
4862 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
4863 * it is first placed into the on-chip ram. When the packet's length
4864 * is known, it walks down the TG3_BDINFO entries to select the ring.
4865 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
4866 * which is within the range of the new packet's length is chosen.
4867 *
4868 * The "separate ring for rx status" scheme may sound queer, but it makes
4869 * sense from a cache coherency perspective. If only the host writes
4870 * to the buffer post rings, and only the chip writes to the rx status
4871 * rings, then cache lines never move beyond shared-modified state.
4872 * If both the host and chip were to write into the same ring, cache line
4873 * eviction could occur since both entities want it in an exclusive state.
4874 */
Matt Carlson17375d22009-08-28 14:02:18 +00004875static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876{
Matt Carlson17375d22009-08-28 14:02:18 +00004877 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07004878 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004879 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00004880 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07004881 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004883 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004885 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 /*
4887 * We need to order the read of hw_idx and the read of
4888 * the opaque cookie.
4889 */
4890 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 work_mask = 0;
4892 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004893 std_prod_idx = tpr->rx_std_prod_idx;
4894 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00004896 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00004897 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 unsigned int len;
4899 struct sk_buff *skb;
4900 dma_addr_t dma_addr;
4901 u32 opaque_key, desc_idx, *post_ptr;
4902
4903 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
4904 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
4905 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004906 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004907 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004908 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004909 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07004910 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004912 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004913 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004914 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004915 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00004916 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918
4919 work_mask |= opaque_key;
4920
4921 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
4922 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
4923 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00004924 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 desc_idx, *post_ptr);
4926 drop_it_no_recycle:
4927 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00004928 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 goto next_pkt;
4930 }
4931
Matt Carlsonad829262008-11-21 17:16:16 -08004932 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
4933 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934
Matt Carlsond2757fc2010-04-12 06:58:27 +00004935 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 int skb_size;
4937
Matt Carlson86b21e52009-11-13 13:03:45 +00004938 skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00004939 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 if (skb_size < 0)
4941 goto drop_it;
4942
Matt Carlson287be122009-08-28 13:58:46 +00004943 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 PCI_DMA_FROMDEVICE);
4945
Matt Carlson61e800c2010-02-17 15:16:54 +00004946 /* Ensure that the update to the skb happens
4947 * after the usage of the old DMA mapping.
4948 */
4949 smp_wmb();
4950
4951 ri->skb = NULL;
4952
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 skb_put(skb, len);
4954 } else {
4955 struct sk_buff *copy_skb;
4956
Matt Carlsona3896162009-11-13 13:03:44 +00004957 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 desc_idx, *post_ptr);
4959
Matt Carlsonbf933c82011-01-25 15:58:49 +00004960 copy_skb = netdev_alloc_skb(tp->dev, len +
Matt Carlson9dc7a112010-04-12 06:58:28 +00004961 TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 if (copy_skb == NULL)
4963 goto drop_it_no_recycle;
4964
Matt Carlsonbf933c82011-01-25 15:58:49 +00004965 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 skb_put(copy_skb, len);
4967 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03004968 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
4970
4971 /* We'll reuse the original ring buffer. */
4972 skb = copy_skb;
4973 }
4974
Michał Mirosławdc668912011-04-07 03:35:07 +00004975 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
4977 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
4978 >> RXD_TCPCSUM_SHIFT) == 0xffff))
4979 skb->ip_summed = CHECKSUM_UNNECESSARY;
4980 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07004981 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982
4983 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004984
4985 if (len > (tp->dev->mtu + ETH_HLEN) &&
4986 skb->protocol != htons(ETH_P_8021Q)) {
4987 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00004988 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004989 }
4990
Matt Carlson9dc7a112010-04-12 06:58:28 +00004991 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00004992 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
4993 __vlan_hwaccel_put_tag(skb,
4994 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00004995
Matt Carlsonbf933c82011-01-25 15:58:49 +00004996 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 received++;
4999 budget--;
5000
5001next_pkt:
5002 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07005003
5004 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005005 tpr->rx_std_prod_idx = std_prod_idx &
5006 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00005007 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5008 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07005009 work_mask &= ~RXD_OPAQUE_RING_STD;
5010 rx_std_posted = 0;
5011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07005013 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00005014 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07005015
5016 /* Refresh hw_idx to see if there is new work */
5017 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005018 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07005019 rmb();
5020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 }
5022
5023 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00005024 tnapi->rx_rcb_ptr = sw_idx;
5025 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026
5027 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00005028 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005029 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005030 tpr->rx_std_prod_idx = std_prod_idx &
5031 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005032 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5033 tpr->rx_std_prod_idx);
5034 }
5035 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005036 tpr->rx_jmb_prod_idx = jmb_prod_idx &
5037 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005038 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5039 tpr->rx_jmb_prod_idx);
5040 }
5041 mmiowb();
5042 } else if (work_mask) {
5043 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
5044 * updated before the producer indices can be updated.
5045 */
5046 smp_wmb();
5047
Matt Carlson2c49a442010-09-30 10:34:35 +00005048 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
5049 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005050
Matt Carlsone4af1af2010-02-12 14:47:05 +00005051 if (tnapi != &tp->napi[1])
5052 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054
5055 return received;
5056}
5057
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005058static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00005061 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005062 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
5063
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 if (sblk->status & SD_STATUS_LINK_CHG) {
5065 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005066 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07005067 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005068 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07005069 tw32_f(MAC_STATUS,
5070 (MAC_STATUS_SYNC_CHANGED |
5071 MAC_STATUS_CFG_CHANGED |
5072 MAC_STATUS_MI_COMPLETION |
5073 MAC_STATUS_LNKSTATE_CHANGED));
5074 udelay(40);
5075 } else
5076 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07005077 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 }
5079 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005080}
5081
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005082static int tg3_rx_prodring_xfer(struct tg3 *tp,
5083 struct tg3_rx_prodring_set *dpr,
5084 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005085{
5086 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005087 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005088
5089 while (1) {
5090 src_prod_idx = spr->rx_std_prod_idx;
5091
5092 /* Make sure updates to the rx_std_buffers[] entries and the
5093 * standard producer index are seen in the correct order.
5094 */
5095 smp_rmb();
5096
5097 if (spr->rx_std_cons_idx == src_prod_idx)
5098 break;
5099
5100 if (spr->rx_std_cons_idx < src_prod_idx)
5101 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
5102 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005103 cpycnt = tp->rx_std_ring_mask + 1 -
5104 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005105
Matt Carlson2c49a442010-09-30 10:34:35 +00005106 cpycnt = min(cpycnt,
5107 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005108
5109 si = spr->rx_std_cons_idx;
5110 di = dpr->rx_std_prod_idx;
5111
Matt Carlsone92967b2010-02-12 14:47:06 +00005112 for (i = di; i < di + cpycnt; i++) {
5113 if (dpr->rx_std_buffers[i].skb) {
5114 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005115 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005116 break;
5117 }
5118 }
5119
5120 if (!cpycnt)
5121 break;
5122
5123 /* Ensure that updates to the rx_std_buffers ring and the
5124 * shadowed hardware producer ring from tg3_recycle_skb() are
5125 * ordered correctly WRT the skb check above.
5126 */
5127 smp_rmb();
5128
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005129 memcpy(&dpr->rx_std_buffers[di],
5130 &spr->rx_std_buffers[si],
5131 cpycnt * sizeof(struct ring_info));
5132
5133 for (i = 0; i < cpycnt; i++, di++, si++) {
5134 struct tg3_rx_buffer_desc *sbd, *dbd;
5135 sbd = &spr->rx_std[si];
5136 dbd = &dpr->rx_std[di];
5137 dbd->addr_hi = sbd->addr_hi;
5138 dbd->addr_lo = sbd->addr_lo;
5139 }
5140
Matt Carlson2c49a442010-09-30 10:34:35 +00005141 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
5142 tp->rx_std_ring_mask;
5143 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
5144 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005145 }
5146
5147 while (1) {
5148 src_prod_idx = spr->rx_jmb_prod_idx;
5149
5150 /* Make sure updates to the rx_jmb_buffers[] entries and
5151 * the jumbo producer index are seen in the correct order.
5152 */
5153 smp_rmb();
5154
5155 if (spr->rx_jmb_cons_idx == src_prod_idx)
5156 break;
5157
5158 if (spr->rx_jmb_cons_idx < src_prod_idx)
5159 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
5160 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005161 cpycnt = tp->rx_jmb_ring_mask + 1 -
5162 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005163
5164 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00005165 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005166
5167 si = spr->rx_jmb_cons_idx;
5168 di = dpr->rx_jmb_prod_idx;
5169
Matt Carlsone92967b2010-02-12 14:47:06 +00005170 for (i = di; i < di + cpycnt; i++) {
5171 if (dpr->rx_jmb_buffers[i].skb) {
5172 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005173 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005174 break;
5175 }
5176 }
5177
5178 if (!cpycnt)
5179 break;
5180
5181 /* Ensure that updates to the rx_jmb_buffers ring and the
5182 * shadowed hardware producer ring from tg3_recycle_skb() are
5183 * ordered correctly WRT the skb check above.
5184 */
5185 smp_rmb();
5186
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005187 memcpy(&dpr->rx_jmb_buffers[di],
5188 &spr->rx_jmb_buffers[si],
5189 cpycnt * sizeof(struct ring_info));
5190
5191 for (i = 0; i < cpycnt; i++, di++, si++) {
5192 struct tg3_rx_buffer_desc *sbd, *dbd;
5193 sbd = &spr->rx_jmb[si].std;
5194 dbd = &dpr->rx_jmb[di].std;
5195 dbd->addr_hi = sbd->addr_hi;
5196 dbd->addr_lo = sbd->addr_lo;
5197 }
5198
Matt Carlson2c49a442010-09-30 10:34:35 +00005199 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
5200 tp->rx_jmb_ring_mask;
5201 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
5202 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005203 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005204
5205 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005206}
5207
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005208static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
5209{
5210 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211
5212 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005213 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00005214 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00005215 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07005216 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 }
5218
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 /* run RX thread, within the bounds set by NAPI.
5220 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005221 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005223 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00005224 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225
Joe Perches63c3a662011-04-26 08:12:10 +00005226 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005227 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005228 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00005229 u32 std_prod_idx = dpr->rx_std_prod_idx;
5230 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005231
Matt Carlsone4af1af2010-02-12 14:47:05 +00005232 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005233 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00005234 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005235
5236 wmb();
5237
Matt Carlsone4af1af2010-02-12 14:47:05 +00005238 if (std_prod_idx != dpr->rx_std_prod_idx)
5239 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5240 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005241
Matt Carlsone4af1af2010-02-12 14:47:05 +00005242 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5243 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5244 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005245
5246 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005247
5248 if (err)
5249 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005250 }
5251
David S. Miller6f535762007-10-11 18:08:29 -07005252 return work_done;
5253}
David S. Millerf7383c22005-05-18 22:50:53 -07005254
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005255static int tg3_poll_msix(struct napi_struct *napi, int budget)
5256{
5257 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5258 struct tg3 *tp = tnapi->tp;
5259 int work_done = 0;
5260 struct tg3_hw_status *sblk = tnapi->hw_status;
5261
5262 while (1) {
5263 work_done = tg3_poll_work(tnapi, work_done, budget);
5264
Joe Perches63c3a662011-04-26 08:12:10 +00005265 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005266 goto tx_recovery;
5267
5268 if (unlikely(work_done >= budget))
5269 break;
5270
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005271 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005272 * to tell the hw how much work has been processed,
5273 * so we must read it before checking for more work.
5274 */
5275 tnapi->last_tag = sblk->status_tag;
5276 tnapi->last_irq_tag = tnapi->last_tag;
5277 rmb();
5278
5279 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005280 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5281 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005282 napi_complete(napi);
5283 /* Reenable interrupts. */
5284 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5285 mmiowb();
5286 break;
5287 }
5288 }
5289
5290 return work_done;
5291
5292tx_recovery:
5293 /* work_done is guaranteed to be less than budget. */
5294 napi_complete(napi);
5295 schedule_work(&tp->reset_task);
5296 return work_done;
5297}
5298
Matt Carlsone64de4e2011-04-13 11:05:05 +00005299static void tg3_process_error(struct tg3 *tp)
5300{
5301 u32 val;
5302 bool real_error = false;
5303
Joe Perches63c3a662011-04-26 08:12:10 +00005304 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00005305 return;
5306
5307 /* Check Flow Attention register */
5308 val = tr32(HOSTCC_FLOW_ATTN);
5309 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
5310 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
5311 real_error = true;
5312 }
5313
5314 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
5315 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
5316 real_error = true;
5317 }
5318
5319 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
5320 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
5321 real_error = true;
5322 }
5323
5324 if (!real_error)
5325 return;
5326
5327 tg3_dump_state(tp);
5328
Joe Perches63c3a662011-04-26 08:12:10 +00005329 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsone64de4e2011-04-13 11:05:05 +00005330 schedule_work(&tp->reset_task);
5331}
5332
David S. Miller6f535762007-10-11 18:08:29 -07005333static int tg3_poll(struct napi_struct *napi, int budget)
5334{
Matt Carlson8ef04422009-08-28 14:01:37 +00005335 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5336 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07005337 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00005338 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07005339
5340 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00005341 if (sblk->status & SD_STATUS_ERROR)
5342 tg3_process_error(tp);
5343
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005344 tg3_poll_link(tp);
5345
Matt Carlson17375d22009-08-28 14:02:18 +00005346 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07005347
Joe Perches63c3a662011-04-26 08:12:10 +00005348 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07005349 goto tx_recovery;
5350
5351 if (unlikely(work_done >= budget))
5352 break;
5353
Joe Perches63c3a662011-04-26 08:12:10 +00005354 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00005355 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07005356 * to tell the hw how much work has been processed,
5357 * so we must read it before checking for more work.
5358 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005359 tnapi->last_tag = sblk->status_tag;
5360 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07005361 rmb();
5362 } else
5363 sblk->status &= ~SD_STATUS_UPDATED;
5364
Matt Carlson17375d22009-08-28 14:02:18 +00005365 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005366 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00005367 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005368 break;
5369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 }
5371
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005372 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07005373
5374tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07005375 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08005376 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07005377 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07005378 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379}
5380
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005381static void tg3_napi_disable(struct tg3 *tp)
5382{
5383 int i;
5384
5385 for (i = tp->irq_cnt - 1; i >= 0; i--)
5386 napi_disable(&tp->napi[i].napi);
5387}
5388
5389static void tg3_napi_enable(struct tg3 *tp)
5390{
5391 int i;
5392
5393 for (i = 0; i < tp->irq_cnt; i++)
5394 napi_enable(&tp->napi[i].napi);
5395}
5396
5397static void tg3_napi_init(struct tg3 *tp)
5398{
5399 int i;
5400
5401 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
5402 for (i = 1; i < tp->irq_cnt; i++)
5403 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
5404}
5405
5406static void tg3_napi_fini(struct tg3 *tp)
5407{
5408 int i;
5409
5410 for (i = 0; i < tp->irq_cnt; i++)
5411 netif_napi_del(&tp->napi[i].napi);
5412}
5413
5414static inline void tg3_netif_stop(struct tg3 *tp)
5415{
5416 tp->dev->trans_start = jiffies; /* prevent tx timeout */
5417 tg3_napi_disable(tp);
5418 netif_tx_disable(tp->dev);
5419}
5420
5421static inline void tg3_netif_start(struct tg3 *tp)
5422{
5423 /* NOTE: unconditional netif_tx_wake_all_queues is only
5424 * appropriate so long as all callers are assured to
5425 * have free tx slots (such as after tg3_init_hw)
5426 */
5427 netif_tx_wake_all_queues(tp->dev);
5428
5429 tg3_napi_enable(tp);
5430 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
5431 tg3_enable_ints(tp);
5432}
5433
David S. Millerf47c11e2005-06-24 20:18:35 -07005434static void tg3_irq_quiesce(struct tg3 *tp)
5435{
Matt Carlson4f125f42009-09-01 12:55:02 +00005436 int i;
5437
David S. Millerf47c11e2005-06-24 20:18:35 -07005438 BUG_ON(tp->irq_sync);
5439
5440 tp->irq_sync = 1;
5441 smp_mb();
5442
Matt Carlson4f125f42009-09-01 12:55:02 +00005443 for (i = 0; i < tp->irq_cnt; i++)
5444 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07005445}
5446
David S. Millerf47c11e2005-06-24 20:18:35 -07005447/* Fully shutdown all tg3 driver activity elsewhere in the system.
5448 * If irq_sync is non-zero, then the IRQ handler must be synchronized
5449 * with as well. Most of the time, this is not necessary except when
5450 * shutting down the device.
5451 */
5452static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
5453{
Michael Chan46966542007-07-11 19:47:19 -07005454 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07005455 if (irq_sync)
5456 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005457}
5458
5459static inline void tg3_full_unlock(struct tg3 *tp)
5460{
David S. Millerf47c11e2005-06-24 20:18:35 -07005461 spin_unlock_bh(&tp->lock);
5462}
5463
Michael Chanfcfa0a32006-03-20 22:28:41 -08005464/* One-shot MSI handler - Chip automatically disables interrupt
5465 * after sending MSI so driver doesn't have to do it.
5466 */
David Howells7d12e782006-10-05 14:55:46 +01005467static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08005468{
Matt Carlson09943a12009-08-28 14:01:57 +00005469 struct tg3_napi *tnapi = dev_id;
5470 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08005471
Matt Carlson898a56f2009-08-28 14:02:40 +00005472 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005473 if (tnapi->rx_rcb)
5474 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005475
5476 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005477 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005478
5479 return IRQ_HANDLED;
5480}
5481
Michael Chan88b06bc2005-04-21 17:13:25 -07005482/* MSI ISR - No need to check for interrupt sharing and no need to
5483 * flush status block and interrupt mailbox. PCI ordering rules
5484 * guarantee that MSI will arrive after the status block.
5485 */
David Howells7d12e782006-10-05 14:55:46 +01005486static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc2005-04-21 17:13:25 -07005487{
Matt Carlson09943a12009-08-28 14:01:57 +00005488 struct tg3_napi *tnapi = dev_id;
5489 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc2005-04-21 17:13:25 -07005490
Matt Carlson898a56f2009-08-28 14:02:40 +00005491 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005492 if (tnapi->rx_rcb)
5493 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc2005-04-21 17:13:25 -07005494 /*
David S. Millerfac9b832005-05-18 22:46:34 -07005495 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc2005-04-21 17:13:25 -07005496 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07005497 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc2005-04-21 17:13:25 -07005498 * NIC to stop sending us irqs, engaging "in-intr-handler"
5499 * event coalescing.
5500 */
5501 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07005502 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005503 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07005504
Michael Chan88b06bc2005-04-21 17:13:25 -07005505 return IRQ_RETVAL(1);
5506}
5507
David Howells7d12e782006-10-05 14:55:46 +01005508static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509{
Matt Carlson09943a12009-08-28 14:01:57 +00005510 struct tg3_napi *tnapi = dev_id;
5511 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005512 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 unsigned int handled = 1;
5514
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 /* In INTx mode, it is possible for the interrupt to arrive at
5516 * the CPU before the status block posted prior to the interrupt.
5517 * Reading the PCI State register will confirm whether the
5518 * interrupt is ours and will flush the status block.
5519 */
Michael Chand18edcb2007-03-24 20:57:11 -07005520 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00005521 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07005522 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5523 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005524 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07005525 }
Michael Chand18edcb2007-03-24 20:57:11 -07005526 }
5527
5528 /*
5529 * Writing any value to intr-mbox-0 clears PCI INTA# and
5530 * chip-internal interrupt pending events.
5531 * Writing non-zero to intr-mbox-0 additional tells the
5532 * NIC to stop sending us irqs, engaging "in-intr-handler"
5533 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005534 *
5535 * Flush the mailbox to de-assert the IRQ immediately to prevent
5536 * spurious interrupts. The flush impacts performance but
5537 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005538 */
Michael Chanc04cb342007-05-07 00:26:15 -07005539 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07005540 if (tg3_irq_sync(tp))
5541 goto out;
5542 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00005543 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00005544 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00005545 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07005546 } else {
5547 /* No work, shared interrupt perhaps? re-enable
5548 * interrupts, and flush that PCI write
5549 */
5550 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
5551 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07005552 }
David S. Millerf47c11e2005-06-24 20:18:35 -07005553out:
David S. Millerfac9b832005-05-18 22:46:34 -07005554 return IRQ_RETVAL(handled);
5555}
5556
David Howells7d12e782006-10-05 14:55:46 +01005557static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07005558{
Matt Carlson09943a12009-08-28 14:01:57 +00005559 struct tg3_napi *tnapi = dev_id;
5560 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005561 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07005562 unsigned int handled = 1;
5563
David S. Millerfac9b832005-05-18 22:46:34 -07005564 /* In INTx mode, it is possible for the interrupt to arrive at
5565 * the CPU before the status block posted prior to the interrupt.
5566 * Reading the PCI State register will confirm whether the
5567 * interrupt is ours and will flush the status block.
5568 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005569 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00005570 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07005571 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5572 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005573 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 }
Michael Chand18edcb2007-03-24 20:57:11 -07005575 }
5576
5577 /*
5578 * writing any value to intr-mbox-0 clears PCI INTA# and
5579 * chip-internal interrupt pending events.
5580 * writing non-zero to intr-mbox-0 additional tells the
5581 * NIC to stop sending us irqs, engaging "in-intr-handler"
5582 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005583 *
5584 * Flush the mailbox to de-assert the IRQ immediately to prevent
5585 * spurious interrupts. The flush impacts performance but
5586 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005587 */
Michael Chanc04cb342007-05-07 00:26:15 -07005588 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00005589
5590 /*
5591 * In a shared interrupt configuration, sometimes other devices'
5592 * interrupts will scream. We record the current status tag here
5593 * so that the above check can report that the screaming interrupts
5594 * are unhandled. Eventually they will be silenced.
5595 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005596 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00005597
Michael Chand18edcb2007-03-24 20:57:11 -07005598 if (tg3_irq_sync(tp))
5599 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00005600
Matt Carlson72334482009-08-28 14:03:01 +00005601 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00005602
Matt Carlson09943a12009-08-28 14:01:57 +00005603 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00005604
David S. Millerf47c11e2005-06-24 20:18:35 -07005605out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606 return IRQ_RETVAL(handled);
5607}
5608
Michael Chan79381092005-04-21 17:13:59 -07005609/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01005610static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07005611{
Matt Carlson09943a12009-08-28 14:01:57 +00005612 struct tg3_napi *tnapi = dev_id;
5613 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005614 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07005615
Michael Chanf9804dd2005-09-27 12:13:10 -07005616 if ((sblk->status & SD_STATUS_UPDATED) ||
5617 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07005618 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07005619 return IRQ_RETVAL(1);
5620 }
5621 return IRQ_RETVAL(0);
5622}
5623
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07005624static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07005625static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626
Michael Chanb9ec6c12006-07-25 16:37:27 -07005627/* Restart hardware after configuration changes, self-test, etc.
5628 * Invoked with tp->lock held.
5629 */
5630static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07005631 __releases(tp->lock)
5632 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005633{
5634 int err;
5635
5636 err = tg3_init_hw(tp, reset_phy);
5637 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00005638 netdev_err(tp->dev,
5639 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07005640 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
5641 tg3_full_unlock(tp);
5642 del_timer_sync(&tp->timer);
5643 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00005644 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005645 dev_close(tp->dev);
5646 tg3_full_lock(tp, 0);
5647 }
5648 return err;
5649}
5650
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651#ifdef CONFIG_NET_POLL_CONTROLLER
5652static void tg3_poll_controller(struct net_device *dev)
5653{
Matt Carlson4f125f42009-09-01 12:55:02 +00005654 int i;
Michael Chan88b06bc2005-04-21 17:13:25 -07005655 struct tg3 *tp = netdev_priv(dev);
5656
Matt Carlson4f125f42009-09-01 12:55:02 +00005657 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00005658 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659}
5660#endif
5661
David Howellsc4028952006-11-22 14:57:56 +00005662static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663{
David Howellsc4028952006-11-22 14:57:56 +00005664 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005665 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 unsigned int restart_timer;
5667
Michael Chan7faa0062006-02-02 17:29:28 -08005668 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08005669
5670 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08005671 tg3_full_unlock(tp);
5672 return;
5673 }
5674
5675 tg3_full_unlock(tp);
5676
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005677 tg3_phy_stop(tp);
5678
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 tg3_netif_stop(tp);
5680
David S. Millerf47c11e2005-06-24 20:18:35 -07005681 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682
Joe Perches63c3a662011-04-26 08:12:10 +00005683 restart_timer = tg3_flag(tp, RESTART_TIMER);
5684 tg3_flag_clear(tp, RESTART_TIMER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685
Joe Perches63c3a662011-04-26 08:12:10 +00005686 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
Michael Chandf3e6542006-05-26 17:48:07 -07005687 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5688 tp->write32_rx_mbox = tg3_write_flush_reg32;
Joe Perches63c3a662011-04-26 08:12:10 +00005689 tg3_flag_set(tp, MBOX_WRITE_REORDER);
5690 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005691 }
5692
Michael Chan944d9802005-05-29 14:57:48 -07005693 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005694 err = tg3_init_hw(tp, 1);
5695 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005696 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697
5698 tg3_netif_start(tp);
5699
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 if (restart_timer)
5701 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08005702
Michael Chanb9ec6c12006-07-25 16:37:27 -07005703out:
Michael Chan7faa0062006-02-02 17:29:28 -08005704 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005705
5706 if (!err)
5707 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708}
5709
5710static void tg3_tx_timeout(struct net_device *dev)
5711{
5712 struct tg3 *tp = netdev_priv(dev);
5713
Michael Chanb0408752007-02-13 12:18:30 -08005714 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00005715 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00005716 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08005717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718
5719 schedule_work(&tp->reset_task);
5720}
5721
Michael Chanc58ec932005-09-17 00:46:27 -07005722/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5723static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5724{
5725 u32 base = (u32) mapping & 0xffffffff;
5726
Eric Dumazet807540b2010-09-23 05:40:09 +00005727 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07005728}
5729
Michael Chan72f2afb2006-03-06 19:28:35 -08005730/* Test for DMA addresses > 40-bit */
5731static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5732 int len)
5733{
5734#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00005735 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00005736 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08005737 return 0;
5738#else
5739 return 0;
5740#endif
5741}
5742
Matt Carlson2ffcc982011-05-19 12:12:44 +00005743static void tg3_set_txd(struct tg3_napi *tnapi, int entry,
5744 dma_addr_t mapping, int len, u32 flags,
5745 u32 mss_and_is_end)
5746{
5747 struct tg3_tx_buffer_desc *txd = &tnapi->tx_ring[entry];
5748 int is_end = (mss_and_is_end & 0x1);
5749 u32 mss = (mss_and_is_end >> 1);
5750 u32 vlan_tag = 0;
5751
5752 if (is_end)
5753 flags |= TXD_FLAG_END;
5754 if (flags & TXD_FLAG_VLAN) {
5755 vlan_tag = flags >> 16;
5756 flags &= 0xffff;
5757 }
5758 vlan_tag |= (mss << TXD_MSS_SHIFT);
5759
5760 txd->addr_hi = ((u64) mapping >> 32);
5761 txd->addr_lo = ((u64) mapping & 0xffffffff);
5762 txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
5763 txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
5764}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765
Matt Carlson432aa7e2011-05-19 12:12:45 +00005766static void tg3_skb_error_unmap(struct tg3_napi *tnapi,
5767 struct sk_buff *skb, int last)
5768{
5769 int i;
5770 u32 entry = tnapi->tx_prod;
5771 struct ring_info *txb = &tnapi->tx_buffers[entry];
5772
5773 pci_unmap_single(tnapi->tp->pdev,
5774 dma_unmap_addr(txb, mapping),
5775 skb_headlen(skb),
5776 PCI_DMA_TODEVICE);
Matt Carlson9a2e0fb2011-06-02 13:01:39 +00005777 for (i = 0; i < last; i++) {
Matt Carlson432aa7e2011-05-19 12:12:45 +00005778 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5779
5780 entry = NEXT_TX(entry);
5781 txb = &tnapi->tx_buffers[entry];
5782
5783 pci_unmap_page(tnapi->tp->pdev,
5784 dma_unmap_addr(txb, mapping),
5785 frag->size, PCI_DMA_TODEVICE);
5786 }
5787}
5788
Michael Chan72f2afb2006-03-06 19:28:35 -08005789/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005790static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
Matt Carlson432aa7e2011-05-19 12:12:45 +00005791 struct sk_buff *skb,
5792 u32 base_flags, u32 mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793{
Matt Carlson24f4efd2009-11-13 13:03:35 +00005794 struct tg3 *tp = tnapi->tp;
Matt Carlson41588ba2008-04-19 18:12:33 -07005795 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07005796 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00005797 u32 entry = tnapi->tx_prod;
5798 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799
Matt Carlson41588ba2008-04-19 18:12:33 -07005800 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
5801 new_skb = skb_copy(skb, GFP_ATOMIC);
5802 else {
5803 int more_headroom = 4 - ((unsigned long)skb->data & 3);
5804
5805 new_skb = skb_copy_expand(skb,
5806 skb_headroom(skb) + more_headroom,
5807 skb_tailroom(skb), GFP_ATOMIC);
5808 }
5809
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07005811 ret = -1;
5812 } else {
5813 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00005814 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
5815 PCI_DMA_TODEVICE);
5816 /* Make sure the mapping succeeded */
5817 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
5818 ret = -1;
5819 dev_kfree_skb(new_skb);
David S. Miller90079ce2008-09-11 04:52:51 -07005820
Michael Chanc58ec932005-09-17 00:46:27 -07005821 /* Make sure new skb does not cross any 4G boundaries.
5822 * Drop the packet if it does.
5823 */
Joe Perches63c3a662011-04-26 08:12:10 +00005824 } else if (tg3_flag(tp, 4G_DMA_BNDRY_BUG) &&
5825 tg3_4g_overflow_test(new_addr, new_skb->len)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00005826 pci_unmap_single(tp->pdev, new_addr, new_skb->len,
5827 PCI_DMA_TODEVICE);
Michael Chanc58ec932005-09-17 00:46:27 -07005828 ret = -1;
5829 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07005830 } else {
Matt Carlson432aa7e2011-05-19 12:12:45 +00005831 tnapi->tx_buffers[entry].skb = new_skb;
5832 dma_unmap_addr_set(&tnapi->tx_buffers[entry],
5833 mapping, new_addr);
5834
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005835 tg3_set_txd(tnapi, entry, new_addr, new_skb->len,
Michael Chanc58ec932005-09-17 00:46:27 -07005836 base_flags, 1 | (mss << 1));
Michael Chanc58ec932005-09-17 00:46:27 -07005837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838 }
5839
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 dev_kfree_skb(skb);
5841
Michael Chanc58ec932005-09-17 00:46:27 -07005842 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843}
5844
Matt Carlson2ffcc982011-05-19 12:12:44 +00005845static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07005846
5847/* Use GSO to workaround a rare TSO bug that may be triggered when the
5848 * TSO header is greater than 80 bytes.
5849 */
5850static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
5851{
5852 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005853 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07005854
5855 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005856 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07005857 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00005858
5859 /* netif_tx_stop_queue() must be done before checking
5860 * checking tx index in tg3_tx_avail() below, because in
5861 * tg3_tx(), we update tx index before checking for
5862 * netif_tx_queue_stopped().
5863 */
5864 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005865 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08005866 return NETDEV_TX_BUSY;
5867
5868 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005869 }
5870
5871 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07005872 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07005873 goto tg3_tso_bug_end;
5874
5875 do {
5876 nskb = segs;
5877 segs = segs->next;
5878 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00005879 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005880 } while (segs);
5881
5882tg3_tso_bug_end:
5883 dev_kfree_skb(skb);
5884
5885 return NETDEV_TX_OK;
5886}
Michael Chan52c0fd82006-06-29 20:15:54 -07005887
Michael Chan5a6f3072006-03-20 22:28:05 -08005888/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00005889 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08005890 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00005891static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08005892{
5893 struct tg3 *tp = netdev_priv(dev);
Michael Chan5a6f3072006-03-20 22:28:05 -08005894 u32 len, entry, base_flags, mss;
Matt Carlson432aa7e2011-05-19 12:12:45 +00005895 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07005896 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005897 struct tg3_napi *tnapi;
5898 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00005899 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005900
Matt Carlson24f4efd2009-11-13 13:03:35 +00005901 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5902 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00005903 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00005904 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905
Michael Chan00b70502006-06-17 21:58:45 -07005906 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005907 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07005908 * interrupt. Furthermore, IRQ processing runs lockless so we have
5909 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005911 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00005912 if (!netif_tx_queue_stopped(txq)) {
5913 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005914
5915 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00005916 netdev_err(dev,
5917 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919 return NETDEV_TX_BUSY;
5920 }
5921
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005922 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07005924 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005926
Matt Carlsonbe98da62010-07-11 09:31:46 +00005927 mss = skb_shinfo(skb)->gso_size;
5928 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005929 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00005930 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931
5932 if (skb_header_cloned(skb) &&
5933 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5934 dev_kfree_skb(skb);
5935 goto out_unlock;
5936 }
5937
Matt Carlson34195c32010-07-11 09:31:42 +00005938 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005939 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940
Matt Carlson02e96082010-09-15 08:59:59 +00005941 if (skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00005942 hdr_len = skb_headlen(skb) - ETH_HLEN;
5943 } else {
5944 u32 ip_tcp_len;
5945
5946 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
5947 hdr_len = ip_tcp_len + tcp_opt_len;
5948
5949 iph->check = 0;
5950 iph->tot_len = htons(mss + hdr_len);
5951 }
5952
Michael Chan52c0fd82006-06-29 20:15:54 -07005953 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00005954 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00005955 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07005956
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5958 TXD_FLAG_CPU_POST_DMA);
5959
Joe Perches63c3a662011-04-26 08:12:10 +00005960 if (tg3_flag(tp, HW_TSO_1) ||
5961 tg3_flag(tp, HW_TSO_2) ||
5962 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005963 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005965 } else
5966 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
5967 iph->daddr, 0,
5968 IPPROTO_TCP,
5969 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970
Joe Perches63c3a662011-04-26 08:12:10 +00005971 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00005972 mss |= (hdr_len & 0xc) << 12;
5973 if (hdr_len & 0x10)
5974 base_flags |= 0x00000010;
5975 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00005976 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005977 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00005978 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005979 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005980 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981 int tsflags;
5982
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005983 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984 mss |= (tsflags << 11);
5985 }
5986 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005987 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988 int tsflags;
5989
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005990 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 base_flags |= tsflags << 12;
5992 }
5993 }
5994 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00005995
Jesse Grosseab6d182010-10-20 13:56:03 +00005996 if (vlan_tx_tag_present(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997 base_flags |= (TXD_FLAG_VLAN |
5998 (vlan_tx_tag_get(skb) << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999
Joe Perches63c3a662011-04-26 08:12:10 +00006000 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
Matt Carlson8fc2f992010-12-06 08:28:49 +00006001 !mss && skb->len > VLAN_ETH_FRAME_LEN)
Matt Carlson615774f2009-11-13 13:03:39 +00006002 base_flags |= TXD_FLAG_JMB_PKT;
6003
Alexander Duyckf4188d82009-12-02 16:48:38 +00006004 len = skb_headlen(skb);
6005
6006 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
6007 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07006008 dev_kfree_skb(skb);
6009 goto out_unlock;
6010 }
6011
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006012 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006013 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014
6015 would_hit_hwbug = 0;
6016
Joe Perches63c3a662011-04-26 08:12:10 +00006017 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006018 would_hit_hwbug = 1;
6019
Joe Perches63c3a662011-04-26 08:12:10 +00006020 if (tg3_flag(tp, 4G_DMA_BNDRY_BUG) &&
Matt Carlson0e1406d2009-11-02 12:33:33 +00006021 tg3_4g_overflow_test(mapping, len))
Matt Carlson41588ba2008-04-19 18:12:33 -07006022 would_hit_hwbug = 1;
Matt Carlson0e1406d2009-11-02 12:33:33 +00006023
Joe Perches63c3a662011-04-26 08:12:10 +00006024 if (tg3_flag(tp, 40BIT_DMA_LIMIT_BUG) &&
Matt Carlson0e1406d2009-11-02 12:33:33 +00006025 tg3_40bit_overflow_test(tp, mapping, len))
6026 would_hit_hwbug = 1;
6027
Joe Perches63c3a662011-04-26 08:12:10 +00006028 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07006029 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006031 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
6033
6034 entry = NEXT_TX(entry);
6035
6036 /* Now loop through additional data fragments, and queue them. */
6037 if (skb_shinfo(skb)->nr_frags > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038 last = skb_shinfo(skb)->nr_frags - 1;
6039 for (i = 0; i <= last; i++) {
6040 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6041
6042 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006043 mapping = pci_map_page(tp->pdev,
6044 frag->page,
6045 frag->page_offset,
6046 len, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006048 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006049 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006050 mapping);
6051 if (pci_dma_mapping_error(tp->pdev, mapping))
6052 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053
Joe Perches63c3a662011-04-26 08:12:10 +00006054 if (tg3_flag(tp, SHORT_DMA_BUG) &&
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006055 len <= 8)
6056 would_hit_hwbug = 1;
6057
Joe Perches63c3a662011-04-26 08:12:10 +00006058 if (tg3_flag(tp, 4G_DMA_BNDRY_BUG) &&
Matt Carlson0e1406d2009-11-02 12:33:33 +00006059 tg3_4g_overflow_test(mapping, len))
Michael Chanc58ec932005-09-17 00:46:27 -07006060 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061
Joe Perches63c3a662011-04-26 08:12:10 +00006062 if (tg3_flag(tp, 40BIT_DMA_LIMIT_BUG) &&
Matt Carlson0e1406d2009-11-02 12:33:33 +00006063 tg3_40bit_overflow_test(tp, mapping, len))
Michael Chan72f2afb2006-03-06 19:28:35 -08006064 would_hit_hwbug = 1;
6065
Joe Perches63c3a662011-04-26 08:12:10 +00006066 if (tg3_flag(tp, HW_TSO_1) ||
6067 tg3_flag(tp, HW_TSO_2) ||
6068 tg3_flag(tp, HW_TSO_3))
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006069 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070 base_flags, (i == last)|(mss << 1));
6071 else
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006072 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073 base_flags, (i == last));
6074
6075 entry = NEXT_TX(entry);
6076 }
6077 }
6078
6079 if (would_hit_hwbug) {
Matt Carlson432aa7e2011-05-19 12:12:45 +00006080 tg3_skb_error_unmap(tnapi, skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081
6082 /* If the workaround fails due to memory/mapping
6083 * failure, silently drop this packet.
6084 */
Matt Carlson432aa7e2011-05-19 12:12:45 +00006085 if (tigon3_dma_hwbug_workaround(tnapi, skb, base_flags, mss))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086 goto out_unlock;
6087
Matt Carlson432aa7e2011-05-19 12:12:45 +00006088 entry = NEXT_TX(tnapi->tx_prod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089 }
6090
6091 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006092 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006094 tnapi->tx_prod = entry;
6095 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006096 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006097
6098 /* netif_tx_stop_queue() must be done before checking
6099 * checking tx index in tg3_tx_avail() below, because in
6100 * tg3_tx(), we update tx index before checking for
6101 * netif_tx_queue_stopped().
6102 */
6103 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006104 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006105 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107
6108out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00006109 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110
6111 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006112
6113dma_error:
Matt Carlson432aa7e2011-05-19 12:12:45 +00006114 tg3_skb_error_unmap(tnapi, skb, i);
Alexander Duyckf4188d82009-12-02 16:48:38 +00006115 dev_kfree_skb(skb);
Matt Carlson432aa7e2011-05-19 12:12:45 +00006116 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006117 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118}
6119
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006120static void tg3_set_loopback(struct net_device *dev, u32 features)
6121{
6122 struct tg3 *tp = netdev_priv(dev);
6123
6124 if (features & NETIF_F_LOOPBACK) {
6125 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
6126 return;
6127
6128 /*
6129 * Clear MAC_MODE_HALF_DUPLEX or you won't get packets back in
6130 * loopback mode if Half-Duplex mode was negotiated earlier.
6131 */
6132 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
6133
6134 /* Enable internal MAC loopback mode */
6135 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
6136 spin_lock_bh(&tp->lock);
6137 tw32(MAC_MODE, tp->mac_mode);
6138 netif_carrier_on(tp->dev);
6139 spin_unlock_bh(&tp->lock);
6140 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
6141 } else {
6142 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
6143 return;
6144
6145 /* Disable internal MAC loopback mode */
6146 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
6147 spin_lock_bh(&tp->lock);
6148 tw32(MAC_MODE, tp->mac_mode);
6149 /* Force link status check */
6150 tg3_setup_phy(tp, 1);
6151 spin_unlock_bh(&tp->lock);
6152 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
6153 }
6154}
6155
Michał Mirosławdc668912011-04-07 03:35:07 +00006156static u32 tg3_fix_features(struct net_device *dev, u32 features)
6157{
6158 struct tg3 *tp = netdev_priv(dev);
6159
Joe Perches63c3a662011-04-26 08:12:10 +00006160 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00006161 features &= ~NETIF_F_ALL_TSO;
6162
6163 return features;
6164}
6165
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006166static int tg3_set_features(struct net_device *dev, u32 features)
6167{
6168 u32 changed = dev->features ^ features;
6169
6170 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
6171 tg3_set_loopback(dev, features);
6172
6173 return 0;
6174}
6175
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
6177 int new_mtu)
6178{
6179 dev->mtu = new_mtu;
6180
Michael Chanef7f5ec2005-07-25 12:32:25 -07006181 if (new_mtu > ETH_DATA_LEN) {
Joe Perches63c3a662011-04-26 08:12:10 +00006182 if (tg3_flag(tp, 5780_CLASS)) {
Michał Mirosławdc668912011-04-07 03:35:07 +00006183 netdev_update_features(dev);
Joe Perches63c3a662011-04-26 08:12:10 +00006184 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson859a5882010-04-05 10:19:28 +00006185 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00006186 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Matt Carlson859a5882010-04-05 10:19:28 +00006187 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07006188 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00006189 if (tg3_flag(tp, 5780_CLASS)) {
6190 tg3_flag_set(tp, TSO_CAPABLE);
Michał Mirosławdc668912011-04-07 03:35:07 +00006191 netdev_update_features(dev);
6192 }
Joe Perches63c3a662011-04-26 08:12:10 +00006193 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
Michael Chanef7f5ec2005-07-25 12:32:25 -07006194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195}
6196
6197static int tg3_change_mtu(struct net_device *dev, int new_mtu)
6198{
6199 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07006200 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201
6202 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
6203 return -EINVAL;
6204
6205 if (!netif_running(dev)) {
6206 /* We'll just catch it later when the
6207 * device is up'd.
6208 */
6209 tg3_set_mtu(dev, tp, new_mtu);
6210 return 0;
6211 }
6212
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006213 tg3_phy_stop(tp);
6214
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006216
6217 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218
Michael Chan944d9802005-05-29 14:57:48 -07006219 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220
6221 tg3_set_mtu(dev, tp, new_mtu);
6222
Michael Chanb9ec6c12006-07-25 16:37:27 -07006223 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224
Michael Chanb9ec6c12006-07-25 16:37:27 -07006225 if (!err)
6226 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227
David S. Millerf47c11e2005-06-24 20:18:35 -07006228 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006230 if (!err)
6231 tg3_phy_start(tp);
6232
Michael Chanb9ec6c12006-07-25 16:37:27 -07006233 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234}
6235
Matt Carlson21f581a2009-08-28 14:00:25 +00006236static void tg3_rx_prodring_free(struct tg3 *tp,
6237 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239 int i;
6240
Matt Carlson8fea32b2010-09-15 08:59:58 +00006241 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006242 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006243 i = (i + 1) & tp->rx_std_ring_mask)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006244 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6245 tp->rx_pkt_map_sz);
6246
Joe Perches63c3a662011-04-26 08:12:10 +00006247 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006248 for (i = tpr->rx_jmb_cons_idx;
6249 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006250 i = (i + 1) & tp->rx_jmb_ring_mask) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006251 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6252 TG3_RX_JMB_MAP_SZ);
6253 }
6254 }
6255
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006256 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258
Matt Carlson2c49a442010-09-30 10:34:35 +00006259 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006260 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6261 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262
Joe Perches63c3a662011-04-26 08:12:10 +00006263 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006264 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006265 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6266 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267 }
6268}
6269
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006270/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 *
6272 * The chip has been shut down and the driver detached from
6273 * the networking, so no interrupts or new tx packets will
6274 * end up in the driver. tp->{tx,}lock are held and thus
6275 * we may not sleep.
6276 */
Matt Carlson21f581a2009-08-28 14:00:25 +00006277static int tg3_rx_prodring_alloc(struct tg3 *tp,
6278 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279{
Matt Carlson287be122009-08-28 13:58:46 +00006280 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006282 tpr->rx_std_cons_idx = 0;
6283 tpr->rx_std_prod_idx = 0;
6284 tpr->rx_jmb_cons_idx = 0;
6285 tpr->rx_jmb_prod_idx = 0;
6286
Matt Carlson8fea32b2010-09-15 08:59:58 +00006287 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006288 memset(&tpr->rx_std_buffers[0], 0,
6289 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00006290 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006291 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00006292 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006293 goto done;
6294 }
6295
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00006297 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298
Matt Carlson287be122009-08-28 13:58:46 +00006299 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00006300 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00006301 tp->dev->mtu > ETH_DATA_LEN)
6302 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
6303 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07006304
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305 /* Initialize invariants of the rings, we only set this
6306 * stuff once. This works because the card does not
6307 * write into the rx buffer posting rings.
6308 */
Matt Carlson2c49a442010-09-30 10:34:35 +00006309 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006310 struct tg3_rx_buffer_desc *rxd;
6311
Matt Carlson21f581a2009-08-28 14:00:25 +00006312 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00006313 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
6315 rxd->opaque = (RXD_OPAQUE_RING_STD |
6316 (i << RXD_OPAQUE_INDEX_SHIFT));
6317 }
6318
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006319 /* Now allocate fresh SKBs for each rx ring. */
6320 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson86b21e52009-11-13 13:03:45 +00006321 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006322 netdev_warn(tp->dev,
6323 "Using a smaller RX standard ring. Only "
6324 "%d out of %d buffers were allocated "
6325 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006326 if (i == 0)
6327 goto initfail;
6328 tp->rx_pending = i;
6329 break;
6330 }
6331 }
6332
Joe Perches63c3a662011-04-26 08:12:10 +00006333 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006334 goto done;
6335
Matt Carlson2c49a442010-09-30 10:34:35 +00006336 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006337
Joe Perches63c3a662011-04-26 08:12:10 +00006338 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00006339 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340
Matt Carlson2c49a442010-09-30 10:34:35 +00006341 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00006342 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343
Matt Carlson0d86df82010-02-17 15:17:00 +00006344 rxd = &tpr->rx_jmb[i].std;
6345 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
6346 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
6347 RXD_FLAG_JUMBO;
6348 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
6349 (i << RXD_OPAQUE_INDEX_SHIFT));
6350 }
6351
6352 for (i = 0; i < tp->rx_jumbo_pending; i++) {
6353 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006354 netdev_warn(tp->dev,
6355 "Using a smaller RX jumbo ring. Only %d "
6356 "out of %d buffers were allocated "
6357 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00006358 if (i == 0)
6359 goto initfail;
6360 tp->rx_jumbo_pending = i;
6361 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362 }
6363 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006364
6365done:
Michael Chan32d8c572006-07-25 16:38:29 -07006366 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006367
6368initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00006369 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006370 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371}
6372
Matt Carlson21f581a2009-08-28 14:00:25 +00006373static void tg3_rx_prodring_fini(struct tg3 *tp,
6374 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375{
Matt Carlson21f581a2009-08-28 14:00:25 +00006376 kfree(tpr->rx_std_buffers);
6377 tpr->rx_std_buffers = NULL;
6378 kfree(tpr->rx_jmb_buffers);
6379 tpr->rx_jmb_buffers = NULL;
6380 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006381 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
6382 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006383 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384 }
Matt Carlson21f581a2009-08-28 14:00:25 +00006385 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006386 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
6387 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006388 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006389 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006390}
6391
Matt Carlson21f581a2009-08-28 14:00:25 +00006392static int tg3_rx_prodring_init(struct tg3 *tp,
6393 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006394{
Matt Carlson2c49a442010-09-30 10:34:35 +00006395 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
6396 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006397 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006398 return -ENOMEM;
6399
Matt Carlson4bae65c2010-11-24 08:31:52 +00006400 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
6401 TG3_RX_STD_RING_BYTES(tp),
6402 &tpr->rx_std_mapping,
6403 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006404 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006405 goto err_out;
6406
Joe Perches63c3a662011-04-26 08:12:10 +00006407 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006408 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00006409 GFP_KERNEL);
6410 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006411 goto err_out;
6412
Matt Carlson4bae65c2010-11-24 08:31:52 +00006413 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
6414 TG3_RX_JMB_RING_BYTES(tp),
6415 &tpr->rx_jmb_mapping,
6416 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006417 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006418 goto err_out;
6419 }
6420
6421 return 0;
6422
6423err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00006424 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006425 return -ENOMEM;
6426}
6427
6428/* Free up pending packets in all rx/tx rings.
6429 *
6430 * The chip has been shut down and the driver detached from
6431 * the networking, so no interrupts or new tx packets will
6432 * end up in the driver. tp->{tx,}lock is not held and we are not
6433 * in an interrupt context and thus may sleep.
6434 */
6435static void tg3_free_rings(struct tg3 *tp)
6436{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006437 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006438
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006439 for (j = 0; j < tp->irq_cnt; j++) {
6440 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006441
Matt Carlson8fea32b2010-09-15 08:59:58 +00006442 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00006443
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006444 if (!tnapi->tx_buffers)
6445 continue;
6446
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006447 for (i = 0; i < TG3_TX_RING_SIZE; ) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006448 struct ring_info *txp;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006449 struct sk_buff *skb;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006450 unsigned int k;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006451
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006452 txp = &tnapi->tx_buffers[i];
6453 skb = txp->skb;
6454
6455 if (skb == NULL) {
6456 i++;
6457 continue;
6458 }
6459
Alexander Duyckf4188d82009-12-02 16:48:38 +00006460 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006461 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006462 skb_headlen(skb),
6463 PCI_DMA_TODEVICE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006464 txp->skb = NULL;
6465
Alexander Duyckf4188d82009-12-02 16:48:38 +00006466 i++;
6467
6468 for (k = 0; k < skb_shinfo(skb)->nr_frags; k++) {
6469 txp = &tnapi->tx_buffers[i & (TG3_TX_RING_SIZE - 1)];
6470 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006471 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006472 skb_shinfo(skb)->frags[k].size,
6473 PCI_DMA_TODEVICE);
6474 i++;
6475 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006476
6477 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006478 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006479 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006480}
6481
6482/* Initialize tx/rx rings for packet processing.
6483 *
6484 * The chip has been shut down and the driver detached from
6485 * the networking, so no interrupts or new tx packets will
6486 * end up in the driver. tp->{tx,}lock are held and thus
6487 * we may not sleep.
6488 */
6489static int tg3_init_rings(struct tg3 *tp)
6490{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006491 int i;
Matt Carlson72334482009-08-28 14:03:01 +00006492
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006493 /* Free up all the SKBs. */
6494 tg3_free_rings(tp);
6495
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006496 for (i = 0; i < tp->irq_cnt; i++) {
6497 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006498
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006499 tnapi->last_tag = 0;
6500 tnapi->last_irq_tag = 0;
6501 tnapi->hw_status->status = 0;
6502 tnapi->hw_status->status_tag = 0;
6503 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6504
6505 tnapi->tx_prod = 0;
6506 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006507 if (tnapi->tx_ring)
6508 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006509
6510 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006511 if (tnapi->rx_rcb)
6512 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006513
Matt Carlson8fea32b2010-09-15 08:59:58 +00006514 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00006515 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006516 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006517 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006518 }
Matt Carlson72334482009-08-28 14:03:01 +00006519
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006520 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006521}
6522
6523/*
6524 * Must not be invoked with interrupt sources disabled and
6525 * the hardware shutdown down.
6526 */
6527static void tg3_free_consistent(struct tg3 *tp)
6528{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006529 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006530
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006531 for (i = 0; i < tp->irq_cnt; i++) {
6532 struct tg3_napi *tnapi = &tp->napi[i];
6533
6534 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006535 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006536 tnapi->tx_ring, tnapi->tx_desc_mapping);
6537 tnapi->tx_ring = NULL;
6538 }
6539
6540 kfree(tnapi->tx_buffers);
6541 tnapi->tx_buffers = NULL;
6542
6543 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006544 dma_free_coherent(&tp->pdev->dev,
6545 TG3_RX_RCB_RING_BYTES(tp),
6546 tnapi->rx_rcb,
6547 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006548 tnapi->rx_rcb = NULL;
6549 }
6550
Matt Carlson8fea32b2010-09-15 08:59:58 +00006551 tg3_rx_prodring_fini(tp, &tnapi->prodring);
6552
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006553 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006554 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
6555 tnapi->hw_status,
6556 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006557 tnapi->hw_status = NULL;
6558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006559 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006560
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006562 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
6563 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006564 tp->hw_stats = NULL;
6565 }
6566}
6567
6568/*
6569 * Must not be invoked with interrupt sources disabled and
6570 * the hardware shutdown down. Can sleep.
6571 */
6572static int tg3_alloc_consistent(struct tg3 *tp)
6573{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006574 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006575
Matt Carlson4bae65c2010-11-24 08:31:52 +00006576 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
6577 sizeof(struct tg3_hw_stats),
6578 &tp->stats_mapping,
6579 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580 if (!tp->hw_stats)
6581 goto err_out;
6582
Linus Torvalds1da177e2005-04-16 15:20:36 -07006583 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6584
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006585 for (i = 0; i < tp->irq_cnt; i++) {
6586 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006587 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006588
Matt Carlson4bae65c2010-11-24 08:31:52 +00006589 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
6590 TG3_HW_STATUS_SIZE,
6591 &tnapi->status_mapping,
6592 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006593 if (!tnapi->hw_status)
6594 goto err_out;
6595
6596 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006597 sblk = tnapi->hw_status;
6598
Matt Carlson8fea32b2010-09-15 08:59:58 +00006599 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
6600 goto err_out;
6601
Matt Carlson19cfaec2009-12-03 08:36:20 +00006602 /* If multivector TSS is enabled, vector 0 does not handle
6603 * tx interrupts. Don't allocate any resources for it.
6604 */
Joe Perches63c3a662011-04-26 08:12:10 +00006605 if ((!i && !tg3_flag(tp, ENABLE_TSS)) ||
6606 (i && tg3_flag(tp, ENABLE_TSS))) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00006607 tnapi->tx_buffers = kzalloc(sizeof(struct ring_info) *
6608 TG3_TX_RING_SIZE,
6609 GFP_KERNEL);
6610 if (!tnapi->tx_buffers)
6611 goto err_out;
6612
Matt Carlson4bae65c2010-11-24 08:31:52 +00006613 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
6614 TG3_TX_RING_BYTES,
6615 &tnapi->tx_desc_mapping,
6616 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00006617 if (!tnapi->tx_ring)
6618 goto err_out;
6619 }
6620
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006621 /*
6622 * When RSS is enabled, the status block format changes
6623 * slightly. The "rx_jumbo_consumer", "reserved",
6624 * and "rx_mini_consumer" members get mapped to the
6625 * other three rx return ring producer indexes.
6626 */
6627 switch (i) {
6628 default:
6629 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
6630 break;
6631 case 2:
6632 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
6633 break;
6634 case 3:
6635 tnapi->rx_rcb_prod_idx = &sblk->reserved;
6636 break;
6637 case 4:
6638 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
6639 break;
6640 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006641
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006642 /*
6643 * If multivector RSS is enabled, vector 0 does not handle
6644 * rx or tx interrupts. Don't allocate any resources for it.
6645 */
Joe Perches63c3a662011-04-26 08:12:10 +00006646 if (!i && tg3_flag(tp, ENABLE_RSS))
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006647 continue;
6648
Matt Carlson4bae65c2010-11-24 08:31:52 +00006649 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
6650 TG3_RX_RCB_RING_BYTES(tp),
6651 &tnapi->rx_rcb_mapping,
6652 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006653 if (!tnapi->rx_rcb)
6654 goto err_out;
6655
6656 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006657 }
6658
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659 return 0;
6660
6661err_out:
6662 tg3_free_consistent(tp);
6663 return -ENOMEM;
6664}
6665
6666#define MAX_WAIT_CNT 1000
6667
6668/* To stop a block, clear the enable bit and poll till it
6669 * clears. tp->lock is held.
6670 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006671static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672{
6673 unsigned int i;
6674 u32 val;
6675
Joe Perches63c3a662011-04-26 08:12:10 +00006676 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006677 switch (ofs) {
6678 case RCVLSC_MODE:
6679 case DMAC_MODE:
6680 case MBFREE_MODE:
6681 case BUFMGR_MODE:
6682 case MEMARB_MODE:
6683 /* We can't enable/disable these bits of the
6684 * 5705/5750, just say success.
6685 */
6686 return 0;
6687
6688 default:
6689 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006691 }
6692
6693 val = tr32(ofs);
6694 val &= ~enable_bit;
6695 tw32_f(ofs, val);
6696
6697 for (i = 0; i < MAX_WAIT_CNT; i++) {
6698 udelay(100);
6699 val = tr32(ofs);
6700 if ((val & enable_bit) == 0)
6701 break;
6702 }
6703
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006704 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00006705 dev_err(&tp->pdev->dev,
6706 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
6707 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708 return -ENODEV;
6709 }
6710
6711 return 0;
6712}
6713
6714/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006715static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716{
6717 int i, err;
6718
6719 tg3_disable_ints(tp);
6720
6721 tp->rx_mode &= ~RX_MODE_ENABLE;
6722 tw32_f(MAC_RX_MODE, tp->rx_mode);
6723 udelay(10);
6724
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006725 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
6726 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
6727 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
6728 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
6729 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
6730 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006731
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006732 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
6733 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
6734 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
6735 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
6736 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
6737 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
6738 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006739
6740 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
6741 tw32_f(MAC_MODE, tp->mac_mode);
6742 udelay(40);
6743
6744 tp->tx_mode &= ~TX_MODE_ENABLE;
6745 tw32_f(MAC_TX_MODE, tp->tx_mode);
6746
6747 for (i = 0; i < MAX_WAIT_CNT; i++) {
6748 udelay(100);
6749 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
6750 break;
6751 }
6752 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00006753 dev_err(&tp->pdev->dev,
6754 "%s timed out, TX_MODE_ENABLE will not clear "
6755 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07006756 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006757 }
6758
Michael Chane6de8ad2005-05-05 14:42:41 -07006759 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006760 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
6761 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762
6763 tw32(FTQ_RESET, 0xffffffff);
6764 tw32(FTQ_RESET, 0x00000000);
6765
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006766 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
6767 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006769 for (i = 0; i < tp->irq_cnt; i++) {
6770 struct tg3_napi *tnapi = &tp->napi[i];
6771 if (tnapi->hw_status)
6772 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774 if (tp->hw_stats)
6775 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6776
Linus Torvalds1da177e2005-04-16 15:20:36 -07006777 return err;
6778}
6779
Matt Carlson0d3031d2007-10-10 18:02:43 -07006780static void tg3_ape_send_event(struct tg3 *tp, u32 event)
6781{
6782 int i;
6783 u32 apedata;
6784
Matt Carlsondc6d0742010-09-15 08:59:55 +00006785 /* NCSI does not support APE events */
Joe Perches63c3a662011-04-26 08:12:10 +00006786 if (tg3_flag(tp, APE_HAS_NCSI))
Matt Carlsondc6d0742010-09-15 08:59:55 +00006787 return;
6788
Matt Carlson0d3031d2007-10-10 18:02:43 -07006789 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
6790 if (apedata != APE_SEG_SIG_MAGIC)
6791 return;
6792
6793 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07006794 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006795 return;
6796
6797 /* Wait for up to 1 millisecond for APE to service previous event. */
6798 for (i = 0; i < 10; i++) {
6799 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
6800 return;
6801
6802 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
6803
6804 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6805 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
6806 event | APE_EVENT_STATUS_EVENT_PENDING);
6807
6808 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
6809
6810 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6811 break;
6812
6813 udelay(100);
6814 }
6815
6816 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6817 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
6818}
6819
6820static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
6821{
6822 u32 event;
6823 u32 apedata;
6824
Joe Perches63c3a662011-04-26 08:12:10 +00006825 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006826 return;
6827
6828 switch (kind) {
Matt Carlson33f401a2010-04-05 10:19:27 +00006829 case RESET_KIND_INIT:
6830 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
6831 APE_HOST_SEG_SIG_MAGIC);
6832 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
6833 APE_HOST_SEG_LEN_MAGIC);
6834 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
6835 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
6836 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
Matt Carlson6867c842010-07-11 09:31:44 +00006837 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
Matt Carlson33f401a2010-04-05 10:19:27 +00006838 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
6839 APE_HOST_BEHAV_NO_PHYLOCK);
Matt Carlsondc6d0742010-09-15 08:59:55 +00006840 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
6841 TG3_APE_HOST_DRVR_STATE_START);
Matt Carlson0d3031d2007-10-10 18:02:43 -07006842
Matt Carlson33f401a2010-04-05 10:19:27 +00006843 event = APE_EVENT_STATUS_STATE_START;
6844 break;
6845 case RESET_KIND_SHUTDOWN:
6846 /* With the interface we are currently using,
6847 * APE does not track driver state. Wiping
6848 * out the HOST SEGMENT SIGNATURE forces
6849 * the APE to assume OS absent status.
6850 */
6851 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
Matt Carlsonb2aee152008-11-03 16:51:11 -08006852
Matt Carlsondc6d0742010-09-15 08:59:55 +00006853 if (device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00006854 tg3_flag(tp, WOL_ENABLE)) {
Matt Carlsondc6d0742010-09-15 08:59:55 +00006855 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
6856 TG3_APE_HOST_WOL_SPEED_AUTO);
6857 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
6858 } else
6859 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
6860
6861 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
6862
Matt Carlson33f401a2010-04-05 10:19:27 +00006863 event = APE_EVENT_STATUS_STATE_UNLOAD;
6864 break;
6865 case RESET_KIND_SUSPEND:
6866 event = APE_EVENT_STATUS_STATE_SUSPEND;
6867 break;
6868 default:
6869 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006870 }
6871
6872 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
6873
6874 tg3_ape_send_event(tp, event);
6875}
6876
Michael Chane6af3012005-04-21 17:12:05 -07006877/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
6879{
David S. Millerf49639e2006-06-09 11:58:36 -07006880 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
6881 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006882
Joe Perches63c3a662011-04-26 08:12:10 +00006883 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006884 switch (kind) {
6885 case RESET_KIND_INIT:
6886 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6887 DRV_STATE_START);
6888 break;
6889
6890 case RESET_KIND_SHUTDOWN:
6891 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6892 DRV_STATE_UNLOAD);
6893 break;
6894
6895 case RESET_KIND_SUSPEND:
6896 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6897 DRV_STATE_SUSPEND);
6898 break;
6899
6900 default:
6901 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006904
6905 if (kind == RESET_KIND_INIT ||
6906 kind == RESET_KIND_SUSPEND)
6907 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908}
6909
6910/* tp->lock is held. */
6911static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
6912{
Joe Perches63c3a662011-04-26 08:12:10 +00006913 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914 switch (kind) {
6915 case RESET_KIND_INIT:
6916 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6917 DRV_STATE_START_DONE);
6918 break;
6919
6920 case RESET_KIND_SHUTDOWN:
6921 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6922 DRV_STATE_UNLOAD_DONE);
6923 break;
6924
6925 default:
6926 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006928 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006929
6930 if (kind == RESET_KIND_SHUTDOWN)
6931 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006932}
6933
6934/* tp->lock is held. */
6935static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
6936{
Joe Perches63c3a662011-04-26 08:12:10 +00006937 if (tg3_flag(tp, ENABLE_ASF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006938 switch (kind) {
6939 case RESET_KIND_INIT:
6940 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6941 DRV_STATE_START);
6942 break;
6943
6944 case RESET_KIND_SHUTDOWN:
6945 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6946 DRV_STATE_UNLOAD);
6947 break;
6948
6949 case RESET_KIND_SUSPEND:
6950 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6951 DRV_STATE_SUSPEND);
6952 break;
6953
6954 default:
6955 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006957 }
6958}
6959
Michael Chan7a6f4362006-09-27 16:03:31 -07006960static int tg3_poll_fw(struct tg3 *tp)
6961{
6962 int i;
6963 u32 val;
6964
Michael Chanb5d37722006-09-27 16:06:21 -07006965 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08006966 /* Wait up to 20ms for init done. */
6967 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07006968 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
6969 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08006970 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07006971 }
6972 return -ENODEV;
6973 }
6974
Michael Chan7a6f4362006-09-27 16:03:31 -07006975 /* Wait for firmware initialization to complete. */
6976 for (i = 0; i < 100000; i++) {
6977 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
6978 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
6979 break;
6980 udelay(10);
6981 }
6982
6983 /* Chip might not be fitted with firmware. Some Sun onboard
6984 * parts are configured like that. So don't signal the timeout
6985 * of the above loop as an error, but do report the lack of
6986 * running firmware once.
6987 */
Joe Perches63c3a662011-04-26 08:12:10 +00006988 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
6989 tg3_flag_set(tp, NO_FWARE_REPORTED);
Michael Chan7a6f4362006-09-27 16:03:31 -07006990
Joe Perches05dbe002010-02-17 19:44:19 +00006991 netdev_info(tp->dev, "No firmware running\n");
Michael Chan7a6f4362006-09-27 16:03:31 -07006992 }
6993
Matt Carlson6b10c162010-02-12 14:47:08 +00006994 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
6995 /* The 57765 A0 needs a little more
6996 * time to do some important work.
6997 */
6998 mdelay(10);
6999 }
7000
Michael Chan7a6f4362006-09-27 16:03:31 -07007001 return 0;
7002}
7003
Michael Chanee6a99b2007-07-18 21:49:10 -07007004/* Save PCI command register before chip reset */
7005static void tg3_save_pci_state(struct tg3 *tp)
7006{
Matt Carlson8a6eac92007-10-21 16:17:55 -07007007 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007008}
7009
7010/* Restore PCI state after chip reset */
7011static void tg3_restore_pci_state(struct tg3 *tp)
7012{
7013 u32 val;
7014
7015 /* Re-enable indirect register accesses. */
7016 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7017 tp->misc_host_ctrl);
7018
7019 /* Set MAX PCI retry to zero. */
7020 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7021 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007022 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07007023 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007024 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00007025 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007026 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +00007027 PCISTATE_ALLOW_APE_SHMEM_WR |
7028 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07007029 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7030
Matt Carlson8a6eac92007-10-21 16:17:55 -07007031 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007032
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007033 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
Joe Perches63c3a662011-04-26 08:12:10 +00007034 if (tg3_flag(tp, PCI_EXPRESS))
Matt Carlsoncf790032010-11-24 08:31:48 +00007035 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007036 else {
7037 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7038 tp->pci_cacheline_sz);
7039 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7040 tp->pci_lat_timer);
7041 }
Michael Chan114342f2007-10-15 02:12:26 -07007042 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08007043
Michael Chanee6a99b2007-07-18 21:49:10 -07007044 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00007045 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07007046 u16 pcix_cmd;
7047
7048 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7049 &pcix_cmd);
7050 pcix_cmd &= ~PCI_X_CMD_ERO;
7051 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7052 pcix_cmd);
7053 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007054
Joe Perches63c3a662011-04-26 08:12:10 +00007055 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007056
7057 /* Chip reset on 5780 will reset MSI enable bit,
7058 * so need to restore it.
7059 */
Joe Perches63c3a662011-04-26 08:12:10 +00007060 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007061 u16 ctrl;
7062
7063 pci_read_config_word(tp->pdev,
7064 tp->msi_cap + PCI_MSI_FLAGS,
7065 &ctrl);
7066 pci_write_config_word(tp->pdev,
7067 tp->msi_cap + PCI_MSI_FLAGS,
7068 ctrl | PCI_MSI_FLAGS_ENABLE);
7069 val = tr32(MSGINT_MODE);
7070 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7071 }
7072 }
7073}
7074
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075static void tg3_stop_fw(struct tg3 *);
7076
7077/* tp->lock is held. */
7078static int tg3_chip_reset(struct tg3 *tp)
7079{
7080 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007081 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007082 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007083
David S. Millerf49639e2006-06-09 11:58:36 -07007084 tg3_nvram_lock(tp);
7085
Matt Carlson77b483f2008-08-15 14:07:24 -07007086 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7087
David S. Millerf49639e2006-06-09 11:58:36 -07007088 /* No matching tg3_nvram_unlock() after this because
7089 * chip reset below will undo the nvram lock.
7090 */
7091 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092
Michael Chanee6a99b2007-07-18 21:49:10 -07007093 /* GRC_MISC_CFG core clock reset will clear the memory
7094 * enable bit in PCI register 4 and the MSI enable bit
7095 * on some chips, so we save relevant registers here.
7096 */
7097 tg3_save_pci_state(tp);
7098
Michael Chand9ab5ad2006-03-20 22:27:35 -08007099 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00007100 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08007101 tw32(GRC_FASTBOOT_PC, 0);
7102
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103 /*
7104 * We must avoid the readl() that normally takes place.
7105 * It locks machines, causes machine checks, and other
7106 * fun things. So, temporarily disable the 5701
7107 * hardware workaround, while we do the reset.
7108 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007109 write_op = tp->write32;
7110 if (write_op == tg3_write_flush_reg32)
7111 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007112
Michael Chand18edcb2007-03-24 20:57:11 -07007113 /* Prevent the irq handler from reading or writing PCI registers
7114 * during chip reset when the memory enable bit in the PCI command
7115 * register may be cleared. The chip does not generate interrupt
7116 * at this time, but the irq handler may still be called due to irq
7117 * sharing or irqpoll.
7118 */
Joe Perches63c3a662011-04-26 08:12:10 +00007119 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007120 for (i = 0; i < tp->irq_cnt; i++) {
7121 struct tg3_napi *tnapi = &tp->napi[i];
7122 if (tnapi->hw_status) {
7123 tnapi->hw_status->status = 0;
7124 tnapi->hw_status->status_tag = 0;
7125 }
7126 tnapi->last_tag = 0;
7127 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007128 }
Michael Chand18edcb2007-03-24 20:57:11 -07007129 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007130
7131 for (i = 0; i < tp->irq_cnt; i++)
7132 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007133
Matt Carlson255ca312009-08-25 10:07:27 +00007134 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7135 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7136 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7137 }
7138
Linus Torvalds1da177e2005-04-16 15:20:36 -07007139 /* do the reset */
7140 val = GRC_MISC_CFG_CORECLK_RESET;
7141
Joe Perches63c3a662011-04-26 08:12:10 +00007142 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00007143 /* Force PCIe 1.0a mode */
7144 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007145 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00007146 tr32(TG3_PCIE_PHY_TSTCTL) ==
7147 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7148 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7149
Linus Torvalds1da177e2005-04-16 15:20:36 -07007150 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7151 tw32(GRC_MISC_CFG, (1 << 29));
7152 val |= (1 << 29);
7153 }
7154 }
7155
Michael Chanb5d37722006-09-27 16:06:21 -07007156 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7157 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7158 tw32(GRC_VCPU_EXT_CTRL,
7159 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7160 }
7161
Matt Carlsonf37500d2010-08-02 11:25:59 +00007162 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00007163 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007164 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007165
Linus Torvalds1da177e2005-04-16 15:20:36 -07007166 tw32(GRC_MISC_CFG, val);
7167
Michael Chan1ee582d2005-08-09 20:16:46 -07007168 /* restore 5701 hardware bug workaround write method */
7169 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007170
7171 /* Unfortunately, we have to delay before the PCI read back.
7172 * Some 575X chips even will not respond to a PCI cfg access
7173 * when the reset command is given to the chip.
7174 *
7175 * How do these hardware designers expect things to work
7176 * properly if the PCI write is posted for a long period
7177 * of time? It is always necessary to have some method by
7178 * which a register read back can occur to push the write
7179 * out which does the reset.
7180 *
7181 * For most tg3 variants the trick below was working.
7182 * Ho hum...
7183 */
7184 udelay(120);
7185
7186 /* Flush PCI posted writes. The normal MMIO registers
7187 * are inaccessible at this time so this is the only
7188 * way to make this reliably (actually, this is no longer
7189 * the case, see above). I tried to use indirect
7190 * register read/write but this upset some 5701 variants.
7191 */
7192 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7193
7194 udelay(120);
7195
Joe Perches63c3a662011-04-26 08:12:10 +00007196 if (tg3_flag(tp, PCI_EXPRESS) && tp->pcie_cap) {
Matt Carlsone7126992009-08-25 10:08:16 +00007197 u16 val16;
7198
Linus Torvalds1da177e2005-04-16 15:20:36 -07007199 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7200 int i;
7201 u32 cfg_val;
7202
7203 /* Wait for link training to complete. */
7204 for (i = 0; i < 5000; i++)
7205 udelay(100);
7206
7207 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7208 pci_write_config_dword(tp->pdev, 0xc4,
7209 cfg_val | (1 << 15));
7210 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007211
Matt Carlsone7126992009-08-25 10:08:16 +00007212 /* Clear the "no snoop" and "relaxed ordering" bits. */
7213 pci_read_config_word(tp->pdev,
7214 tp->pcie_cap + PCI_EXP_DEVCTL,
7215 &val16);
7216 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7217 PCI_EXP_DEVCTL_NOSNOOP_EN);
7218 /*
7219 * Older PCIe devices only support the 128 byte
7220 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007221 */
Joe Perches63c3a662011-04-26 08:12:10 +00007222 if (!tg3_flag(tp, CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007223 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007224 pci_write_config_word(tp->pdev,
7225 tp->pcie_cap + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007226 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007227
Matt Carlsoncf790032010-11-24 08:31:48 +00007228 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007229
7230 /* Clear error status */
7231 pci_write_config_word(tp->pdev,
7232 tp->pcie_cap + PCI_EXP_DEVSTA,
7233 PCI_EXP_DEVSTA_CED |
7234 PCI_EXP_DEVSTA_NFED |
7235 PCI_EXP_DEVSTA_FED |
7236 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007237 }
7238
Michael Chanee6a99b2007-07-18 21:49:10 -07007239 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007240
Joe Perches63c3a662011-04-26 08:12:10 +00007241 tg3_flag_clear(tp, CHIP_RESETTING);
7242 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07007243
Michael Chanee6a99b2007-07-18 21:49:10 -07007244 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007245 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07007246 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007247 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007248
7249 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7250 tg3_stop_fw(tp);
7251 tw32(0x5000, 0x400);
7252 }
7253
7254 tw32(GRC_MODE, tp->grc_mode);
7255
7256 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007257 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007258
7259 tw32(0xc4, val | (1 << 15));
7260 }
7261
7262 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7263 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7264 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7265 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7266 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7267 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7268 }
7269
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007270 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlsonb7d4d462011-07-20 10:20:50 +00007271 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007272 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007273 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlsonb7d4d462011-07-20 10:20:50 +00007274 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007275 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007276 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007277 val = 0;
7278
7279 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007280 udelay(40);
7281
Matt Carlson77b483f2008-08-15 14:07:24 -07007282 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7283
Michael Chan7a6f4362006-09-27 16:03:31 -07007284 err = tg3_poll_fw(tp);
7285 if (err)
7286 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287
Matt Carlson0a9140c2009-08-28 12:27:50 +00007288 tg3_mdio_start(tp);
7289
Joe Perches63c3a662011-04-26 08:12:10 +00007290 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007291 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7292 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007293 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007294 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007295
7296 tw32(0x7c00, val | (1 << 25));
7297 }
7298
Matt Carlsond78b59f2011-04-05 14:22:46 +00007299 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
7300 val = tr32(TG3_CPMU_CLCK_ORIDE);
7301 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
7302 }
7303
Linus Torvalds1da177e2005-04-16 15:20:36 -07007304 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00007305 tg3_flag_clear(tp, ENABLE_ASF);
7306 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007307 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7308 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7309 u32 nic_cfg;
7310
7311 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7312 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00007313 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007314 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00007315 if (tg3_flag(tp, 5750_PLUS))
7316 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007317 }
7318 }
7319
7320 return 0;
7321}
7322
7323/* tp->lock is held. */
7324static void tg3_stop_fw(struct tg3 *tp)
7325{
Joe Perches63c3a662011-04-26 08:12:10 +00007326 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07007327 /* Wait for RX cpu to ACK the previous event. */
7328 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007329
7330 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007331
7332 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007333
Matt Carlson7c5026a2008-05-02 16:49:29 -07007334 /* Wait for RX cpu to ACK this event. */
7335 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007336 }
7337}
7338
7339/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007340static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007341{
7342 int err;
7343
7344 tg3_stop_fw(tp);
7345
Michael Chan944d9802005-05-29 14:57:48 -07007346 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007347
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007348 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007349 err = tg3_chip_reset(tp);
7350
Matt Carlsondaba2a62009-04-20 06:58:52 +00007351 __tg3_set_mac_addr(tp, 0);
7352
Michael Chan944d9802005-05-29 14:57:48 -07007353 tg3_write_sig_legacy(tp, kind);
7354 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355
7356 if (err)
7357 return err;
7358
7359 return 0;
7360}
7361
Linus Torvalds1da177e2005-04-16 15:20:36 -07007362#define RX_CPU_SCRATCH_BASE 0x30000
7363#define RX_CPU_SCRATCH_SIZE 0x04000
7364#define TX_CPU_SCRATCH_BASE 0x34000
7365#define TX_CPU_SCRATCH_SIZE 0x04000
7366
7367/* tp->lock is held. */
7368static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
7369{
7370 int i;
7371
Joe Perches63c3a662011-04-26 08:12:10 +00007372 BUG_ON(offset == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007373
Michael Chanb5d37722006-09-27 16:06:21 -07007374 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7375 u32 val = tr32(GRC_VCPU_EXT_CTRL);
7376
7377 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
7378 return 0;
7379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007380 if (offset == RX_CPU_BASE) {
7381 for (i = 0; i < 10000; i++) {
7382 tw32(offset + CPU_STATE, 0xffffffff);
7383 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7384 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7385 break;
7386 }
7387
7388 tw32(offset + CPU_STATE, 0xffffffff);
7389 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
7390 udelay(10);
7391 } else {
7392 for (i = 0; i < 10000; i++) {
7393 tw32(offset + CPU_STATE, 0xffffffff);
7394 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7395 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7396 break;
7397 }
7398 }
7399
7400 if (i >= 10000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007401 netdev_err(tp->dev, "%s timed out, %s CPU\n",
7402 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007403 return -ENODEV;
7404 }
Michael Chanec41c7d2006-01-17 02:40:55 -08007405
7406 /* Clear firmware's nvram arbitration. */
Joe Perches63c3a662011-04-26 08:12:10 +00007407 if (tg3_flag(tp, NVRAM))
Michael Chanec41c7d2006-01-17 02:40:55 -08007408 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007409 return 0;
7410}
7411
7412struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007413 unsigned int fw_base;
7414 unsigned int fw_len;
7415 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007416};
7417
7418/* tp->lock is held. */
7419static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
7420 int cpu_scratch_size, struct fw_info *info)
7421{
Michael Chanec41c7d2006-01-17 02:40:55 -08007422 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007423 void (*write_op)(struct tg3 *, u32, u32);
7424
Joe Perches63c3a662011-04-26 08:12:10 +00007425 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007426 netdev_err(tp->dev,
7427 "%s: Trying to load TX cpu firmware which is 5705\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007428 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007429 return -EINVAL;
7430 }
7431
Joe Perches63c3a662011-04-26 08:12:10 +00007432 if (tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007433 write_op = tg3_write_mem;
7434 else
7435 write_op = tg3_write_indirect_reg32;
7436
Michael Chan1b628152005-05-29 14:59:49 -07007437 /* It is possible that bootcode is still loading at this point.
7438 * Get the nvram lock first before halting the cpu.
7439 */
Michael Chanec41c7d2006-01-17 02:40:55 -08007440 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007441 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08007442 if (!lock_err)
7443 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007444 if (err)
7445 goto out;
7446
7447 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
7448 write_op(tp, cpu_scratch_base + i, 0);
7449 tw32(cpu_base + CPU_STATE, 0xffffffff);
7450 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007451 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007452 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007453 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07007454 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007455 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007456
7457 err = 0;
7458
7459out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007460 return err;
7461}
7462
7463/* tp->lock is held. */
7464static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
7465{
7466 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007467 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007468 int err, i;
7469
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007470 fw_data = (void *)tp->fw->data;
7471
7472 /* Firmware blob starts with version numbers, followed by
7473 start address and length. We are setting complete length.
7474 length = end_address_of_bss - start_address_of_text.
7475 Remainder is the blob to be loaded contiguously
7476 from start address. */
7477
7478 info.fw_base = be32_to_cpu(fw_data[1]);
7479 info.fw_len = tp->fw->size - 12;
7480 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007481
7482 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
7483 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
7484 &info);
7485 if (err)
7486 return err;
7487
7488 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
7489 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
7490 &info);
7491 if (err)
7492 return err;
7493
7494 /* Now startup only the RX cpu. */
7495 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007496 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007497
7498 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007499 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007500 break;
7501 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7502 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007503 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007504 udelay(1000);
7505 }
7506 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007507 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
7508 "should be %08x\n", __func__,
Joe Perches05dbe002010-02-17 19:44:19 +00007509 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007510 return -ENODEV;
7511 }
7512 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7513 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
7514
7515 return 0;
7516}
7517
Linus Torvalds1da177e2005-04-16 15:20:36 -07007518/* tp->lock is held. */
7519static int tg3_load_tso_firmware(struct tg3 *tp)
7520{
7521 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007522 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007523 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
7524 int err, i;
7525
Joe Perches63c3a662011-04-26 08:12:10 +00007526 if (tg3_flag(tp, HW_TSO_1) ||
7527 tg3_flag(tp, HW_TSO_2) ||
7528 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007529 return 0;
7530
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007531 fw_data = (void *)tp->fw->data;
7532
7533 /* Firmware blob starts with version numbers, followed by
7534 start address and length. We are setting complete length.
7535 length = end_address_of_bss - start_address_of_text.
7536 Remainder is the blob to be loaded contiguously
7537 from start address. */
7538
7539 info.fw_base = be32_to_cpu(fw_data[1]);
7540 cpu_scratch_size = tp->fw_len;
7541 info.fw_len = tp->fw->size - 12;
7542 info.fw_data = &fw_data[3];
7543
Linus Torvalds1da177e2005-04-16 15:20:36 -07007544 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007545 cpu_base = RX_CPU_BASE;
7546 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007547 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007548 cpu_base = TX_CPU_BASE;
7549 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
7550 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
7551 }
7552
7553 err = tg3_load_firmware_cpu(tp, cpu_base,
7554 cpu_scratch_base, cpu_scratch_size,
7555 &info);
7556 if (err)
7557 return err;
7558
7559 /* Now startup the cpu. */
7560 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007561 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007562
7563 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007564 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007565 break;
7566 tw32(cpu_base + CPU_STATE, 0xffffffff);
7567 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007568 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007569 udelay(1000);
7570 }
7571 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007572 netdev_err(tp->dev,
7573 "%s fails to set CPU PC, is %08x should be %08x\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007574 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007575 return -ENODEV;
7576 }
7577 tw32(cpu_base + CPU_STATE, 0xffffffff);
7578 tw32_f(cpu_base + CPU_MODE, 0x00000000);
7579 return 0;
7580}
7581
Linus Torvalds1da177e2005-04-16 15:20:36 -07007582
Linus Torvalds1da177e2005-04-16 15:20:36 -07007583static int tg3_set_mac_addr(struct net_device *dev, void *p)
7584{
7585 struct tg3 *tp = netdev_priv(dev);
7586 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007587 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007588
Michael Chanf9804dd2005-09-27 12:13:10 -07007589 if (!is_valid_ether_addr(addr->sa_data))
7590 return -EINVAL;
7591
Linus Torvalds1da177e2005-04-16 15:20:36 -07007592 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7593
Michael Chane75f7c92006-03-20 21:33:26 -08007594 if (!netif_running(dev))
7595 return 0;
7596
Joe Perches63c3a662011-04-26 08:12:10 +00007597 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007598 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007599
Michael Chan986e0ae2007-05-05 12:10:20 -07007600 addr0_high = tr32(MAC_ADDR_0_HIGH);
7601 addr0_low = tr32(MAC_ADDR_0_LOW);
7602 addr1_high = tr32(MAC_ADDR_1_HIGH);
7603 addr1_low = tr32(MAC_ADDR_1_LOW);
7604
7605 /* Skip MAC addr 1 if ASF is using it. */
7606 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7607 !(addr1_high == 0 && addr1_low == 0))
7608 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007609 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007610 spin_lock_bh(&tp->lock);
7611 __tg3_set_mac_addr(tp, skip_mac_1);
7612 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007613
Michael Chanb9ec6c12006-07-25 16:37:27 -07007614 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007615}
7616
7617/* tp->lock is held. */
7618static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7619 dma_addr_t mapping, u32 maxlen_flags,
7620 u32 nic_addr)
7621{
7622 tg3_write_mem(tp,
7623 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7624 ((u64) mapping >> 32));
7625 tg3_write_mem(tp,
7626 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7627 ((u64) mapping & 0xffffffff));
7628 tg3_write_mem(tp,
7629 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7630 maxlen_flags);
7631
Joe Perches63c3a662011-04-26 08:12:10 +00007632 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007633 tg3_write_mem(tp,
7634 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7635 nic_addr);
7636}
7637
7638static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007639static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007640{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007641 int i;
7642
Joe Perches63c3a662011-04-26 08:12:10 +00007643 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007644 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7645 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7646 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007647 } else {
7648 tw32(HOSTCC_TXCOL_TICKS, 0);
7649 tw32(HOSTCC_TXMAX_FRAMES, 0);
7650 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007651 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007652
Joe Perches63c3a662011-04-26 08:12:10 +00007653 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007654 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
7655 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
7656 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
7657 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007658 tw32(HOSTCC_RXCOL_TICKS, 0);
7659 tw32(HOSTCC_RXMAX_FRAMES, 0);
7660 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07007661 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007662
Joe Perches63c3a662011-04-26 08:12:10 +00007663 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07007664 u32 val = ec->stats_block_coalesce_usecs;
7665
Matt Carlsonb6080e12009-09-01 13:12:00 +00007666 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
7667 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
7668
David S. Miller15f98502005-05-18 22:49:26 -07007669 if (!netif_carrier_ok(tp->dev))
7670 val = 0;
7671
7672 tw32(HOSTCC_STAT_COAL_TICKS, val);
7673 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007674
7675 for (i = 0; i < tp->irq_cnt - 1; i++) {
7676 u32 reg;
7677
7678 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
7679 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007680 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
7681 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007682 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
7683 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007684
Joe Perches63c3a662011-04-26 08:12:10 +00007685 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007686 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
7687 tw32(reg, ec->tx_coalesce_usecs);
7688 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
7689 tw32(reg, ec->tx_max_coalesced_frames);
7690 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
7691 tw32(reg, ec->tx_max_coalesced_frames_irq);
7692 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007693 }
7694
7695 for (; i < tp->irq_max - 1; i++) {
7696 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007697 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007698 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007699
Joe Perches63c3a662011-04-26 08:12:10 +00007700 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007701 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
7702 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
7703 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7704 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007705 }
David S. Miller15f98502005-05-18 22:49:26 -07007706}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007707
7708/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00007709static void tg3_rings_reset(struct tg3 *tp)
7710{
7711 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007712 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007713 struct tg3_napi *tnapi = &tp->napi[0];
7714
7715 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00007716 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007717 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00007718 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00007719 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlsonb703df62009-12-03 08:36:21 +00007720 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
7721 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007722 else
7723 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7724
7725 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7726 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
7727 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
7728 BDINFO_FLAGS_DISABLED);
7729
7730
7731 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00007732 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007733 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00007734 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007735 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007736 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7737 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00007738 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
7739 else
7740 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7741
7742 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7743 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
7744 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
7745 BDINFO_FLAGS_DISABLED);
7746
7747 /* Disable interrupts */
7748 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
7749
7750 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00007751 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00007752 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007753 tp->napi[i].tx_prod = 0;
7754 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007755 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00007756 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007757 tw32_rx_mbox(tp->napi[i].consmbox, 0);
7758 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
7759 }
Joe Perches63c3a662011-04-26 08:12:10 +00007760 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00007761 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007762 } else {
7763 tp->napi[0].tx_prod = 0;
7764 tp->napi[0].tx_cons = 0;
7765 tw32_mailbox(tp->napi[0].prodmbox, 0);
7766 tw32_rx_mbox(tp->napi[0].consmbox, 0);
7767 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007768
7769 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00007770 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00007771 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
7772 for (i = 0; i < 16; i++)
7773 tw32_tx_mbox(mbox + i * 8, 0);
7774 }
7775
7776 txrcb = NIC_SRAM_SEND_RCB;
7777 rxrcb = NIC_SRAM_RCV_RET_RCB;
7778
7779 /* Clear status block in ram. */
7780 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7781
7782 /* Set status block DMA address */
7783 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7784 ((u64) tnapi->status_mapping >> 32));
7785 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7786 ((u64) tnapi->status_mapping & 0xffffffff));
7787
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007788 if (tnapi->tx_ring) {
7789 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7790 (TG3_TX_RING_SIZE <<
7791 BDINFO_FLAGS_MAXLEN_SHIFT),
7792 NIC_SRAM_TX_BUFFER_DESC);
7793 txrcb += TG3_BDINFO_SIZE;
7794 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007795
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007796 if (tnapi->rx_rcb) {
7797 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007798 (tp->rx_ret_ring_mask + 1) <<
7799 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007800 rxrcb += TG3_BDINFO_SIZE;
7801 }
7802
7803 stblk = HOSTCC_STATBLCK_RING1;
7804
7805 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
7806 u64 mapping = (u64)tnapi->status_mapping;
7807 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
7808 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
7809
7810 /* Clear status block in ram. */
7811 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7812
Matt Carlson19cfaec2009-12-03 08:36:20 +00007813 if (tnapi->tx_ring) {
7814 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7815 (TG3_TX_RING_SIZE <<
7816 BDINFO_FLAGS_MAXLEN_SHIFT),
7817 NIC_SRAM_TX_BUFFER_DESC);
7818 txrcb += TG3_BDINFO_SIZE;
7819 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007820
7821 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007822 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007823 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7824
7825 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007826 rxrcb += TG3_BDINFO_SIZE;
7827 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007828}
7829
Matt Carlsoneb07a942011-04-20 07:57:36 +00007830static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
7831{
7832 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
7833
Joe Perches63c3a662011-04-26 08:12:10 +00007834 if (!tg3_flag(tp, 5750_PLUS) ||
7835 tg3_flag(tp, 5780_CLASS) ||
Matt Carlsoneb07a942011-04-20 07:57:36 +00007836 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
7837 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
7838 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
7839 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7840 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
7841 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
7842 else
7843 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
7844
7845 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
7846 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
7847
7848 val = min(nic_rep_thresh, host_rep_thresh);
7849 tw32(RCVBDI_STD_THRESH, val);
7850
Joe Perches63c3a662011-04-26 08:12:10 +00007851 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00007852 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
7853
Joe Perches63c3a662011-04-26 08:12:10 +00007854 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00007855 return;
7856
Joe Perches63c3a662011-04-26 08:12:10 +00007857 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00007858 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
7859 else
7860 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5717;
7861
7862 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
7863
7864 val = min(bdcache_maxcnt / 2, host_rep_thresh);
7865 tw32(RCVBDI_JUMBO_THRESH, val);
7866
Joe Perches63c3a662011-04-26 08:12:10 +00007867 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00007868 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
7869}
7870
Matt Carlson2d31eca2009-09-01 12:53:31 +00007871/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007872static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007873{
7874 u32 val, rdmac_mode;
7875 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00007876 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007877
7878 tg3_disable_ints(tp);
7879
7880 tg3_stop_fw(tp);
7881
7882 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
7883
Joe Perches63c3a662011-04-26 08:12:10 +00007884 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07007885 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007886
Matt Carlson699c0192010-12-06 08:28:51 +00007887 /* Enable MAC control of LPI */
7888 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
7889 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
7890 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
7891 TG3_CPMU_EEE_LNKIDL_UART_IDL);
7892
7893 tw32_f(TG3_CPMU_EEE_CTRL,
7894 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
7895
Matt Carlsona386b902010-12-06 08:28:53 +00007896 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
7897 TG3_CPMU_EEEMD_LPI_IN_TX |
7898 TG3_CPMU_EEEMD_LPI_IN_RX |
7899 TG3_CPMU_EEEMD_EEE_ENABLE;
7900
7901 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
7902 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
7903
Joe Perches63c3a662011-04-26 08:12:10 +00007904 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00007905 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
7906
7907 tw32_f(TG3_CPMU_EEE_MODE, val);
7908
7909 tw32_f(TG3_CPMU_EEE_DBTMR1,
7910 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
7911 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
7912
7913 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00007914 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00007915 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00007916 }
7917
Matt Carlson603f1172010-02-12 14:47:10 +00007918 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08007919 tg3_phy_reset(tp);
7920
Linus Torvalds1da177e2005-04-16 15:20:36 -07007921 err = tg3_chip_reset(tp);
7922 if (err)
7923 return err;
7924
7925 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
7926
Matt Carlsonbcb37f62008-11-03 16:52:09 -08007927 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007928 val = tr32(TG3_CPMU_CTRL);
7929 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
7930 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08007931
7932 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7933 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7934 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7935 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
7936
7937 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
7938 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
7939 val |= CPMU_LNK_AWARE_MACCLK_6_25;
7940 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
7941
7942 val = tr32(TG3_CPMU_HST_ACC);
7943 val &= ~CPMU_HST_ACC_MACCLK_MASK;
7944 val |= CPMU_HST_ACC_MACCLK_6_25;
7945 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07007946 }
7947
Matt Carlson33466d92009-04-20 06:57:41 +00007948 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7949 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
7950 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
7951 PCIE_PWR_MGMT_L1_THRESH_4MS;
7952 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00007953
7954 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
7955 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
7956
7957 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00007958
Matt Carlsonf40386c2009-11-02 14:24:02 +00007959 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7960 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00007961 }
7962
Joe Perches63c3a662011-04-26 08:12:10 +00007963 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00007964 u32 grc_mode = tr32(GRC_MODE);
7965
7966 /* Access the lower 1K of PL PCIE block registers. */
7967 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7968 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
7969
7970 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
7971 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
7972 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
7973
7974 tw32(GRC_MODE, grc_mode);
7975 }
7976
Matt Carlson5093eed2010-11-24 08:31:45 +00007977 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
7978 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
7979 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00007980
Matt Carlson5093eed2010-11-24 08:31:45 +00007981 /* Access the lower 1K of PL PCIE block registers. */
7982 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7983 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00007984
Matt Carlson5093eed2010-11-24 08:31:45 +00007985 val = tr32(TG3_PCIE_TLDLPL_PORT +
7986 TG3_PCIE_PL_LO_PHYCTL5);
7987 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
7988 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00007989
Matt Carlson5093eed2010-11-24 08:31:45 +00007990 tw32(GRC_MODE, grc_mode);
7991 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00007992
Matt Carlson1ff30a52011-05-19 12:12:46 +00007993 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
7994 u32 grc_mode = tr32(GRC_MODE);
7995
7996 /* Access the lower 1K of DL PCIE block registers. */
7997 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7998 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
7999
8000 val = tr32(TG3_PCIE_TLDLPL_PORT +
8001 TG3_PCIE_DL_LO_FTSMAX);
8002 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
8003 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
8004 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
8005
8006 tw32(GRC_MODE, grc_mode);
8007 }
8008
Matt Carlsona977dbe2010-04-12 06:58:26 +00008009 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8010 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8011 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8012 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008013 }
8014
Linus Torvalds1da177e2005-04-16 15:20:36 -07008015 /* This works around an issue with Athlon chipsets on
8016 * B3 tigon3 silicon. This bit has no effect on any
8017 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008018 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008019 */
Joe Perches63c3a662011-04-26 08:12:10 +00008020 if (!tg3_flag(tp, CPMU_PRESENT)) {
8021 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07008022 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8023 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008025
8026 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008027 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008028 val = tr32(TG3PCI_PCISTATE);
8029 val |= PCISTATE_RETRY_SAME_DMA;
8030 tw32(TG3PCI_PCISTATE, val);
8031 }
8032
Joe Perches63c3a662011-04-26 08:12:10 +00008033 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07008034 /* Allow reads and writes to the
8035 * APE register and memory space.
8036 */
8037 val = tr32(TG3PCI_PCISTATE);
8038 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +00008039 PCISTATE_ALLOW_APE_SHMEM_WR |
8040 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008041 tw32(TG3PCI_PCISTATE, val);
8042 }
8043
Linus Torvalds1da177e2005-04-16 15:20:36 -07008044 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8045 /* Enable some hw fixes. */
8046 val = tr32(TG3PCI_MSI_DATA);
8047 val |= (1 << 26) | (1 << 28) | (1 << 29);
8048 tw32(TG3PCI_MSI_DATA, val);
8049 }
8050
8051 /* Descriptor ring init may make accesses to the
8052 * NIC SRAM area to setup the TX descriptors, so we
8053 * can only do this after the hardware has been
8054 * successfully reset.
8055 */
Michael Chan32d8c572006-07-25 16:38:29 -07008056 err = tg3_init_rings(tp);
8057 if (err)
8058 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008059
Joe Perches63c3a662011-04-26 08:12:10 +00008060 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008061 val = tr32(TG3PCI_DMA_RW_CTRL) &
8062 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008063 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8064 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson0aebff42011-04-25 12:42:45 +00008065 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765 &&
8066 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8067 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008068 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8069 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8070 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008071 /* This value is determined during the probe time DMA
8072 * engine test, tg3_test_dma.
8073 */
8074 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008076
8077 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8078 GRC_MODE_4X_NIC_SEND_RINGS |
8079 GRC_MODE_NO_TX_PHDR_CSUM |
8080 GRC_MODE_NO_RX_PHDR_CSUM);
8081 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07008082
8083 /* Pseudo-header checksum is done by hardware logic and not
8084 * the offload processers, so make the chip do the pseudo-
8085 * header checksums on receive. For transmit it is more
8086 * convenient to do the pseudo-header checksum in software
8087 * as Linux does that on transmit for us in all cases.
8088 */
8089 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008090
8091 tw32(GRC_MODE,
8092 tp->grc_mode |
8093 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8094
8095 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8096 val = tr32(GRC_MISC_CFG);
8097 val &= ~0xff;
8098 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8099 tw32(GRC_MISC_CFG, val);
8100
8101 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00008102 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008103 /* Do nothing. */
8104 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8105 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8106 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8107 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8108 else
8109 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8110 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8111 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00008112 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008113 int fw_len;
8114
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008115 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008116 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8117 tw32(BUFMGR_MB_POOL_ADDR,
8118 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8119 tw32(BUFMGR_MB_POOL_SIZE,
8120 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008122
Michael Chan0f893dc2005-07-25 12:30:38 -07008123 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008124 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8125 tp->bufmgr_config.mbuf_read_dma_low_water);
8126 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8127 tp->bufmgr_config.mbuf_mac_rx_low_water);
8128 tw32(BUFMGR_MB_HIGH_WATER,
8129 tp->bufmgr_config.mbuf_high_water);
8130 } else {
8131 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8132 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8133 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8134 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8135 tw32(BUFMGR_MB_HIGH_WATER,
8136 tp->bufmgr_config.mbuf_high_water_jumbo);
8137 }
8138 tw32(BUFMGR_DMA_LOW_WATER,
8139 tp->bufmgr_config.dma_low_water);
8140 tw32(BUFMGR_DMA_HIGH_WATER,
8141 tp->bufmgr_config.dma_high_water);
8142
Matt Carlsond309a462010-09-30 10:34:31 +00008143 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8144 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8145 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00008146 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8147 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
8148 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
8149 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00008150 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008151 for (i = 0; i < 2000; i++) {
8152 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8153 break;
8154 udelay(10);
8155 }
8156 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008157 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008158 return -ENODEV;
8159 }
8160
Matt Carlsoneb07a942011-04-20 07:57:36 +00008161 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8162 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07008163
Matt Carlsoneb07a942011-04-20 07:57:36 +00008164 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008165
8166 /* Initialize TG3_BDINFO's at:
8167 * RCVDBDI_STD_BD: standard eth size rx ring
8168 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8169 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8170 *
8171 * like so:
8172 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8173 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8174 * ring attribute flags
8175 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8176 *
8177 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8178 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8179 *
8180 * The size of each ring is fixed in the firmware, but the location is
8181 * configurable.
8182 */
8183 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008184 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008185 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008186 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00008187 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00008188 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8189 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008190
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008191 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00008192 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008193 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8194 BDINFO_FLAGS_DISABLED);
8195
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008196 /* Program the jumbo buffer descriptor ring control
8197 * blocks on those devices that have them.
8198 */
Matt Carlsonbb18bb92011-03-09 16:58:19 +00008199 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008200 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008201
Joe Perches63c3a662011-04-26 08:12:10 +00008202 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008203 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008204 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008205 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008206 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008207 val = TG3_RX_JMB_RING_SIZE(tp) <<
8208 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008209 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008210 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00008211 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Matt Carlsona50d0792010-06-05 17:24:37 +00008212 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson87668d32009-11-13 13:03:34 +00008213 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8214 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008215 } else {
8216 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8217 BDINFO_FLAGS_DISABLED);
8218 }
8219
Joe Perches63c3a662011-04-26 08:12:10 +00008220 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008221 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsonde9f5232011-04-05 14:22:43 +00008222 val = TG3_RX_STD_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008223 else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008224 val = TG3_RX_STD_MAX_SIZE_5717;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008225 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8226 val |= (TG3_RX_STD_DMA_SZ << 2);
8227 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008228 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008229 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008230 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008231
8232 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008233
Matt Carlson411da642009-11-13 13:03:46 +00008234 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00008235 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008236
Joe Perches63c3a662011-04-26 08:12:10 +00008237 tpr->rx_jmb_prod_idx =
8238 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00008239 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008240
Matt Carlson2d31eca2009-09-01 12:53:31 +00008241 tg3_rings_reset(tp);
8242
Linus Torvalds1da177e2005-04-16 15:20:36 -07008243 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008244 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008245
8246 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008247 tw32(MAC_RX_MTU_SIZE,
8248 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008249
8250 /* The slot time is changed by tg3_setup_phy if we
8251 * run at gigabit with half duplex.
8252 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00008253 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8254 (6 << TX_LENGTHS_IPG_SHIFT) |
8255 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8256
8257 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8258 val |= tr32(MAC_TX_LENGTHS) &
8259 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8260 TX_LENGTHS_CNT_DWN_VAL_MSK);
8261
8262 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008263
8264 /* Receive rules. */
8265 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8266 tw32(RCVLPC_CONFIG, 0x0181);
8267
8268 /* Calculate RDMAC_MODE setting early, we need it to determine
8269 * the RCVLPC_STATE_ENABLE mask.
8270 */
8271 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8272 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8273 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8274 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8275 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008276
Matt Carlsondeabaac2010-11-24 08:31:50 +00008277 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008278 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8279
Matt Carlson57e69832008-05-25 23:48:31 -07008280 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008281 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8282 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008283 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8284 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8285 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8286
Matt Carlsonc5908932011-03-09 16:58:25 +00008287 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8288 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008289 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008290 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008291 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8292 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008293 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008294 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8295 }
8296 }
8297
Joe Perches63c3a662011-04-26 08:12:10 +00008298 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07008299 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8300
Joe Perches63c3a662011-04-26 08:12:10 +00008301 if (tg3_flag(tp, HW_TSO_1) ||
8302 tg3_flag(tp, HW_TSO_2) ||
8303 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08008304 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8305
Matt Carlson108a6c12011-05-19 12:12:47 +00008306 if (tg3_flag(tp, 57765_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00008307 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008308 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8309 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008310
Matt Carlsonf2096f92011-04-05 14:22:48 +00008311 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8312 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8313
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008314 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8315 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8316 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8317 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008318 tg3_flag(tp, 57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008319 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00008320 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8321 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008322 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8323 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8324 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8325 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8326 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8327 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008328 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008329 tw32(TG3_RDMA_RSRVCTRL_REG,
8330 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8331 }
8332
Matt Carlsond78b59f2011-04-05 14:22:46 +00008333 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8334 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00008335 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8336 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8337 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8338 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8339 }
8340
Linus Torvalds1da177e2005-04-16 15:20:36 -07008341 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00008342 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07008343 val = tr32(RCVLPC_STATS_ENABLE);
8344 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8345 tw32(RCVLPC_STATS_ENABLE, val);
8346 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008347 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008348 val = tr32(RCVLPC_STATS_ENABLE);
8349 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8350 tw32(RCVLPC_STATS_ENABLE, val);
8351 } else {
8352 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8353 }
8354 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8355 tw32(SNDDATAI_STATSENAB, 0xffffff);
8356 tw32(SNDDATAI_STATSCTRL,
8357 (SNDDATAI_SCTRL_ENABLE |
8358 SNDDATAI_SCTRL_FASTUPD));
8359
8360 /* Setup host coalescing engine. */
8361 tw32(HOSTCC_MODE, 0);
8362 for (i = 0; i < 2000; i++) {
8363 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8364 break;
8365 udelay(10);
8366 }
8367
Michael Chand244c892005-07-05 14:42:33 -07008368 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008369
Joe Perches63c3a662011-04-26 08:12:10 +00008370 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008371 /* Status/statistics block address. See tg3_timer,
8372 * the tg3_periodic_fetch_stats call there, and
8373 * tg3_get_stats to see how this works for 5705/5750 chips.
8374 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008375 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8376 ((u64) tp->stats_mapping >> 32));
8377 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8378 ((u64) tp->stats_mapping & 0xffffffff));
8379 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008380
Linus Torvalds1da177e2005-04-16 15:20:36 -07008381 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008382
8383 /* Clear statistics and status block memory areas */
8384 for (i = NIC_SRAM_STATS_BLK;
8385 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8386 i += sizeof(u32)) {
8387 tg3_write_mem(tp, i, 0);
8388 udelay(40);
8389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008390 }
8391
8392 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8393
8394 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8395 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008396 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008397 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8398
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008399 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8400 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07008401 /* reset to prevent losing 1st rx packet intermittently */
8402 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8403 udelay(10);
8404 }
8405
Matt Carlson3bda1252008-08-15 14:08:22 -07008406 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlsonb7d4d462011-07-20 10:20:50 +00008407 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
8408 MAC_MODE_FHDE_ENABLE;
8409 if (tg3_flag(tp, ENABLE_APE))
8410 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +00008411 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008412 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008413 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8414 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008415 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8416 udelay(40);
8417
Michael Chan314fba32005-04-21 17:07:04 -07008418 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00008419 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008420 * register to preserve the GPIO settings for LOMs. The GPIOs,
8421 * whether used as inputs or outputs, are set by boot code after
8422 * reset.
8423 */
Joe Perches63c3a662011-04-26 08:12:10 +00008424 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008425 u32 gpio_mask;
8426
Michael Chan9d26e212006-12-07 00:21:14 -08008427 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8428 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8429 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008430
8431 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8432 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8433 GRC_LCLCTRL_GPIO_OUTPUT3;
8434
Michael Chanaf36e6b2006-03-23 01:28:06 -08008435 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8436 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8437
Gary Zambranoaaf84462007-05-05 11:51:45 -07008438 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008439 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8440
8441 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00008442 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08008443 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8444 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008446 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8447 udelay(100);
8448
Joe Perches63c3a662011-04-26 08:12:10 +00008449 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008450 val = tr32(MSGINT_MODE);
8451 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
8452 tw32(MSGINT_MODE, val);
8453 }
8454
Joe Perches63c3a662011-04-26 08:12:10 +00008455 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008456 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8457 udelay(40);
8458 }
8459
8460 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8461 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8462 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8463 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8464 WDMAC_MODE_LNGREAD_ENAB);
8465
Matt Carlsonc5908932011-03-09 16:58:25 +00008466 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8467 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008468 if (tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008469 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8470 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8471 /* nothing */
8472 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008473 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008474 val |= WDMAC_MODE_RX_ACCEL;
8475 }
8476 }
8477
Michael Chand9ab5ad2006-03-20 22:27:35 -08008478 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00008479 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07008480 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -08008481
Matt Carlson788a0352009-11-02 14:26:03 +00008482 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8483 val |= WDMAC_MODE_BURST_ALL_DATA;
8484
Linus Torvalds1da177e2005-04-16 15:20:36 -07008485 tw32_f(WDMAC_MODE, val);
8486 udelay(40);
8487
Joe Perches63c3a662011-04-26 08:12:10 +00008488 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008489 u16 pcix_cmd;
8490
8491 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8492 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008493 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008494 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8495 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008496 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008497 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8498 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008499 }
Matt Carlson9974a352007-10-07 23:27:28 -07008500 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8501 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008502 }
8503
8504 tw32_f(RDMAC_MODE, rdmac_mode);
8505 udelay(40);
8506
8507 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008508 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008509 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008510
8511 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8512 tw32(SNDDATAC_MODE,
8513 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8514 else
8515 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8516
Linus Torvalds1da177e2005-04-16 15:20:36 -07008517 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8518 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008519 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00008520 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008521 val |= RCVDBDI_MODE_LRG_RING_SZ;
8522 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008523 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008524 if (tg3_flag(tp, HW_TSO_1) ||
8525 tg3_flag(tp, HW_TSO_2) ||
8526 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008527 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008528 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008529 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008530 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8531 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008532 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8533
8534 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8535 err = tg3_load_5701_a0_firmware_fix(tp);
8536 if (err)
8537 return err;
8538 }
8539
Joe Perches63c3a662011-04-26 08:12:10 +00008540 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008541 err = tg3_load_tso_firmware(tp);
8542 if (err)
8543 return err;
8544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008545
8546 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008547
Joe Perches63c3a662011-04-26 08:12:10 +00008548 if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsonb1d05212010-06-05 17:24:31 +00008549 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8550 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008551
8552 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8553 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
8554 tp->tx_mode &= ~val;
8555 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
8556 }
8557
Linus Torvalds1da177e2005-04-16 15:20:36 -07008558 tw32_f(MAC_TX_MODE, tp->tx_mode);
8559 udelay(100);
8560
Joe Perches63c3a662011-04-26 08:12:10 +00008561 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008562 u32 reg = MAC_RSS_INDIR_TBL_0;
8563 u8 *ent = (u8 *)&val;
8564
8565 /* Setup the indirection table */
8566 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8567 int idx = i % sizeof(val);
8568
Matt Carlson5efeeea2010-07-11 09:31:40 +00008569 ent[idx] = i % (tp->irq_cnt - 1);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008570 if (idx == sizeof(val) - 1) {
8571 tw32(reg, val);
8572 reg += 4;
8573 }
8574 }
8575
8576 /* Setup the "secret" hash key. */
8577 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8578 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8579 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8580 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8581 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8582 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8583 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8584 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8585 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8586 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8587 }
8588
Linus Torvalds1da177e2005-04-16 15:20:36 -07008589 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008590 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08008591 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8592
Joe Perches63c3a662011-04-26 08:12:10 +00008593 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008594 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8595 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8596 RX_MODE_RSS_IPV6_HASH_EN |
8597 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8598 RX_MODE_RSS_IPV4_HASH_EN |
8599 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8600
Linus Torvalds1da177e2005-04-16 15:20:36 -07008601 tw32_f(MAC_RX_MODE, tp->rx_mode);
8602 udelay(10);
8603
Linus Torvalds1da177e2005-04-16 15:20:36 -07008604 tw32(MAC_LED_CTRL, tp->led_ctrl);
8605
8606 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008607 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008608 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8609 udelay(10);
8610 }
8611 tw32_f(MAC_RX_MODE, tp->rx_mode);
8612 udelay(10);
8613
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008614 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008615 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008616 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008617 /* Set drive transmission level to 1.2V */
8618 /* only if the signal pre-emphasis bit is not set */
8619 val = tr32(MAC_SERDES_CFG);
8620 val &= 0xfffff000;
8621 val |= 0x880;
8622 tw32(MAC_SERDES_CFG, val);
8623 }
8624 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8625 tw32(MAC_SERDES_CFG, 0x616000);
8626 }
8627
8628 /* Prevent chip from dropping frames when flow control
8629 * is enabled.
8630 */
Matt Carlson666bc832010-01-20 16:58:03 +00008631 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8632 val = 1;
8633 else
8634 val = 2;
8635 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008636
8637 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008638 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008639 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00008640 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008641 }
8642
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008643 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +00008644 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08008645 u32 tmp;
8646
8647 tmp = tr32(SERDES_RX_CTRL);
8648 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
8649 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
8650 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
8651 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8652 }
8653
Joe Perches63c3a662011-04-26 08:12:10 +00008654 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson80096062010-08-02 11:26:06 +00008655 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
8656 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07008657 tp->link_config.speed = tp->link_config.orig_speed;
8658 tp->link_config.duplex = tp->link_config.orig_duplex;
8659 tp->link_config.autoneg = tp->link_config.orig_autoneg;
8660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008661
Matt Carlsondd477002008-05-25 23:45:58 -07008662 err = tg3_setup_phy(tp, 0);
8663 if (err)
8664 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008665
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008666 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
8667 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07008668 u32 tmp;
8669
8670 /* Clear CRC stats. */
8671 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
8672 tg3_writephy(tp, MII_TG3_TEST1,
8673 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00008674 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07008675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008676 }
8677 }
8678
8679 __tg3_set_rx_mode(tp->dev);
8680
8681 /* Initialize receive rules. */
8682 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
8683 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
8684 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
8685 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
8686
Joe Perches63c3a662011-04-26 08:12:10 +00008687 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008688 limit = 8;
8689 else
8690 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008691 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008692 limit -= 4;
8693 switch (limit) {
8694 case 16:
8695 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
8696 case 15:
8697 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
8698 case 14:
8699 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
8700 case 13:
8701 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
8702 case 12:
8703 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
8704 case 11:
8705 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
8706 case 10:
8707 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
8708 case 9:
8709 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
8710 case 8:
8711 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
8712 case 7:
8713 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
8714 case 6:
8715 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
8716 case 5:
8717 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
8718 case 4:
8719 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
8720 case 3:
8721 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
8722 case 2:
8723 case 1:
8724
8725 default:
8726 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008728
Joe Perches63c3a662011-04-26 08:12:10 +00008729 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07008730 /* Write our heartbeat update interval to APE. */
8731 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
8732 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07008733
Linus Torvalds1da177e2005-04-16 15:20:36 -07008734 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
8735
Linus Torvalds1da177e2005-04-16 15:20:36 -07008736 return 0;
8737}
8738
8739/* Called at device open time to get the chip ready for
8740 * packet processing. Invoked with tp->lock held.
8741 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008742static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008743{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008744 tg3_switch_clocks(tp);
8745
8746 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
8747
Matt Carlson2f751b62008-08-04 23:17:34 -07008748 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008749}
8750
8751#define TG3_STAT_ADD32(PSTAT, REG) \
8752do { u32 __val = tr32(REG); \
8753 (PSTAT)->low += __val; \
8754 if ((PSTAT)->low < __val) \
8755 (PSTAT)->high += 1; \
8756} while (0)
8757
8758static void tg3_periodic_fetch_stats(struct tg3 *tp)
8759{
8760 struct tg3_hw_stats *sp = tp->hw_stats;
8761
8762 if (!netif_carrier_ok(tp->dev))
8763 return;
8764
8765 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
8766 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
8767 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
8768 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
8769 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
8770 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
8771 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
8772 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
8773 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
8774 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
8775 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
8776 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
8777 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
8778
8779 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
8780 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
8781 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
8782 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
8783 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
8784 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
8785 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
8786 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
8787 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
8788 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
8789 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
8790 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
8791 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
8792 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07008793
8794 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson310050f2011-05-19 12:12:55 +00008795 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
8796 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0 &&
8797 tp->pci_chip_rev_id != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +00008798 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
8799 } else {
8800 u32 val = tr32(HOSTCC_FLOW_ATTN);
8801 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
8802 if (val) {
8803 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
8804 sp->rx_discards.low += val;
8805 if (sp->rx_discards.low < val)
8806 sp->rx_discards.high += 1;
8807 }
8808 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
8809 }
Michael Chan463d3052006-05-22 16:36:27 -07008810 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008811}
8812
8813static void tg3_timer(unsigned long __opaque)
8814{
8815 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008816
Michael Chanf475f162006-03-27 23:20:14 -08008817 if (tp->irq_sync)
8818 goto restart_timer;
8819
David S. Millerf47c11e2005-06-24 20:18:35 -07008820 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008821
Joe Perches63c3a662011-04-26 08:12:10 +00008822 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -07008823 /* All of this garbage is because when using non-tagged
8824 * IRQ status the mailbox/status_block protocol the chip
8825 * uses with the cpu is race prone.
8826 */
Matt Carlson898a56f2009-08-28 14:02:40 +00008827 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07008828 tw32(GRC_LOCAL_CTRL,
8829 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
8830 } else {
8831 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008832 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07008833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008834
David S. Millerfac9b832005-05-18 22:46:34 -07008835 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +00008836 tg3_flag_set(tp, RESTART_TIMER);
David S. Millerf47c11e2005-06-24 20:18:35 -07008837 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07008838 schedule_work(&tp->reset_task);
8839 return;
8840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008841 }
8842
Linus Torvalds1da177e2005-04-16 15:20:36 -07008843 /* This part only runs once per second. */
8844 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00008845 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -07008846 tg3_periodic_fetch_stats(tp);
8847
Matt Carlsonb0c59432011-05-19 12:12:48 +00008848 if (tp->setlpicnt && !--tp->setlpicnt)
8849 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +00008850
Joe Perches63c3a662011-04-26 08:12:10 +00008851 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008852 u32 mac_stat;
8853 int phy_event;
8854
8855 mac_stat = tr32(MAC_STATUS);
8856
8857 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008858 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008859 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
8860 phy_event = 1;
8861 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
8862 phy_event = 1;
8863
8864 if (phy_event)
8865 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +00008866 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008867 u32 mac_stat = tr32(MAC_STATUS);
8868 int need_setup = 0;
8869
8870 if (netif_carrier_ok(tp->dev) &&
8871 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
8872 need_setup = 1;
8873 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00008874 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008875 (mac_stat & (MAC_STATUS_PCS_SYNCED |
8876 MAC_STATUS_SIGNAL_DET))) {
8877 need_setup = 1;
8878 }
8879 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07008880 if (!tp->serdes_counter) {
8881 tw32_f(MAC_MODE,
8882 (tp->mac_mode &
8883 ~MAC_MODE_PORT_MODE_MASK));
8884 udelay(40);
8885 tw32_f(MAC_MODE, tp->mac_mode);
8886 udelay(40);
8887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008888 tg3_setup_phy(tp, 0);
8889 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008890 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008891 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07008892 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00008893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008894
8895 tp->timer_counter = tp->timer_multiplier;
8896 }
8897
Michael Chan130b8e42006-09-27 16:00:40 -07008898 /* Heartbeat is only sent once every 2 seconds.
8899 *
8900 * The heartbeat is to tell the ASF firmware that the host
8901 * driver is still alive. In the event that the OS crashes,
8902 * ASF needs to reset the hardware to free up the FIFO space
8903 * that may be filled with rx packets destined for the host.
8904 * If the FIFO is full, ASF will no longer function properly.
8905 *
8906 * Unintended resets have been reported on real time kernels
8907 * where the timer doesn't run on time. Netpoll will also have
8908 * same problem.
8909 *
8910 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
8911 * to check the ring condition when the heartbeat is expiring
8912 * before doing the reset. This will prevent most unintended
8913 * resets.
8914 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008915 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00008916 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07008917 tg3_wait_for_event_ack(tp);
8918
Michael Chanbbadf502006-04-06 21:46:34 -07008919 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07008920 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07008921 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00008922 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
8923 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008924
8925 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008926 }
8927 tp->asf_counter = tp->asf_multiplier;
8928 }
8929
David S. Millerf47c11e2005-06-24 20:18:35 -07008930 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008931
Michael Chanf475f162006-03-27 23:20:14 -08008932restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07008933 tp->timer.expires = jiffies + tp->timer_offset;
8934 add_timer(&tp->timer);
8935}
8936
Matt Carlson4f125f42009-09-01 12:55:02 +00008937static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08008938{
David Howells7d12e782006-10-05 14:55:46 +01008939 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008940 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00008941 char *name;
8942 struct tg3_napi *tnapi = &tp->napi[irq_num];
8943
8944 if (tp->irq_cnt == 1)
8945 name = tp->dev->name;
8946 else {
8947 name = &tnapi->irq_lbl[0];
8948 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
8949 name[IFNAMSIZ-1] = 0;
8950 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008951
Joe Perches63c3a662011-04-26 08:12:10 +00008952 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08008953 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +00008954 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -08008955 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00008956 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008957 } else {
8958 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +00008959 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -08008960 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00008961 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008962 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008963
8964 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008965}
8966
Michael Chan79381092005-04-21 17:13:59 -07008967static int tg3_test_interrupt(struct tg3 *tp)
8968{
Matt Carlson09943a12009-08-28 14:01:57 +00008969 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07008970 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07008971 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008972 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07008973
Michael Chand4bc3922005-05-29 14:59:20 -07008974 if (!netif_running(dev))
8975 return -ENODEV;
8976
Michael Chan79381092005-04-21 17:13:59 -07008977 tg3_disable_ints(tp);
8978
Matt Carlson4f125f42009-09-01 12:55:02 +00008979 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008980
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008981 /*
8982 * Turn off MSI one shot mode. Otherwise this test has no
8983 * observable way to know whether the interrupt was delivered.
8984 */
Matt Carlson352d0ff2011-07-20 10:20:55 +00008985 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008986 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
8987 tw32(MSGINT_MODE, val);
8988 }
8989
Matt Carlson4f125f42009-09-01 12:55:02 +00008990 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00008991 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008992 if (err)
8993 return err;
8994
Matt Carlson898a56f2009-08-28 14:02:40 +00008995 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07008996 tg3_enable_ints(tp);
8997
8998 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008999 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07009000
9001 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07009002 u32 int_mbox, misc_host_ctrl;
9003
Matt Carlson898a56f2009-08-28 14:02:40 +00009004 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07009005 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
9006
9007 if ((int_mbox != 0) ||
9008 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
9009 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07009010 break;
Michael Chanb16250e2006-09-27 16:10:14 -07009011 }
9012
Matt Carlson352d0ff2011-07-20 10:20:55 +00009013 if (tg3_flag(tp, 57765_PLUS) &&
9014 tnapi->hw_status->status_tag != tnapi->last_tag)
9015 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
9016
Michael Chan79381092005-04-21 17:13:59 -07009017 msleep(10);
9018 }
9019
9020 tg3_disable_ints(tp);
9021
Matt Carlson4f125f42009-09-01 12:55:02 +00009022 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009023
Matt Carlson4f125f42009-09-01 12:55:02 +00009024 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009025
9026 if (err)
9027 return err;
9028
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009029 if (intr_ok) {
9030 /* Reenable MSI one shot mode. */
Matt Carlson352d0ff2011-07-20 10:20:55 +00009031 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009032 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
9033 tw32(MSGINT_MODE, val);
9034 }
Michael Chan79381092005-04-21 17:13:59 -07009035 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009036 }
Michael Chan79381092005-04-21 17:13:59 -07009037
9038 return -EIO;
9039}
9040
9041/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
9042 * successfully restored
9043 */
9044static int tg3_test_msi(struct tg3 *tp)
9045{
Michael Chan79381092005-04-21 17:13:59 -07009046 int err;
9047 u16 pci_cmd;
9048
Joe Perches63c3a662011-04-26 08:12:10 +00009049 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -07009050 return 0;
9051
9052 /* Turn off SERR reporting in case MSI terminates with Master
9053 * Abort.
9054 */
9055 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
9056 pci_write_config_word(tp->pdev, PCI_COMMAND,
9057 pci_cmd & ~PCI_COMMAND_SERR);
9058
9059 err = tg3_test_interrupt(tp);
9060
9061 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
9062
9063 if (!err)
9064 return 0;
9065
9066 /* other failures */
9067 if (err != -EIO)
9068 return err;
9069
9070 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009071 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
9072 "to INTx mode. Please report this failure to the PCI "
9073 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07009074
Matt Carlson4f125f42009-09-01 12:55:02 +00009075 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00009076
Michael Chan79381092005-04-21 17:13:59 -07009077 pci_disable_msi(tp->pdev);
9078
Joe Perches63c3a662011-04-26 08:12:10 +00009079 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +00009080 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07009081
Matt Carlson4f125f42009-09-01 12:55:02 +00009082 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009083 if (err)
9084 return err;
9085
9086 /* Need to reset the chip because the MSI cycle may have terminated
9087 * with Master Abort.
9088 */
David S. Millerf47c11e2005-06-24 20:18:35 -07009089 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009090
Michael Chan944d9802005-05-29 14:57:48 -07009091 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009092 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009093
David S. Millerf47c11e2005-06-24 20:18:35 -07009094 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009095
9096 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00009097 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07009098
9099 return err;
9100}
9101
Matt Carlson9e9fd122009-01-19 16:57:45 -08009102static int tg3_request_firmware(struct tg3 *tp)
9103{
9104 const __be32 *fw_data;
9105
9106 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009107 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
9108 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009109 return -ENOENT;
9110 }
9111
9112 fw_data = (void *)tp->fw->data;
9113
9114 /* Firmware blob starts with version numbers, followed by
9115 * start address and _full_ length including BSS sections
9116 * (which must be longer than the actual data, of course
9117 */
9118
9119 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9120 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009121 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9122 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009123 release_firmware(tp->fw);
9124 tp->fw = NULL;
9125 return -EINVAL;
9126 }
9127
9128 /* We no longer need firmware; we have it. */
9129 tp->fw_needed = NULL;
9130 return 0;
9131}
9132
Matt Carlson679563f2009-09-01 12:55:46 +00009133static bool tg3_enable_msix(struct tg3 *tp)
9134{
9135 int i, rc, cpus = num_online_cpus();
9136 struct msix_entry msix_ent[tp->irq_max];
9137
9138 if (cpus == 1)
9139 /* Just fallback to the simpler MSI mode. */
9140 return false;
9141
9142 /*
9143 * We want as many rx rings enabled as there are cpus.
9144 * The first MSIX vector only deals with link interrupts, etc,
9145 * so we add one to the number of vectors we are requesting.
9146 */
9147 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
9148
9149 for (i = 0; i < tp->irq_max; i++) {
9150 msix_ent[i].entry = i;
9151 msix_ent[i].vector = 0;
9152 }
9153
9154 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009155 if (rc < 0) {
9156 return false;
9157 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009158 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9159 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009160 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9161 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009162 tp->irq_cnt = rc;
9163 }
9164
9165 for (i = 0; i < tp->irq_max; i++)
9166 tp->napi[i].irq_vec = msix_ent[i].vector;
9167
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009168 netif_set_real_num_tx_queues(tp->dev, 1);
9169 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9170 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9171 pci_disable_msix(tp->pdev);
9172 return false;
9173 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009174
9175 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +00009176 tg3_flag_set(tp, ENABLE_RSS);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009177
9178 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9179 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Joe Perches63c3a662011-04-26 08:12:10 +00009180 tg3_flag_set(tp, ENABLE_TSS);
Matt Carlsonb92b9042010-11-24 08:31:51 +00009181 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9182 }
9183 }
Matt Carlson2430b032010-06-05 17:24:34 +00009184
Matt Carlson679563f2009-09-01 12:55:46 +00009185 return true;
9186}
9187
Matt Carlson07b01732009-08-28 14:01:15 +00009188static void tg3_ints_init(struct tg3 *tp)
9189{
Joe Perches63c3a662011-04-26 08:12:10 +00009190 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
9191 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009192 /* All MSI supporting chips should support tagged
9193 * status. Assert that this is the case.
9194 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009195 netdev_warn(tp->dev,
9196 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009197 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009198 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009199
Joe Perches63c3a662011-04-26 08:12:10 +00009200 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
9201 tg3_flag_set(tp, USING_MSIX);
9202 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
9203 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +00009204
Joe Perches63c3a662011-04-26 08:12:10 +00009205 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009206 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +00009207 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009208 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson679563f2009-09-01 12:55:46 +00009209 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9210 }
9211defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +00009212 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009213 tp->irq_cnt = 1;
9214 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009215 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009216 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009217 }
Matt Carlson07b01732009-08-28 14:01:15 +00009218}
9219
9220static void tg3_ints_fini(struct tg3 *tp)
9221{
Joe Perches63c3a662011-04-26 08:12:10 +00009222 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +00009223 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009224 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +00009225 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009226 tg3_flag_clear(tp, USING_MSI);
9227 tg3_flag_clear(tp, USING_MSIX);
9228 tg3_flag_clear(tp, ENABLE_RSS);
9229 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009230}
9231
Linus Torvalds1da177e2005-04-16 15:20:36 -07009232static int tg3_open(struct net_device *dev)
9233{
9234 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009235 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009236
Matt Carlson9e9fd122009-01-19 16:57:45 -08009237 if (tp->fw_needed) {
9238 err = tg3_request_firmware(tp);
9239 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9240 if (err)
9241 return err;
9242 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009243 netdev_warn(tp->dev, "TSO capability disabled\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009244 tg3_flag_clear(tp, TSO_CAPABLE);
9245 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009246 netdev_notice(tp->dev, "TSO capability restored\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009247 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009248 }
9249 }
9250
Michael Chanc49a1562006-12-17 17:07:29 -08009251 netif_carrier_off(tp->dev);
9252
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009253 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -07009254 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009255 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009256
9257 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009258
Linus Torvalds1da177e2005-04-16 15:20:36 -07009259 tg3_disable_ints(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009260 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009261
David S. Millerf47c11e2005-06-24 20:18:35 -07009262 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009263
Matt Carlson679563f2009-09-01 12:55:46 +00009264 /*
9265 * Setup interrupts first so we know how
9266 * many NAPI resources to allocate
9267 */
9268 tg3_ints_init(tp);
9269
Linus Torvalds1da177e2005-04-16 15:20:36 -07009270 /* The placement of this call is tied
9271 * to the setup and use of Host TX descriptors.
9272 */
9273 err = tg3_alloc_consistent(tp);
9274 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009275 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009276
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009277 tg3_napi_init(tp);
9278
Matt Carlsonfed97812009-09-01 13:10:19 +00009279 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07009280
Matt Carlson4f125f42009-09-01 12:55:02 +00009281 for (i = 0; i < tp->irq_cnt; i++) {
9282 struct tg3_napi *tnapi = &tp->napi[i];
9283 err = tg3_request_irq(tp, i);
9284 if (err) {
9285 for (i--; i >= 0; i--)
9286 free_irq(tnapi->irq_vec, tnapi);
9287 break;
9288 }
9289 }
Matt Carlson07b01732009-08-28 14:01:15 +00009290
9291 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009292 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00009293
David S. Millerf47c11e2005-06-24 20:18:35 -07009294 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009295
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009296 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009297 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07009298 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009299 tg3_free_rings(tp);
9300 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00009301 if (tg3_flag(tp, TAGGED_STATUS))
David S. Millerfac9b832005-05-18 22:46:34 -07009302 tp->timer_offset = HZ;
9303 else
9304 tp->timer_offset = HZ / 10;
9305
9306 BUG_ON(tp->timer_offset > HZ);
9307 tp->timer_counter = tp->timer_multiplier =
9308 (HZ / tp->timer_offset);
9309 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07009310 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009311
9312 init_timer(&tp->timer);
9313 tp->timer.expires = jiffies + tp->timer_offset;
9314 tp->timer.data = (unsigned long) tp;
9315 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009316 }
9317
David S. Millerf47c11e2005-06-24 20:18:35 -07009318 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009319
Matt Carlson07b01732009-08-28 14:01:15 +00009320 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009321 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009322
Joe Perches63c3a662011-04-26 08:12:10 +00009323 if (tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -07009324 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07009325
Michael Chan79381092005-04-21 17:13:59 -07009326 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009327 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07009328 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07009329 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07009330 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009331
Matt Carlson679563f2009-09-01 12:55:46 +00009332 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07009333 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009334
Joe Perches63c3a662011-04-26 08:12:10 +00009335 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009336 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009337
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009338 tw32(PCIE_TRANSACTION_CFG,
9339 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009340 }
Michael Chan79381092005-04-21 17:13:59 -07009341 }
9342
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009343 tg3_phy_start(tp);
9344
David S. Millerf47c11e2005-06-24 20:18:35 -07009345 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009346
Michael Chan79381092005-04-21 17:13:59 -07009347 add_timer(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +00009348 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009349 tg3_enable_ints(tp);
9350
David S. Millerf47c11e2005-06-24 20:18:35 -07009351 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009352
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009353 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009354
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00009355 /*
9356 * Reset loopback feature if it was turned on while the device was down
9357 * make sure that it's installed properly now.
9358 */
9359 if (dev->features & NETIF_F_LOOPBACK)
9360 tg3_set_loopback(dev, dev->features);
9361
Linus Torvalds1da177e2005-04-16 15:20:36 -07009362 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00009363
Matt Carlson679563f2009-09-01 12:55:46 +00009364err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00009365 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9366 struct tg3_napi *tnapi = &tp->napi[i];
9367 free_irq(tnapi->irq_vec, tnapi);
9368 }
Matt Carlson07b01732009-08-28 14:01:15 +00009369
Matt Carlson679563f2009-09-01 12:55:46 +00009370err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00009371 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009372 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009373 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00009374
9375err_out1:
9376 tg3_ints_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009377 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009378}
9379
Eric Dumazet511d2222010-07-07 20:44:24 +00009380static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
9381 struct rtnl_link_stats64 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009382static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
9383
9384static int tg3_close(struct net_device *dev)
9385{
Matt Carlson4f125f42009-09-01 12:55:02 +00009386 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009387 struct tg3 *tp = netdev_priv(dev);
9388
Matt Carlsonfed97812009-09-01 13:10:19 +00009389 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07009390 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08009391
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009392 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009393
9394 del_timer_sync(&tp->timer);
9395
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009396 tg3_phy_stop(tp);
9397
David S. Millerf47c11e2005-06-24 20:18:35 -07009398 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009399
9400 tg3_disable_ints(tp);
9401
Michael Chan944d9802005-05-29 14:57:48 -07009402 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009403 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009404 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009405
David S. Millerf47c11e2005-06-24 20:18:35 -07009406 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009407
Matt Carlson4f125f42009-09-01 12:55:02 +00009408 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9409 struct tg3_napi *tnapi = &tp->napi[i];
9410 free_irq(tnapi->irq_vec, tnapi);
9411 }
Matt Carlson07b01732009-08-28 14:01:15 +00009412
9413 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009414
Eric Dumazet511d2222010-07-07 20:44:24 +00009415 tg3_get_stats64(tp->dev, &tp->net_stats_prev);
9416
Linus Torvalds1da177e2005-04-16 15:20:36 -07009417 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9418 sizeof(tp->estats_prev));
9419
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009420 tg3_napi_fini(tp);
9421
Linus Torvalds1da177e2005-04-16 15:20:36 -07009422 tg3_free_consistent(tp);
9423
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009424 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -08009425
9426 netif_carrier_off(tp->dev);
9427
Linus Torvalds1da177e2005-04-16 15:20:36 -07009428 return 0;
9429}
9430
Eric Dumazet511d2222010-07-07 20:44:24 +00009431static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -07009432{
9433 return ((u64)val->high << 32) | ((u64)val->low);
9434}
9435
Eric Dumazet511d2222010-07-07 20:44:24 +00009436static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009437{
9438 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9439
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009440 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009441 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9442 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009443 u32 val;
9444
David S. Millerf47c11e2005-06-24 20:18:35 -07009445 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009446 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9447 tg3_writephy(tp, MII_TG3_TEST1,
9448 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009449 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009450 } else
9451 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009452 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009453
9454 tp->phy_crc_errors += val;
9455
9456 return tp->phy_crc_errors;
9457 }
9458
9459 return get_stat64(&hw_stats->rx_fcs_errors);
9460}
9461
9462#define ESTAT_ADD(member) \
9463 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +00009464 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009465
9466static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9467{
9468 struct tg3_ethtool_stats *estats = &tp->estats;
9469 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9470 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9471
9472 if (!hw_stats)
9473 return old_estats;
9474
9475 ESTAT_ADD(rx_octets);
9476 ESTAT_ADD(rx_fragments);
9477 ESTAT_ADD(rx_ucast_packets);
9478 ESTAT_ADD(rx_mcast_packets);
9479 ESTAT_ADD(rx_bcast_packets);
9480 ESTAT_ADD(rx_fcs_errors);
9481 ESTAT_ADD(rx_align_errors);
9482 ESTAT_ADD(rx_xon_pause_rcvd);
9483 ESTAT_ADD(rx_xoff_pause_rcvd);
9484 ESTAT_ADD(rx_mac_ctrl_rcvd);
9485 ESTAT_ADD(rx_xoff_entered);
9486 ESTAT_ADD(rx_frame_too_long_errors);
9487 ESTAT_ADD(rx_jabbers);
9488 ESTAT_ADD(rx_undersize_packets);
9489 ESTAT_ADD(rx_in_length_errors);
9490 ESTAT_ADD(rx_out_length_errors);
9491 ESTAT_ADD(rx_64_or_less_octet_packets);
9492 ESTAT_ADD(rx_65_to_127_octet_packets);
9493 ESTAT_ADD(rx_128_to_255_octet_packets);
9494 ESTAT_ADD(rx_256_to_511_octet_packets);
9495 ESTAT_ADD(rx_512_to_1023_octet_packets);
9496 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9497 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9498 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9499 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9500 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9501
9502 ESTAT_ADD(tx_octets);
9503 ESTAT_ADD(tx_collisions);
9504 ESTAT_ADD(tx_xon_sent);
9505 ESTAT_ADD(tx_xoff_sent);
9506 ESTAT_ADD(tx_flow_control);
9507 ESTAT_ADD(tx_mac_errors);
9508 ESTAT_ADD(tx_single_collisions);
9509 ESTAT_ADD(tx_mult_collisions);
9510 ESTAT_ADD(tx_deferred);
9511 ESTAT_ADD(tx_excessive_collisions);
9512 ESTAT_ADD(tx_late_collisions);
9513 ESTAT_ADD(tx_collide_2times);
9514 ESTAT_ADD(tx_collide_3times);
9515 ESTAT_ADD(tx_collide_4times);
9516 ESTAT_ADD(tx_collide_5times);
9517 ESTAT_ADD(tx_collide_6times);
9518 ESTAT_ADD(tx_collide_7times);
9519 ESTAT_ADD(tx_collide_8times);
9520 ESTAT_ADD(tx_collide_9times);
9521 ESTAT_ADD(tx_collide_10times);
9522 ESTAT_ADD(tx_collide_11times);
9523 ESTAT_ADD(tx_collide_12times);
9524 ESTAT_ADD(tx_collide_13times);
9525 ESTAT_ADD(tx_collide_14times);
9526 ESTAT_ADD(tx_collide_15times);
9527 ESTAT_ADD(tx_ucast_packets);
9528 ESTAT_ADD(tx_mcast_packets);
9529 ESTAT_ADD(tx_bcast_packets);
9530 ESTAT_ADD(tx_carrier_sense_errors);
9531 ESTAT_ADD(tx_discards);
9532 ESTAT_ADD(tx_errors);
9533
9534 ESTAT_ADD(dma_writeq_full);
9535 ESTAT_ADD(dma_write_prioq_full);
9536 ESTAT_ADD(rxbds_empty);
9537 ESTAT_ADD(rx_discards);
9538 ESTAT_ADD(rx_errors);
9539 ESTAT_ADD(rx_threshold_hit);
9540
9541 ESTAT_ADD(dma_readq_full);
9542 ESTAT_ADD(dma_read_prioq_full);
9543 ESTAT_ADD(tx_comp_queue_full);
9544
9545 ESTAT_ADD(ring_set_send_prod_index);
9546 ESTAT_ADD(ring_status_update);
9547 ESTAT_ADD(nic_irqs);
9548 ESTAT_ADD(nic_avoided_irqs);
9549 ESTAT_ADD(nic_tx_threshold_hit);
9550
Matt Carlson4452d092011-05-19 12:12:51 +00009551 ESTAT_ADD(mbuf_lwm_thresh_hit);
9552
Linus Torvalds1da177e2005-04-16 15:20:36 -07009553 return estats;
9554}
9555
Eric Dumazet511d2222010-07-07 20:44:24 +00009556static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9557 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009558{
9559 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +00009560 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009561 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9562
9563 if (!hw_stats)
9564 return old_stats;
9565
9566 stats->rx_packets = old_stats->rx_packets +
9567 get_stat64(&hw_stats->rx_ucast_packets) +
9568 get_stat64(&hw_stats->rx_mcast_packets) +
9569 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009570
Linus Torvalds1da177e2005-04-16 15:20:36 -07009571 stats->tx_packets = old_stats->tx_packets +
9572 get_stat64(&hw_stats->tx_ucast_packets) +
9573 get_stat64(&hw_stats->tx_mcast_packets) +
9574 get_stat64(&hw_stats->tx_bcast_packets);
9575
9576 stats->rx_bytes = old_stats->rx_bytes +
9577 get_stat64(&hw_stats->rx_octets);
9578 stats->tx_bytes = old_stats->tx_bytes +
9579 get_stat64(&hw_stats->tx_octets);
9580
9581 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009582 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009583 stats->tx_errors = old_stats->tx_errors +
9584 get_stat64(&hw_stats->tx_errors) +
9585 get_stat64(&hw_stats->tx_mac_errors) +
9586 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9587 get_stat64(&hw_stats->tx_discards);
9588
9589 stats->multicast = old_stats->multicast +
9590 get_stat64(&hw_stats->rx_mcast_packets);
9591 stats->collisions = old_stats->collisions +
9592 get_stat64(&hw_stats->tx_collisions);
9593
9594 stats->rx_length_errors = old_stats->rx_length_errors +
9595 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9596 get_stat64(&hw_stats->rx_undersize_packets);
9597
9598 stats->rx_over_errors = old_stats->rx_over_errors +
9599 get_stat64(&hw_stats->rxbds_empty);
9600 stats->rx_frame_errors = old_stats->rx_frame_errors +
9601 get_stat64(&hw_stats->rx_align_errors);
9602 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9603 get_stat64(&hw_stats->tx_discards);
9604 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
9605 get_stat64(&hw_stats->tx_carrier_sense_errors);
9606
9607 stats->rx_crc_errors = old_stats->rx_crc_errors +
9608 calc_crc_errors(tp);
9609
John W. Linville4f63b872005-09-12 14:43:18 -07009610 stats->rx_missed_errors = old_stats->rx_missed_errors +
9611 get_stat64(&hw_stats->rx_discards);
9612
Eric Dumazetb0057c52010-10-10 19:55:52 +00009613 stats->rx_dropped = tp->rx_dropped;
9614
Linus Torvalds1da177e2005-04-16 15:20:36 -07009615 return stats;
9616}
9617
9618static inline u32 calc_crc(unsigned char *buf, int len)
9619{
9620 u32 reg;
9621 u32 tmp;
9622 int j, k;
9623
9624 reg = 0xffffffff;
9625
9626 for (j = 0; j < len; j++) {
9627 reg ^= buf[j];
9628
9629 for (k = 0; k < 8; k++) {
9630 tmp = reg & 0x01;
9631
9632 reg >>= 1;
9633
Matt Carlson859a5882010-04-05 10:19:28 +00009634 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009635 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009636 }
9637 }
9638
9639 return ~reg;
9640}
9641
9642static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9643{
9644 /* accept or reject all multicast frames */
9645 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9646 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9647 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9648 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9649}
9650
9651static void __tg3_set_rx_mode(struct net_device *dev)
9652{
9653 struct tg3 *tp = netdev_priv(dev);
9654 u32 rx_mode;
9655
9656 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9657 RX_MODE_KEEP_VLAN_TAG);
9658
Matt Carlsonbf933c82011-01-25 15:58:49 +00009659#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009660 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9661 * flag clear.
9662 */
Joe Perches63c3a662011-04-26 08:12:10 +00009663 if (!tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009664 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9665#endif
9666
9667 if (dev->flags & IFF_PROMISC) {
9668 /* Promiscuous mode. */
9669 rx_mode |= RX_MODE_PROMISC;
9670 } else if (dev->flags & IFF_ALLMULTI) {
9671 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009672 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00009673 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009674 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009675 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009676 } else {
9677 /* Accept one or more multicast(s). */
Jiri Pirko22bedad2010-04-01 21:22:57 +00009678 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009679 u32 mc_filter[4] = { 0, };
9680 u32 regidx;
9681 u32 bit;
9682 u32 crc;
9683
Jiri Pirko22bedad2010-04-01 21:22:57 +00009684 netdev_for_each_mc_addr(ha, dev) {
9685 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009686 bit = ~crc & 0x7f;
9687 regidx = (bit & 0x60) >> 5;
9688 bit &= 0x1f;
9689 mc_filter[regidx] |= (1 << bit);
9690 }
9691
9692 tw32(MAC_HASH_REG_0, mc_filter[0]);
9693 tw32(MAC_HASH_REG_1, mc_filter[1]);
9694 tw32(MAC_HASH_REG_2, mc_filter[2]);
9695 tw32(MAC_HASH_REG_3, mc_filter[3]);
9696 }
9697
9698 if (rx_mode != tp->rx_mode) {
9699 tp->rx_mode = rx_mode;
9700 tw32_f(MAC_RX_MODE, rx_mode);
9701 udelay(10);
9702 }
9703}
9704
9705static void tg3_set_rx_mode(struct net_device *dev)
9706{
9707 struct tg3 *tp = netdev_priv(dev);
9708
Michael Chane75f7c92006-03-20 21:33:26 -08009709 if (!netif_running(dev))
9710 return;
9711
David S. Millerf47c11e2005-06-24 20:18:35 -07009712 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009713 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -07009714 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009715}
9716
Linus Torvalds1da177e2005-04-16 15:20:36 -07009717static int tg3_get_regs_len(struct net_device *dev)
9718{
Matt Carlson97bd8e42011-04-13 11:05:04 +00009719 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009720}
9721
9722static void tg3_get_regs(struct net_device *dev,
9723 struct ethtool_regs *regs, void *_p)
9724{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009725 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009726
9727 regs->version = 0;
9728
Matt Carlson97bd8e42011-04-13 11:05:04 +00009729 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009730
Matt Carlson80096062010-08-02 11:26:06 +00009731 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009732 return;
9733
David S. Millerf47c11e2005-06-24 20:18:35 -07009734 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009735
Matt Carlson97bd8e42011-04-13 11:05:04 +00009736 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009737
David S. Millerf47c11e2005-06-24 20:18:35 -07009738 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009739}
9740
9741static int tg3_get_eeprom_len(struct net_device *dev)
9742{
9743 struct tg3 *tp = netdev_priv(dev);
9744
9745 return tp->nvram_size;
9746}
9747
Linus Torvalds1da177e2005-04-16 15:20:36 -07009748static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9749{
9750 struct tg3 *tp = netdev_priv(dev);
9751 int ret;
9752 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -08009753 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009754 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009755
Joe Perches63c3a662011-04-26 08:12:10 +00009756 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +00009757 return -EINVAL;
9758
Matt Carlson80096062010-08-02 11:26:06 +00009759 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009760 return -EAGAIN;
9761
Linus Torvalds1da177e2005-04-16 15:20:36 -07009762 offset = eeprom->offset;
9763 len = eeprom->len;
9764 eeprom->len = 0;
9765
9766 eeprom->magic = TG3_EEPROM_MAGIC;
9767
9768 if (offset & 3) {
9769 /* adjustments to start on required 4 byte boundary */
9770 b_offset = offset & 3;
9771 b_count = 4 - b_offset;
9772 if (b_count > len) {
9773 /* i.e. offset=1 len=2 */
9774 b_count = len;
9775 }
Matt Carlsona9dc5292009-02-25 14:25:30 +00009776 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009777 if (ret)
9778 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +00009779 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009780 len -= b_count;
9781 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009782 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009783 }
9784
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009785 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009786 pd = &data[eeprom->len];
9787 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00009788 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009789 if (ret) {
9790 eeprom->len += i;
9791 return ret;
9792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009793 memcpy(pd + i, &val, 4);
9794 }
9795 eeprom->len += i;
9796
9797 if (len & 3) {
9798 /* read last bytes not ending on 4 byte boundary */
9799 pd = &data[eeprom->len];
9800 b_count = len & 3;
9801 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009802 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009803 if (ret)
9804 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009805 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009806 eeprom->len += b_count;
9807 }
9808 return 0;
9809}
9810
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009811static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009812
9813static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9814{
9815 struct tg3 *tp = netdev_priv(dev);
9816 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009817 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009818 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009819 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009820
Matt Carlson80096062010-08-02 11:26:06 +00009821 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009822 return -EAGAIN;
9823
Joe Perches63c3a662011-04-26 08:12:10 +00009824 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +00009825 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009826 return -EINVAL;
9827
9828 offset = eeprom->offset;
9829 len = eeprom->len;
9830
9831 if ((b_offset = (offset & 3))) {
9832 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +00009833 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009834 if (ret)
9835 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009836 len += b_offset;
9837 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -07009838 if (len < 4)
9839 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009840 }
9841
9842 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -07009843 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009844 /* adjustments to end on required 4 byte boundary */
9845 odd_len = 1;
9846 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009847 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009848 if (ret)
9849 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009850 }
9851
9852 buf = data;
9853 if (b_offset || odd_len) {
9854 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009855 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009856 return -ENOMEM;
9857 if (b_offset)
9858 memcpy(buf, &start, 4);
9859 if (odd_len)
9860 memcpy(buf+len-4, &end, 4);
9861 memcpy(buf + b_offset, data, eeprom->len);
9862 }
9863
9864 ret = tg3_nvram_write_block(tp, offset, len, buf);
9865
9866 if (buf != data)
9867 kfree(buf);
9868
9869 return ret;
9870}
9871
9872static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9873{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009874 struct tg3 *tp = netdev_priv(dev);
9875
Joe Perches63c3a662011-04-26 08:12:10 +00009876 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009877 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009878 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009879 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009880 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9881 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009882 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009883
Linus Torvalds1da177e2005-04-16 15:20:36 -07009884 cmd->supported = (SUPPORTED_Autoneg);
9885
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009886 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009887 cmd->supported |= (SUPPORTED_1000baseT_Half |
9888 SUPPORTED_1000baseT_Full);
9889
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009890 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009891 cmd->supported |= (SUPPORTED_100baseT_Half |
9892 SUPPORTED_100baseT_Full |
9893 SUPPORTED_10baseT_Half |
9894 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -08009895 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -07009896 cmd->port = PORT_TP;
9897 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009898 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -07009899 cmd->port = PORT_FIBRE;
9900 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009901
Linus Torvalds1da177e2005-04-16 15:20:36 -07009902 cmd->advertising = tp->link_config.advertising;
9903 if (netif_running(dev)) {
David Decotigny70739492011-04-27 18:32:40 +00009904 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009905 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson64c22182010-10-14 10:37:44 +00009906 } else {
David Decotigny70739492011-04-27 18:32:40 +00009907 ethtool_cmd_speed_set(cmd, SPEED_INVALID);
Matt Carlson64c22182010-10-14 10:37:44 +00009908 cmd->duplex = DUPLEX_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009909 }
Matt Carlson882e9792009-09-01 13:21:36 +00009910 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009911 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009912 cmd->autoneg = tp->link_config.autoneg;
9913 cmd->maxtxpkt = 0;
9914 cmd->maxrxpkt = 0;
9915 return 0;
9916}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009917
Linus Torvalds1da177e2005-04-16 15:20:36 -07009918static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9919{
9920 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +00009921 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009922
Joe Perches63c3a662011-04-26 08:12:10 +00009923 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009924 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009925 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009926 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009927 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9928 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009929 }
9930
Matt Carlson7e5856b2009-02-25 14:23:01 +00009931 if (cmd->autoneg != AUTONEG_ENABLE &&
9932 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -07009933 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009934
9935 if (cmd->autoneg == AUTONEG_DISABLE &&
9936 cmd->duplex != DUPLEX_FULL &&
9937 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -07009938 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009939
Matt Carlson7e5856b2009-02-25 14:23:01 +00009940 if (cmd->autoneg == AUTONEG_ENABLE) {
9941 u32 mask = ADVERTISED_Autoneg |
9942 ADVERTISED_Pause |
9943 ADVERTISED_Asym_Pause;
9944
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009945 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +00009946 mask |= ADVERTISED_1000baseT_Half |
9947 ADVERTISED_1000baseT_Full;
9948
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009949 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +00009950 mask |= ADVERTISED_100baseT_Half |
9951 ADVERTISED_100baseT_Full |
9952 ADVERTISED_10baseT_Half |
9953 ADVERTISED_10baseT_Full |
9954 ADVERTISED_TP;
9955 else
9956 mask |= ADVERTISED_FIBRE;
9957
9958 if (cmd->advertising & ~mask)
9959 return -EINVAL;
9960
9961 mask &= (ADVERTISED_1000baseT_Half |
9962 ADVERTISED_1000baseT_Full |
9963 ADVERTISED_100baseT_Half |
9964 ADVERTISED_100baseT_Full |
9965 ADVERTISED_10baseT_Half |
9966 ADVERTISED_10baseT_Full);
9967
9968 cmd->advertising &= mask;
9969 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009970 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +00009971 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +00009972 return -EINVAL;
9973
9974 if (cmd->duplex != DUPLEX_FULL)
9975 return -EINVAL;
9976 } else {
David Decotigny25db0332011-04-27 18:32:39 +00009977 if (speed != SPEED_100 &&
9978 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +00009979 return -EINVAL;
9980 }
9981 }
9982
David S. Millerf47c11e2005-06-24 20:18:35 -07009983 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009984
9985 tp->link_config.autoneg = cmd->autoneg;
9986 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -07009987 tp->link_config.advertising = (cmd->advertising |
9988 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009989 tp->link_config.speed = SPEED_INVALID;
9990 tp->link_config.duplex = DUPLEX_INVALID;
9991 } else {
9992 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +00009993 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009994 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009995 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009996
Michael Chan24fcad62006-12-17 17:06:46 -08009997 tp->link_config.orig_speed = tp->link_config.speed;
9998 tp->link_config.orig_duplex = tp->link_config.duplex;
9999 tp->link_config.orig_autoneg = tp->link_config.autoneg;
10000
Linus Torvalds1da177e2005-04-16 15:20:36 -070010001 if (netif_running(dev))
10002 tg3_setup_phy(tp, 1);
10003
David S. Millerf47c11e2005-06-24 20:18:35 -070010004 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010005
Linus Torvalds1da177e2005-04-16 15:20:36 -070010006 return 0;
10007}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010008
Linus Torvalds1da177e2005-04-16 15:20:36 -070010009static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10010{
10011 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010012
Linus Torvalds1da177e2005-04-16 15:20:36 -070010013 strcpy(info->driver, DRV_MODULE_NAME);
10014 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -080010015 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010016 strcpy(info->bus_info, pci_name(tp->pdev));
10017}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010018
Linus Torvalds1da177e2005-04-16 15:20:36 -070010019static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10020{
10021 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010022
Joe Perches63c3a662011-04-26 08:12:10 +000010023 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070010024 wol->supported = WAKE_MAGIC;
10025 else
10026 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010027 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010028 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010029 wol->wolopts = WAKE_MAGIC;
10030 memset(&wol->sopass, 0, sizeof(wol->sopass));
10031}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010032
Linus Torvalds1da177e2005-04-16 15:20:36 -070010033static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10034{
10035 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010036 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010037
Linus Torvalds1da177e2005-04-16 15:20:36 -070010038 if (wol->wolopts & ~WAKE_MAGIC)
10039 return -EINVAL;
10040 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010041 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010042 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010043
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010044 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
10045
David S. Millerf47c11e2005-06-24 20:18:35 -070010046 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010047 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000010048 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010049 else
Joe Perches63c3a662011-04-26 08:12:10 +000010050 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070010051 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010052
Linus Torvalds1da177e2005-04-16 15:20:36 -070010053 return 0;
10054}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010055
Linus Torvalds1da177e2005-04-16 15:20:36 -070010056static u32 tg3_get_msglevel(struct net_device *dev)
10057{
10058 struct tg3 *tp = netdev_priv(dev);
10059 return tp->msg_enable;
10060}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010061
Linus Torvalds1da177e2005-04-16 15:20:36 -070010062static void tg3_set_msglevel(struct net_device *dev, u32 value)
10063{
10064 struct tg3 *tp = netdev_priv(dev);
10065 tp->msg_enable = value;
10066}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010067
Linus Torvalds1da177e2005-04-16 15:20:36 -070010068static int tg3_nway_reset(struct net_device *dev)
10069{
10070 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010071 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010072
Linus Torvalds1da177e2005-04-16 15:20:36 -070010073 if (!netif_running(dev))
10074 return -EAGAIN;
10075
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010076 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010077 return -EINVAL;
10078
Joe Perches63c3a662011-04-26 08:12:10 +000010079 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010080 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010081 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010082 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010083 } else {
10084 u32 bmcr;
10085
10086 spin_lock_bh(&tp->lock);
10087 r = -EINVAL;
10088 tg3_readphy(tp, MII_BMCR, &bmcr);
10089 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10090 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010091 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010092 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10093 BMCR_ANENABLE);
10094 r = 0;
10095 }
10096 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010097 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010098
Linus Torvalds1da177e2005-04-16 15:20:36 -070010099 return r;
10100}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010101
Linus Torvalds1da177e2005-04-16 15:20:36 -070010102static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10103{
10104 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010105
Matt Carlson2c49a442010-09-30 10:34:35 +000010106 ering->rx_max_pending = tp->rx_std_ring_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010107 ering->rx_mini_max_pending = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010108 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000010109 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010110 else
10111 ering->rx_jumbo_max_pending = 0;
10112
10113 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010114
10115 ering->rx_pending = tp->rx_pending;
10116 ering->rx_mini_pending = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010117 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080010118 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10119 else
10120 ering->rx_jumbo_pending = 0;
10121
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010122 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010123}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010124
Linus Torvalds1da177e2005-04-16 15:20:36 -070010125static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10126{
10127 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010128 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010129
Matt Carlson2c49a442010-09-30 10:34:35 +000010130 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10131 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010132 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10133 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000010134 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010135 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010136 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010137
Michael Chanbbe832c2005-06-24 20:20:04 -070010138 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010139 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010140 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010141 irq_sync = 1;
10142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010143
Michael Chanbbe832c2005-06-24 20:20:04 -070010144 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010145
Linus Torvalds1da177e2005-04-16 15:20:36 -070010146 tp->rx_pending = ering->rx_pending;
10147
Joe Perches63c3a662011-04-26 08:12:10 +000010148 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010149 tp->rx_pending > 63)
10150 tp->rx_pending = 63;
10151 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010152
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010153 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010154 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010155
10156 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010157 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010158 err = tg3_restart_hw(tp, 1);
10159 if (!err)
10160 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010161 }
10162
David S. Millerf47c11e2005-06-24 20:18:35 -070010163 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010164
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010165 if (irq_sync && !err)
10166 tg3_phy_start(tp);
10167
Michael Chanb9ec6c12006-07-25 16:37:27 -070010168 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010169}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010170
Linus Torvalds1da177e2005-04-16 15:20:36 -070010171static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10172{
10173 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010174
Joe Perches63c3a662011-04-26 08:12:10 +000010175 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080010176
Steve Glendinninge18ce342008-12-16 02:00:00 -080010177 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010178 epause->rx_pause = 1;
10179 else
10180 epause->rx_pause = 0;
10181
Steve Glendinninge18ce342008-12-16 02:00:00 -080010182 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010183 epause->tx_pause = 1;
10184 else
10185 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010186}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010187
Linus Torvalds1da177e2005-04-16 15:20:36 -070010188static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10189{
10190 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010191 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010192
Joe Perches63c3a662011-04-26 08:12:10 +000010193 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000010194 u32 newadv;
10195 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010196
Matt Carlson27121682010-02-17 15:16:57 +000010197 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010198
Matt Carlson27121682010-02-17 15:16:57 +000010199 if (!(phydev->supported & SUPPORTED_Pause) ||
10200 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010201 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010202 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010203
Matt Carlson27121682010-02-17 15:16:57 +000010204 tp->link_config.flowctrl = 0;
10205 if (epause->rx_pause) {
10206 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010207
Matt Carlson27121682010-02-17 15:16:57 +000010208 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010209 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010210 newadv = ADVERTISED_Pause;
10211 } else
10212 newadv = ADVERTISED_Pause |
10213 ADVERTISED_Asym_Pause;
10214 } else if (epause->tx_pause) {
10215 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10216 newadv = ADVERTISED_Asym_Pause;
10217 } else
10218 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010219
Matt Carlson27121682010-02-17 15:16:57 +000010220 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010221 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010222 else
Joe Perches63c3a662011-04-26 08:12:10 +000010223 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010224
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010225 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010226 u32 oldadv = phydev->advertising &
10227 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10228 if (oldadv != newadv) {
10229 phydev->advertising &=
10230 ~(ADVERTISED_Pause |
10231 ADVERTISED_Asym_Pause);
10232 phydev->advertising |= newadv;
10233 if (phydev->autoneg) {
10234 /*
10235 * Always renegotiate the link to
10236 * inform our link partner of our
10237 * flow control settings, even if the
10238 * flow control is forced. Let
10239 * tg3_adjust_link() do the final
10240 * flow control setup.
10241 */
10242 return phy_start_aneg(phydev);
10243 }
10244 }
10245
10246 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010247 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010248 } else {
10249 tp->link_config.orig_advertising &=
10250 ~(ADVERTISED_Pause |
10251 ADVERTISED_Asym_Pause);
10252 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010253 }
10254 } else {
10255 int irq_sync = 0;
10256
10257 if (netif_running(dev)) {
10258 tg3_netif_stop(tp);
10259 irq_sync = 1;
10260 }
10261
10262 tg3_full_lock(tp, irq_sync);
10263
10264 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010265 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010266 else
Joe Perches63c3a662011-04-26 08:12:10 +000010267 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010268 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010269 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010270 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010271 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010272 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010273 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010274 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010275 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010276
10277 if (netif_running(dev)) {
10278 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10279 err = tg3_restart_hw(tp, 1);
10280 if (!err)
10281 tg3_netif_start(tp);
10282 }
10283
10284 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010286
Michael Chanb9ec6c12006-07-25 16:37:27 -070010287 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010288}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010289
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010290static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010291{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010292 switch (sset) {
10293 case ETH_SS_TEST:
10294 return TG3_NUM_TEST;
10295 case ETH_SS_STATS:
10296 return TG3_NUM_STATS;
10297 default:
10298 return -EOPNOTSUPP;
10299 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010300}
10301
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010302static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010303{
10304 switch (stringset) {
10305 case ETH_SS_STATS:
10306 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10307 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010308 case ETH_SS_TEST:
10309 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10310 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010311 default:
10312 WARN_ON(1); /* we need a WARN() */
10313 break;
10314 }
10315}
10316
stephen hemminger81b87092011-04-04 08:43:50 +000010317static int tg3_set_phys_id(struct net_device *dev,
10318 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070010319{
10320 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070010321
10322 if (!netif_running(tp->dev))
10323 return -EAGAIN;
10324
stephen hemminger81b87092011-04-04 08:43:50 +000010325 switch (state) {
10326 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000010327 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070010328
stephen hemminger81b87092011-04-04 08:43:50 +000010329 case ETHTOOL_ID_ON:
10330 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10331 LED_CTRL_1000MBPS_ON |
10332 LED_CTRL_100MBPS_ON |
10333 LED_CTRL_10MBPS_ON |
10334 LED_CTRL_TRAFFIC_OVERRIDE |
10335 LED_CTRL_TRAFFIC_BLINK |
10336 LED_CTRL_TRAFFIC_LED);
10337 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010338
stephen hemminger81b87092011-04-04 08:43:50 +000010339 case ETHTOOL_ID_OFF:
10340 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10341 LED_CTRL_TRAFFIC_OVERRIDE);
10342 break;
Michael Chan4009a932005-09-05 17:52:54 -070010343
stephen hemminger81b87092011-04-04 08:43:50 +000010344 case ETHTOOL_ID_INACTIVE:
10345 tw32(MAC_LED_CTRL, tp->led_ctrl);
10346 break;
Michael Chan4009a932005-09-05 17:52:54 -070010347 }
stephen hemminger81b87092011-04-04 08:43:50 +000010348
Michael Chan4009a932005-09-05 17:52:54 -070010349 return 0;
10350}
10351
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010352static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010353 struct ethtool_stats *estats, u64 *tmp_stats)
10354{
10355 struct tg3 *tp = netdev_priv(dev);
10356 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10357}
10358
Matt Carlsonc3e94502011-04-13 11:05:08 +000010359static __be32 * tg3_vpd_readblock(struct tg3 *tp)
10360{
10361 int i;
10362 __be32 *buf;
10363 u32 offset = 0, len = 0;
10364 u32 magic, val;
10365
Joe Perches63c3a662011-04-26 08:12:10 +000010366 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000010367 return NULL;
10368
10369 if (magic == TG3_EEPROM_MAGIC) {
10370 for (offset = TG3_NVM_DIR_START;
10371 offset < TG3_NVM_DIR_END;
10372 offset += TG3_NVM_DIRENT_SIZE) {
10373 if (tg3_nvram_read(tp, offset, &val))
10374 return NULL;
10375
10376 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
10377 TG3_NVM_DIRTYPE_EXTVPD)
10378 break;
10379 }
10380
10381 if (offset != TG3_NVM_DIR_END) {
10382 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
10383 if (tg3_nvram_read(tp, offset + 4, &offset))
10384 return NULL;
10385
10386 offset = tg3_nvram_logical_addr(tp, offset);
10387 }
10388 }
10389
10390 if (!offset || !len) {
10391 offset = TG3_NVM_VPD_OFF;
10392 len = TG3_NVM_VPD_LEN;
10393 }
10394
10395 buf = kmalloc(len, GFP_KERNEL);
10396 if (buf == NULL)
10397 return NULL;
10398
10399 if (magic == TG3_EEPROM_MAGIC) {
10400 for (i = 0; i < len; i += 4) {
10401 /* The data is in little-endian format in NVRAM.
10402 * Use the big-endian read routines to preserve
10403 * the byte order as it exists in NVRAM.
10404 */
10405 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
10406 goto error;
10407 }
10408 } else {
10409 u8 *ptr;
10410 ssize_t cnt;
10411 unsigned int pos = 0;
10412
10413 ptr = (u8 *)&buf[0];
10414 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
10415 cnt = pci_read_vpd(tp->pdev, pos,
10416 len - pos, ptr);
10417 if (cnt == -ETIMEDOUT || cnt == -EINTR)
10418 cnt = 0;
10419 else if (cnt < 0)
10420 goto error;
10421 }
10422 if (pos != len)
10423 goto error;
10424 }
10425
10426 return buf;
10427
10428error:
10429 kfree(buf);
10430 return NULL;
10431}
10432
Michael Chan566f86a2005-05-29 14:56:58 -070010433#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010434#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10435#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10436#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Michael Chanb16250e2006-09-27 16:10:14 -070010437#define NVRAM_SELFBOOT_HW_SIZE 0x20
10438#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010439
10440static int tg3_test_nvram(struct tg3 *tp)
10441{
Al Virob9fc7dc2007-12-17 22:59:57 -080010442 u32 csum, magic;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010443 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010444 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010445
Joe Perches63c3a662011-04-26 08:12:10 +000010446 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010447 return 0;
10448
Matt Carlsone4f34112009-02-25 14:25:00 +000010449 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010450 return -EIO;
10451
Michael Chan1b277772006-03-20 22:27:48 -080010452 if (magic == TG3_EEPROM_MAGIC)
10453 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010454 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010455 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10456 TG3_EEPROM_SB_FORMAT_1) {
10457 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10458 case TG3_EEPROM_SB_REVISION_0:
10459 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10460 break;
10461 case TG3_EEPROM_SB_REVISION_2:
10462 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10463 break;
10464 case TG3_EEPROM_SB_REVISION_3:
10465 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10466 break;
10467 default:
10468 return 0;
10469 }
10470 } else
Michael Chan1b277772006-03-20 22:27:48 -080010471 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010472 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10473 size = NVRAM_SELFBOOT_HW_SIZE;
10474 else
Michael Chan1b277772006-03-20 22:27:48 -080010475 return -EIO;
10476
10477 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010478 if (buf == NULL)
10479 return -ENOMEM;
10480
Michael Chan1b277772006-03-20 22:27:48 -080010481 err = -EIO;
10482 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010483 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10484 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010485 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010486 }
Michael Chan1b277772006-03-20 22:27:48 -080010487 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010488 goto out;
10489
Michael Chan1b277772006-03-20 22:27:48 -080010490 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010491 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010492 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010493 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010494 u8 *buf8 = (u8 *) buf, csum8 = 0;
10495
Al Virob9fc7dc2007-12-17 22:59:57 -080010496 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010497 TG3_EEPROM_SB_REVISION_2) {
10498 /* For rev 2, the csum doesn't include the MBA. */
10499 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10500 csum8 += buf8[i];
10501 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10502 csum8 += buf8[i];
10503 } else {
10504 for (i = 0; i < size; i++)
10505 csum8 += buf8[i];
10506 }
Michael Chan1b277772006-03-20 22:27:48 -080010507
Adrian Bunkad96b482006-04-05 22:21:04 -070010508 if (csum8 == 0) {
10509 err = 0;
10510 goto out;
10511 }
10512
10513 err = -EIO;
10514 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010515 }
Michael Chan566f86a2005-05-29 14:56:58 -070010516
Al Virob9fc7dc2007-12-17 22:59:57 -080010517 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010518 TG3_EEPROM_MAGIC_HW) {
10519 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010520 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010521 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010522
10523 /* Separate the parity bits and the data bytes. */
10524 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10525 if ((i == 0) || (i == 8)) {
10526 int l;
10527 u8 msk;
10528
10529 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10530 parity[k++] = buf8[i] & msk;
10531 i++;
Matt Carlson859a5882010-04-05 10:19:28 +000010532 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010533 int l;
10534 u8 msk;
10535
10536 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10537 parity[k++] = buf8[i] & msk;
10538 i++;
10539
10540 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10541 parity[k++] = buf8[i] & msk;
10542 i++;
10543 }
10544 data[j++] = buf8[i];
10545 }
10546
10547 err = -EIO;
10548 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10549 u8 hw8 = hweight8(data[i]);
10550
10551 if ((hw8 & 0x1) && parity[i])
10552 goto out;
10553 else if (!(hw8 & 0x1) && !parity[i])
10554 goto out;
10555 }
10556 err = 0;
10557 goto out;
10558 }
10559
Matt Carlson01c3a392011-03-09 16:58:20 +000010560 err = -EIO;
10561
Michael Chan566f86a2005-05-29 14:56:58 -070010562 /* Bootstrap checksum at offset 0x10 */
10563 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000010564 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010565 goto out;
10566
10567 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10568 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000010569 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000010570 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010571
Matt Carlsonc3e94502011-04-13 11:05:08 +000010572 kfree(buf);
10573
10574 buf = tg3_vpd_readblock(tp);
10575 if (!buf)
10576 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000010577
10578 i = pci_vpd_find_tag((u8 *)buf, 0, TG3_NVM_VPD_LEN,
10579 PCI_VPD_LRDT_RO_DATA);
10580 if (i > 0) {
10581 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
10582 if (j < 0)
10583 goto out;
10584
10585 if (i + PCI_VPD_LRDT_TAG_SIZE + j > TG3_NVM_VPD_LEN)
10586 goto out;
10587
10588 i += PCI_VPD_LRDT_TAG_SIZE;
10589 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
10590 PCI_VPD_RO_KEYWORD_CHKSUM);
10591 if (j > 0) {
10592 u8 csum8 = 0;
10593
10594 j += PCI_VPD_INFO_FLD_HDR_SIZE;
10595
10596 for (i = 0; i <= j; i++)
10597 csum8 += ((u8 *)buf)[i];
10598
10599 if (csum8)
10600 goto out;
10601 }
10602 }
10603
Michael Chan566f86a2005-05-29 14:56:58 -070010604 err = 0;
10605
10606out:
10607 kfree(buf);
10608 return err;
10609}
10610
Michael Chanca430072005-05-29 14:57:23 -070010611#define TG3_SERDES_TIMEOUT_SEC 2
10612#define TG3_COPPER_TIMEOUT_SEC 6
10613
10614static int tg3_test_link(struct tg3 *tp)
10615{
10616 int i, max;
10617
10618 if (!netif_running(tp->dev))
10619 return -ENODEV;
10620
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010621 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070010622 max = TG3_SERDES_TIMEOUT_SEC;
10623 else
10624 max = TG3_COPPER_TIMEOUT_SEC;
10625
10626 for (i = 0; i < max; i++) {
10627 if (netif_carrier_ok(tp->dev))
10628 return 0;
10629
10630 if (msleep_interruptible(1000))
10631 break;
10632 }
10633
10634 return -EIO;
10635}
10636
Michael Chana71116d2005-05-29 14:58:11 -070010637/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080010638static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070010639{
Michael Chanb16250e2006-09-27 16:10:14 -070010640 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070010641 u32 offset, read_mask, write_mask, val, save_val, read_val;
10642 static struct {
10643 u16 offset;
10644 u16 flags;
10645#define TG3_FL_5705 0x1
10646#define TG3_FL_NOT_5705 0x2
10647#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070010648#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070010649 u32 read_mask;
10650 u32 write_mask;
10651 } reg_tbl[] = {
10652 /* MAC Control Registers */
10653 { MAC_MODE, TG3_FL_NOT_5705,
10654 0x00000000, 0x00ef6f8c },
10655 { MAC_MODE, TG3_FL_5705,
10656 0x00000000, 0x01ef6b8c },
10657 { MAC_STATUS, TG3_FL_NOT_5705,
10658 0x03800107, 0x00000000 },
10659 { MAC_STATUS, TG3_FL_5705,
10660 0x03800100, 0x00000000 },
10661 { MAC_ADDR_0_HIGH, 0x0000,
10662 0x00000000, 0x0000ffff },
10663 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010664 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070010665 { MAC_RX_MTU_SIZE, 0x0000,
10666 0x00000000, 0x0000ffff },
10667 { MAC_TX_MODE, 0x0000,
10668 0x00000000, 0x00000070 },
10669 { MAC_TX_LENGTHS, 0x0000,
10670 0x00000000, 0x00003fff },
10671 { MAC_RX_MODE, TG3_FL_NOT_5705,
10672 0x00000000, 0x000007fc },
10673 { MAC_RX_MODE, TG3_FL_5705,
10674 0x00000000, 0x000007dc },
10675 { MAC_HASH_REG_0, 0x0000,
10676 0x00000000, 0xffffffff },
10677 { MAC_HASH_REG_1, 0x0000,
10678 0x00000000, 0xffffffff },
10679 { MAC_HASH_REG_2, 0x0000,
10680 0x00000000, 0xffffffff },
10681 { MAC_HASH_REG_3, 0x0000,
10682 0x00000000, 0xffffffff },
10683
10684 /* Receive Data and Receive BD Initiator Control Registers. */
10685 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
10686 0x00000000, 0xffffffff },
10687 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
10688 0x00000000, 0xffffffff },
10689 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
10690 0x00000000, 0x00000003 },
10691 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
10692 0x00000000, 0xffffffff },
10693 { RCVDBDI_STD_BD+0, 0x0000,
10694 0x00000000, 0xffffffff },
10695 { RCVDBDI_STD_BD+4, 0x0000,
10696 0x00000000, 0xffffffff },
10697 { RCVDBDI_STD_BD+8, 0x0000,
10698 0x00000000, 0xffff0002 },
10699 { RCVDBDI_STD_BD+0xc, 0x0000,
10700 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010701
Michael Chana71116d2005-05-29 14:58:11 -070010702 /* Receive BD Initiator Control Registers. */
10703 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
10704 0x00000000, 0xffffffff },
10705 { RCVBDI_STD_THRESH, TG3_FL_5705,
10706 0x00000000, 0x000003ff },
10707 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
10708 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010709
Michael Chana71116d2005-05-29 14:58:11 -070010710 /* Host Coalescing Control Registers. */
10711 { HOSTCC_MODE, TG3_FL_NOT_5705,
10712 0x00000000, 0x00000004 },
10713 { HOSTCC_MODE, TG3_FL_5705,
10714 0x00000000, 0x000000f6 },
10715 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
10716 0x00000000, 0xffffffff },
10717 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
10718 0x00000000, 0x000003ff },
10719 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
10720 0x00000000, 0xffffffff },
10721 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
10722 0x00000000, 0x000003ff },
10723 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
10724 0x00000000, 0xffffffff },
10725 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10726 0x00000000, 0x000000ff },
10727 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
10728 0x00000000, 0xffffffff },
10729 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10730 0x00000000, 0x000000ff },
10731 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
10732 0x00000000, 0xffffffff },
10733 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
10734 0x00000000, 0xffffffff },
10735 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10736 0x00000000, 0xffffffff },
10737 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10738 0x00000000, 0x000000ff },
10739 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10740 0x00000000, 0xffffffff },
10741 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10742 0x00000000, 0x000000ff },
10743 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
10744 0x00000000, 0xffffffff },
10745 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
10746 0x00000000, 0xffffffff },
10747 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
10748 0x00000000, 0xffffffff },
10749 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
10750 0x00000000, 0xffffffff },
10751 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
10752 0x00000000, 0xffffffff },
10753 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
10754 0xffffffff, 0x00000000 },
10755 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
10756 0xffffffff, 0x00000000 },
10757
10758 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070010759 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010760 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070010761 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010762 0x00000000, 0x007fffff },
10763 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
10764 0x00000000, 0x0000003f },
10765 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
10766 0x00000000, 0x000001ff },
10767 { BUFMGR_MB_HIGH_WATER, 0x0000,
10768 0x00000000, 0x000001ff },
10769 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
10770 0xffffffff, 0x00000000 },
10771 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
10772 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010773
Michael Chana71116d2005-05-29 14:58:11 -070010774 /* Mailbox Registers */
10775 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
10776 0x00000000, 0x000001ff },
10777 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
10778 0x00000000, 0x000001ff },
10779 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
10780 0x00000000, 0x000007ff },
10781 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
10782 0x00000000, 0x000001ff },
10783
10784 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
10785 };
10786
Michael Chanb16250e2006-09-27 16:10:14 -070010787 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010788 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070010789 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000010790 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070010791 is_5750 = 1;
10792 }
Michael Chana71116d2005-05-29 14:58:11 -070010793
10794 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
10795 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
10796 continue;
10797
10798 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
10799 continue;
10800
Joe Perches63c3a662011-04-26 08:12:10 +000010801 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070010802 (reg_tbl[i].flags & TG3_FL_NOT_5788))
10803 continue;
10804
Michael Chanb16250e2006-09-27 16:10:14 -070010805 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
10806 continue;
10807
Michael Chana71116d2005-05-29 14:58:11 -070010808 offset = (u32) reg_tbl[i].offset;
10809 read_mask = reg_tbl[i].read_mask;
10810 write_mask = reg_tbl[i].write_mask;
10811
10812 /* Save the original register content */
10813 save_val = tr32(offset);
10814
10815 /* Determine the read-only value. */
10816 read_val = save_val & read_mask;
10817
10818 /* Write zero to the register, then make sure the read-only bits
10819 * are not changed and the read/write bits are all zeros.
10820 */
10821 tw32(offset, 0);
10822
10823 val = tr32(offset);
10824
10825 /* Test the read-only and read/write bits. */
10826 if (((val & read_mask) != read_val) || (val & write_mask))
10827 goto out;
10828
10829 /* Write ones to all the bits defined by RdMask and WrMask, then
10830 * make sure the read-only bits are not changed and the
10831 * read/write bits are all ones.
10832 */
10833 tw32(offset, read_mask | write_mask);
10834
10835 val = tr32(offset);
10836
10837 /* Test the read-only bits. */
10838 if ((val & read_mask) != read_val)
10839 goto out;
10840
10841 /* Test the read/write bits. */
10842 if ((val & write_mask) != write_mask)
10843 goto out;
10844
10845 tw32(offset, save_val);
10846 }
10847
10848 return 0;
10849
10850out:
Michael Chan9f88f292006-12-07 00:22:54 -080010851 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000010852 netdev_err(tp->dev,
10853 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070010854 tw32(offset, save_val);
10855 return -EIO;
10856}
10857
Michael Chan7942e1d2005-05-29 14:58:36 -070010858static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
10859{
Arjan van de Venf71e1302006-03-03 21:33:57 -050010860 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070010861 int i;
10862 u32 j;
10863
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020010864 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070010865 for (j = 0; j < len; j += 4) {
10866 u32 val;
10867
10868 tg3_write_mem(tp, offset + j, test_pattern[i]);
10869 tg3_read_mem(tp, offset + j, &val);
10870 if (val != test_pattern[i])
10871 return -EIO;
10872 }
10873 }
10874 return 0;
10875}
10876
10877static int tg3_test_memory(struct tg3 *tp)
10878{
10879 static struct mem_entry {
10880 u32 offset;
10881 u32 len;
10882 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080010883 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070010884 { 0x00002000, 0x1c000},
10885 { 0xffffffff, 0x00000}
10886 }, mem_tbl_5705[] = {
10887 { 0x00000100, 0x0000c},
10888 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070010889 { 0x00004000, 0x00800},
10890 { 0x00006000, 0x01000},
10891 { 0x00008000, 0x02000},
10892 { 0x00010000, 0x0e000},
10893 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080010894 }, mem_tbl_5755[] = {
10895 { 0x00000200, 0x00008},
10896 { 0x00004000, 0x00800},
10897 { 0x00006000, 0x00800},
10898 { 0x00008000, 0x02000},
10899 { 0x00010000, 0x0c000},
10900 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070010901 }, mem_tbl_5906[] = {
10902 { 0x00000200, 0x00008},
10903 { 0x00004000, 0x00400},
10904 { 0x00006000, 0x00400},
10905 { 0x00008000, 0x01000},
10906 { 0x00010000, 0x01000},
10907 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010908 }, mem_tbl_5717[] = {
10909 { 0x00000200, 0x00008},
10910 { 0x00010000, 0x0a000},
10911 { 0x00020000, 0x13c00},
10912 { 0xffffffff, 0x00000}
10913 }, mem_tbl_57765[] = {
10914 { 0x00000200, 0x00008},
10915 { 0x00004000, 0x00800},
10916 { 0x00006000, 0x09800},
10917 { 0x00010000, 0x0a000},
10918 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070010919 };
10920 struct mem_entry *mem_tbl;
10921 int err = 0;
10922 int i;
10923
Joe Perches63c3a662011-04-26 08:12:10 +000010924 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010925 mem_tbl = mem_tbl_5717;
10926 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
10927 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000010928 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080010929 mem_tbl = mem_tbl_5755;
10930 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
10931 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000010932 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080010933 mem_tbl = mem_tbl_5705;
10934 else
Michael Chan7942e1d2005-05-29 14:58:36 -070010935 mem_tbl = mem_tbl_570x;
10936
10937 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000010938 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
10939 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070010940 break;
10941 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010942
Michael Chan7942e1d2005-05-29 14:58:36 -070010943 return err;
10944}
10945
Michael Chan9f40dea2005-09-05 17:53:06 -070010946#define TG3_MAC_LOOPBACK 0
10947#define TG3_PHY_LOOPBACK 1
Matt Carlsonbb158d62011-04-25 12:42:47 +000010948#define TG3_TSO_LOOPBACK 2
10949
10950#define TG3_TSO_MSS 500
10951
10952#define TG3_TSO_IP_HDR_LEN 20
10953#define TG3_TSO_TCP_HDR_LEN 20
10954#define TG3_TSO_TCP_OPT_LEN 12
10955
10956static const u8 tg3_tso_header[] = {
109570x08, 0x00,
109580x45, 0x00, 0x00, 0x00,
109590x00, 0x00, 0x40, 0x00,
109600x40, 0x06, 0x00, 0x00,
109610x0a, 0x00, 0x00, 0x01,
109620x0a, 0x00, 0x00, 0x02,
109630x0d, 0x00, 0xe0, 0x00,
109640x00, 0x00, 0x01, 0x00,
109650x00, 0x00, 0x02, 0x00,
109660x80, 0x10, 0x10, 0x00,
109670x14, 0x09, 0x00, 0x00,
109680x01, 0x01, 0x08, 0x0a,
109690x11, 0x11, 0x11, 0x11,
109700x11, 0x11, 0x11, 0x11,
10971};
Michael Chan9f40dea2005-09-05 17:53:06 -070010972
Matt Carlson4852a862011-04-13 11:05:07 +000010973static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, int loopback_mode)
Michael Chanc76949a2005-05-29 14:58:59 -070010974{
Michael Chan9f40dea2005-09-05 17:53:06 -070010975 u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000010976 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Michael Chanc76949a2005-05-29 14:58:59 -070010977 struct sk_buff *skb, *rx_skb;
10978 u8 *tx_data;
10979 dma_addr_t map;
10980 int num_pkts, tx_len, rx_len, i, err;
10981 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000010982 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000010983 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070010984
Matt Carlsonc8873402010-02-12 14:47:11 +000010985 tnapi = &tp->napi[0];
10986 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010987 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000010988 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000010989 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000010990 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000010991 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010992 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010993 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000010994
Michael Chan9f40dea2005-09-05 17:53:06 -070010995 if (loopback_mode == TG3_MAC_LOOPBACK) {
Michael Chanc94e3942005-09-27 12:12:42 -070010996 /* HW errata - mac loopback fails in some cases on 5780.
10997 * Normal traffic and PHY loopback are not affected by
Matt Carlsonaba49f22011-01-25 15:58:53 +000010998 * errata. Also, the MAC loopback test is deprecated for
10999 * all newer ASIC revisions.
Michael Chanc94e3942005-09-27 12:12:42 -070011000 */
Matt Carlsonaba49f22011-01-25 15:58:53 +000011001 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000011002 tg3_flag(tp, CPMU_PRESENT))
Michael Chanc94e3942005-09-27 12:12:42 -070011003 return 0;
11004
Matt Carlson49692ca2011-01-25 15:58:52 +000011005 mac_mode = tp->mac_mode &
11006 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
11007 mac_mode |= MAC_MODE_PORT_INT_LPBACK;
Joe Perches63c3a662011-04-26 08:12:10 +000011008 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011009 mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011010 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
Michael Chan3f7045c2006-09-27 16:02:29 -070011011 mac_mode |= MAC_MODE_PORT_MODE_MII;
11012 else
11013 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chan9f40dea2005-09-05 17:53:06 -070011014 tw32(MAC_MODE, mac_mode);
Matt Carlsonbb158d62011-04-25 12:42:47 +000011015 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011016 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +000011017 tg3_phy_fet_toggle_apd(tp, false);
Michael Chan5d64ad32006-12-07 00:19:40 -080011018 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
11019 } else
11020 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
Michael Chan3f7045c2006-09-27 16:02:29 -070011021
Matt Carlson9ef8ca92007-07-11 19:48:29 -070011022 tg3_phy_toggle_automdix(tp, 0);
11023
Michael Chan3f7045c2006-09-27 16:02:29 -070011024 tg3_writephy(tp, MII_BMCR, val);
Michael Chanc94e3942005-09-27 12:12:42 -070011025 udelay(40);
Michael Chan5d64ad32006-12-07 00:19:40 -080011026
Matt Carlson49692ca2011-01-25 15:58:52 +000011027 mac_mode = tp->mac_mode &
11028 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011029 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson1061b7c2010-02-12 14:47:12 +000011030 tg3_writephy(tp, MII_TG3_FET_PTEST,
11031 MII_TG3_FET_PTEST_FRC_TX_LINK |
11032 MII_TG3_FET_PTEST_FRC_TX_LOCK);
11033 /* The write needs to be flushed for the AC131 */
11034 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
11035 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
Michael Chan5d64ad32006-12-07 00:19:40 -080011036 mac_mode |= MAC_MODE_PORT_MODE_MII;
11037 } else
11038 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chanb16250e2006-09-27 16:10:14 -070011039
Michael Chanc94e3942005-09-27 12:12:42 -070011040 /* reset to prevent losing 1st rx packet intermittently */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011041 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Michael Chanc94e3942005-09-27 12:12:42 -070011042 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
11043 udelay(10);
11044 tw32_f(MAC_RX_MODE, tp->rx_mode);
11045 }
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011046 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlson79eb6902010-02-17 15:17:03 +000011047 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
11048 if (masked_phy_id == TG3_PHY_ID_BCM5401)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011049 mac_mode &= ~MAC_MODE_LINK_POLARITY;
Matt Carlson79eb6902010-02-17 15:17:03 +000011050 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011051 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chanff18ff02006-03-27 23:17:27 -080011052 tg3_writephy(tp, MII_TG3_EXT_CTRL,
11053 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
11054 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011055 tw32(MAC_MODE, mac_mode);
Matt Carlson49692ca2011-01-25 15:58:52 +000011056
11057 /* Wait for link */
11058 for (i = 0; i < 100; i++) {
11059 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
11060 break;
11061 mdelay(1);
11062 }
Matt Carlson859a5882010-04-05 10:19:28 +000011063 }
Michael Chanc76949a2005-05-29 14:58:59 -070011064
11065 err = -EIO;
11066
Matt Carlson4852a862011-04-13 11:05:07 +000011067 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070011068 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070011069 if (!skb)
11070 return -ENOMEM;
11071
Michael Chanc76949a2005-05-29 14:58:59 -070011072 tx_data = skb_put(skb, tx_len);
11073 memcpy(tx_data, tp->dev->dev_addr, 6);
11074 memset(tx_data + 6, 0x0, 8);
11075
Matt Carlson4852a862011-04-13 11:05:07 +000011076 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070011077
Matt Carlsonbb158d62011-04-25 12:42:47 +000011078 if (loopback_mode == TG3_TSO_LOOPBACK) {
11079 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
11080
11081 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
11082 TG3_TSO_TCP_OPT_LEN;
11083
11084 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
11085 sizeof(tg3_tso_header));
11086 mss = TG3_TSO_MSS;
11087
11088 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
11089 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
11090
11091 /* Set the total length field in the IP header */
11092 iph->tot_len = htons((u16)(mss + hdr_len));
11093
11094 base_flags = (TXD_FLAG_CPU_PRE_DMA |
11095 TXD_FLAG_CPU_POST_DMA);
11096
Joe Perches63c3a662011-04-26 08:12:10 +000011097 if (tg3_flag(tp, HW_TSO_1) ||
11098 tg3_flag(tp, HW_TSO_2) ||
11099 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011100 struct tcphdr *th;
11101 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
11102 th = (struct tcphdr *)&tx_data[val];
11103 th->check = 0;
11104 } else
11105 base_flags |= TXD_FLAG_TCPUDP_CSUM;
11106
Joe Perches63c3a662011-04-26 08:12:10 +000011107 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011108 mss |= (hdr_len & 0xc) << 12;
11109 if (hdr_len & 0x10)
11110 base_flags |= 0x00000010;
11111 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000011112 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011113 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000011114 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlsonbb158d62011-04-25 12:42:47 +000011115 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
11116 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
11117 } else {
11118 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
11119 }
11120
11121 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
11122 } else {
11123 num_pkts = 1;
11124 data_off = ETH_HLEN;
11125 }
11126
11127 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070011128 tx_data[i] = (u8) (i & 0xff);
11129
Alexander Duyckf4188d82009-12-02 16:48:38 +000011130 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
11131 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000011132 dev_kfree_skb(skb);
11133 return -EIO;
11134 }
Michael Chanc76949a2005-05-29 14:58:59 -070011135
11136 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011137 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011138
11139 udelay(10);
11140
Matt Carlson898a56f2009-08-28 14:02:40 +000011141 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011142
Matt Carlsonbb158d62011-04-25 12:42:47 +000011143 tg3_set_txd(tnapi, tnapi->tx_prod, map, tx_len,
11144 base_flags, (mss << 1) | 1);
Michael Chanc76949a2005-05-29 14:58:59 -070011145
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011146 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011147
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011148 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11149 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011150
11151 udelay(10);
11152
Matt Carlson303fc922009-11-02 14:27:34 +000011153 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11154 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011155 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011156 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011157
11158 udelay(10);
11159
Matt Carlson898a56f2009-08-28 14:02:40 +000011160 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11161 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011162 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011163 (rx_idx == (rx_start_idx + num_pkts)))
11164 break;
11165 }
11166
Alexander Duyckf4188d82009-12-02 16:48:38 +000011167 pci_unmap_single(tp->pdev, map, tx_len, PCI_DMA_TODEVICE);
Michael Chanc76949a2005-05-29 14:58:59 -070011168 dev_kfree_skb(skb);
11169
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011170 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011171 goto out;
11172
11173 if (rx_idx != rx_start_idx + num_pkts)
11174 goto out;
11175
Matt Carlsonbb158d62011-04-25 12:42:47 +000011176 val = data_off;
11177 while (rx_idx != rx_start_idx) {
11178 desc = &rnapi->rx_rcb[rx_start_idx++];
11179 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11180 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070011181
Matt Carlsonbb158d62011-04-25 12:42:47 +000011182 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11183 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000011184 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070011185
Matt Carlsonbb158d62011-04-25 12:42:47 +000011186 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
11187 - ETH_FCS_LEN;
11188
11189 if (loopback_mode != TG3_TSO_LOOPBACK) {
11190 if (rx_len != tx_len)
11191 goto out;
11192
11193 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
11194 if (opaque_key != RXD_OPAQUE_RING_STD)
11195 goto out;
11196 } else {
11197 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
11198 goto out;
11199 }
11200 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
11201 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000011202 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011203 goto out;
11204 }
11205
11206 if (opaque_key == RXD_OPAQUE_RING_STD) {
11207 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
11208 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
11209 mapping);
11210 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
11211 rx_skb = tpr->rx_jmb_buffers[desc_idx].skb;
11212 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
11213 mapping);
11214 } else
Matt Carlson4852a862011-04-13 11:05:07 +000011215 goto out;
11216
Matt Carlsonbb158d62011-04-25 12:42:47 +000011217 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
11218 PCI_DMA_FROMDEVICE);
11219
11220 for (i = data_off; i < rx_len; i++, val++) {
11221 if (*(rx_skb->data + i) != (u8) (val & 0xff))
11222 goto out;
11223 }
Matt Carlson4852a862011-04-13 11:05:07 +000011224 }
11225
Michael Chanc76949a2005-05-29 14:58:59 -070011226 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011227
Michael Chanc76949a2005-05-29 14:58:59 -070011228 /* tg3_free_rings will unmap and free the rx_skb */
11229out:
11230 return err;
11231}
11232
Matt Carlson00c266b2011-04-25 12:42:46 +000011233#define TG3_STD_LOOPBACK_FAILED 1
11234#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000011235#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson00c266b2011-04-25 12:42:46 +000011236
11237#define TG3_MAC_LOOPBACK_SHIFT 0
11238#define TG3_PHY_LOOPBACK_SHIFT 4
Matt Carlsonbb158d62011-04-25 12:42:47 +000011239#define TG3_LOOPBACK_FAILED 0x00000077
Michael Chan9f40dea2005-09-05 17:53:06 -070011240
11241static int tg3_test_loopback(struct tg3 *tp)
11242{
11243 int err = 0;
Matt Carlsonab789042011-01-25 15:58:54 +000011244 u32 eee_cap, cpmuctrl = 0;
Michael Chan9f40dea2005-09-05 17:53:06 -070011245
11246 if (!netif_running(tp->dev))
11247 return TG3_LOOPBACK_FAILED;
11248
Matt Carlsonab789042011-01-25 15:58:54 +000011249 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11250 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11251
Michael Chanb9ec6c12006-07-25 16:37:27 -070011252 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011253 if (err) {
11254 err = TG3_LOOPBACK_FAILED;
11255 goto done;
11256 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011257
Joe Perches63c3a662011-04-26 08:12:10 +000011258 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000011259 int i;
11260
11261 /* Reroute all rx packets to the 1st queue */
11262 for (i = MAC_RSS_INDIR_TBL_0;
11263 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
11264 tw32(i, 0x0);
11265 }
11266
Matt Carlson6833c042008-11-21 17:18:59 -080011267 /* Turn off gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011268 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080011269 tg3_phy_toggle_apd(tp, false);
11270
Joe Perches63c3a662011-04-26 08:12:10 +000011271 if (tg3_flag(tp, CPMU_PRESENT)) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070011272 int i;
11273 u32 status;
11274
11275 tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
11276
11277 /* Wait for up to 40 microseconds to acquire lock. */
11278 for (i = 0; i < 4; i++) {
11279 status = tr32(TG3_CPMU_MUTEX_GNT);
11280 if (status == CPMU_MUTEX_GNT_DRIVER)
11281 break;
11282 udelay(10);
11283 }
11284
Matt Carlsonab789042011-01-25 15:58:54 +000011285 if (status != CPMU_MUTEX_GNT_DRIVER) {
11286 err = TG3_LOOPBACK_FAILED;
11287 goto done;
11288 }
Matt Carlson9936bcf2007-10-10 18:03:07 -070011289
Matt Carlsonb2a5c192008-04-03 21:44:44 -070011290 /* Turn off link-based power management. */
Matt Carlsone8750932007-11-12 21:11:51 -080011291 cpmuctrl = tr32(TG3_CPMU_CTRL);
Matt Carlson109115e2008-05-02 16:48:59 -070011292 tw32(TG3_CPMU_CTRL,
11293 cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
11294 CPMU_CTRL_LINK_AWARE_MODE));
Matt Carlson9936bcf2007-10-10 18:03:07 -070011295 }
11296
Matt Carlson4852a862011-04-13 11:05:07 +000011297 if (tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_MAC_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011298 err |= TG3_STD_LOOPBACK_FAILED << TG3_MAC_LOOPBACK_SHIFT;
Matt Carlson9936bcf2007-10-10 18:03:07 -070011299
Joe Perches63c3a662011-04-26 08:12:10 +000011300 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson4852a862011-04-13 11:05:07 +000011301 tg3_run_loopback(tp, 9000 + ETH_HLEN, TG3_MAC_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011302 err |= TG3_JMB_LOOPBACK_FAILED << TG3_MAC_LOOPBACK_SHIFT;
Matt Carlson4852a862011-04-13 11:05:07 +000011303
Joe Perches63c3a662011-04-26 08:12:10 +000011304 if (tg3_flag(tp, CPMU_PRESENT)) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070011305 tw32(TG3_CPMU_CTRL, cpmuctrl);
11306
11307 /* Release the mutex */
11308 tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
11309 }
11310
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011311 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011312 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson4852a862011-04-13 11:05:07 +000011313 if (tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_PHY_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011314 err |= TG3_STD_LOOPBACK_FAILED <<
11315 TG3_PHY_LOOPBACK_SHIFT;
Joe Perches63c3a662011-04-26 08:12:10 +000011316 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonbb158d62011-04-25 12:42:47 +000011317 tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_TSO_LOOPBACK))
11318 err |= TG3_TSO_LOOPBACK_FAILED <<
11319 TG3_PHY_LOOPBACK_SHIFT;
Joe Perches63c3a662011-04-26 08:12:10 +000011320 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson4852a862011-04-13 11:05:07 +000011321 tg3_run_loopback(tp, 9000 + ETH_HLEN, TG3_PHY_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011322 err |= TG3_JMB_LOOPBACK_FAILED <<
11323 TG3_PHY_LOOPBACK_SHIFT;
Michael Chan9f40dea2005-09-05 17:53:06 -070011324 }
11325
Matt Carlson6833c042008-11-21 17:18:59 -080011326 /* Re-enable gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011327 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080011328 tg3_phy_toggle_apd(tp, true);
11329
Matt Carlsonab789042011-01-25 15:58:54 +000011330done:
11331 tp->phy_flags |= eee_cap;
11332
Michael Chan9f40dea2005-09-05 17:53:06 -070011333 return err;
11334}
11335
Michael Chan4cafd3f2005-05-29 14:56:34 -070011336static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11337 u64 *data)
11338{
Michael Chan566f86a2005-05-29 14:56:58 -070011339 struct tg3 *tp = netdev_priv(dev);
11340
Matt Carlson80096062010-08-02 11:26:06 +000011341 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011342 tg3_power_up(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011343
Michael Chan566f86a2005-05-29 14:56:58 -070011344 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
11345
11346 if (tg3_test_nvram(tp) != 0) {
11347 etest->flags |= ETH_TEST_FL_FAILED;
11348 data[0] = 1;
11349 }
Michael Chanca430072005-05-29 14:57:23 -070011350 if (tg3_test_link(tp) != 0) {
11351 etest->flags |= ETH_TEST_FL_FAILED;
11352 data[1] = 1;
11353 }
Michael Chana71116d2005-05-29 14:58:11 -070011354 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011355 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070011356
Michael Chanbbe832c2005-06-24 20:20:04 -070011357 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011358 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011359 tg3_netif_stop(tp);
11360 irq_sync = 1;
11361 }
11362
11363 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070011364
11365 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080011366 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011367 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000011368 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070011369 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080011370 if (!err)
11371 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011372
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011373 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad2006-03-20 22:27:35 -080011374 tg3_phy_reset(tp);
11375
Michael Chana71116d2005-05-29 14:58:11 -070011376 if (tg3_test_registers(tp) != 0) {
11377 etest->flags |= ETH_TEST_FL_FAILED;
11378 data[2] = 1;
11379 }
Michael Chan7942e1d2005-05-29 14:58:36 -070011380 if (tg3_test_memory(tp) != 0) {
11381 etest->flags |= ETH_TEST_FL_FAILED;
11382 data[3] = 1;
11383 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011384 if ((data[4] = tg3_test_loopback(tp)) != 0)
Michael Chanc76949a2005-05-29 14:58:59 -070011385 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070011386
David S. Millerf47c11e2005-06-24 20:18:35 -070011387 tg3_full_unlock(tp);
11388
Michael Chand4bc3922005-05-29 14:59:20 -070011389 if (tg3_test_interrupt(tp) != 0) {
11390 etest->flags |= ETH_TEST_FL_FAILED;
11391 data[5] = 1;
11392 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011393
11394 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070011395
Michael Chana71116d2005-05-29 14:58:11 -070011396 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11397 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011398 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011399 err2 = tg3_restart_hw(tp, 1);
11400 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070011401 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011402 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011403
11404 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011405
11406 if (irq_sync && !err2)
11407 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011408 }
Matt Carlson80096062010-08-02 11:26:06 +000011409 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011410 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011411
Michael Chan4cafd3f2005-05-29 14:56:34 -070011412}
11413
Linus Torvalds1da177e2005-04-16 15:20:36 -070011414static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
11415{
11416 struct mii_ioctl_data *data = if_mii(ifr);
11417 struct tg3 *tp = netdev_priv(dev);
11418 int err;
11419
Joe Perches63c3a662011-04-26 08:12:10 +000011420 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011421 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011422 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011423 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011424 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000011425 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011426 }
11427
Matt Carlson33f401a2010-04-05 10:19:27 +000011428 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011429 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000011430 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011431
11432 /* fallthru */
11433 case SIOCGMIIREG: {
11434 u32 mii_regval;
11435
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011436 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011437 break; /* We have no PHY */
11438
Matt Carlson34eea5a2011-04-20 07:57:38 +000011439 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011440 return -EAGAIN;
11441
David S. Millerf47c11e2005-06-24 20:18:35 -070011442 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011443 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070011444 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011445
11446 data->val_out = mii_regval;
11447
11448 return err;
11449 }
11450
11451 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011452 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011453 break; /* We have no PHY */
11454
Matt Carlson34eea5a2011-04-20 07:57:38 +000011455 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011456 return -EAGAIN;
11457
David S. Millerf47c11e2005-06-24 20:18:35 -070011458 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011459 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070011460 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011461
11462 return err;
11463
11464 default:
11465 /* do nothing */
11466 break;
11467 }
11468 return -EOPNOTSUPP;
11469}
11470
David S. Miller15f98502005-05-18 22:49:26 -070011471static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11472{
11473 struct tg3 *tp = netdev_priv(dev);
11474
11475 memcpy(ec, &tp->coal, sizeof(*ec));
11476 return 0;
11477}
11478
Michael Chand244c892005-07-05 14:42:33 -070011479static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11480{
11481 struct tg3 *tp = netdev_priv(dev);
11482 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11483 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11484
Joe Perches63c3a662011-04-26 08:12:10 +000011485 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070011486 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11487 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11488 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11489 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11490 }
11491
11492 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11493 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11494 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11495 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11496 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11497 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11498 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11499 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11500 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11501 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11502 return -EINVAL;
11503
11504 /* No rx interrupts will be generated if both are zero */
11505 if ((ec->rx_coalesce_usecs == 0) &&
11506 (ec->rx_max_coalesced_frames == 0))
11507 return -EINVAL;
11508
11509 /* No tx interrupts will be generated if both are zero */
11510 if ((ec->tx_coalesce_usecs == 0) &&
11511 (ec->tx_max_coalesced_frames == 0))
11512 return -EINVAL;
11513
11514 /* Only copy relevant parameters, ignore all others. */
11515 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11516 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11517 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11518 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11519 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11520 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11521 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11522 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11523 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11524
11525 if (netif_running(dev)) {
11526 tg3_full_lock(tp, 0);
11527 __tg3_set_coalesce(tp, &tp->coal);
11528 tg3_full_unlock(tp);
11529 }
11530 return 0;
11531}
11532
Jeff Garzik7282d492006-09-13 14:30:00 -040011533static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011534 .get_settings = tg3_get_settings,
11535 .set_settings = tg3_set_settings,
11536 .get_drvinfo = tg3_get_drvinfo,
11537 .get_regs_len = tg3_get_regs_len,
11538 .get_regs = tg3_get_regs,
11539 .get_wol = tg3_get_wol,
11540 .set_wol = tg3_set_wol,
11541 .get_msglevel = tg3_get_msglevel,
11542 .set_msglevel = tg3_set_msglevel,
11543 .nway_reset = tg3_nway_reset,
11544 .get_link = ethtool_op_get_link,
11545 .get_eeprom_len = tg3_get_eeprom_len,
11546 .get_eeprom = tg3_get_eeprom,
11547 .set_eeprom = tg3_set_eeprom,
11548 .get_ringparam = tg3_get_ringparam,
11549 .set_ringparam = tg3_set_ringparam,
11550 .get_pauseparam = tg3_get_pauseparam,
11551 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011552 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011553 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000011554 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011555 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011556 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011557 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011558 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011559};
11560
11561static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11562{
Michael Chan1b277772006-03-20 22:27:48 -080011563 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011564
11565 tp->nvram_size = EEPROM_CHIP_SIZE;
11566
Matt Carlsone4f34112009-02-25 14:25:00 +000011567 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011568 return;
11569
Michael Chanb16250e2006-09-27 16:10:14 -070011570 if ((magic != TG3_EEPROM_MAGIC) &&
11571 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11572 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011573 return;
11574
11575 /*
11576 * Size the chip by reading offsets at increasing powers of two.
11577 * When we encounter our validation signature, we know the addressing
11578 * has wrapped around, and thus have our chip size.
11579 */
Michael Chan1b277772006-03-20 22:27:48 -080011580 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011581
11582 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011583 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011584 return;
11585
Michael Chan18201802006-03-20 22:29:15 -080011586 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011587 break;
11588
11589 cursize <<= 1;
11590 }
11591
11592 tp->nvram_size = cursize;
11593}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011594
Linus Torvalds1da177e2005-04-16 15:20:36 -070011595static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11596{
11597 u32 val;
11598
Joe Perches63c3a662011-04-26 08:12:10 +000011599 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011600 return;
11601
11602 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011603 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011604 tg3_get_eeprom_size(tp);
11605 return;
11606 }
11607
Matt Carlson6d348f22009-02-25 14:25:52 +000011608 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011609 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011610 /* This is confusing. We want to operate on the
11611 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11612 * call will read from NVRAM and byteswap the data
11613 * according to the byteswapping settings for all
11614 * other register accesses. This ensures the data we
11615 * want will always reside in the lower 16-bits.
11616 * However, the data in NVRAM is in LE format, which
11617 * means the data from the NVRAM read will always be
11618 * opposite the endianness of the CPU. The 16-bit
11619 * byteswap then brings the data to CPU endianness.
11620 */
11621 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011622 return;
11623 }
11624 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070011625 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011626}
11627
11628static void __devinit tg3_get_nvram_info(struct tg3 *tp)
11629{
11630 u32 nvcfg1;
11631
11632 nvcfg1 = tr32(NVRAM_CFG1);
11633 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000011634 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011635 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011636 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11637 tw32(NVRAM_CFG1, nvcfg1);
11638 }
11639
Matt Carlson6ff6f812011-05-19 12:12:54 +000011640 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000011641 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011642 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011643 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
11644 tp->nvram_jedecnum = JEDEC_ATMEL;
11645 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011646 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011647 break;
11648 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
11649 tp->nvram_jedecnum = JEDEC_ATMEL;
11650 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
11651 break;
11652 case FLASH_VENDOR_ATMEL_EEPROM:
11653 tp->nvram_jedecnum = JEDEC_ATMEL;
11654 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011655 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011656 break;
11657 case FLASH_VENDOR_ST:
11658 tp->nvram_jedecnum = JEDEC_ST;
11659 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011660 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011661 break;
11662 case FLASH_VENDOR_SAIFUN:
11663 tp->nvram_jedecnum = JEDEC_SAIFUN;
11664 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
11665 break;
11666 case FLASH_VENDOR_SST_SMALL:
11667 case FLASH_VENDOR_SST_LARGE:
11668 tp->nvram_jedecnum = JEDEC_SST;
11669 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
11670 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011671 }
Matt Carlson8590a602009-08-28 12:29:16 +000011672 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011673 tp->nvram_jedecnum = JEDEC_ATMEL;
11674 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011675 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011676 }
11677}
11678
Matt Carlsona1b950d2009-09-01 13:20:17 +000011679static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
11680{
11681 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
11682 case FLASH_5752PAGE_SIZE_256:
11683 tp->nvram_pagesize = 256;
11684 break;
11685 case FLASH_5752PAGE_SIZE_512:
11686 tp->nvram_pagesize = 512;
11687 break;
11688 case FLASH_5752PAGE_SIZE_1K:
11689 tp->nvram_pagesize = 1024;
11690 break;
11691 case FLASH_5752PAGE_SIZE_2K:
11692 tp->nvram_pagesize = 2048;
11693 break;
11694 case FLASH_5752PAGE_SIZE_4K:
11695 tp->nvram_pagesize = 4096;
11696 break;
11697 case FLASH_5752PAGE_SIZE_264:
11698 tp->nvram_pagesize = 264;
11699 break;
11700 case FLASH_5752PAGE_SIZE_528:
11701 tp->nvram_pagesize = 528;
11702 break;
11703 }
11704}
11705
Michael Chan361b4ac2005-04-21 17:11:21 -070011706static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
11707{
11708 u32 nvcfg1;
11709
11710 nvcfg1 = tr32(NVRAM_CFG1);
11711
Michael Chane6af3012005-04-21 17:12:05 -070011712 /* NVRAM protection for TPM */
11713 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000011714 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070011715
Michael Chan361b4ac2005-04-21 17:11:21 -070011716 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011717 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
11718 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
11719 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011720 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011721 break;
11722 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11723 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011724 tg3_flag_set(tp, NVRAM_BUFFERED);
11725 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011726 break;
11727 case FLASH_5752VENDOR_ST_M45PE10:
11728 case FLASH_5752VENDOR_ST_M45PE20:
11729 case FLASH_5752VENDOR_ST_M45PE40:
11730 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011731 tg3_flag_set(tp, NVRAM_BUFFERED);
11732 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011733 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070011734 }
11735
Joe Perches63c3a662011-04-26 08:12:10 +000011736 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000011737 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000011738 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070011739 /* For eeprom, set pagesize to maximum eeprom size */
11740 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11741
11742 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11743 tw32(NVRAM_CFG1, nvcfg1);
11744 }
11745}
11746
Michael Chand3c7b882006-03-23 01:28:25 -080011747static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
11748{
Matt Carlson989a9d22007-05-05 11:51:05 -070011749 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080011750
11751 nvcfg1 = tr32(NVRAM_CFG1);
11752
11753 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070011754 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011755 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070011756 protect = 1;
11757 }
Michael Chand3c7b882006-03-23 01:28:25 -080011758
Matt Carlson989a9d22007-05-05 11:51:05 -070011759 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11760 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011761 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11762 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11763 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11764 case FLASH_5755VENDOR_ATMEL_FLASH_5:
11765 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011766 tg3_flag_set(tp, NVRAM_BUFFERED);
11767 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011768 tp->nvram_pagesize = 264;
11769 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
11770 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
11771 tp->nvram_size = (protect ? 0x3e200 :
11772 TG3_NVRAM_SIZE_512KB);
11773 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
11774 tp->nvram_size = (protect ? 0x1f200 :
11775 TG3_NVRAM_SIZE_256KB);
11776 else
11777 tp->nvram_size = (protect ? 0x1f200 :
11778 TG3_NVRAM_SIZE_128KB);
11779 break;
11780 case FLASH_5752VENDOR_ST_M45PE10:
11781 case FLASH_5752VENDOR_ST_M45PE20:
11782 case FLASH_5752VENDOR_ST_M45PE40:
11783 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011784 tg3_flag_set(tp, NVRAM_BUFFERED);
11785 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011786 tp->nvram_pagesize = 256;
11787 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
11788 tp->nvram_size = (protect ?
11789 TG3_NVRAM_SIZE_64KB :
11790 TG3_NVRAM_SIZE_128KB);
11791 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
11792 tp->nvram_size = (protect ?
11793 TG3_NVRAM_SIZE_64KB :
11794 TG3_NVRAM_SIZE_256KB);
11795 else
11796 tp->nvram_size = (protect ?
11797 TG3_NVRAM_SIZE_128KB :
11798 TG3_NVRAM_SIZE_512KB);
11799 break;
Michael Chand3c7b882006-03-23 01:28:25 -080011800 }
11801}
11802
Michael Chan1b277772006-03-20 22:27:48 -080011803static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
11804{
11805 u32 nvcfg1;
11806
11807 nvcfg1 = tr32(NVRAM_CFG1);
11808
11809 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011810 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
11811 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11812 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
11813 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11814 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011815 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011816 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080011817
Matt Carlson8590a602009-08-28 12:29:16 +000011818 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11819 tw32(NVRAM_CFG1, nvcfg1);
11820 break;
11821 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11822 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11823 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11824 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11825 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011826 tg3_flag_set(tp, NVRAM_BUFFERED);
11827 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011828 tp->nvram_pagesize = 264;
11829 break;
11830 case FLASH_5752VENDOR_ST_M45PE10:
11831 case FLASH_5752VENDOR_ST_M45PE20:
11832 case FLASH_5752VENDOR_ST_M45PE40:
11833 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011834 tg3_flag_set(tp, NVRAM_BUFFERED);
11835 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011836 tp->nvram_pagesize = 256;
11837 break;
Michael Chan1b277772006-03-20 22:27:48 -080011838 }
11839}
11840
Matt Carlson6b91fa02007-10-10 18:01:09 -070011841static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
11842{
11843 u32 nvcfg1, protect = 0;
11844
11845 nvcfg1 = tr32(NVRAM_CFG1);
11846
11847 /* NVRAM protection for TPM */
11848 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011849 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070011850 protect = 1;
11851 }
11852
11853 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11854 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011855 case FLASH_5761VENDOR_ATMEL_ADB021D:
11856 case FLASH_5761VENDOR_ATMEL_ADB041D:
11857 case FLASH_5761VENDOR_ATMEL_ADB081D:
11858 case FLASH_5761VENDOR_ATMEL_ADB161D:
11859 case FLASH_5761VENDOR_ATMEL_MDB021D:
11860 case FLASH_5761VENDOR_ATMEL_MDB041D:
11861 case FLASH_5761VENDOR_ATMEL_MDB081D:
11862 case FLASH_5761VENDOR_ATMEL_MDB161D:
11863 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011864 tg3_flag_set(tp, NVRAM_BUFFERED);
11865 tg3_flag_set(tp, FLASH);
11866 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000011867 tp->nvram_pagesize = 256;
11868 break;
11869 case FLASH_5761VENDOR_ST_A_M45PE20:
11870 case FLASH_5761VENDOR_ST_A_M45PE40:
11871 case FLASH_5761VENDOR_ST_A_M45PE80:
11872 case FLASH_5761VENDOR_ST_A_M45PE16:
11873 case FLASH_5761VENDOR_ST_M_M45PE20:
11874 case FLASH_5761VENDOR_ST_M_M45PE40:
11875 case FLASH_5761VENDOR_ST_M_M45PE80:
11876 case FLASH_5761VENDOR_ST_M_M45PE16:
11877 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011878 tg3_flag_set(tp, NVRAM_BUFFERED);
11879 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011880 tp->nvram_pagesize = 256;
11881 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011882 }
11883
11884 if (protect) {
11885 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
11886 } else {
11887 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011888 case FLASH_5761VENDOR_ATMEL_ADB161D:
11889 case FLASH_5761VENDOR_ATMEL_MDB161D:
11890 case FLASH_5761VENDOR_ST_A_M45PE16:
11891 case FLASH_5761VENDOR_ST_M_M45PE16:
11892 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
11893 break;
11894 case FLASH_5761VENDOR_ATMEL_ADB081D:
11895 case FLASH_5761VENDOR_ATMEL_MDB081D:
11896 case FLASH_5761VENDOR_ST_A_M45PE80:
11897 case FLASH_5761VENDOR_ST_M_M45PE80:
11898 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
11899 break;
11900 case FLASH_5761VENDOR_ATMEL_ADB041D:
11901 case FLASH_5761VENDOR_ATMEL_MDB041D:
11902 case FLASH_5761VENDOR_ST_A_M45PE40:
11903 case FLASH_5761VENDOR_ST_M_M45PE40:
11904 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11905 break;
11906 case FLASH_5761VENDOR_ATMEL_ADB021D:
11907 case FLASH_5761VENDOR_ATMEL_MDB021D:
11908 case FLASH_5761VENDOR_ST_A_M45PE20:
11909 case FLASH_5761VENDOR_ST_M_M45PE20:
11910 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11911 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011912 }
11913 }
11914}
11915
Michael Chanb5d37722006-09-27 16:06:21 -070011916static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
11917{
11918 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011919 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070011920 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11921}
11922
Matt Carlson321d32a2008-11-21 17:22:19 -080011923static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
11924{
11925 u32 nvcfg1;
11926
11927 nvcfg1 = tr32(NVRAM_CFG1);
11928
11929 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11930 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11931 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11932 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011933 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080011934 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11935
11936 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11937 tw32(NVRAM_CFG1, nvcfg1);
11938 return;
11939 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11940 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11941 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11942 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11943 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11944 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11945 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11946 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011947 tg3_flag_set(tp, NVRAM_BUFFERED);
11948 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080011949
11950 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11951 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11952 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11953 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11954 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11955 break;
11956 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11957 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11958 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11959 break;
11960 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11961 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11962 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11963 break;
11964 }
11965 break;
11966 case FLASH_5752VENDOR_ST_M45PE10:
11967 case FLASH_5752VENDOR_ST_M45PE20:
11968 case FLASH_5752VENDOR_ST_M45PE40:
11969 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011970 tg3_flag_set(tp, NVRAM_BUFFERED);
11971 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080011972
11973 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11974 case FLASH_5752VENDOR_ST_M45PE10:
11975 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11976 break;
11977 case FLASH_5752VENDOR_ST_M45PE20:
11978 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11979 break;
11980 case FLASH_5752VENDOR_ST_M45PE40:
11981 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11982 break;
11983 }
11984 break;
11985 default:
Joe Perches63c3a662011-04-26 08:12:10 +000011986 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080011987 return;
11988 }
11989
Matt Carlsona1b950d2009-09-01 13:20:17 +000011990 tg3_nvram_get_pagesize(tp, nvcfg1);
11991 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000011992 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000011993}
11994
11995
11996static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
11997{
11998 u32 nvcfg1;
11999
12000 nvcfg1 = tr32(NVRAM_CFG1);
12001
12002 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12003 case FLASH_5717VENDOR_ATMEL_EEPROM:
12004 case FLASH_5717VENDOR_MICRO_EEPROM:
12005 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012006 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012007 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12008
12009 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12010 tw32(NVRAM_CFG1, nvcfg1);
12011 return;
12012 case FLASH_5717VENDOR_ATMEL_MDB011D:
12013 case FLASH_5717VENDOR_ATMEL_ADB011B:
12014 case FLASH_5717VENDOR_ATMEL_ADB011D:
12015 case FLASH_5717VENDOR_ATMEL_MDB021D:
12016 case FLASH_5717VENDOR_ATMEL_ADB021B:
12017 case FLASH_5717VENDOR_ATMEL_ADB021D:
12018 case FLASH_5717VENDOR_ATMEL_45USPT:
12019 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012020 tg3_flag_set(tp, NVRAM_BUFFERED);
12021 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012022
12023 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12024 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012025 /* Detect size with tg3_nvram_get_size() */
12026 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012027 case FLASH_5717VENDOR_ATMEL_ADB021B:
12028 case FLASH_5717VENDOR_ATMEL_ADB021D:
12029 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12030 break;
12031 default:
12032 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12033 break;
12034 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012035 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012036 case FLASH_5717VENDOR_ST_M_M25PE10:
12037 case FLASH_5717VENDOR_ST_A_M25PE10:
12038 case FLASH_5717VENDOR_ST_M_M45PE10:
12039 case FLASH_5717VENDOR_ST_A_M45PE10:
12040 case FLASH_5717VENDOR_ST_M_M25PE20:
12041 case FLASH_5717VENDOR_ST_A_M25PE20:
12042 case FLASH_5717VENDOR_ST_M_M45PE20:
12043 case FLASH_5717VENDOR_ST_A_M45PE20:
12044 case FLASH_5717VENDOR_ST_25USPT:
12045 case FLASH_5717VENDOR_ST_45USPT:
12046 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012047 tg3_flag_set(tp, NVRAM_BUFFERED);
12048 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012049
12050 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12051 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012052 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012053 /* Detect size with tg3_nvram_get_size() */
12054 break;
12055 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012056 case FLASH_5717VENDOR_ST_A_M45PE20:
12057 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12058 break;
12059 default:
12060 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12061 break;
12062 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012063 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012064 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012065 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012066 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080012067 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000012068
12069 tg3_nvram_get_pagesize(tp, nvcfg1);
12070 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012071 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080012072}
12073
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012074static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
12075{
12076 u32 nvcfg1, nvmpinstrp;
12077
12078 nvcfg1 = tr32(NVRAM_CFG1);
12079 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
12080
12081 switch (nvmpinstrp) {
12082 case FLASH_5720_EEPROM_HD:
12083 case FLASH_5720_EEPROM_LD:
12084 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012085 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012086
12087 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12088 tw32(NVRAM_CFG1, nvcfg1);
12089 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
12090 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12091 else
12092 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
12093 return;
12094 case FLASH_5720VENDOR_M_ATMEL_DB011D:
12095 case FLASH_5720VENDOR_A_ATMEL_DB011B:
12096 case FLASH_5720VENDOR_A_ATMEL_DB011D:
12097 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12098 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12099 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12100 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12101 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12102 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12103 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12104 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12105 case FLASH_5720VENDOR_ATMEL_45USPT:
12106 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012107 tg3_flag_set(tp, NVRAM_BUFFERED);
12108 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012109
12110 switch (nvmpinstrp) {
12111 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12112 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12113 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12114 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12115 break;
12116 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12117 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12118 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12119 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12120 break;
12121 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12122 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12123 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12124 break;
12125 default:
12126 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12127 break;
12128 }
12129 break;
12130 case FLASH_5720VENDOR_M_ST_M25PE10:
12131 case FLASH_5720VENDOR_M_ST_M45PE10:
12132 case FLASH_5720VENDOR_A_ST_M25PE10:
12133 case FLASH_5720VENDOR_A_ST_M45PE10:
12134 case FLASH_5720VENDOR_M_ST_M25PE20:
12135 case FLASH_5720VENDOR_M_ST_M45PE20:
12136 case FLASH_5720VENDOR_A_ST_M25PE20:
12137 case FLASH_5720VENDOR_A_ST_M45PE20:
12138 case FLASH_5720VENDOR_M_ST_M25PE40:
12139 case FLASH_5720VENDOR_M_ST_M45PE40:
12140 case FLASH_5720VENDOR_A_ST_M25PE40:
12141 case FLASH_5720VENDOR_A_ST_M45PE40:
12142 case FLASH_5720VENDOR_M_ST_M25PE80:
12143 case FLASH_5720VENDOR_M_ST_M45PE80:
12144 case FLASH_5720VENDOR_A_ST_M25PE80:
12145 case FLASH_5720VENDOR_A_ST_M45PE80:
12146 case FLASH_5720VENDOR_ST_25USPT:
12147 case FLASH_5720VENDOR_ST_45USPT:
12148 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012149 tg3_flag_set(tp, NVRAM_BUFFERED);
12150 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012151
12152 switch (nvmpinstrp) {
12153 case FLASH_5720VENDOR_M_ST_M25PE20:
12154 case FLASH_5720VENDOR_M_ST_M45PE20:
12155 case FLASH_5720VENDOR_A_ST_M25PE20:
12156 case FLASH_5720VENDOR_A_ST_M45PE20:
12157 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12158 break;
12159 case FLASH_5720VENDOR_M_ST_M25PE40:
12160 case FLASH_5720VENDOR_M_ST_M45PE40:
12161 case FLASH_5720VENDOR_A_ST_M25PE40:
12162 case FLASH_5720VENDOR_A_ST_M45PE40:
12163 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12164 break;
12165 case FLASH_5720VENDOR_M_ST_M25PE80:
12166 case FLASH_5720VENDOR_M_ST_M45PE80:
12167 case FLASH_5720VENDOR_A_ST_M25PE80:
12168 case FLASH_5720VENDOR_A_ST_M45PE80:
12169 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12170 break;
12171 default:
12172 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12173 break;
12174 }
12175 break;
12176 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012177 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012178 return;
12179 }
12180
12181 tg3_nvram_get_pagesize(tp, nvcfg1);
12182 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012183 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012184}
12185
Linus Torvalds1da177e2005-04-16 15:20:36 -070012186/* Chips other than 5700/5701 use the NVRAM for fetching info. */
12187static void __devinit tg3_nvram_init(struct tg3 *tp)
12188{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012189 tw32_f(GRC_EEPROM_ADDR,
12190 (EEPROM_ADDR_FSM_RESET |
12191 (EEPROM_DEFAULT_CLOCK_PERIOD <<
12192 EEPROM_ADDR_CLKPERD_SHIFT)));
12193
Michael Chan9d57f012006-12-07 00:23:25 -080012194 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012195
12196 /* Enable seeprom accesses. */
12197 tw32_f(GRC_LOCAL_CTRL,
12198 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
12199 udelay(100);
12200
12201 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12202 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000012203 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012204
Michael Chanec41c7d2006-01-17 02:40:55 -080012205 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000012206 netdev_warn(tp->dev,
12207 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000012208 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080012209 return;
12210 }
Michael Chane6af3012005-04-21 17:12:05 -070012211 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012212
Matt Carlson989a9d22007-05-05 11:51:05 -070012213 tp->nvram_size = 0;
12214
Michael Chan361b4ac2005-04-21 17:11:21 -070012215 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12216 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080012217 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
12218 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070012219 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012220 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12221 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080012222 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012223 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
12224 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070012225 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12226 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000012227 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
12228 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080012229 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012230 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12231 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000012232 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012233 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
12234 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070012235 else
12236 tg3_get_nvram_info(tp);
12237
Matt Carlson989a9d22007-05-05 11:51:05 -070012238 if (tp->nvram_size == 0)
12239 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012240
Michael Chane6af3012005-04-21 17:12:05 -070012241 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080012242 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012243
12244 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000012245 tg3_flag_clear(tp, NVRAM);
12246 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012247
12248 tg3_get_eeprom_size(tp);
12249 }
12250}
12251
Linus Torvalds1da177e2005-04-16 15:20:36 -070012252static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
12253 u32 offset, u32 len, u8 *buf)
12254{
12255 int i, j, rc = 0;
12256 u32 val;
12257
12258 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012259 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012260 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012261
12262 addr = offset + i;
12263
12264 memcpy(&data, buf + i, 4);
12265
Matt Carlson62cedd12009-04-20 14:52:29 -070012266 /*
12267 * The SEEPROM interface expects the data to always be opposite
12268 * the native endian format. We accomplish this by reversing
12269 * all the operations that would have been performed on the
12270 * data from a call to tg3_nvram_read_be32().
12271 */
12272 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012273
12274 val = tr32(GRC_EEPROM_ADDR);
12275 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
12276
12277 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
12278 EEPROM_ADDR_READ);
12279 tw32(GRC_EEPROM_ADDR, val |
12280 (0 << EEPROM_ADDR_DEVID_SHIFT) |
12281 (addr & EEPROM_ADDR_ADDR_MASK) |
12282 EEPROM_ADDR_START |
12283 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012284
Michael Chan9d57f012006-12-07 00:23:25 -080012285 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012286 val = tr32(GRC_EEPROM_ADDR);
12287
12288 if (val & EEPROM_ADDR_COMPLETE)
12289 break;
Michael Chan9d57f012006-12-07 00:23:25 -080012290 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012291 }
12292 if (!(val & EEPROM_ADDR_COMPLETE)) {
12293 rc = -EBUSY;
12294 break;
12295 }
12296 }
12297
12298 return rc;
12299}
12300
12301/* offset and length are dword aligned */
12302static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
12303 u8 *buf)
12304{
12305 int ret = 0;
12306 u32 pagesize = tp->nvram_pagesize;
12307 u32 pagemask = pagesize - 1;
12308 u32 nvram_cmd;
12309 u8 *tmp;
12310
12311 tmp = kmalloc(pagesize, GFP_KERNEL);
12312 if (tmp == NULL)
12313 return -ENOMEM;
12314
12315 while (len) {
12316 int j;
Michael Chane6af3012005-04-21 17:12:05 -070012317 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012318
12319 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012320
Linus Torvalds1da177e2005-04-16 15:20:36 -070012321 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012322 ret = tg3_nvram_read_be32(tp, phy_addr + j,
12323 (__be32 *) (tmp + j));
12324 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012325 break;
12326 }
12327 if (ret)
12328 break;
12329
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012330 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012331 size = pagesize;
12332 if (len < size)
12333 size = len;
12334
12335 len -= size;
12336
12337 memcpy(tmp + page_off, buf, size);
12338
12339 offset = offset + (pagesize - page_off);
12340
Michael Chane6af3012005-04-21 17:12:05 -070012341 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012342
12343 /*
12344 * Before we can erase the flash page, we need
12345 * to issue a special "write enable" command.
12346 */
12347 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12348
12349 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12350 break;
12351
12352 /* Erase the target page */
12353 tw32(NVRAM_ADDR, phy_addr);
12354
12355 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
12356 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
12357
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012358 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012359 break;
12360
12361 /* Issue another write enable to start the write. */
12362 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12363
12364 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12365 break;
12366
12367 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012368 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012369
Al Virob9fc7dc2007-12-17 22:59:57 -080012370 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000012371
Al Virob9fc7dc2007-12-17 22:59:57 -080012372 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012373
12374 tw32(NVRAM_ADDR, phy_addr + j);
12375
12376 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
12377 NVRAM_CMD_WR;
12378
12379 if (j == 0)
12380 nvram_cmd |= NVRAM_CMD_FIRST;
12381 else if (j == (pagesize - 4))
12382 nvram_cmd |= NVRAM_CMD_LAST;
12383
12384 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12385 break;
12386 }
12387 if (ret)
12388 break;
12389 }
12390
12391 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12392 tg3_nvram_exec_cmd(tp, nvram_cmd);
12393
12394 kfree(tmp);
12395
12396 return ret;
12397}
12398
12399/* offset and length are dword aligned */
12400static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
12401 u8 *buf)
12402{
12403 int i, ret = 0;
12404
12405 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012406 u32 page_off, phy_addr, nvram_cmd;
12407 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012408
12409 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080012410 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012411
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012412 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012413
Michael Chan18201802006-03-20 22:29:15 -080012414 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012415
12416 tw32(NVRAM_ADDR, phy_addr);
12417
12418 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
12419
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012420 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012421 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070012422 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012423 nvram_cmd |= NVRAM_CMD_LAST;
12424
12425 if (i == (len - 4))
12426 nvram_cmd |= NVRAM_CMD_LAST;
12427
Matt Carlson321d32a2008-11-21 17:22:19 -080012428 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
Joe Perches63c3a662011-04-26 08:12:10 +000012429 !tg3_flag(tp, 5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070012430 (tp->nvram_jedecnum == JEDEC_ST) &&
12431 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012432
12433 if ((ret = tg3_nvram_exec_cmd(tp,
12434 NVRAM_CMD_WREN | NVRAM_CMD_GO |
12435 NVRAM_CMD_DONE)))
12436
12437 break;
12438 }
Joe Perches63c3a662011-04-26 08:12:10 +000012439 if (!tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012440 /* We always do complete word writes to eeprom. */
12441 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
12442 }
12443
12444 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12445 break;
12446 }
12447 return ret;
12448}
12449
12450/* offset and length are dword aligned */
12451static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
12452{
12453 int ret;
12454
Joe Perches63c3a662011-04-26 08:12:10 +000012455 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012456 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
12457 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012458 udelay(40);
12459 }
12460
Joe Perches63c3a662011-04-26 08:12:10 +000012461 if (!tg3_flag(tp, NVRAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012462 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a5882010-04-05 10:19:28 +000012463 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012464 u32 grc_mode;
12465
Michael Chanec41c7d2006-01-17 02:40:55 -080012466 ret = tg3_nvram_lock(tp);
12467 if (ret)
12468 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012469
Michael Chane6af3012005-04-21 17:12:05 -070012470 tg3_enable_nvram_access(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000012471 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012472 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012473
12474 grc_mode = tr32(GRC_MODE);
12475 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
12476
Joe Perches63c3a662011-04-26 08:12:10 +000012477 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012478 ret = tg3_nvram_write_block_buffered(tp, offset, len,
12479 buf);
Matt Carlson859a5882010-04-05 10:19:28 +000012480 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012481 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
12482 buf);
12483 }
12484
12485 grc_mode = tr32(GRC_MODE);
12486 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
12487
Michael Chane6af3012005-04-21 17:12:05 -070012488 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012489 tg3_nvram_unlock(tp);
12490 }
12491
Joe Perches63c3a662011-04-26 08:12:10 +000012492 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012493 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012494 udelay(40);
12495 }
12496
12497 return ret;
12498}
12499
12500struct subsys_tbl_ent {
12501 u16 subsys_vendor, subsys_devid;
12502 u32 phy_id;
12503};
12504
Matt Carlson24daf2b2010-02-17 15:17:02 +000012505static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012506 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012507 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012508 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012509 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012510 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012511 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012512 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012513 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12514 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
12515 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012516 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012517 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012518 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012519 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12520 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
12521 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012522 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012523 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012524 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012525 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012526 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012527 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012528 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012529
12530 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012531 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012532 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012533 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012534 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012535 { TG3PCI_SUBVENDOR_ID_3COM,
12536 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
12537 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012538 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012539 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012540 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012541
12542 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012543 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012544 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012545 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012546 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012547 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012548 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012549 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012550 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012551
12552 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012553 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012554 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012555 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012556 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012557 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12558 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
12559 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012560 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012561 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012562 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012563
12564 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012565 { TG3PCI_SUBVENDOR_ID_IBM,
12566 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012567};
12568
Matt Carlson24daf2b2010-02-17 15:17:02 +000012569static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012570{
12571 int i;
12572
12573 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
12574 if ((subsys_id_to_phy_id[i].subsys_vendor ==
12575 tp->pdev->subsystem_vendor) &&
12576 (subsys_id_to_phy_id[i].subsys_devid ==
12577 tp->pdev->subsystem_device))
12578 return &subsys_id_to_phy_id[i];
12579 }
12580 return NULL;
12581}
12582
Michael Chan7d0c41e2005-04-21 17:06:20 -070012583static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012584{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012585 u32 val;
Michael Chancaf636c72006-03-22 01:05:31 -080012586 u16 pmcsr;
12587
12588 /* On some early chips the SRAM cannot be accessed in D3hot state,
12589 * so need make sure we're in D0.
12590 */
12591 pci_read_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, &pmcsr);
12592 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
12593 pci_write_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, pmcsr);
12594 msleep(1);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012595
12596 /* Make sure register accesses (indirect or otherwise)
12597 * will function correctly.
12598 */
12599 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12600 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012601
David S. Millerf49639e2006-06-09 11:58:36 -070012602 /* The memory arbiter has to be enabled in order for SRAM accesses
12603 * to succeed. Normally on powerup the tg3 chip firmware will make
12604 * sure it is enabled, but other entities such as system netboot
12605 * code might disable it.
12606 */
12607 val = tr32(MEMARB_MODE);
12608 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
12609
Matt Carlson79eb6902010-02-17 15:17:03 +000012610 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012611 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12612
Gary Zambranoa85feb82007-05-05 11:52:19 -070012613 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000012614 tg3_flag_set(tp, EEPROM_WRITE_PROT);
12615 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080012616
Michael Chanb5d37722006-09-27 16:06:21 -070012617 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012618 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012619 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
12620 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080012621 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012622 val = tr32(VCPU_CFGSHDW);
12623 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000012624 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070012625 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012626 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012627 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012628 device_set_wakeup_enable(&tp->pdev->dev, true);
12629 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012630 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070012631 }
12632
Linus Torvalds1da177e2005-04-16 15:20:36 -070012633 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12634 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12635 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012636 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012637 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012638
12639 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12640 tp->nic_sram_data_cfg = nic_cfg;
12641
12642 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
12643 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Matt Carlson6ff6f812011-05-19 12:12:54 +000012644 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12645 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
12646 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012647 (ver > 0) && (ver < 0x100))
12648 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
12649
Matt Carlsona9daf362008-05-25 23:49:44 -070012650 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
12651 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
12652
Linus Torvalds1da177e2005-04-16 15:20:36 -070012653 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
12654 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
12655 eeprom_phy_serdes = 1;
12656
12657 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
12658 if (nic_phy_id != 0) {
12659 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
12660 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
12661
12662 eeprom_phy_id = (id1 >> 16) << 10;
12663 eeprom_phy_id |= (id2 & 0xfc00) << 16;
12664 eeprom_phy_id |= (id2 & 0x03ff) << 0;
12665 } else
12666 eeprom_phy_id = 0;
12667
Michael Chan7d0c41e2005-04-21 17:06:20 -070012668 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070012669 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000012670 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012671 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000012672 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012673 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070012674 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070012675
Joe Perches63c3a662011-04-26 08:12:10 +000012676 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012677 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
12678 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070012679 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070012680 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
12681
12682 switch (led_cfg) {
12683 default:
12684 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
12685 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12686 break;
12687
12688 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
12689 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12690 break;
12691
12692 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
12693 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070012694
12695 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
12696 * read on some older 5700/5701 bootcode.
12697 */
12698 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
12699 ASIC_REV_5700 ||
12700 GET_ASIC_REV(tp->pci_chip_rev_id) ==
12701 ASIC_REV_5701)
12702 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12703
Linus Torvalds1da177e2005-04-16 15:20:36 -070012704 break;
12705
12706 case SHASTA_EXT_LED_SHARED:
12707 tp->led_ctrl = LED_CTRL_MODE_SHARED;
12708 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
12709 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
12710 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12711 LED_CTRL_MODE_PHY_2);
12712 break;
12713
12714 case SHASTA_EXT_LED_MAC:
12715 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
12716 break;
12717
12718 case SHASTA_EXT_LED_COMBO:
12719 tp->led_ctrl = LED_CTRL_MODE_COMBO;
12720 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
12721 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12722 LED_CTRL_MODE_PHY_2);
12723 break;
12724
Stephen Hemminger855e1112008-04-16 16:37:28 -070012725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012726
12727 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12728 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
12729 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
12730 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12731
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012732 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
12733 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080012734
Michael Chan9d26e212006-12-07 00:21:14 -080012735 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000012736 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080012737 if ((tp->pdev->subsystem_vendor ==
12738 PCI_VENDOR_ID_ARIMA) &&
12739 (tp->pdev->subsystem_device == 0x205a ||
12740 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000012741 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080012742 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000012743 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
12744 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080012745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012746
12747 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000012748 tg3_flag_set(tp, ENABLE_ASF);
12749 if (tg3_flag(tp, 5750_PLUS))
12750 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012751 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012752
12753 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000012754 tg3_flag(tp, 5750_PLUS))
12755 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012756
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012757 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070012758 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000012759 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012760
Joe Perches63c3a662011-04-26 08:12:10 +000012761 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012762 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012763 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012764 device_set_wakeup_enable(&tp->pdev->dev, true);
12765 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012766
Linus Torvalds1da177e2005-04-16 15:20:36 -070012767 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012768 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012769
12770 /* serdes signal pre-emphasis in register 0x590 set by */
12771 /* bootcode if bit 18 is set */
12772 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012773 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070012774
Joe Perches63c3a662011-04-26 08:12:10 +000012775 if ((tg3_flag(tp, 57765_PLUS) ||
12776 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
12777 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080012778 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012779 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080012780
Joe Perches63c3a662011-04-26 08:12:10 +000012781 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson8c69b1e2010-08-02 11:26:00 +000012782 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000012783 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070012784 u32 cfg3;
12785
12786 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
12787 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000012788 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070012789 }
Matt Carlsona9daf362008-05-25 23:49:44 -070012790
Matt Carlson14417062010-02-17 15:16:59 +000012791 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000012792 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070012793 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000012794 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070012795 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000012796 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012797 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012798done:
Joe Perches63c3a662011-04-26 08:12:10 +000012799 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000012800 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000012801 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000012802 else
12803 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012804}
12805
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012806static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
12807{
12808 int i;
12809 u32 val;
12810
12811 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
12812 tw32(OTP_CTRL, cmd);
12813
12814 /* Wait for up to 1 ms for command to execute. */
12815 for (i = 0; i < 100; i++) {
12816 val = tr32(OTP_STATUS);
12817 if (val & OTP_STATUS_CMD_DONE)
12818 break;
12819 udelay(10);
12820 }
12821
12822 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
12823}
12824
12825/* Read the gphy configuration from the OTP region of the chip. The gphy
12826 * configuration is a 32-bit value that straddles the alignment boundary.
12827 * We do two 32-bit reads and then shift and merge the results.
12828 */
12829static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
12830{
12831 u32 bhalf_otp, thalf_otp;
12832
12833 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
12834
12835 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
12836 return 0;
12837
12838 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
12839
12840 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12841 return 0;
12842
12843 thalf_otp = tr32(OTP_READ_DATA);
12844
12845 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
12846
12847 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12848 return 0;
12849
12850 bhalf_otp = tr32(OTP_READ_DATA);
12851
12852 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
12853}
12854
Matt Carlsone256f8a2011-03-09 16:58:24 +000012855static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
12856{
12857 u32 adv = ADVERTISED_Autoneg |
12858 ADVERTISED_Pause;
12859
12860 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
12861 adv |= ADVERTISED_1000baseT_Half |
12862 ADVERTISED_1000baseT_Full;
12863
12864 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
12865 adv |= ADVERTISED_100baseT_Half |
12866 ADVERTISED_100baseT_Full |
12867 ADVERTISED_10baseT_Half |
12868 ADVERTISED_10baseT_Full |
12869 ADVERTISED_TP;
12870 else
12871 adv |= ADVERTISED_FIBRE;
12872
12873 tp->link_config.advertising = adv;
12874 tp->link_config.speed = SPEED_INVALID;
12875 tp->link_config.duplex = DUPLEX_INVALID;
12876 tp->link_config.autoneg = AUTONEG_ENABLE;
12877 tp->link_config.active_speed = SPEED_INVALID;
12878 tp->link_config.active_duplex = DUPLEX_INVALID;
12879 tp->link_config.orig_speed = SPEED_INVALID;
12880 tp->link_config.orig_duplex = DUPLEX_INVALID;
12881 tp->link_config.orig_autoneg = AUTONEG_INVALID;
12882}
12883
Michael Chan7d0c41e2005-04-21 17:06:20 -070012884static int __devinit tg3_phy_probe(struct tg3 *tp)
12885{
12886 u32 hw_phy_id_1, hw_phy_id_2;
12887 u32 hw_phy_id, hw_phy_id_masked;
12888 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012889
Matt Carlsone256f8a2011-03-09 16:58:24 +000012890 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000012891 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000012892 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
12893
Joe Perches63c3a662011-04-26 08:12:10 +000012894 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012895 return tg3_phy_init(tp);
12896
Linus Torvalds1da177e2005-04-16 15:20:36 -070012897 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010012898 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012899 */
12900 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000012901 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000012902 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012903 } else {
12904 /* Now read the physical PHY_ID from the chip and verify
12905 * that it is sane. If it doesn't look good, we fall back
12906 * to either the hard-coded table based PHY_ID and failing
12907 * that the value found in the eeprom area.
12908 */
12909 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
12910 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
12911
12912 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
12913 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
12914 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
12915
Matt Carlson79eb6902010-02-17 15:17:03 +000012916 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012917 }
12918
Matt Carlson79eb6902010-02-17 15:17:03 +000012919 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012920 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000012921 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012922 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070012923 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012924 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012925 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000012926 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070012927 /* Do nothing, phy ID already set up in
12928 * tg3_get_eeprom_hw_cfg().
12929 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012930 } else {
12931 struct subsys_tbl_ent *p;
12932
12933 /* No eeprom signature? Try the hardcoded
12934 * subsys device table.
12935 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012936 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012937 if (!p)
12938 return -ENODEV;
12939
12940 tp->phy_id = p->phy_id;
12941 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000012942 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012943 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012944 }
12945 }
12946
Matt Carlsona6b68da2010-12-06 08:28:52 +000012947 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlsonf7838b52011-07-20 10:20:53 +000012948 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
12949 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
12950 (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +000012951 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
12952 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12953 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000012954 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
12955
Matt Carlsone256f8a2011-03-09 16:58:24 +000012956 tg3_phy_init_link_config(tp);
12957
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012958 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000012959 !tg3_flag(tp, ENABLE_APE) &&
12960 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000012961 u32 bmsr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012962
12963 tg3_readphy(tp, MII_BMSR, &bmsr);
12964 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
12965 (bmsr & BMSR_LSTATUS))
12966 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012967
Linus Torvalds1da177e2005-04-16 15:20:36 -070012968 err = tg3_phy_reset(tp);
12969 if (err)
12970 return err;
12971
Matt Carlson42b64a42011-05-19 12:12:49 +000012972 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012973
Michael Chan3600d912006-12-07 00:21:48 -080012974 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
12975 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
12976 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
12977 if (!tg3_copper_is_advertising_all(tp, mask)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000012978 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
12979 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012980
12981 tg3_writephy(tp, MII_BMCR,
12982 BMCR_ANENABLE | BMCR_ANRESTART);
12983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012984 }
12985
12986skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000012987 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012988 err = tg3_init_5401phy_dsp(tp);
12989 if (err)
12990 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012991
Linus Torvalds1da177e2005-04-16 15:20:36 -070012992 err = tg3_init_5401phy_dsp(tp);
12993 }
12994
Linus Torvalds1da177e2005-04-16 15:20:36 -070012995 return err;
12996}
12997
Matt Carlson184b8902010-04-05 10:19:25 +000012998static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012999{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013000 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013001 unsigned int block_end, rosize, len;
Matt Carlson184b8902010-04-05 10:19:25 +000013002 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013003
Matt Carlsonc3e94502011-04-13 11:05:08 +000013004 vpd_data = (u8 *)tg3_vpd_readblock(tp);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013005 if (!vpd_data)
13006 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013007
Matt Carlson4181b2c2010-02-26 14:04:45 +000013008 i = pci_vpd_find_tag(vpd_data, 0, TG3_NVM_VPD_LEN,
13009 PCI_VPD_LRDT_RO_DATA);
13010 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013011 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013012
13013 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
13014 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
13015 i += PCI_VPD_LRDT_TAG_SIZE;
13016
13017 if (block_end > TG3_NVM_VPD_LEN)
13018 goto out_not_found;
13019
Matt Carlson184b8902010-04-05 10:19:25 +000013020 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13021 PCI_VPD_RO_KEYWORD_MFR_ID);
13022 if (j > 0) {
13023 len = pci_vpd_info_field_size(&vpd_data[j]);
13024
13025 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13026 if (j + len > block_end || len != 4 ||
13027 memcmp(&vpd_data[j], "1028", 4))
13028 goto partno;
13029
13030 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13031 PCI_VPD_RO_KEYWORD_VENDOR0);
13032 if (j < 0)
13033 goto partno;
13034
13035 len = pci_vpd_info_field_size(&vpd_data[j]);
13036
13037 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13038 if (j + len > block_end)
13039 goto partno;
13040
13041 memcpy(tp->fw_ver, &vpd_data[j], len);
13042 strncat(tp->fw_ver, " bc ", TG3_NVM_VPD_LEN - len - 1);
13043 }
13044
13045partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000013046 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13047 PCI_VPD_RO_KEYWORD_PARTNO);
13048 if (i < 0)
13049 goto out_not_found;
13050
13051 len = pci_vpd_info_field_size(&vpd_data[i]);
13052
13053 i += PCI_VPD_INFO_FLD_HDR_SIZE;
13054 if (len > TG3_BPN_SIZE ||
13055 (len + i) > TG3_NVM_VPD_LEN)
13056 goto out_not_found;
13057
13058 memcpy(tp->board_part_number, &vpd_data[i], len);
13059
Linus Torvalds1da177e2005-04-16 15:20:36 -070013060out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013061 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000013062 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013063 return;
13064
13065out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000013066 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13067 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
13068 strcpy(tp->board_part_number, "BCM5717");
13069 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
13070 strcpy(tp->board_part_number, "BCM5718");
13071 else
13072 goto nomatch;
13073 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
13074 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
13075 strcpy(tp->board_part_number, "BCM57780");
13076 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
13077 strcpy(tp->board_part_number, "BCM57760");
13078 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
13079 strcpy(tp->board_part_number, "BCM57790");
13080 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
13081 strcpy(tp->board_part_number, "BCM57788");
13082 else
13083 goto nomatch;
13084 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
13085 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
13086 strcpy(tp->board_part_number, "BCM57761");
13087 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
13088 strcpy(tp->board_part_number, "BCM57765");
13089 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
13090 strcpy(tp->board_part_number, "BCM57781");
13091 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
13092 strcpy(tp->board_part_number, "BCM57785");
13093 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
13094 strcpy(tp->board_part_number, "BCM57791");
13095 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13096 strcpy(tp->board_part_number, "BCM57795");
13097 else
13098 goto nomatch;
13099 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070013100 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000013101 } else {
13102nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070013103 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000013104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013105}
13106
Matt Carlson9c8a6202007-10-21 16:16:08 -070013107static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
13108{
13109 u32 val;
13110
Matt Carlsone4f34112009-02-25 14:25:00 +000013111 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013112 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013113 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013114 val != 0)
13115 return 0;
13116
13117 return 1;
13118}
13119
Matt Carlsonacd9c112009-02-25 14:26:33 +000013120static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13121{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013122 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000013123 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013124 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013125
13126 if (tg3_nvram_read(tp, 0xc, &offset) ||
13127 tg3_nvram_read(tp, 0x4, &start))
13128 return;
13129
13130 offset = tg3_nvram_logical_addr(tp, offset);
13131
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013132 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013133 return;
13134
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013135 if ((val & 0xfc000000) == 0x0c000000) {
13136 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013137 return;
13138
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013139 if (val == 0)
13140 newver = true;
13141 }
13142
Matt Carlson75f99362010-04-05 10:19:24 +000013143 dst_off = strlen(tp->fw_ver);
13144
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013145 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013146 if (TG3_VER_SIZE - dst_off < 16 ||
13147 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013148 return;
13149
13150 offset = offset + ver_offset - start;
13151 for (i = 0; i < 16; i += 4) {
13152 __be32 v;
13153 if (tg3_nvram_read_be32(tp, offset + i, &v))
13154 return;
13155
Matt Carlson75f99362010-04-05 10:19:24 +000013156 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013157 }
13158 } else {
13159 u32 major, minor;
13160
13161 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13162 return;
13163
13164 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13165 TG3_NVM_BCVER_MAJSFT;
13166 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013167 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13168 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013169 }
13170}
13171
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013172static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13173{
13174 u32 val, major, minor;
13175
13176 /* Use native endian representation */
13177 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13178 return;
13179
13180 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13181 TG3_NVM_HWSB_CFG1_MAJSFT;
13182 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13183 TG3_NVM_HWSB_CFG1_MINSFT;
13184
13185 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13186}
13187
Matt Carlsondfe00d72008-11-21 17:19:41 -080013188static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13189{
13190 u32 offset, major, minor, build;
13191
Matt Carlson75f99362010-04-05 10:19:24 +000013192 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013193
13194 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13195 return;
13196
13197 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13198 case TG3_EEPROM_SB_REVISION_0:
13199 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13200 break;
13201 case TG3_EEPROM_SB_REVISION_2:
13202 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13203 break;
13204 case TG3_EEPROM_SB_REVISION_3:
13205 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13206 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013207 case TG3_EEPROM_SB_REVISION_4:
13208 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13209 break;
13210 case TG3_EEPROM_SB_REVISION_5:
13211 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13212 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013213 case TG3_EEPROM_SB_REVISION_6:
13214 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13215 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013216 default:
13217 return;
13218 }
13219
Matt Carlsone4f34112009-02-25 14:25:00 +000013220 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013221 return;
13222
13223 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13224 TG3_EEPROM_SB_EDH_BLD_SHFT;
13225 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13226 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13227 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13228
13229 if (minor > 99 || build > 26)
13230 return;
13231
Matt Carlson75f99362010-04-05 10:19:24 +000013232 offset = strlen(tp->fw_ver);
13233 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13234 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013235
13236 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013237 offset = strlen(tp->fw_ver);
13238 if (offset < TG3_VER_SIZE - 1)
13239 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013240 }
13241}
13242
Matt Carlsonacd9c112009-02-25 14:26:33 +000013243static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013244{
13245 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013246 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013247
13248 for (offset = TG3_NVM_DIR_START;
13249 offset < TG3_NVM_DIR_END;
13250 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013251 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013252 return;
13253
13254 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13255 break;
13256 }
13257
13258 if (offset == TG3_NVM_DIR_END)
13259 return;
13260
Joe Perches63c3a662011-04-26 08:12:10 +000013261 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013262 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013263 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013264 return;
13265
Matt Carlsone4f34112009-02-25 14:25:00 +000013266 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013267 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013268 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013269 return;
13270
13271 offset += val - start;
13272
Matt Carlsonacd9c112009-02-25 14:26:33 +000013273 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013274
Matt Carlsonacd9c112009-02-25 14:26:33 +000013275 tp->fw_ver[vlen++] = ',';
13276 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013277
13278 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013279 __be32 v;
13280 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013281 return;
13282
Al Virob9fc7dc2007-12-17 22:59:57 -080013283 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013284
Matt Carlsonacd9c112009-02-25 14:26:33 +000013285 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13286 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013287 break;
13288 }
13289
Matt Carlsonacd9c112009-02-25 14:26:33 +000013290 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13291 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013292 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013293}
13294
Matt Carlson7fd76442009-02-25 14:27:20 +000013295static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13296{
13297 int vlen;
13298 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013299 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013300
Joe Perches63c3a662011-04-26 08:12:10 +000013301 if (!tg3_flag(tp, ENABLE_APE) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000013302 return;
13303
13304 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13305 if (apedata != APE_SEG_SIG_MAGIC)
13306 return;
13307
13308 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13309 if (!(apedata & APE_FW_STATUS_READY))
13310 return;
13311
13312 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13313
Matt Carlsondc6d0742010-09-15 08:59:55 +000013314 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
Joe Perches63c3a662011-04-26 08:12:10 +000013315 tg3_flag_set(tp, APE_HAS_NCSI);
Matt Carlsonecc79642010-08-02 11:26:01 +000013316 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013317 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013318 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013319 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013320
Matt Carlson7fd76442009-02-25 14:27:20 +000013321 vlen = strlen(tp->fw_ver);
13322
Matt Carlsonecc79642010-08-02 11:26:01 +000013323 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13324 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013325 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13326 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13327 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13328 (apedata & APE_FW_VERSION_BLDMSK));
13329}
13330
Matt Carlsonacd9c112009-02-25 14:26:33 +000013331static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13332{
13333 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013334 bool vpd_vers = false;
13335
13336 if (tp->fw_ver[0] != 0)
13337 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013338
Joe Perches63c3a662011-04-26 08:12:10 +000013339 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000013340 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013341 return;
13342 }
13343
Matt Carlsonacd9c112009-02-25 14:26:33 +000013344 if (tg3_nvram_read(tp, 0, &val))
13345 return;
13346
13347 if (val == TG3_EEPROM_MAGIC)
13348 tg3_read_bc_ver(tp);
13349 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13350 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013351 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13352 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013353 else
13354 return;
13355
Joe Perches63c3a662011-04-26 08:12:10 +000013356 if (!tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || vpd_vers)
Matt Carlson75f99362010-04-05 10:19:24 +000013357 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013358
13359 tg3_read_mgmtfw_ver(tp);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013360
Matt Carlson75f99362010-04-05 10:19:24 +000013361done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013362 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013363}
13364
Michael Chan7544b092007-05-05 13:08:32 -070013365static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
13366
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013367static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13368{
Joe Perches63c3a662011-04-26 08:12:10 +000013369 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013370 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000013371 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013372 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013373 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013374 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013375}
13376
Matt Carlson41434702011-03-09 16:58:22 +000013377static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013378 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13379 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13380 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13381 { },
13382};
13383
Linus Torvalds1da177e2005-04-16 15:20:36 -070013384static int __devinit tg3_get_invariants(struct tg3 *tp)
13385{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013386 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013387 u32 pci_state_reg, grc_misc_cfg;
13388 u32 val;
13389 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013390 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013391
Linus Torvalds1da177e2005-04-16 15:20:36 -070013392 /* Force memory write invalidate off. If we leave it on,
13393 * then on 5700_BX chips we have to enable a workaround.
13394 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13395 * to match the cacheline size. The Broadcom driver have this
13396 * workaround but turns MWI off all the times so never uses
13397 * it. This seems to suggest that the workaround is insufficient.
13398 */
13399 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13400 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13401 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13402
13403 /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
13404 * has the register indirect write enable bit set before
13405 * we try to access any of the MMIO registers. It is also
13406 * critical that the PCI-X hw workaround situation is decided
13407 * before that as well.
13408 */
13409 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13410 &misc_ctrl_reg);
13411
13412 tp->pci_chip_rev_id = (misc_ctrl_reg >>
13413 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070013414 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13415 u32 prod_id_asic_rev;
13416
Matt Carlson5001e2f2009-11-13 13:03:51 +000013417 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13418 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013419 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
13420 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013421 pci_read_config_dword(tp->pdev,
13422 TG3PCI_GEN2_PRODID_ASICREV,
13423 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000013424 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13425 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13426 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13427 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13428 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13429 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13430 pci_read_config_dword(tp->pdev,
13431 TG3PCI_GEN15_PRODID_ASICREV,
13432 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013433 else
13434 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
13435 &prod_id_asic_rev);
13436
Matt Carlson321d32a2008-11-21 17:22:19 -080013437 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070013438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013439
Michael Chanff645be2005-04-21 17:09:53 -070013440 /* Wrong chip ID in 5752 A0. This code can be removed later
13441 * as A0 is not in production.
13442 */
13443 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13444 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13445
Michael Chan68929142005-08-09 20:17:14 -070013446 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
13447 * we need to disable memory and use config. cycles
13448 * only to access all registers. The 5702/03 chips
13449 * can mistakenly decode the special cycles from the
13450 * ICH chipsets as memory write cycles, causing corruption
13451 * of register and memory space. Only certain ICH bridges
13452 * will drive special cycles with non-zero data during the
13453 * address phase which can fall within the 5703's address
13454 * range. This is not an ICH bug as the PCI spec allows
13455 * non-zero address during special cycles. However, only
13456 * these ICH bridges are known to drive non-zero addresses
13457 * during special cycles.
13458 *
13459 * Since special cycles do not cross PCI bridges, we only
13460 * enable this workaround if the 5703 is on the secondary
13461 * bus of these ICH bridges.
13462 */
13463 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
13464 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
13465 static struct tg3_dev_id {
13466 u32 vendor;
13467 u32 device;
13468 u32 rev;
13469 } ich_chipsets[] = {
13470 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
13471 PCI_ANY_ID },
13472 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
13473 PCI_ANY_ID },
13474 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
13475 0xa },
13476 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
13477 PCI_ANY_ID },
13478 { },
13479 };
13480 struct tg3_dev_id *pci_id = &ich_chipsets[0];
13481 struct pci_dev *bridge = NULL;
13482
13483 while (pci_id->vendor != 0) {
13484 bridge = pci_get_device(pci_id->vendor, pci_id->device,
13485 bridge);
13486 if (!bridge) {
13487 pci_id++;
13488 continue;
13489 }
13490 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070013491 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070013492 continue;
13493 }
13494 if (bridge->subordinate &&
13495 (bridge->subordinate->number ==
13496 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013497 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070013498 pci_dev_put(bridge);
13499 break;
13500 }
13501 }
13502 }
13503
Matt Carlson6ff6f812011-05-19 12:12:54 +000013504 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070013505 static struct tg3_dev_id {
13506 u32 vendor;
13507 u32 device;
13508 } bridge_chipsets[] = {
13509 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
13510 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
13511 { },
13512 };
13513 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
13514 struct pci_dev *bridge = NULL;
13515
13516 while (pci_id->vendor != 0) {
13517 bridge = pci_get_device(pci_id->vendor,
13518 pci_id->device,
13519 bridge);
13520 if (!bridge) {
13521 pci_id++;
13522 continue;
13523 }
13524 if (bridge->subordinate &&
13525 (bridge->subordinate->number <=
13526 tp->pdev->bus->number) &&
13527 (bridge->subordinate->subordinate >=
13528 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013529 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070013530 pci_dev_put(bridge);
13531 break;
13532 }
13533 }
13534 }
13535
Michael Chan4a29cc22006-03-19 13:21:12 -080013536 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
13537 * DMA addresses > 40-bit. This bridge may have other additional
13538 * 57xx devices behind it in some 4-port NIC designs for example.
13539 * Any tg3 device found behind the bridge will also need the 40-bit
13540 * DMA workaround.
13541 */
Michael Chana4e2b342005-10-26 15:46:52 -070013542 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
13543 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Joe Perches63c3a662011-04-26 08:12:10 +000013544 tg3_flag_set(tp, 5780_CLASS);
13545 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070013546 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a5882010-04-05 10:19:28 +000013547 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080013548 struct pci_dev *bridge = NULL;
13549
13550 do {
13551 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
13552 PCI_DEVICE_ID_SERVERWORKS_EPB,
13553 bridge);
13554 if (bridge && bridge->subordinate &&
13555 (bridge->subordinate->number <=
13556 tp->pdev->bus->number) &&
13557 (bridge->subordinate->subordinate >=
13558 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013559 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080013560 pci_dev_put(bridge);
13561 break;
13562 }
13563 } while (bridge);
13564 }
Michael Chan4cf78e42005-07-25 12:29:19 -070013565
Linus Torvalds1da177e2005-04-16 15:20:36 -070013566 /* Initialize misc host control in PCI block. */
13567 tp->misc_host_ctrl |= (misc_ctrl_reg &
13568 MISC_HOST_CTRL_CHIPREV);
13569 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13570 tp->misc_host_ctrl);
13571
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013572 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
13573 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013574 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13575 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Michael Chan7544b092007-05-05 13:08:32 -070013576 tp->pdev_peer = tg3_find_peer(tp);
13577
Matt Carlsonc885e822010-08-02 11:25:57 +000013578 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013579 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13580 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000013581 tg3_flag_set(tp, 5717_PLUS);
Matt Carlson0a58d662011-04-05 14:22:45 +000013582
13583 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013584 tg3_flag(tp, 5717_PLUS))
13585 tg3_flag_set(tp, 57765_PLUS);
Matt Carlsonc885e822010-08-02 11:25:57 +000013586
Matt Carlson321d32a2008-11-21 17:22:19 -080013587 /* Intentionally exclude ASIC_REV_5906 */
13588 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad2006-03-20 22:27:35 -080013589 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013590 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013591 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013592 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013593 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013594 tg3_flag(tp, 57765_PLUS))
13595 tg3_flag_set(tp, 5755_PLUS);
Matt Carlson321d32a2008-11-21 17:22:19 -080013596
13597 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13598 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070013599 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013600 tg3_flag(tp, 5755_PLUS) ||
13601 tg3_flag(tp, 5780_CLASS))
13602 tg3_flag_set(tp, 5750_PLUS);
John W. Linville6708e5c2005-04-21 17:00:52 -070013603
Matt Carlson6ff6f812011-05-19 12:12:54 +000013604 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013605 tg3_flag(tp, 5750_PLUS))
13606 tg3_flag_set(tp, 5705_PLUS);
John W. Linville1b440c562005-04-21 17:03:18 -070013607
Matt Carlson507399f2009-11-13 13:03:37 +000013608 /* Determine TSO capabilities */
Matt Carlson2866d952011-02-10 20:06:46 -080013609 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlson4d163b72011-01-25 15:58:48 +000013610 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000013611 else if (tg3_flag(tp, 57765_PLUS))
13612 tg3_flag_set(tp, HW_TSO_3);
13613 else if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000013614 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000013615 tg3_flag_set(tp, HW_TSO_2);
13616 else if (tg3_flag(tp, 5750_PLUS)) {
13617 tg3_flag_set(tp, HW_TSO_1);
13618 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013619 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13620 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000013621 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013622 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13623 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13624 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000013625 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013626 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13627 tp->fw_needed = FIRMWARE_TG3TSO5;
13628 else
13629 tp->fw_needed = FIRMWARE_TG3TSO;
13630 }
13631
Matt Carlsondabc5c62011-05-19 12:12:52 +000013632 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000013633 if (tg3_flag(tp, HW_TSO_1) ||
13634 tg3_flag(tp, HW_TSO_2) ||
13635 tg3_flag(tp, HW_TSO_3) ||
Matt Carlsondabc5c62011-05-19 12:12:52 +000013636 (tp->fw_needed && !tg3_flag(tp, ENABLE_ASF)))
13637 tg3_flag_set(tp, TSO_CAPABLE);
13638 else {
13639 tg3_flag_clear(tp, TSO_CAPABLE);
13640 tg3_flag_clear(tp, TSO_BUG);
13641 tp->fw_needed = NULL;
13642 }
13643
13644 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
13645 tp->fw_needed = FIRMWARE_TG3;
13646
Matt Carlson507399f2009-11-13 13:03:37 +000013647 tp->irq_max = 1;
13648
Joe Perches63c3a662011-04-26 08:12:10 +000013649 if (tg3_flag(tp, 5750_PLUS)) {
13650 tg3_flag_set(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070013651 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
13652 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
13653 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
13654 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
13655 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000013656 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070013657
Joe Perches63c3a662011-04-26 08:12:10 +000013658 if (tg3_flag(tp, 5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070013659 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000013660 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070013661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013662
Joe Perches63c3a662011-04-26 08:12:10 +000013663 if (tg3_flag(tp, 57765_PLUS)) {
13664 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000013665 tp->irq_max = TG3_IRQ_MAX_VECS;
13666 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013667 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000013668
Matt Carlson2ffcc982011-05-19 12:12:44 +000013669 /* All chips can get confused if TX buffers
13670 * straddle the 4GB address boundary.
13671 */
13672 tg3_flag_set(tp, 4G_DMA_BNDRY_BUG);
13673
13674 if (tg3_flag(tp, 5755_PLUS))
Joe Perches63c3a662011-04-26 08:12:10 +000013675 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlson2ffcc982011-05-19 12:12:44 +000013676 else
Joe Perches63c3a662011-04-26 08:12:10 +000013677 tg3_flag_set(tp, 40BIT_DMA_LIMIT_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013678
Joe Perches63c3a662011-04-26 08:12:10 +000013679 if (tg3_flag(tp, 5717_PLUS))
13680 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000013681
Joe Perches63c3a662011-04-26 08:12:10 +000013682 if (tg3_flag(tp, 57765_PLUS) &&
Matt Carlson2866d952011-02-10 20:06:46 -080013683 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
Joe Perches63c3a662011-04-26 08:12:10 +000013684 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000013685
Joe Perches63c3a662011-04-26 08:12:10 +000013686 if (!tg3_flag(tp, 5705_PLUS) ||
13687 tg3_flag(tp, 5780_CLASS) ||
13688 tg3_flag(tp, USE_JUMBO_BDFLAG))
13689 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070013690
Matt Carlson52f44902008-11-21 17:17:04 -080013691 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13692 &pci_state_reg);
13693
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013694 tp->pcie_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_EXP);
13695 if (tp->pcie_cap != 0) {
13696 u16 lnkctl;
13697
Joe Perches63c3a662011-04-26 08:12:10 +000013698 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013699
Matt Carlsoncf790032010-11-24 08:31:48 +000013700 tp->pcie_readrq = 4096;
Matt Carlsond78b59f2011-04-05 14:22:46 +000013701 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13702 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Matt Carlsonb4495ed2011-01-25 15:58:47 +000013703 tp->pcie_readrq = 2048;
Matt Carlsoncf790032010-11-24 08:31:48 +000013704
13705 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013706
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013707 pci_read_config_word(tp->pdev,
13708 tp->pcie_cap + PCI_EXP_LNKCTL,
13709 &lnkctl);
13710 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Matt Carlson7196cd62011-05-19 16:02:44 +000013711 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
13712 ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000013713 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000013714 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000013715 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013716 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013717 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000013718 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
13719 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000013720 tg3_flag_set(tp, CLKREQ_BUG);
Matt Carlson614b0592010-01-20 16:58:02 +000013721 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000013722 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080013723 }
Matt Carlson52f44902008-11-21 17:17:04 -080013724 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Joe Perches63c3a662011-04-26 08:12:10 +000013725 tg3_flag_set(tp, PCI_EXPRESS);
13726 } else if (!tg3_flag(tp, 5705_PLUS) ||
13727 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080013728 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
13729 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000013730 dev_err(&tp->pdev->dev,
13731 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080013732 return -EIO;
13733 }
13734
13735 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000013736 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080013737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013738
Michael Chan399de502005-10-03 14:02:39 -070013739 /* If we have an AMD 762 or VIA K8T800 chipset, write
13740 * reordering to the mailbox registers done by the host
13741 * controller can cause major troubles. We read back from
13742 * every mailbox register write to force the writes to be
13743 * posted to the chip in order.
13744 */
Matt Carlson41434702011-03-09 16:58:22 +000013745 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013746 !tg3_flag(tp, PCI_EXPRESS))
13747 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070013748
Matt Carlson69fc4052008-12-21 20:19:57 -080013749 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
13750 &tp->pci_cacheline_sz);
13751 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13752 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013753 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13754 tp->pci_lat_timer < 64) {
13755 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080013756 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13757 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013758 }
13759
Matt Carlson52f44902008-11-21 17:17:04 -080013760 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
13761 /* 5700 BX chips need to have their TX producer index
13762 * mailboxes written twice to workaround a bug.
13763 */
Joe Perches63c3a662011-04-26 08:12:10 +000013764 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070013765
Matt Carlson52f44902008-11-21 17:17:04 -080013766 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013767 *
13768 * The workaround is to use indirect register accesses
13769 * for all chip writes not to mailbox registers.
13770 */
Joe Perches63c3a662011-04-26 08:12:10 +000013771 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013772 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013773
Joe Perches63c3a662011-04-26 08:12:10 +000013774 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013775
13776 /* The chip can have it's power management PCI config
13777 * space registers clobbered due to this bug.
13778 * So explicitly force the chip into D0 here.
13779 */
Matt Carlson9974a352007-10-07 23:27:28 -070013780 pci_read_config_dword(tp->pdev,
13781 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013782 &pm_reg);
13783 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
13784 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070013785 pci_write_config_dword(tp->pdev,
13786 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013787 pm_reg);
13788
13789 /* Also, force SERR#/PERR# in PCI command. */
13790 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13791 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
13792 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13793 }
13794 }
13795
Linus Torvalds1da177e2005-04-16 15:20:36 -070013796 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000013797 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013798 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000013799 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013800
13801 /* Chip-specific fixup from Broadcom driver */
13802 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
13803 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
13804 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
13805 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
13806 }
13807
Michael Chan1ee582d2005-08-09 20:16:46 -070013808 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070013809 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013810 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070013811 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070013812 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013813 tp->write32_tx_mbox = tg3_write32;
13814 tp->write32_rx_mbox = tg3_write32;
13815
13816 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000013817 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070013818 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013819 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013820 (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson98efd8a2007-05-05 12:47:25 -070013821 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
13822 /*
13823 * Back to back register writes can cause problems on these
13824 * chips, the workaround is to read back all reg writes
13825 * except those to mailbox regs.
13826 *
13827 * See tg3_write_indirect_reg32().
13828 */
Michael Chan1ee582d2005-08-09 20:16:46 -070013829 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013830 }
13831
Joe Perches63c3a662011-04-26 08:12:10 +000013832 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070013833 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000013834 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070013835 tp->write32_rx_mbox = tg3_write_flush_reg32;
13836 }
Michael Chan20094932005-08-09 20:16:32 -070013837
Joe Perches63c3a662011-04-26 08:12:10 +000013838 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070013839 tp->read32 = tg3_read_indirect_reg32;
13840 tp->write32 = tg3_write_indirect_reg32;
13841 tp->read32_mbox = tg3_read_indirect_mbox;
13842 tp->write32_mbox = tg3_write_indirect_mbox;
13843 tp->write32_tx_mbox = tg3_write_indirect_mbox;
13844 tp->write32_rx_mbox = tg3_write_indirect_mbox;
13845
13846 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070013847 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070013848
13849 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13850 pci_cmd &= ~PCI_COMMAND_MEMORY;
13851 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13852 }
Michael Chanb5d37722006-09-27 16:06:21 -070013853 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13854 tp->read32_mbox = tg3_read32_mbox_5906;
13855 tp->write32_mbox = tg3_write32_mbox_5906;
13856 tp->write32_tx_mbox = tg3_write32_mbox_5906;
13857 tp->write32_rx_mbox = tg3_write32_mbox_5906;
13858 }
Michael Chan68929142005-08-09 20:17:14 -070013859
Michael Chanbbadf502006-04-06 21:46:34 -070013860 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013861 (tg3_flag(tp, PCIX_MODE) &&
Michael Chanbbadf502006-04-06 21:46:34 -070013862 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070013863 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000013864 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070013865
Michael Chan7d0c41e2005-04-21 17:06:20 -070013866 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000013867 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070013868 * determined before calling tg3_set_power_state() so that
13869 * we know whether or not to switch out of Vaux power.
13870 * When the flag is set, it means that GPIO1 is used for eeprom
13871 * write protect and also implies that it is a LOM where GPIOs
13872 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013873 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070013874 tg3_get_eeprom_hw_cfg(tp);
13875
Joe Perches63c3a662011-04-26 08:12:10 +000013876 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070013877 /* Allow reads and writes to the
13878 * APE register and memory space.
13879 */
13880 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +000013881 PCISTATE_ALLOW_APE_SHMEM_WR |
13882 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070013883 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
13884 pci_state_reg);
13885 }
13886
Matt Carlson9936bcf2007-10-10 18:03:07 -070013887 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013888 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013889 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013890 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013891 tg3_flag(tp, 57765_PLUS))
13892 tg3_flag_set(tp, CPMU_PRESENT);
Matt Carlsond30cdd22007-10-07 23:28:35 -070013893
Matt Carlsonbea8a632011-04-25 12:42:49 +000013894 /* Set up tp->grc_local_ctrl before calling tg3_power_up().
Michael Chan314fba32005-04-21 17:07:04 -070013895 * GPIO1 driven high will bring 5700's external PHY out of reset.
13896 * It is also used as eeprom write protect on LOMs.
13897 */
13898 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Matt Carlson6ff6f812011-05-19 12:12:54 +000013899 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013900 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070013901 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
13902 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070013903 /* Unused GPIO3 must be driven as output on 5752 because there
13904 * are no pull-up resistors on unused GPIO pins.
13905 */
13906 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
13907 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070013908
Matt Carlson321d32a2008-11-21 17:22:19 -080013909 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000013910 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
13911 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Michael Chanaf36e6b2006-03-23 01:28:06 -080013912 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13913
Matt Carlson8d519ab2009-04-20 06:58:01 +000013914 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
13915 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070013916 /* Turn off the debug UART. */
13917 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013918 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070013919 /* Keep VMain power. */
13920 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
13921 GRC_LCLCTRL_GPIO_OUTPUT0;
13922 }
13923
Linus Torvalds1da177e2005-04-16 15:20:36 -070013924 /* Force the chip into D0. */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000013925 err = tg3_power_up(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013926 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000013927 dev_err(&tp->pdev->dev, "Transition to D0 failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070013928 return err;
13929 }
13930
Linus Torvalds1da177e2005-04-16 15:20:36 -070013931 /* Derive initial jumbo mode from MTU assigned in
13932 * ether_setup() via the alloc_etherdev() call
13933 */
Joe Perches63c3a662011-04-26 08:12:10 +000013934 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
13935 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013936
13937 /* Determine WakeOnLan speed to use. */
13938 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13939 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
13940 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
13941 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000013942 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013943 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013944 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013945 }
13946
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013947 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013948 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013949
Linus Torvalds1da177e2005-04-16 15:20:36 -070013950 /* A few boards don't want Ethernet@WireSpeed phy feature */
Matt Carlson6ff6f812011-05-19 12:12:54 +000013951 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13952 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013953 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070013954 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013955 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
13956 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13957 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013958
13959 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
13960 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013961 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013962 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013963 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013964
Joe Perches63c3a662011-04-26 08:12:10 +000013965 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013966 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080013967 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013968 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000013969 !tg3_flag(tp, 57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070013970 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013971 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013972 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13973 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080013974 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
13975 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013976 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080013977 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013978 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080013979 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013980 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070013981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013982
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013983 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13984 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
13985 tp->phy_otp = tg3_read_otp_phycfg(tp);
13986 if (tp->phy_otp == 0)
13987 tp->phy_otp = TG3_OTP_DEFAULT;
13988 }
13989
Joe Perches63c3a662011-04-26 08:12:10 +000013990 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070013991 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
13992 else
13993 tp->mi_mode = MAC_MI_MODE_BASE;
13994
Linus Torvalds1da177e2005-04-16 15:20:36 -070013995 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013996 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
13997 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
13998 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
13999
Matt Carlson4d958472011-04-20 07:57:35 +000014000 /* Set these bits to enable statistics workaround. */
14001 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14002 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
14003 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
14004 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
14005 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
14006 }
14007
Matt Carlson321d32a2008-11-21 17:22:19 -080014008 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14009 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000014010 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070014011
Matt Carlson158d7ab2008-05-29 01:37:54 -070014012 err = tg3_mdio_init(tp);
14013 if (err)
14014 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014015
14016 /* Initialize data/descriptor byte/word swapping. */
14017 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000014018 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14019 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
14020 GRC_MODE_WORD_SWAP_B2HRX_DATA |
14021 GRC_MODE_B2HRX_ENABLE |
14022 GRC_MODE_HTX2B_ENABLE |
14023 GRC_MODE_HOST_STACKUP);
14024 else
14025 val &= GRC_MODE_HOST_STACKUP;
14026
Linus Torvalds1da177e2005-04-16 15:20:36 -070014027 tw32(GRC_MODE, val | tp->grc_mode);
14028
14029 tg3_switch_clocks(tp);
14030
14031 /* Clear this out for sanity. */
14032 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
14033
14034 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14035 &pci_state_reg);
14036 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014037 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014038 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
14039
14040 if (chiprevid == CHIPREV_ID_5701_A0 ||
14041 chiprevid == CHIPREV_ID_5701_B0 ||
14042 chiprevid == CHIPREV_ID_5701_B2 ||
14043 chiprevid == CHIPREV_ID_5701_B5) {
14044 void __iomem *sram_base;
14045
14046 /* Write some dummy words into the SRAM status block
14047 * area, see if it reads back correctly. If the return
14048 * value is bad, force enable the PCIX workaround.
14049 */
14050 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
14051
14052 writel(0x00000000, sram_base);
14053 writel(0x00000000, sram_base + 4);
14054 writel(0xffffffff, sram_base + 4);
14055 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000014056 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014057 }
14058 }
14059
14060 udelay(50);
14061 tg3_nvram_init(tp);
14062
14063 grc_misc_cfg = tr32(GRC_MISC_CFG);
14064 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
14065
Linus Torvalds1da177e2005-04-16 15:20:36 -070014066 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14067 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
14068 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000014069 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014070
Joe Perches63c3a662011-04-26 08:12:10 +000014071 if (!tg3_flag(tp, IS_5788) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +000014072 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014073 tg3_flag_set(tp, TAGGED_STATUS);
14074 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070014075 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
14076 HOSTCC_MODE_CLRTICK_TXBD);
14077
14078 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
14079 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14080 tp->misc_host_ctrl);
14081 }
14082
Matt Carlson3bda1252008-08-15 14:08:22 -070014083 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000014084 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000014085 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070014086 else
14087 tp->mac_mode = TG3_DEF_MAC_MODE;
14088
Linus Torvalds1da177e2005-04-16 15:20:36 -070014089 /* these are limited to 10/100 only */
14090 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14091 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14092 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14093 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14094 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
14095 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
14096 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
14097 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14098 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080014099 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
14100 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014101 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000014102 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14103 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014104 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14105 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014106
14107 err = tg3_phy_probe(tp);
14108 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014109 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014110 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014111 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014112 }
14113
Matt Carlson184b8902010-04-05 10:19:25 +000014114 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080014115 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014116
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014117 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
14118 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014119 } else {
14120 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014121 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014122 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014123 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014124 }
14125
14126 /* 5700 {AX,BX} chips have a broken status block link
14127 * change bit implementation, so we must use the
14128 * status register in those cases.
14129 */
14130 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014131 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014132 else
Joe Perches63c3a662011-04-26 08:12:10 +000014133 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014134
14135 /* The led_ctrl is set during tg3_phy_probe, here we might
14136 * have to force the link status polling mechanism based
14137 * upon subsystem IDs.
14138 */
14139 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070014140 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014141 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
14142 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000014143 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014144 }
14145
14146 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014147 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000014148 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014149 else
Joe Perches63c3a662011-04-26 08:12:10 +000014150 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014151
Matt Carlsonbf933c82011-01-25 15:58:49 +000014152 tp->rx_offset = NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014153 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014154 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014155 tg3_flag(tp, PCIX_MODE)) {
Matt Carlsonbf933c82011-01-25 15:58:49 +000014156 tp->rx_offset = 0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014157#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000014158 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014159#endif
14160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014161
Matt Carlson2c49a442010-09-30 10:34:35 +000014162 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14163 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014164 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14165
Matt Carlson2c49a442010-09-30 10:34:35 +000014166 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070014167
14168 /* Increment the rx prod index on the rx std ring by at most
14169 * 8 for these chips to workaround hw errata.
14170 */
14171 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14172 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14173 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14174 tp->rx_std_max_post = 8;
14175
Joe Perches63c3a662011-04-26 08:12:10 +000014176 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070014177 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14178 PCIE_PWR_MGMT_L1_THRESH_MSK;
14179
Linus Torvalds1da177e2005-04-16 15:20:36 -070014180 return err;
14181}
14182
David S. Miller49b6e95f2007-03-29 01:38:42 -070014183#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014184static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14185{
14186 struct net_device *dev = tp->dev;
14187 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014188 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070014189 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014190 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014191
David S. Miller49b6e95f2007-03-29 01:38:42 -070014192 addr = of_get_property(dp, "local-mac-address", &len);
14193 if (addr && len == 6) {
14194 memcpy(dev->dev_addr, addr, 6);
14195 memcpy(dev->perm_addr, dev->dev_addr, 6);
14196 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014197 }
14198 return -ENODEV;
14199}
14200
14201static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14202{
14203 struct net_device *dev = tp->dev;
14204
14205 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070014206 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014207 return 0;
14208}
14209#endif
14210
14211static int __devinit tg3_get_device_address(struct tg3 *tp)
14212{
14213 struct net_device *dev = tp->dev;
14214 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080014215 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014216
David S. Miller49b6e95f2007-03-29 01:38:42 -070014217#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014218 if (!tg3_get_macaddr_sparc(tp))
14219 return 0;
14220#endif
14221
14222 mac_offset = 0x7c;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014223 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014224 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014225 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14226 mac_offset = 0xcc;
14227 if (tg3_nvram_lock(tp))
14228 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14229 else
14230 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000014231 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlsona50d0792010-06-05 17:24:37 +000014232 if (PCI_FUNC(tp->pdev->devfn) & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014233 mac_offset = 0xcc;
Matt Carlsona50d0792010-06-05 17:24:37 +000014234 if (PCI_FUNC(tp->pdev->devfn) > 1)
14235 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014236 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014237 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014238
14239 /* First try to get it from MAC address mailbox. */
14240 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14241 if ((hi >> 16) == 0x484b) {
14242 dev->dev_addr[0] = (hi >> 8) & 0xff;
14243 dev->dev_addr[1] = (hi >> 0) & 0xff;
14244
14245 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14246 dev->dev_addr[2] = (lo >> 24) & 0xff;
14247 dev->dev_addr[3] = (lo >> 16) & 0xff;
14248 dev->dev_addr[4] = (lo >> 8) & 0xff;
14249 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014250
Michael Chan008652b2006-03-27 23:14:53 -080014251 /* Some old bootcode may report a 0 MAC address in SRAM */
14252 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14253 }
14254 if (!addr_ok) {
14255 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000014256 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000014257 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000014258 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070014259 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14260 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080014261 }
14262 /* Finally just fetch it out of the MAC control regs. */
14263 else {
14264 hi = tr32(MAC_ADDR_0_HIGH);
14265 lo = tr32(MAC_ADDR_0_LOW);
14266
14267 dev->dev_addr[5] = lo & 0xff;
14268 dev->dev_addr[4] = (lo >> 8) & 0xff;
14269 dev->dev_addr[3] = (lo >> 16) & 0xff;
14270 dev->dev_addr[2] = (lo >> 24) & 0xff;
14271 dev->dev_addr[1] = hi & 0xff;
14272 dev->dev_addr[0] = (hi >> 8) & 0xff;
14273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014274 }
14275
14276 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014277#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014278 if (!tg3_get_default_macaddr_sparc(tp))
14279 return 0;
14280#endif
14281 return -EINVAL;
14282 }
John W. Linville2ff43692005-09-12 14:44:20 -070014283 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014284 return 0;
14285}
14286
David S. Miller59e6b432005-05-18 22:50:10 -070014287#define BOUNDARY_SINGLE_CACHELINE 1
14288#define BOUNDARY_MULTI_CACHELINE 2
14289
14290static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14291{
14292 int cacheline_size;
14293 u8 byte;
14294 int goal;
14295
14296 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14297 if (byte == 0)
14298 cacheline_size = 1024;
14299 else
14300 cacheline_size = (int) byte * 4;
14301
14302 /* On 5703 and later chips, the boundary bits have no
14303 * effect.
14304 */
14305 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14306 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014307 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070014308 goto out;
14309
14310#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14311 goal = BOUNDARY_MULTI_CACHELINE;
14312#else
14313#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14314 goal = BOUNDARY_SINGLE_CACHELINE;
14315#else
14316 goal = 0;
14317#endif
14318#endif
14319
Joe Perches63c3a662011-04-26 08:12:10 +000014320 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014321 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14322 goto out;
14323 }
14324
David S. Miller59e6b432005-05-18 22:50:10 -070014325 if (!goal)
14326 goto out;
14327
14328 /* PCI controllers on most RISC systems tend to disconnect
14329 * when a device tries to burst across a cache-line boundary.
14330 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14331 *
14332 * Unfortunately, for PCI-E there are only limited
14333 * write-side controls for this, and thus for reads
14334 * we will still get the disconnects. We'll also waste
14335 * these PCI cycles for both read and write for chips
14336 * other than 5700 and 5701 which do not implement the
14337 * boundary bits.
14338 */
Joe Perches63c3a662011-04-26 08:12:10 +000014339 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014340 switch (cacheline_size) {
14341 case 16:
14342 case 32:
14343 case 64:
14344 case 128:
14345 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14346 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14347 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14348 } else {
14349 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14350 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14351 }
14352 break;
14353
14354 case 256:
14355 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14356 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14357 break;
14358
14359 default:
14360 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14361 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14362 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014363 }
Joe Perches63c3a662011-04-26 08:12:10 +000014364 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014365 switch (cacheline_size) {
14366 case 16:
14367 case 32:
14368 case 64:
14369 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14370 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14371 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14372 break;
14373 }
14374 /* fallthrough */
14375 case 128:
14376 default:
14377 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14378 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14379 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014380 }
David S. Miller59e6b432005-05-18 22:50:10 -070014381 } else {
14382 switch (cacheline_size) {
14383 case 16:
14384 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14385 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14386 DMA_RWCTRL_WRITE_BNDRY_16);
14387 break;
14388 }
14389 /* fallthrough */
14390 case 32:
14391 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14392 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14393 DMA_RWCTRL_WRITE_BNDRY_32);
14394 break;
14395 }
14396 /* fallthrough */
14397 case 64:
14398 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14399 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14400 DMA_RWCTRL_WRITE_BNDRY_64);
14401 break;
14402 }
14403 /* fallthrough */
14404 case 128:
14405 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14406 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14407 DMA_RWCTRL_WRITE_BNDRY_128);
14408 break;
14409 }
14410 /* fallthrough */
14411 case 256:
14412 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14413 DMA_RWCTRL_WRITE_BNDRY_256);
14414 break;
14415 case 512:
14416 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14417 DMA_RWCTRL_WRITE_BNDRY_512);
14418 break;
14419 case 1024:
14420 default:
14421 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14422 DMA_RWCTRL_WRITE_BNDRY_1024);
14423 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014424 }
David S. Miller59e6b432005-05-18 22:50:10 -070014425 }
14426
14427out:
14428 return val;
14429}
14430
Linus Torvalds1da177e2005-04-16 15:20:36 -070014431static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
14432{
14433 struct tg3_internal_buffer_desc test_desc;
14434 u32 sram_dma_descs;
14435 int i, ret;
14436
14437 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
14438
14439 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
14440 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
14441 tw32(RDMAC_STATUS, 0);
14442 tw32(WDMAC_STATUS, 0);
14443
14444 tw32(BUFMGR_MODE, 0);
14445 tw32(FTQ_RESET, 0);
14446
14447 test_desc.addr_hi = ((u64) buf_dma) >> 32;
14448 test_desc.addr_lo = buf_dma & 0xffffffff;
14449 test_desc.nic_mbuf = 0x00002100;
14450 test_desc.len = size;
14451
14452 /*
14453 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
14454 * the *second* time the tg3 driver was getting loaded after an
14455 * initial scan.
14456 *
14457 * Broadcom tells me:
14458 * ...the DMA engine is connected to the GRC block and a DMA
14459 * reset may affect the GRC block in some unpredictable way...
14460 * The behavior of resets to individual blocks has not been tested.
14461 *
14462 * Broadcom noted the GRC reset will also reset all sub-components.
14463 */
14464 if (to_device) {
14465 test_desc.cqid_sqid = (13 << 8) | 2;
14466
14467 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
14468 udelay(40);
14469 } else {
14470 test_desc.cqid_sqid = (16 << 8) | 7;
14471
14472 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
14473 udelay(40);
14474 }
14475 test_desc.flags = 0x00000005;
14476
14477 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
14478 u32 val;
14479
14480 val = *(((u32 *)&test_desc) + i);
14481 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
14482 sram_dma_descs + (i * sizeof(u32)));
14483 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
14484 }
14485 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
14486
Matt Carlson859a5882010-04-05 10:19:28 +000014487 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014488 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a5882010-04-05 10:19:28 +000014489 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014490 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014491
14492 ret = -ENODEV;
14493 for (i = 0; i < 40; i++) {
14494 u32 val;
14495
14496 if (to_device)
14497 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
14498 else
14499 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
14500 if ((val & 0xffff) == sram_dma_descs) {
14501 ret = 0;
14502 break;
14503 }
14504
14505 udelay(100);
14506 }
14507
14508 return ret;
14509}
14510
David S. Millerded73402005-05-23 13:59:47 -070014511#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070014512
Matt Carlson41434702011-03-09 16:58:22 +000014513static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080014514 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14515 { },
14516};
14517
Linus Torvalds1da177e2005-04-16 15:20:36 -070014518static int __devinit tg3_test_dma(struct tg3 *tp)
14519{
14520 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070014521 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014522 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014523
Matt Carlson4bae65c2010-11-24 08:31:52 +000014524 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
14525 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014526 if (!buf) {
14527 ret = -ENOMEM;
14528 goto out_nofree;
14529 }
14530
14531 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
14532 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
14533
David S. Miller59e6b432005-05-18 22:50:10 -070014534 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014535
Joe Perches63c3a662011-04-26 08:12:10 +000014536 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014537 goto out;
14538
Joe Perches63c3a662011-04-26 08:12:10 +000014539 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014540 /* DMA read watermark not used on PCIE */
14541 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000014542 } else if (!tg3_flag(tp, PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070014543 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14544 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014545 tp->dma_rwctrl |= 0x003f0000;
14546 else
14547 tp->dma_rwctrl |= 0x003f000f;
14548 } else {
14549 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14550 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
14551 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080014552 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014553
Michael Chan4a29cc22006-03-19 13:21:12 -080014554 /* If the 5704 is behind the EPB bridge, we can
14555 * do the less restrictive ONE_DMA workaround for
14556 * better performance.
14557 */
Joe Perches63c3a662011-04-26 08:12:10 +000014558 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Michael Chan4a29cc22006-03-19 13:21:12 -080014559 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14560 tp->dma_rwctrl |= 0x8000;
14561 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014562 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
14563
Michael Chan49afdeb2007-02-13 12:17:03 -080014564 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
14565 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070014566 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080014567 tp->dma_rwctrl |=
14568 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
14569 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
14570 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070014571 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
14572 /* 5780 always in PCIX mode */
14573 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070014574 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
14575 /* 5714 always in PCIX mode */
14576 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014577 } else {
14578 tp->dma_rwctrl |= 0x001b000f;
14579 }
14580 }
14581
14582 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14583 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14584 tp->dma_rwctrl &= 0xfffffff0;
14585
14586 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14587 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
14588 /* Remove this if it causes problems for some boards. */
14589 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
14590
14591 /* On 5700/5701 chips, we need to set this bit.
14592 * Otherwise the chip will issue cacheline transactions
14593 * to streamable DMA memory with not all the byte
14594 * enables turned on. This is an error on several
14595 * RISC PCI controllers, in particular sparc64.
14596 *
14597 * On 5703/5704 chips, this bit has been reassigned
14598 * a different meaning. In particular, it is used
14599 * on those chips to enable a PCI-X workaround.
14600 */
14601 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
14602 }
14603
14604 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14605
14606#if 0
14607 /* Unneeded, already done by tg3_get_invariants. */
14608 tg3_switch_clocks(tp);
14609#endif
14610
Linus Torvalds1da177e2005-04-16 15:20:36 -070014611 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14612 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
14613 goto out;
14614
David S. Miller59e6b432005-05-18 22:50:10 -070014615 /* It is best to perform DMA test with maximum write burst size
14616 * to expose the 5700/5701 write DMA bug.
14617 */
14618 saved_dma_rwctrl = tp->dma_rwctrl;
14619 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14620 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14621
Linus Torvalds1da177e2005-04-16 15:20:36 -070014622 while (1) {
14623 u32 *p = buf, i;
14624
14625 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
14626 p[i] = i;
14627
14628 /* Send the buffer to the chip. */
14629 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
14630 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000014631 dev_err(&tp->pdev->dev,
14632 "%s: Buffer write failed. err = %d\n",
14633 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014634 break;
14635 }
14636
14637#if 0
14638 /* validate data reached card RAM correctly. */
14639 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14640 u32 val;
14641 tg3_read_mem(tp, 0x2100 + (i*4), &val);
14642 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000014643 dev_err(&tp->pdev->dev,
14644 "%s: Buffer corrupted on device! "
14645 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014646 /* ret = -ENODEV here? */
14647 }
14648 p[i] = 0;
14649 }
14650#endif
14651 /* Now read it back. */
14652 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
14653 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014654 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
14655 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014656 break;
14657 }
14658
14659 /* Verify it. */
14660 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14661 if (p[i] == i)
14662 continue;
14663
David S. Miller59e6b432005-05-18 22:50:10 -070014664 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14665 DMA_RWCTRL_WRITE_BNDRY_16) {
14666 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014667 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
14668 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14669 break;
14670 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000014671 dev_err(&tp->pdev->dev,
14672 "%s: Buffer corrupted on read back! "
14673 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014674 ret = -ENODEV;
14675 goto out;
14676 }
14677 }
14678
14679 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
14680 /* Success. */
14681 ret = 0;
14682 break;
14683 }
14684 }
David S. Miller59e6b432005-05-18 22:50:10 -070014685 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14686 DMA_RWCTRL_WRITE_BNDRY_16) {
14687 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070014688 * now look for chipsets that are known to expose the
14689 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070014690 */
Matt Carlson41434702011-03-09 16:58:22 +000014691 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014692 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14693 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a5882010-04-05 10:19:28 +000014694 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014695 /* Safe to use the calculated DMA boundary. */
14696 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a5882010-04-05 10:19:28 +000014697 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070014698
David S. Miller59e6b432005-05-18 22:50:10 -070014699 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014701
14702out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000014703 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014704out_nofree:
14705 return ret;
14706}
14707
Linus Torvalds1da177e2005-04-16 15:20:36 -070014708static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
14709{
Joe Perches63c3a662011-04-26 08:12:10 +000014710 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000014711 tp->bufmgr_config.mbuf_read_dma_low_water =
14712 DEFAULT_MB_RDMA_LOW_WATER_5705;
14713 tp->bufmgr_config.mbuf_mac_rx_low_water =
14714 DEFAULT_MB_MACRX_LOW_WATER_57765;
14715 tp->bufmgr_config.mbuf_high_water =
14716 DEFAULT_MB_HIGH_WATER_57765;
14717
14718 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14719 DEFAULT_MB_RDMA_LOW_WATER_5705;
14720 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14721 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
14722 tp->bufmgr_config.mbuf_high_water_jumbo =
14723 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000014724 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec172005-07-25 12:31:48 -070014725 tp->bufmgr_config.mbuf_read_dma_low_water =
14726 DEFAULT_MB_RDMA_LOW_WATER_5705;
14727 tp->bufmgr_config.mbuf_mac_rx_low_water =
14728 DEFAULT_MB_MACRX_LOW_WATER_5705;
14729 tp->bufmgr_config.mbuf_high_water =
14730 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070014731 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14732 tp->bufmgr_config.mbuf_mac_rx_low_water =
14733 DEFAULT_MB_MACRX_LOW_WATER_5906;
14734 tp->bufmgr_config.mbuf_high_water =
14735 DEFAULT_MB_HIGH_WATER_5906;
14736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014737
Michael Chanfdfec172005-07-25 12:31:48 -070014738 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14739 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
14740 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14741 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
14742 tp->bufmgr_config.mbuf_high_water_jumbo =
14743 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
14744 } else {
14745 tp->bufmgr_config.mbuf_read_dma_low_water =
14746 DEFAULT_MB_RDMA_LOW_WATER;
14747 tp->bufmgr_config.mbuf_mac_rx_low_water =
14748 DEFAULT_MB_MACRX_LOW_WATER;
14749 tp->bufmgr_config.mbuf_high_water =
14750 DEFAULT_MB_HIGH_WATER;
14751
14752 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14753 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
14754 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14755 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
14756 tp->bufmgr_config.mbuf_high_water_jumbo =
14757 DEFAULT_MB_HIGH_WATER_JUMBO;
14758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014759
14760 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
14761 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
14762}
14763
14764static char * __devinit tg3_phy_string(struct tg3 *tp)
14765{
Matt Carlson79eb6902010-02-17 15:17:03 +000014766 switch (tp->phy_id & TG3_PHY_ID_MASK) {
14767 case TG3_PHY_ID_BCM5400: return "5400";
14768 case TG3_PHY_ID_BCM5401: return "5401";
14769 case TG3_PHY_ID_BCM5411: return "5411";
14770 case TG3_PHY_ID_BCM5701: return "5701";
14771 case TG3_PHY_ID_BCM5703: return "5703";
14772 case TG3_PHY_ID_BCM5704: return "5704";
14773 case TG3_PHY_ID_BCM5705: return "5705";
14774 case TG3_PHY_ID_BCM5750: return "5750";
14775 case TG3_PHY_ID_BCM5752: return "5752";
14776 case TG3_PHY_ID_BCM5714: return "5714";
14777 case TG3_PHY_ID_BCM5780: return "5780";
14778 case TG3_PHY_ID_BCM5755: return "5755";
14779 case TG3_PHY_ID_BCM5787: return "5787";
14780 case TG3_PHY_ID_BCM5784: return "5784";
14781 case TG3_PHY_ID_BCM5756: return "5722/5756";
14782 case TG3_PHY_ID_BCM5906: return "5906";
14783 case TG3_PHY_ID_BCM5761: return "5761";
14784 case TG3_PHY_ID_BCM5718C: return "5718C";
14785 case TG3_PHY_ID_BCM5718S: return "5718S";
14786 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000014787 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000014788 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000014789 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070014790 case 0: return "serdes";
14791 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070014792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014793}
14794
Michael Chanf9804dd2005-09-27 12:13:10 -070014795static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
14796{
Joe Perches63c3a662011-04-26 08:12:10 +000014797 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070014798 strcpy(str, "PCI Express");
14799 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000014800 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070014801 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
14802
14803 strcpy(str, "PCIX:");
14804
14805 if ((clock_ctrl == 7) ||
14806 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
14807 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
14808 strcat(str, "133MHz");
14809 else if (clock_ctrl == 0)
14810 strcat(str, "33MHz");
14811 else if (clock_ctrl == 2)
14812 strcat(str, "50MHz");
14813 else if (clock_ctrl == 4)
14814 strcat(str, "66MHz");
14815 else if (clock_ctrl == 6)
14816 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070014817 } else {
14818 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000014819 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070014820 strcat(str, "66MHz");
14821 else
14822 strcat(str, "33MHz");
14823 }
Joe Perches63c3a662011-04-26 08:12:10 +000014824 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070014825 strcat(str, ":32-bit");
14826 else
14827 strcat(str, ":64-bit");
14828 return str;
14829}
14830
Michael Chan8c2dc7e2005-12-19 16:26:02 -080014831static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014832{
14833 struct pci_dev *peer;
14834 unsigned int func, devnr = tp->pdev->devfn & ~7;
14835
14836 for (func = 0; func < 8; func++) {
14837 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14838 if (peer && peer != tp->pdev)
14839 break;
14840 pci_dev_put(peer);
14841 }
Michael Chan16fe9d72005-12-13 21:09:54 -080014842 /* 5704 can be configured in single-port mode, set peer to
14843 * tp->pdev in that case.
14844 */
14845 if (!peer) {
14846 peer = tp->pdev;
14847 return peer;
14848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014849
14850 /*
14851 * We don't need to keep the refcount elevated; there's no way
14852 * to remove one half of this device without removing the other
14853 */
14854 pci_dev_put(peer);
14855
14856 return peer;
14857}
14858
David S. Miller15f98502005-05-18 22:49:26 -070014859static void __devinit tg3_init_coal(struct tg3 *tp)
14860{
14861 struct ethtool_coalesce *ec = &tp->coal;
14862
14863 memset(ec, 0, sizeof(*ec));
14864 ec->cmd = ETHTOOL_GCOALESCE;
14865 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
14866 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
14867 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
14868 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
14869 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
14870 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
14871 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
14872 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
14873 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
14874
14875 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
14876 HOSTCC_MODE_CLRTICK_TXBD)) {
14877 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
14878 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
14879 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
14880 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
14881 }
Michael Chand244c892005-07-05 14:42:33 -070014882
Joe Perches63c3a662011-04-26 08:12:10 +000014883 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070014884 ec->rx_coalesce_usecs_irq = 0;
14885 ec->tx_coalesce_usecs_irq = 0;
14886 ec->stats_block_coalesce_usecs = 0;
14887 }
David S. Miller15f98502005-05-18 22:49:26 -070014888}
14889
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014890static const struct net_device_ops tg3_netdev_ops = {
14891 .ndo_open = tg3_open,
14892 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080014893 .ndo_start_xmit = tg3_start_xmit,
Eric Dumazet511d2222010-07-07 20:44:24 +000014894 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -080014895 .ndo_validate_addr = eth_validate_addr,
14896 .ndo_set_multicast_list = tg3_set_rx_mode,
14897 .ndo_set_mac_address = tg3_set_mac_addr,
14898 .ndo_do_ioctl = tg3_ioctl,
14899 .ndo_tx_timeout = tg3_tx_timeout,
14900 .ndo_change_mtu = tg3_change_mtu,
Michał Mirosławdc668912011-04-07 03:35:07 +000014901 .ndo_fix_features = tg3_fix_features,
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000014902 .ndo_set_features = tg3_set_features,
Stephen Hemminger00829822008-11-20 20:14:53 -080014903#ifdef CONFIG_NET_POLL_CONTROLLER
14904 .ndo_poll_controller = tg3_poll_controller,
14905#endif
14906};
14907
Linus Torvalds1da177e2005-04-16 15:20:36 -070014908static int __devinit tg3_init_one(struct pci_dev *pdev,
14909 const struct pci_device_id *ent)
14910{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014911 struct net_device *dev;
14912 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000014913 int i, err, pm_cap;
14914 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070014915 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080014916 u64 dma_mask, persist_dma_mask;
Matt Carlson0da06062011-05-19 12:12:53 +000014917 u32 features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014918
Joe Perches05dbe002010-02-17 19:44:19 +000014919 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014920
14921 err = pci_enable_device(pdev);
14922 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014923 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014924 return err;
14925 }
14926
Linus Torvalds1da177e2005-04-16 15:20:36 -070014927 err = pci_request_regions(pdev, DRV_MODULE_NAME);
14928 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014929 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014930 goto err_out_disable_pdev;
14931 }
14932
14933 pci_set_master(pdev);
14934
14935 /* Find power-management capability. */
14936 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
14937 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000014938 dev_err(&pdev->dev,
14939 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014940 err = -EIO;
14941 goto err_out_free_res;
14942 }
14943
Matt Carlsonfe5f5782009-09-01 13:09:39 +000014944 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014945 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000014946 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014947 err = -ENOMEM;
14948 goto err_out_free_res;
14949 }
14950
Linus Torvalds1da177e2005-04-16 15:20:36 -070014951 SET_NETDEV_DEV(dev, &pdev->dev);
14952
Linus Torvalds1da177e2005-04-16 15:20:36 -070014953 tp = netdev_priv(dev);
14954 tp->pdev = pdev;
14955 tp->dev = dev;
14956 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014957 tp->rx_mode = TG3_DEF_RX_MODE;
14958 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070014959
Linus Torvalds1da177e2005-04-16 15:20:36 -070014960 if (tg3_debug > 0)
14961 tp->msg_enable = tg3_debug;
14962 else
14963 tp->msg_enable = TG3_DEF_MSG_ENABLE;
14964
14965 /* The word/byte swap controls here control register access byte
14966 * swapping. DMA data byte swapping is controlled in the GRC_MODE
14967 * setting below.
14968 */
14969 tp->misc_host_ctrl =
14970 MISC_HOST_CTRL_MASK_PCI_INT |
14971 MISC_HOST_CTRL_WORD_SWAP |
14972 MISC_HOST_CTRL_INDIR_ACCESS |
14973 MISC_HOST_CTRL_PCISTATE_RW;
14974
14975 /* The NONFRM (non-frame) byte/word swap controls take effect
14976 * on descriptor entries, anything which isn't packet data.
14977 *
14978 * The StrongARM chips on the board (one for tx, one for rx)
14979 * are running in big-endian mode.
14980 */
14981 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
14982 GRC_MODE_WSWAP_NONFRM_DATA);
14983#ifdef __BIG_ENDIAN
14984 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
14985#endif
14986 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014987 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000014988 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014989
Matt Carlsond5fe4882008-11-21 17:20:32 -080014990 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010014991 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014992 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014993 err = -ENOMEM;
14994 goto err_out_free_dev;
14995 }
14996
Linus Torvalds1da177e2005-04-16 15:20:36 -070014997 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
14998 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014999
Linus Torvalds1da177e2005-04-16 15:20:36 -070015000 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015001 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000015002 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015003 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015004
15005 err = tg3_get_invariants(tp);
15006 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015007 dev_err(&pdev->dev,
15008 "Problem fetching invariants of chip, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015009 goto err_out_iounmap;
15010 }
15011
Michael Chan4a29cc22006-03-19 13:21:12 -080015012 /* The EPB bridge inside 5714, 5715, and 5780 and any
15013 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080015014 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
15015 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
15016 * do DMA address check in tg3_start_xmit().
15017 */
Joe Perches63c3a662011-04-26 08:12:10 +000015018 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070015019 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000015020 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070015021 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080015022#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070015023 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015024#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080015025 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070015026 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015027
15028 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070015029 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080015030 err = pci_set_dma_mask(pdev, dma_mask);
15031 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000015032 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080015033 err = pci_set_consistent_dma_mask(pdev,
15034 persist_dma_mask);
15035 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015036 dev_err(&pdev->dev, "Unable to obtain 64 bit "
15037 "DMA for consistent allocations\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080015038 goto err_out_iounmap;
15039 }
15040 }
15041 }
Yang Hongyang284901a2009-04-06 19:01:15 -070015042 if (err || dma_mask == DMA_BIT_MASK(32)) {
15043 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080015044 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015045 dev_err(&pdev->dev,
15046 "No usable DMA configuration, aborting\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080015047 goto err_out_iounmap;
15048 }
15049 }
15050
Michael Chanfdfec172005-07-25 12:31:48 -070015051 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015052
Matt Carlson0da06062011-05-19 12:12:53 +000015053 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
15054
15055 /* 5700 B0 chips do not support checksumming correctly due
15056 * to hardware bugs.
15057 */
15058 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
15059 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
15060
15061 if (tg3_flag(tp, 5755_PLUS))
15062 features |= NETIF_F_IPV6_CSUM;
15063 }
15064
Michael Chan4e3a7aa2006-03-20 17:47:44 -080015065 /* TSO is on by default on chips that support hardware TSO.
15066 * Firmware TSO on older chips gives lower performance, so it
15067 * is off by default, but can be enabled using ethtool.
15068 */
Joe Perches63c3a662011-04-26 08:12:10 +000015069 if ((tg3_flag(tp, HW_TSO_1) ||
15070 tg3_flag(tp, HW_TSO_2) ||
15071 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000015072 (features & NETIF_F_IP_CSUM))
15073 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000015074 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000015075 if (features & NETIF_F_IPV6_CSUM)
15076 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000015077 if (tg3_flag(tp, HW_TSO_3) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000015078 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070015079 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15080 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Joe Perches63c3a662011-04-26 08:12:10 +000015081 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000015082 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000015083 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070015084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015085
Matt Carlsond542fe22011-05-19 16:02:43 +000015086 dev->features |= features;
15087 dev->vlan_features |= features;
15088
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015089 /*
15090 * Add loopback capability only for a subset of devices that support
15091 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
15092 * loopback for the remaining devices.
15093 */
15094 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
15095 !tg3_flag(tp, CPMU_PRESENT))
15096 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000015097 features |= NETIF_F_LOOPBACK;
15098
Matt Carlson0da06062011-05-19 12:12:53 +000015099 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015100
Linus Torvalds1da177e2005-04-16 15:20:36 -070015101 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015102 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015103 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015104 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015105 tp->rx_pending = 63;
15106 }
15107
Linus Torvalds1da177e2005-04-16 15:20:36 -070015108 err = tg3_get_device_address(tp);
15109 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015110 dev_err(&pdev->dev,
15111 "Could not obtain valid ethernet address, aborting\n");
Matt Carlson026a6c22009-12-03 08:36:24 +000015112 goto err_out_iounmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015113 }
15114
Joe Perches63c3a662011-04-26 08:12:10 +000015115 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson63532392008-11-03 16:49:57 -080015116 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
Al Viro79ea13c2008-01-24 02:06:46 -080015117 if (!tp->aperegs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015118 dev_err(&pdev->dev,
15119 "Cannot map APE registers, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015120 err = -ENOMEM;
Matt Carlson026a6c22009-12-03 08:36:24 +000015121 goto err_out_iounmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015122 }
15123
15124 tg3_ape_lock_init(tp);
Matt Carlson7fd76442009-02-25 14:27:20 +000015125
Joe Perches63c3a662011-04-26 08:12:10 +000015126 if (tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000015127 tg3_read_dash_ver(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015128 }
15129
Matt Carlsonc88864d2007-11-12 21:07:01 -080015130 /*
15131 * Reset chip in case UNDI or EFI driver did not shutdown
15132 * DMA self test will enable WDMAC and we'll see (spurious)
15133 * pending DMA on the PCI bus at that point.
15134 */
15135 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15136 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15137 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15138 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15139 }
15140
15141 err = tg3_test_dma(tp);
15142 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015143 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080015144 goto err_out_apeunmap;
15145 }
15146
Matt Carlson78f90dc2009-11-13 13:03:42 +000015147 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15148 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15149 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000015150 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000015151 struct tg3_napi *tnapi = &tp->napi[i];
15152
15153 tnapi->tp = tp;
15154 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15155
15156 tnapi->int_mbox = intmbx;
15157 if (i < 4)
15158 intmbx += 0x8;
15159 else
15160 intmbx += 0x4;
15161
15162 tnapi->consmbox = rcvmbx;
15163 tnapi->prodmbox = sndmbx;
15164
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015165 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015166 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015167 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000015168 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000015169
Joe Perches63c3a662011-04-26 08:12:10 +000015170 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000015171 break;
15172
15173 /*
15174 * If we support MSIX, we'll be using RSS. If we're using
15175 * RSS, the first vector only handles link interrupts and the
15176 * remaining vectors handle rx and tx interrupts. Reuse the
15177 * mailbox values for the next iteration. The values we setup
15178 * above are still useful for the single vectored mode.
15179 */
15180 if (!i)
15181 continue;
15182
15183 rcvmbx += 0x8;
15184
15185 if (sndmbx & 0x4)
15186 sndmbx -= 0x4;
15187 else
15188 sndmbx += 0xc;
15189 }
15190
Matt Carlsonc88864d2007-11-12 21:07:01 -080015191 tg3_init_coal(tp);
15192
Michael Chanc49a1562006-12-17 17:07:29 -080015193 pci_set_drvdata(pdev, dev);
15194
Linus Torvalds1da177e2005-04-16 15:20:36 -070015195 err = register_netdev(dev);
15196 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015197 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015198 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015199 }
15200
Joe Perches05dbe002010-02-17 19:44:19 +000015201 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15202 tp->board_part_number,
15203 tp->pci_chip_rev_id,
15204 tg3_bus_string(tp, str),
15205 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015206
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015207 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000015208 struct phy_device *phydev;
15209 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000015210 netdev_info(dev,
15211 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015212 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015213 } else {
15214 char *ethtype;
15215
15216 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15217 ethtype = "10/100Base-TX";
15218 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15219 ethtype = "1000Base-SX";
15220 else
15221 ethtype = "10/100/1000Base-T";
15222
Matt Carlson5129c3a2010-04-05 10:19:23 +000015223 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000015224 "(WireSpeed[%d], EEE[%d])\n",
15225 tg3_phy_string(tp), ethtype,
15226 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
15227 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015228 }
Matt Carlsondf59c942008-11-03 16:52:56 -080015229
Joe Perches05dbe002010-02-17 19:44:19 +000015230 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000015231 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015232 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015233 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015234 tg3_flag(tp, ENABLE_ASF) != 0,
15235 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000015236 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15237 tp->dma_rwctrl,
15238 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15239 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015240
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015241 pci_save_state(pdev);
15242
Linus Torvalds1da177e2005-04-16 15:20:36 -070015243 return 0;
15244
Matt Carlson0d3031d2007-10-10 18:02:43 -070015245err_out_apeunmap:
15246 if (tp->aperegs) {
15247 iounmap(tp->aperegs);
15248 tp->aperegs = NULL;
15249 }
15250
Linus Torvalds1da177e2005-04-16 15:20:36 -070015251err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070015252 if (tp->regs) {
15253 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015254 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015256
15257err_out_free_dev:
15258 free_netdev(dev);
15259
15260err_out_free_res:
15261 pci_release_regions(pdev);
15262
15263err_out_disable_pdev:
15264 pci_disable_device(pdev);
15265 pci_set_drvdata(pdev, NULL);
15266 return err;
15267}
15268
15269static void __devexit tg3_remove_one(struct pci_dev *pdev)
15270{
15271 struct net_device *dev = pci_get_drvdata(pdev);
15272
15273 if (dev) {
15274 struct tg3 *tp = netdev_priv(dev);
15275
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015276 if (tp->fw)
15277 release_firmware(tp->fw);
15278
Tejun Heo23f333a2010-12-12 16:45:14 +010015279 cancel_work_sync(&tp->reset_task);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015280
Joe Perches63c3a662011-04-26 08:12:10 +000015281 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015282 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015283 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015284 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015285
Linus Torvalds1da177e2005-04-16 15:20:36 -070015286 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015287 if (tp->aperegs) {
15288 iounmap(tp->aperegs);
15289 tp->aperegs = NULL;
15290 }
Michael Chan68929142005-08-09 20:17:14 -070015291 if (tp->regs) {
15292 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015293 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015295 free_netdev(dev);
15296 pci_release_regions(pdev);
15297 pci_disable_device(pdev);
15298 pci_set_drvdata(pdev, NULL);
15299 }
15300}
15301
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015302#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015303static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015304{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015305 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015306 struct net_device *dev = pci_get_drvdata(pdev);
15307 struct tg3 *tp = netdev_priv(dev);
15308 int err;
15309
15310 if (!netif_running(dev))
15311 return 0;
15312
Tejun Heo23f333a2010-12-12 16:45:14 +010015313 flush_work_sync(&tp->reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015314 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015315 tg3_netif_stop(tp);
15316
15317 del_timer_sync(&tp->timer);
15318
David S. Millerf47c11e2005-06-24 20:18:35 -070015319 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015320 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015321 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015322
15323 netif_device_detach(dev);
15324
David S. Millerf47c11e2005-06-24 20:18:35 -070015325 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015326 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000015327 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070015328 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015329
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015330 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015331 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015332 int err2;
15333
David S. Millerf47c11e2005-06-24 20:18:35 -070015334 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015335
Joe Perches63c3a662011-04-26 08:12:10 +000015336 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015337 err2 = tg3_restart_hw(tp, 1);
15338 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015339 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015340
15341 tp->timer.expires = jiffies + tp->timer_offset;
15342 add_timer(&tp->timer);
15343
15344 netif_device_attach(dev);
15345 tg3_netif_start(tp);
15346
Michael Chanb9ec6c12006-07-25 16:37:27 -070015347out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015348 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015349
15350 if (!err2)
15351 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015352 }
15353
15354 return err;
15355}
15356
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015357static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015358{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015359 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015360 struct net_device *dev = pci_get_drvdata(pdev);
15361 struct tg3 *tp = netdev_priv(dev);
15362 int err;
15363
15364 if (!netif_running(dev))
15365 return 0;
15366
Linus Torvalds1da177e2005-04-16 15:20:36 -070015367 netif_device_attach(dev);
15368
David S. Millerf47c11e2005-06-24 20:18:35 -070015369 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015370
Joe Perches63c3a662011-04-26 08:12:10 +000015371 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070015372 err = tg3_restart_hw(tp, 1);
15373 if (err)
15374 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015375
15376 tp->timer.expires = jiffies + tp->timer_offset;
15377 add_timer(&tp->timer);
15378
Linus Torvalds1da177e2005-04-16 15:20:36 -070015379 tg3_netif_start(tp);
15380
Michael Chanb9ec6c12006-07-25 16:37:27 -070015381out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015382 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015383
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015384 if (!err)
15385 tg3_phy_start(tp);
15386
Michael Chanb9ec6c12006-07-25 16:37:27 -070015387 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015388}
15389
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015390static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015391#define TG3_PM_OPS (&tg3_pm_ops)
15392
15393#else
15394
15395#define TG3_PM_OPS NULL
15396
15397#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015398
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015399/**
15400 * tg3_io_error_detected - called when PCI error is detected
15401 * @pdev: Pointer to PCI device
15402 * @state: The current pci connection state
15403 *
15404 * This function is called after a PCI bus error affecting
15405 * this device has been detected.
15406 */
15407static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
15408 pci_channel_state_t state)
15409{
15410 struct net_device *netdev = pci_get_drvdata(pdev);
15411 struct tg3 *tp = netdev_priv(netdev);
15412 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
15413
15414 netdev_info(netdev, "PCI I/O error detected\n");
15415
15416 rtnl_lock();
15417
15418 if (!netif_running(netdev))
15419 goto done;
15420
15421 tg3_phy_stop(tp);
15422
15423 tg3_netif_stop(tp);
15424
15425 del_timer_sync(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +000015426 tg3_flag_clear(tp, RESTART_TIMER);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015427
15428 /* Want to make sure that the reset task doesn't run */
15429 cancel_work_sync(&tp->reset_task);
Joe Perches63c3a662011-04-26 08:12:10 +000015430 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
15431 tg3_flag_clear(tp, RESTART_TIMER);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015432
15433 netif_device_detach(netdev);
15434
15435 /* Clean up software state, even if MMIO is blocked */
15436 tg3_full_lock(tp, 0);
15437 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
15438 tg3_full_unlock(tp);
15439
15440done:
15441 if (state == pci_channel_io_perm_failure)
15442 err = PCI_ERS_RESULT_DISCONNECT;
15443 else
15444 pci_disable_device(pdev);
15445
15446 rtnl_unlock();
15447
15448 return err;
15449}
15450
15451/**
15452 * tg3_io_slot_reset - called after the pci bus has been reset.
15453 * @pdev: Pointer to PCI device
15454 *
15455 * Restart the card from scratch, as if from a cold-boot.
15456 * At this point, the card has exprienced a hard reset,
15457 * followed by fixups by BIOS, and has its config space
15458 * set up identically to what it was at cold boot.
15459 */
15460static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
15461{
15462 struct net_device *netdev = pci_get_drvdata(pdev);
15463 struct tg3 *tp = netdev_priv(netdev);
15464 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
15465 int err;
15466
15467 rtnl_lock();
15468
15469 if (pci_enable_device(pdev)) {
15470 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
15471 goto done;
15472 }
15473
15474 pci_set_master(pdev);
15475 pci_restore_state(pdev);
15476 pci_save_state(pdev);
15477
15478 if (!netif_running(netdev)) {
15479 rc = PCI_ERS_RESULT_RECOVERED;
15480 goto done;
15481 }
15482
15483 err = tg3_power_up(tp);
15484 if (err) {
15485 netdev_err(netdev, "Failed to restore register access.\n");
15486 goto done;
15487 }
15488
15489 rc = PCI_ERS_RESULT_RECOVERED;
15490
15491done:
15492 rtnl_unlock();
15493
15494 return rc;
15495}
15496
15497/**
15498 * tg3_io_resume - called when traffic can start flowing again.
15499 * @pdev: Pointer to PCI device
15500 *
15501 * This callback is called when the error recovery driver tells
15502 * us that its OK to resume normal operation.
15503 */
15504static void tg3_io_resume(struct pci_dev *pdev)
15505{
15506 struct net_device *netdev = pci_get_drvdata(pdev);
15507 struct tg3 *tp = netdev_priv(netdev);
15508 int err;
15509
15510 rtnl_lock();
15511
15512 if (!netif_running(netdev))
15513 goto done;
15514
15515 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000015516 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015517 err = tg3_restart_hw(tp, 1);
15518 tg3_full_unlock(tp);
15519 if (err) {
15520 netdev_err(netdev, "Cannot restart hardware after reset.\n");
15521 goto done;
15522 }
15523
15524 netif_device_attach(netdev);
15525
15526 tp->timer.expires = jiffies + tp->timer_offset;
15527 add_timer(&tp->timer);
15528
15529 tg3_netif_start(tp);
15530
15531 tg3_phy_start(tp);
15532
15533done:
15534 rtnl_unlock();
15535}
15536
15537static struct pci_error_handlers tg3_err_handler = {
15538 .error_detected = tg3_io_error_detected,
15539 .slot_reset = tg3_io_slot_reset,
15540 .resume = tg3_io_resume
15541};
15542
Linus Torvalds1da177e2005-04-16 15:20:36 -070015543static struct pci_driver tg3_driver = {
15544 .name = DRV_MODULE_NAME,
15545 .id_table = tg3_pci_tbl,
15546 .probe = tg3_init_one,
15547 .remove = __devexit_p(tg3_remove_one),
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015548 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015549 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015550};
15551
15552static int __init tg3_init(void)
15553{
Jeff Garzik29917622006-08-19 17:48:59 -040015554 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015555}
15556
15557static void __exit tg3_cleanup(void)
15558{
15559 pci_unregister_driver(&tg3_driver);
15560}
15561
15562module_init(tg3_init);
15563module_exit(tg3_cleanup);